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
9
10
11
12
13
14
15
16
17 public class TestServiceUnavailable extends PluginInContainerTestBase
18 {
19
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
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
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 }