View Javadoc

1   package com.atlassian.plugin.osgi.spring;
2   
3   import com.atlassian.plugin.JarPluginArtifact;
4   import com.atlassian.plugin.osgi.PluginInContainerTestBase;
5   import com.atlassian.plugin.test.PluginJarBuilder;
6   
7   /**
8    * When an optional OSGi service is unavailable at runtime, Spring DM's proxy class will throw a specific exception.
9    * The exception is different in OSGi Blueprint. This test is to demonstrate a plugin that relies on the Spring DM
10   * behaviour so we can consider other approaches.
11   * <ul>
12   * <li>Catch <code>RuntimeException</code> and check for both class names
13   * <li>Fail; don't try to cope with missing services
14   * <li>Rewrite the bytecode for the catch at runtime
15   * </li>
16   */
17  public class TestServiceUnavailable extends PluginInContainerTestBase
18  {
19      /* Spring definition of a bean that takes an optional reference to an OSGi service */
20      static final String beans = "<beans xmlns=\"http://www.springframework.org/schema/beans\"\n" +
21      		"             xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
22      		"             xmlns:osgi=\"http://www.eclipse.org/gemini/blueprint/schema/blueprint\"\n" +
23      		"             xsi:schemaLocation=\"" +
24              "                 http://www.eclipse.org/gemini/blueprint/schema/blueprint http://www.eclipse.org/gemini/blueprint/schema/blueprint/gemini-blueprint.xsd\n" +
25      		"                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd\">\n" +
26      		"\n" +
27      		" <osgi:reference id='a' interface='test.X' cardinality='0..1' timeout='1'/>" +
28              " <bean class='test.FooConsumer'>" +
29              "  <constructor-arg ref='a' />" +
30              " </bean>" +
31  
32      		"</beans>\n";
33  
34      /* Communicate with the plugin through a static field */
35      public static String mutable = null;
36  
37      public void testUnavailableServicesCanBeDetected() throws Exception
38      {
39          TestServiceUnavailable.mutable = null;
40  
41          initPluginManager();
42  
43          PluginJarBuilder plugin = new PluginJarBuilder("testServiceUnavailable")
44          .addFormattedResource("atlassian-plugin.xml",
45                  "<atlassian-plugin name='Test' key='test.serviceunavailable.plugin' pluginsVersion='2'>",
46                  "    <plugin-info>",
47                  "        <version>1.0</version>",
48                  "    </plugin-info>",
49                  "</atlassian-plugin>")
50  
51                  .addFormattedJava("test.X",
52                          "package test;",
53                          "public interface X {}"
54                          )
55  
56                  .addFormattedJava("test.FooConsumer",
57                          "package test;",
58                          "import com.atlassian.plugin.osgi.spring.TestServiceUnavailable;",
59                          /* A constructor that tries to use an OSGi service stub and cope with it being unavailable */
60                          "public class FooConsumer {",
61                          "    FooConsumer (Object foo)",
62                          "    {",
63                          "        TestServiceUnavailable.mutable = \"invoked\";",
64                          "        try",
65                          "        {",
66                          "            foo.toString();",
67                          "            TestServiceUnavailable.mutable = \"succeeded\";",
68                          "        }",
69                          "        catch (RuntimeException e)",
70                          "        {",
71                          "            if (e.getClass().getSimpleName().equals(\"ServiceUnavailableException\"))",
72                          "            {",
73                          "                TestServiceUnavailable.mutable = \"caught\";",
74                          "            }",
75                          "            else",
76                          "            {",
77                          "                throw e;",
78                          "            }",
79                          "        }",
80                          "        catch (Exception e)",
81                          "        {",
82                          "            TestServiceUnavailable.mutable = \"unexpected - \" + e.getClass().getName();",
83                          "        }",
84                          "    }",
85                          "}"
86                          )
87  
88          .addFormattedResource("META-INF/spring/beans.xml", beans);
89  
90          plugin.build(pluginsDir);
91  
92          pluginManager.installPlugin(new JarPluginArtifact(plugin.build()));
93  
94          osgiContainerManager.stop();
95  
96          assertEquals("The specific ServiceUnavailableException should be caught",
97                  "caught", mutable);
98      }
99  }