View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.object;
8   
9   import org.mule.api.lifecycle.InitialisationException;
10  import org.mule.tck.junit4.AbstractMuleContextTestCase;
11  
12  import org.junit.Test;
13  
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertNotNull;
16  import static org.junit.Assert.fail;
17  
18  public abstract class AbstractObjectFactoryTestCase extends AbstractMuleContextTestCase
19  {
20  
21      @Test
22      public void testInitialisationFailureWithoutObjectClass() throws Exception
23      {
24          AbstractObjectFactory factory = getUninitialisedObjectFactory();
25  
26          try
27          {
28              factory.initialise();
29              fail("expected InitialisationException");
30          }
31          catch (InitialisationException iex)
32          {
33              // OK
34          }
35      }
36      
37      @Test
38      public void testInstanceFailureGetInstanceWithoutObjectClass() throws Exception
39      {
40          AbstractObjectFactory factory = getUninitialisedObjectFactory();
41  
42          try
43          {
44              factory.getInstance(muleContext);
45              fail("expected InitialisationException");
46          }
47          catch (InitialisationException iex)
48          {
49              // OK
50          }
51      }
52      
53      @Test
54      public void testCreateWithClassButDoNotInitialise() throws Exception
55      {
56          AbstractObjectFactory factory = new DummyObjectFactory(Object.class);
57          assertObjectClassAndName(factory);
58      }
59      
60      @Test
61      public void testCreateWithClassNameButDoNotInitialise() throws Exception
62      {
63          AbstractObjectFactory factory = new DummyObjectFactory(Object.class.getName());
64          assertObjectClassAndName(factory);
65      }
66      
67      @Test
68      public void testSetObjectClassNameButDoNotInitialise() throws Exception
69      {
70          AbstractObjectFactory factory = getUninitialisedObjectFactory();
71          factory.setObjectClassName(Object.class.getName());
72  
73          assertObjectClassAndName(factory);
74      }
75  
76      @Test
77      public void testSetObjectClassButDoNotInitialise() throws Exception
78      {
79          AbstractObjectFactory factory = getUninitialisedObjectFactory();
80          factory.setObjectClass(Object.class);
81          
82          assertObjectClassAndName(factory);
83      }
84      
85      private void assertObjectClassAndName(AbstractObjectFactory factory)
86      {
87          assertEquals(Object.class, factory.getObjectClass());
88          assertEquals(Object.class.getName(), factory.getObjectClassName());
89      }
90      
91      @Test
92      public void testInitialiseWithClass() throws Exception
93      {
94          AbstractObjectFactory factory = getUninitialisedObjectFactory();
95          factory.setObjectClass(Object.class);
96          // Will init the object        
97          muleContext.getRegistry().applyProcessorsAndLifecycle(factory);
98  
99          assertNotNull(factory.getInstance(muleContext));
100     }
101 
102     @Test
103     public void testInitialiseWithClassName() throws Exception
104     {
105         AbstractObjectFactory factory = getUninitialisedObjectFactory();
106         factory.setObjectClassName(Object.class.getName());
107         // Will init the object
108         muleContext.getRegistry().applyProcessorsAndLifecycle(factory);
109         
110         assertNotNull(factory.getInstance(muleContext));
111     }
112 
113     @Test
114     public void testDispose() throws Exception
115     {
116         AbstractObjectFactory factory = getUninitialisedObjectFactory();
117         factory.setObjectClass(Object.class);
118         // Will init the object
119         muleContext.getRegistry().applyProcessorsAndLifecycle(factory);
120         
121         factory.dispose();
122 
123         try
124         {
125             factory.getInstance(muleContext);
126             fail("expected InitialisationException");
127         }
128         catch (InitialisationException iex)
129         {
130             // OK
131         }
132     }
133     
134     public abstract AbstractObjectFactory getUninitialisedObjectFactory();
135 
136     @Test
137     public abstract void testGetObjectClass() throws Exception;
138 
139     @Test
140     public abstract void testGet() throws Exception;
141     
142     private static class DummyObjectFactory extends AbstractObjectFactory
143     {
144         public DummyObjectFactory(String className)
145         {
146             super(className);
147         }
148         
149         public DummyObjectFactory(Class<?> klass)
150         {
151             super(klass);
152         }
153 
154     }
155 }