1   package com.sourceforge.jpatterns.core;
2   
3   import com.sourceforge.jpatterns.core.configuration.model.IJPatternsConfigBeansBuilder;
4   import com.sourceforge.jpatterns.patterns.factory.IJPFactory;
5   import com.sourceforge.jpatterns.utils.junit.JPatternsTestUtils;
6   import com.zmicer.utils.LoggingUtils;
7   import com.zmicer.utils.ReflexionUtils;
8   import junit.framework.Test;
9   import junit.framework.TestCase;
10  import junit.framework.TestSuite;
11  import org.apache.log4j.Logger;
12  
13  /**
14   * $Author:: zmicer             $<br/>
15   * $Rev:: 67                    $<br/> * $Date:: 2007-08-28 21:37:07 #$<br/>
16   * $Date:: 2007-08-28 21:37:07 #$<br/>
17   */
18  public class JPEngineFactoryTest extends TestCase
19  {
20      /**
21       * Logger instance.
22       */
23      final public static Logger LOG = Logger.getLogger(JPEngineFactoryTest.class);
24  
25      /**
26       * Instance of the covered class to be used below in the tests
27       */
28      private JPEngineFactory m_factory = null;
29  
30      /**
31       * The base name of this test (it could be used as identifier for this test case related folder)
32       */
33      private static String baseName = ReflexionUtils.getBaseName(JPEngineFactoryTest.class);
34  
35      /**
36       * Contructor with name of test attribute.
37       *
38       * @param name name of the test
39       */
40      public JPEngineFactoryTest(String name)
41      {
42          super(name);
43      }
44  
45      /**
46       * Perform the set up functionality for the test.
47       *
48       * @throws Exception may occur in the case of some problems
49       */
50      public void setUp() throws Exception
51      {
52          super.setUp();
53      }
54  
55      /**
56       * Perform the tear down functionality for the test
57       *
58       * @throws Exception may occur in the case of some problems
59       */
60      public void tearDown() throws Exception
61      {
62          super.tearDown();
63      }
64  
65      /**
66       * Test suite method
67       *
68       * @return the built test suite
69       */
70      public static Test suite()
71      {
72          return new TestSuite(JPEngineFactoryTest.class);
73      }
74  
75      /**
76       * Tests {@link JPEngineFactory#getJPEngine}
77       *
78       * @throws Exception in the case smth. wrong occuried.
79       * @forversion 1.
80       */
81      public void testGetJPEngine() throws Exception
82      {
83          // normal sub-case: no exception: just check
84          try
85          {
86              JPatternsTestUtils.setJVMPropsParams(JPEngineFactoryTest.class, false, null, null, null, true);
87              JPatternsTestUtils.setJVMXmlParams(JPEngineFactoryTest.class, false, null, "jpatterns_framework_JPEngineFactoryTest.xml", false);
88              m_factory = JPEngineFactory.getInstance();
89              // todo [zmicer]: think if we need this here? (review the appropriate constructor of the JPEngineFactory)
90              m_factory.init();
91              assertNotNull(m_factory);
92              IJPEngine engine = JPEngineFactory.getJPEngine(JPConstants.EngineConfigCaregory.CONSUMER);
93              assertNotNull(engine);
94              final IJPFactory factory_1 = engine.getFactory("factory_" + baseName, null);
95              assertNotNull(factory_1);
96              final IJPFactory factory_2 = engine.getFactory("factory_" + baseName, JPConstants.DEFAULT_SCOPE_NAME);
97              assertNotNull(factory_2);
98              assertNotSame(factory_1, factory_2);
99              final IJPFactory factory_3 = engine.getFactory("factory_" + baseName, "scope_" + baseName);
100             assertNotNull(factory_3);
101             assertNotSame(factory_2, factory_3);
102             assertNotSame(factory_1, factory_3);
103 
104             IJPatternsConfigBeansBuilder builder =
105                 (IJPatternsConfigBeansBuilder) factory_1.getImplementation(IJPatternsConfigBeansBuilder.class, null);
106             assertNotNull(builder);
107             assertNotNull((IJPatternsConfigBeansBuilder) factory_1.getImplementation(
108                 "item_" + ReflexionUtils.getBaseName(IJPatternsConfigBeansBuilder.class) , "RED", null));
109             assertNotNull((IJPatternsConfigBeansBuilder) factory_1.getImplementation(
110                 "item_" + ReflexionUtils.getBaseName(IJPatternsConfigBeansBuilder.class) , "BLUE", null));
111 
112             engine = JPEngineFactory.getJPEngine(JPConstants.EngineConfigCaregory.FRAMEWORK);
113             assertNotNull(engine);
114 
115             
116             JPatternsTestUtils.resetJVMXmlParams();
117             JPatternsTestUtils.resetJVMProps();
118         }
119         catch (Exception ex)
120         {
121             LoggingUtils.logException(LOG, ex, null, null);
122             fail("Exception should not occur for that case " + ex.getMessage());
123         }
124     }
125 }