1
2
3
4
5
6
7 package org.mule.lifecycle;
8
9 import org.mule.tck.junit4.AbstractMuleContextTestCase;
10
11 import javax.annotation.PostConstruct;
12 import javax.annotation.PreDestroy;
13
14 import org.junit.Test;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.fail;
18
19
20
21
22 public class JSR250ObjectLifcycleTestCase extends AbstractMuleContextTestCase
23 {
24 @Test
25 public void testNormalBehaviour() throws Exception
26 {
27 JSR250ObjectLifecycleTracker tracker = new JSR250ObjectLifecycleTracker();
28 muleContext.getRegistry().registerObject("test", tracker);
29
30 muleContext.dispose();
31 assertEquals("[setMuleContext, initialise, dispose]", tracker.getTracker().toString());
32 }
33
34 @Test
35 public void testTwoPostConstructAnnotations() throws Exception
36 {
37 try
38 {
39 muleContext.getRegistry().registerObject("test", new DupePostConstructJSR250ObjectLifecycleTracker());
40 fail("Object has two @PostConstruct annotations");
41 }
42 catch (IllegalArgumentException e)
43 {
44
45 }
46 }
47
48 @Test
49 public void testTwoPreDestroyAnnotations() throws Exception
50 {
51 try
52 {
53 muleContext.getRegistry().registerObject("test", new DupePreDestroyJSR250ObjectLifecycleTracker());
54 fail("Object has two @PreDestroy annotations");
55 }
56 catch (IllegalArgumentException e)
57 {
58
59 }
60 }
61
62 @Test
63 public void testBadReturnTypePostConstructMethod() throws Exception
64 {
65 try
66 {
67 muleContext.getRegistry().registerObject("test", new BadReturnTypePostConstructLifecycleMethodObject());
68 fail("PostContruct Lifecycle method has a non-void return type");
69 }
70 catch (IllegalArgumentException e)
71 {
72
73 }
74 }
75
76 @Test
77 public void testBadParamPreDestroyMethod() throws Exception
78 {
79 try
80 {
81 muleContext.getRegistry().registerObject("test", new BadParamPreDestroyLifecycleMethodObject());
82 fail("PreDestroy Lifecycle method has a parameter");
83 }
84 catch (IllegalArgumentException e)
85 {
86
87 }
88 }
89
90 @Test
91 public void testBadStaticPreDestroyMethod() throws Exception
92 {
93 try
94 {
95 muleContext.getRegistry().registerObject("test", new BadStaticMethodPostConstructLifecycleMethodObject());
96 fail("PostConstruct Lifecycle method is static");
97 }
98 catch (IllegalArgumentException e)
99 {
100
101 }
102 }
103
104 @Test
105 public void testBadCheckedExceptionPreDestroyMethod() throws Exception
106 {
107 try
108 {
109 muleContext.getRegistry().registerObject("test", new BadCheckedExceptionPreDestroyLifecycleMethodObject());
110 fail("PreDestroy Lifecycle method throws a checked exception");
111 }
112 catch (IllegalArgumentException e)
113 {
114
115 }
116 }
117
118 public class DupePostConstructJSR250ObjectLifecycleTracker extends JSR250ObjectLifecycleTracker
119 {
120
121 @PostConstruct
122 public void init2()
123 {
124 getTracker().add("initialise 2");
125 }
126 }
127
128 public class DupePreDestroyJSR250ObjectLifecycleTracker extends JSR250ObjectLifecycleTracker
129 {
130
131 @PreDestroy
132 public void dispose2()
133 {
134 getTracker().add("dispose 2");
135 }
136 }
137
138 public class BadReturnTypePostConstructLifecycleMethodObject
139 {
140 @PostConstruct
141 public boolean init()
142 {
143 return true;
144 }
145 }
146
147 public class BadParamPreDestroyLifecycleMethodObject
148 {
149 @PreDestroy
150 public void destroy(boolean foo)
151 {
152
153 }
154 }
155
156 public static class BadStaticMethodPostConstructLifecycleMethodObject
157 {
158 @PostConstruct
159 public static void init()
160 {
161
162 }
163 }
164
165 public class BadCheckedExceptionPreDestroyLifecycleMethodObject
166 {
167 @PreDestroy
168 public void destroy() throws Exception
169 {
170
171 }
172 }
173 }