View Javadoc

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