View Javadoc

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