View Javadoc

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