1   package com.sourceforge.jpatterns.core.configuration;
2   
3   import com.sourceforge.jpatterns.core.configuration.model.IJPatternsConfigBeansBuilder;
4   import com.sourceforge.jpatterns.core.configuration.model.JPatternsConfigBeansBuilderImpl;
5   import com.sourceforge.jpatterns.core.configuration.model.JPatternsConfigBeansBuilderMockImpl;
6   import com.sourceforge.jpatterns.utils.junit.JPatternsTestUtils;
7   import com.zmicer.utils.LoggingUtils;
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 PropertiesBasedFactoryTest extends TestCase
19  {
20      /**
21       * Logger instance.
22       */
23      final public static Logger LOG = Logger.getLogger(PropertiesBasedFactoryTest.class);
24  
25      /**
26       * Instance of the covered class to be used below in the tests
27       */
28      private PropertiesBasedFactory m_propertiesBasedFactory = PropertiesBasedFactory.getInstance();
29  
30      /**
31       * Interface defining the cases to be used
32       */
33      interface Cases
34      {
35          String ALL_PROPS = "allPropertiesCase";
36          String CUSTOM_PROP = "customPropertyCase";
37          String DEFAULT_PROP = "defaultPropertyCase";
38          String NO_PROPS = "noPropertiesCase";
39      }
40  
41      /**
42       * Contructor with name of test attribute.
43       *
44       * @param name name of the test
45       */
46      public PropertiesBasedFactoryTest(String name)
47      {
48          super(name);
49      }
50  
51      /**
52       * Perform the set up functionality for the test.
53       *
54       * @throws Exception may occur in the case of some problems
55       */
56      public void setUp() throws Exception
57      {
58          super.setUp();
59      }
60  
61      /**
62       * Perform the tear down functionality for the test
63       *
64       * @throws Exception may occur in the case of some problems
65       */
66      public void tearDown() throws Exception
67      {
68          super.tearDown();
69      }
70  
71      /**
72       * Test suite method
73       *
74       * @return the built test suite
75       */
76      public static Test suite()
77      {
78          return new TestSuite(PropertiesBasedFactoryTest.class);
79      }
80  
81      /**
82       * Tests {@link PropertiesBasedFactory#getInstance}
83       *
84       * @throws Exception in the case smth. wrong occuried.
85       * @forversion 1.0
86       */
87      public void testGetInstance() throws Exception
88      {
89          assertNotNull(m_propertiesBasedFactory);
90      }
91  
92      /**
93       * Tests {@link PropertiesBasedFactory#getPropertiesManager}
94       *
95       * @throws Exception in the case smth. wrong occuried.
96       * @forversion 1.0
97       */
98      public void testGetPropertiesManager() throws Exception
99      {
100         assertNotNull(m_propertiesBasedFactory.getPropertiesManager());
101     }
102 
103     /**
104      * Tests {@link PropertiesBasedFactory#getJPConfigurator}
105      *
106      * @throws Exception in the case smth. wrong occuried.
107      * @forversion 1.0
108      */
109     public void testGetJPatternsConfigBeansBuilder() throws Exception
110     {
111         final IPropertiesManager propsManager = m_propertiesBasedFactory.getPropertiesManager();
112         // normal sub-case: on default it would be got from the custom properties
113         try
114         {
115             // 00: trying to obtain the implementation of the IJPConfigurator, on default mock is defined (custom properties has place)
116             // find the way to check the mock implementation
117             JPatternsTestUtils.setJVMPropsParams(PropertiesBasedFactoryTest.class, false, null, null, null, true);
118             m_propertiesBasedFactory.cleanImplementations();
119             IJPatternsConfigBeansBuilder builder = m_propertiesBasedFactory.getJPatternsConfigBaseBeanBuilder();
120             assertNotNull(builder);
121             assertTrue(builder instanceof JPatternsConfigBeansBuilderMockImpl);
122 
123             // 01: setting the cusom properties name to the default one
124             PropertiesBasedFactory.getInstance().cleanImplementations();
125 
126             JPatternsTestUtils.setJVMPropsParams(PropertiesBasedFactoryTest.class, false, Cases.ALL_PROPS, null, null, true);
127             m_propertiesBasedFactory.cleanImplementations();
128             builder = m_propertiesBasedFactory.getJPatternsConfigBaseBeanBuilder();
129             assertNotNull(builder);
130             assertTrue(builder instanceof JPatternsConfigBeansBuilderImpl);
131 
132             // 01: setting the custom properties name to the not existed and default one to the PropertiesBasedFactoryTest.properties -
133             // this file also contains the default implementation.
134             PropertiesBasedFactory.getInstance().cleanImplementations();
135             JPatternsTestUtils.setJVMPropsParams(PropertiesBasedFactoryTest.class, false, null, null, null, true);
136             m_propertiesBasedFactory.cleanImplementations();
137             builder = m_propertiesBasedFactory.getJPatternsConfigBaseBeanBuilder();
138             assertNotNull(builder);
139             assertTrue(builder instanceof JPatternsConfigBeansBuilderMockImpl);
140             JPatternsTestUtils.resetJVMProps();
141         }
142         catch (Exception ex)
143         {
144             LoggingUtils.logException(LOG, ex, null, null);
145             fail("Exception should not occur for that case params" + ex.getMessage());
146         }
147 
148     }
149 
150     /**
151      * Tests {@link PropertiesBasedFactory#getJPatternsConfigBaseBeanBuilder}
152      *
153      * @throws Exception in the case smth. wrong occuried.
154      * @forversion 1.0
155      */
156     public void testGetJPatternsConfigBaseBeanBuilder() throws Exception
157     {
158         // normal sub-case: no exception: check the default implementation exists
159         try
160         {
161             JPatternsTestUtils.setJVMPropsParams(PropertiesBasedFactoryTest.class, false, null, null, null, true);
162 
163             assertNotNull(PropertiesBasedFactory.getInstance());
164             final IJPatternsConfigBeansBuilder configBeansBuilder =
165                 PropertiesBasedFactory.getInstance().getJPatternsConfigBaseBeanBuilder();
166             assertNotNull(configBeansBuilder);
167 
168             JPatternsTestUtils.resetJVMProps();
169         }
170         catch (Exception ex)
171         {
172             LoggingUtils.logException(LOG, ex, null, null);
173             fail("Exception should not occur for that case " + ex.getMessage());
174         }
175     }
176 
177     /**
178      * Tests {@link PropertiesBasedFactory#getJPConfigurator}
179      *
180      * @throws Exception in the case smth. wrong occuried.
181      * @forversion 1.0
182      */
183     public void testGetJPConfigurator() throws Exception
184     {
185         // normal sub-case: no exception: check the default implementation exists
186         try
187         {
188             JPatternsTestUtils.setJVMPropsParams(PropertiesBasedFactoryTest.class, false, Cases.ALL_PROPS, null, null, true);
189             JPatternsTestUtils.setJVMXmlParams(PropertiesBasedFactoryTest.class, false, Cases.ALL_PROPS, null, false);
190 
191             assertNotNull(PropertiesBasedFactory.getInstance());
192             PropertiesBasedFactory.getInstance().cleanImplementations();
193             final IJPConfigurator configBeansBuilder =
194                 PropertiesBasedFactory.getInstance().getJPConfigurator();
195             assertNotNull(configBeansBuilder);
196             assertTrue(configBeansBuilder instanceof JPConfiguratorImpl);
197 
198             JPatternsTestUtils.resetJVMXmlParams();
199             JPatternsTestUtils.resetJVMProps();
200         }
201         catch (Exception ex)
202         {
203             LoggingUtils.logException(LOG, ex, null, null);
204             fail("Exception should not occur for that case " + ex.getMessage());
205         }
206     }
207 }