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.springframework.org/schema/osgi\"\n" +
23      		"             xsi:schemaLocation=\"http://www.springframework.org/schema/osgi\n" +
24      		"                                 http://www.springframework.org/schema/osgi/spring-osgi.xsd\n" +
25      		"                                 http://www.springframework.org/schema/beans\n" +
26      		"                                 http://www.springframework.org/schema/beans/spring-beans.xsd\">\n" +
27      		"\n" +
28      		" <osgi:reference id='a' interface='test.X' cardinality='0..1' timeout='1'/>" +
29              " <bean class='test.FooConsumer'>" +
30              "  <constructor-arg ref='a' />" +
31              " </bean>" +
32  
33      		"</beans>\n";
34  
35      /* Communicate with the plugin through a static field */
36      public static String mutable = null;
37  
38      public void testUnavailableServicesCanBeDetected() throws Exception
39      {
40          TestServiceUnavailable.mutable = null;
41  
42          initPluginManager();
43  
44          PluginJarBuilder plugin = new PluginJarBuilder("testServiceUnavailable")
45          .addFormattedResource("atlassian-plugin.xml",
46                  "<atlassian-plugin name='Test' key='test.serviceunavailable.plugin' pluginsVersion='2'>",
47                  "    <plugin-info>",
48                  "        <version>1.0</version>",
49                  "    </plugin-info>",
50                  "</atlassian-plugin>")
51  
52                  .addFormattedJava("test.X",
53                          "package test;",
54                          "public interface X {}"
55                          )
56  
57                  .addFormattedJava("test.FooConsumer",
58                          "package test;",
59                          "import com.atlassian.plugin.osgi.spring.TestServiceUnavailable;",
60                          "import org.springframework.osgi.service.ServiceUnavailableException;",
61                          /* A constructor that tries to use an OSGi service stub and cope with it being unavailable */
62                          "public class FooConsumer {FooConsumer(Object foo){",
63                          "        TestServiceUnavailable.mutable = \"invoked\";",
64                          "        try",
65                          "        {",
66                          "            foo.toString();",
67                          "            TestServiceUnavailable.mutable = \"succeeded\";",
68                          "        }",
69                          "        catch (ServiceUnavailableException e)",
70                          "        {",
71                          "            TestServiceUnavailable.mutable = \"caught\";",
72                          "        }",
73                          "        catch (Exception e)",
74                          "        {",
75                          "            TestServiceUnavailable.mutable = \"unexpected - \" + e.getClass().getName();",
76                          "        }",
77                          "}",
78                          "}"
79                          )
80  
81          .addFormattedResource("META-INF/spring/beans.xml", beans);
82  
83          plugin.build(pluginsDir);
84  
85          pluginManager.installPlugin(new JarPluginArtifact(plugin.build()));
86  
87          osgiContainerManager.stop();
88  
89          assertEquals("The specific ServiceUnavailableException should be caught",
90                  "caught", mutable);
91      }
92  }