1   package com.sourceforge.jpatterns.core;
2   
3   import com.sourceforge.jpatterns.core.configuration.exceptions.JPConfigException;
4   import com.sourceforge.jpatterns.core.configuration.exceptions.JPConsumerConfigException;
5   import com.sourceforge.jpatterns.core.configuration.exceptions.JPInitializationException;
6   import com.sourceforge.jpatterns.patterns.config.IJPConfig;
7   import com.sourceforge.jpatterns.patterns.factory.IJPFactory;
8   import com.sourceforge.jpatterns.utils.junit.JPatternsTestUtils;
9   import com.zmicer.utils.LoggingUtils;
10  import com.zmicer.utils.junit.JUnitUtils;
11  import com.zmicer.utils.junit.ZmicerTestUtils;
12  import junit.framework.Test;
13  import junit.framework.TestCase;
14  import junit.framework.TestSuite;
15  import org.apache.log4j.Logger;
16  
17  /**
18   * $Author::                    $<br/>
19   * $Rev::                       $<br/>
20   * $Date::                      $<br/>
21   */
22  public class JPEngineImplTest extends TestCase
23  {
24      /**
25       * Logger instance.
26       */
27      final public static Logger LOG = Logger.getLogger(JPEngineImplTest.class);
28  
29      /**
30       * Instance of the covered class to be used below in the tests
31       */
32      private JPEngineImpl m_jpEngineImpl = null;
33  
34      /**
35       * Contructor with name of test attribute.
36       *
37       * @param name name of the test
38       */
39      public JPEngineImplTest(String name)
40      {
41          super(name);
42      }
43  
44      /**
45       * Perform the set up functionality for the test.
46       *
47       * @throws Exception may occur in the case of some problems
48       */
49      public void setUp() throws Exception
50      {
51          super.setUp();
52      }
53  
54      /**
55       * Perform the tear down functionality for the test
56       *
57       * @throws Exception may occur in the case of some problems
58       */
59      public void tearDown() throws Exception
60      {
61          super.tearDown();
62      }
63  
64      /**
65       * Test suite method
66       *
67       * @return the built test suite
68       */
69      public static Test suite()
70      {
71          return new TestSuite(JPEngineImplTest.class);
72      }
73  
74      /**
75       * Just init the testing environment
76       *
77       * @param testCaseName       the name of the folder where XML config would be placed
78       * @param considerMethodName if it is true - we would apply the test method name to the full path
79       */
80      private void initCastorSections(final String testCaseName, boolean considerMethodName)
81      {
82          JPatternsTestUtils.setJVMPropsParams(ZmicerTestUtils.CommonTestCases.COMMON_PROPS, null, null, true);
83          JPatternsTestUtils.setJVMXmlParams(JPEngineImplTest.class, considerMethodName, testCaseName, null, true);
84          m_jpEngineImpl = new JPEngineImpl();
85          m_jpEngineImpl.init(JPConstants.EngineConfigCaregory.CONSUMER);
86      }
87  
88      /**
89       * Tests {@link JPEngineImpl#getFactory}
90       *
91       * @throws Exception in the case smth. wrong occuried.
92       * @forversion 1.0
93       */
94      public void testGetFactory() throws Exception
95      {
96          final String case1 = "factoryPresents";
97          // failed sub-case: exception occuried: illegal arguments
98          try
99          {
100             initCastorSections(case1, true);
101             m_jpEngineImpl.getFactory(null, null);
102             fail("Exception should have been occuried before this line.");
103         }
104         catch (Exception ex)
105         {
106             assertTrue(ex instanceof IllegalArgumentException);
107         }
108         finally
109         {
110             JPatternsTestUtils.resetAll();
111         }
112 
113         // failed sub-case: exception occuried: illegal arguments
114         try
115         {
116             initCastorSections(case1, true);
117             m_jpEngineImpl.getFactory(null, "DEFAULT_SCOPE");
118             fail("Exception should have been occuried before this line.");
119         }
120         catch (Exception ex)
121         {
122             if (!(ex instanceof IllegalArgumentException))
123             {
124                 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPEngineImplTest.class);
125             }
126             assertTrue(ex instanceof IllegalArgumentException);
127         }
128         finally
129         {
130             JPatternsTestUtils.resetAll();
131         }
132 
133         // normal sub-case: no exception: A: valid configuration, castor factory section pointed at the input parameters presents at the
134         // consumer configuration
135         try
136         {
137             initCastorSections(case1, true);
138             final IJPFactory factory = m_jpEngineImpl.getFactory("FactorySection", "FactoryScope");
139             assertNotNull(factory);
140         }
141         catch (Exception ex)
142         {
143             final String excMessage = "Exception should not occur for that case ";
144             LoggingUtils.logException(LOG, ex, excMessage, null);
145             fail(excMessage + ex.getMessage());
146         }
147         finally
148         {
149             JPatternsTestUtils.resetAll();
150         }
151         final String case2 = "factoryDoesntPresents";
152         // failed sub-case: exception occuried: review the name of case2 
153         try
154         {
155             initCastorSections(case2, true);
156             final IJPFactory factory = m_jpEngineImpl.getFactory("FactorySection", "FactoryScope");
157             fail("Exception should have been occuried before this line.");
158         }
159         catch (Exception ex)
160         {
161             if (!(ex instanceof JPConsumerConfigException))
162             {
163                 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPEngineImplTest.class);
164             }
165             assertTrue(ex instanceof JPConsumerConfigException);
166         }
167         finally
168         {
169             JPatternsTestUtils.resetAll();
170         }
171 
172         final String case3 = "incorrectTypeOfSection";
173         // failed sub-case: exception occuried: review the name of case2
174         try
175         {
176             initCastorSections(case3, true);
177             final IJPFactory factory = m_jpEngineImpl.getFactory("FactorySection", "FactoryScope");
178             fail("Exception should have been occuried before this line.");
179         }
180         catch (Exception ex)
181         {
182             if (!(ex instanceof JPConsumerConfigException))
183             {
184                 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPEngineImplTest.class);
185             }
186             assertTrue(ex instanceof JPConsumerConfigException);
187         }
188         finally
189         {
190             JPatternsTestUtils.resetAll();
191         }
192     }
193 
194     /**
195      * Tests {@link JPEngineImpl#init} && {@link com.sourceforge.jpatterns.core.JPEngineImpl#getCategory()} && {@link JPEngineImpl#isInitialized()}
196      *
197      * @throws Exception in the case smth. wrong occuried.
198      * @forversion 1.0
199      */
200     public void testInit() throws Exception
201     {
202         initCastorSections(null, true);
203         assertNotNull(m_jpEngineImpl);
204         // A: null pointers
205         JUnitUtils.checkOnWrongArgs(m_jpEngineImpl, JUnitUtils.getRunningMethodName(), new boolean[]{true},
206             new Class[]{JPConstants.EngineConfigCaregory.class},
207             JPConstants.EngineConfigCaregory.CONSUMER);
208 
209         // B: not initialized
210         m_jpEngineImpl = new JPEngineImpl();
211         assertTrue(null == m_jpEngineImpl.getCategory());
212         assertFalse(m_jpEngineImpl.isInitialized());
213         JUnitUtils.runFailure(m_jpEngineImpl, "getFactory", JPInitializationException.class,
214             new Class[]{String.class, String.class},
215             "string", "string");
216 
217         // C: initialized
218         try
219         {
220             m_jpEngineImpl.init(JPConstants.EngineConfigCaregory.CONSUMER);
221             assertTrue(m_jpEngineImpl.isInitialized());
222             final IJPFactory factory = m_jpEngineImpl.getFactory("FactorySection", "FactoryScope");
223             assertNotNull(factory);
224             assertEquals(m_jpEngineImpl.getCategory(), JPConstants.EngineConfigCaregory.CONSUMER);
225             assertTrue(m_jpEngineImpl.isInitialized());
226             m_jpEngineImpl.init(JPConstants.EngineConfigCaregory.FRAMEWORK);
227             assertTrue(m_jpEngineImpl.isInitialized());
228             assertEquals(m_jpEngineImpl.getCategory(), JPConstants.EngineConfigCaregory.FRAMEWORK);
229         }
230         catch (Exception ex)
231         {
232             final String excMessage = "Exception should not occur for that case ";
233             LoggingUtils.logException(LOG, ex, excMessage, null);
234             fail(excMessage + ex.getMessage());
235         }
236     }
237 
238     /**
239      * Tests {@link JPEngineImpl#getConfig}
240      *
241      * @throws Exception in the case smth. wrong occuried.
242      * @forversion 1.0
243      */
244     public void testGetConfig() throws Exception
245     {
246         // failed sub-case: exception occuried: illegal arguments
247         try
248         {
249             initCastorSections(null, true);
250             m_jpEngineImpl.getConfig(null, null);
251             fail("Exception should have been occuried before this line.");
252         }
253         catch (Exception ex)
254         {
255             assertTrue(ex instanceof IllegalArgumentException);
256         }
257         finally
258         {
259             JPatternsTestUtils.resetAll();
260         }
261 
262         // failed sub-case: exception occuried: illegal arguments
263         try
264         {
265             initCastorSections(null, true);
266             m_jpEngineImpl.getConfig(null, "DEFAULT_SCOPE");
267             fail("Exception should have been occuried before this line.");
268         }
269         catch (Exception ex)
270         {
271             if (!(ex instanceof IllegalArgumentException))
272             {
273                 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPEngineImplTest.class);
274             }
275             assertTrue(ex instanceof IllegalArgumentException);
276         }
277         finally
278         {
279             JPatternsTestUtils.resetAll();
280         }
281 
282         // normal sub-case: no exception: A: valid configuration, castor factory section pointed at the input parameters presents at the
283         // consumer configuration
284         try
285         {
286             initCastorSections(null, true);
287             final IJPConfig config = m_jpEngineImpl.getConfig("ConfigSection", JPConstants.DEFAULT_SCOPE_NAME);
288             assertNotNull(config);
289             final String value = config.getValue("ConfigItemName1", null);
290             assertNotNull(value);
291             assertEquals(value, "value1");
292         }
293         catch (Exception ex)
294         {
295             final String excMessage = "Exception should not occur for that case ";
296             LoggingUtils.logException(LOG, ex, excMessage, null);
297             fail(excMessage + ex.getMessage());
298         }
299         finally
300         {
301             JPatternsTestUtils.resetAll();
302         }
303 
304         // the case config doesn't exist
305         try
306         {
307             initCastorSections(null, true);
308             final IJPConfig config = m_jpEngineImpl.getConfig("ConfigSection_notExisted", JPConstants.DEFAULT_SCOPE_NAME);
309             fail("Exception should have been occuried before this line.");
310         }
311         catch (Exception ex)
312         {
313             if (!(ex instanceof JPConfigException))
314             {
315                 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPEngineImplTest.class);
316             }
317             assertTrue(ex instanceof JPConfigException);
318         }
319         finally
320         {
321             JPatternsTestUtils.resetAll();
322         }
323     }
324 }