1   package com.sourceforge.jpatterns.core.configuration;
2   
3   import com.sourceforge.jpatterns.utils.junit.JPatternsTestUtils;
4   import com.zmicer.utils.LoggingUtils;
5   import junit.framework.Test;
6   import junit.framework.TestCase;
7   import junit.framework.TestSuite;
8   import org.apache.log4j.Logger;
9   
10  /**
11   * $Author:: zmicer             $<br/>
12   * $Rev:: 67                    $<br/> * $Date:: 2007-08-28 21:37:07 #$<br/>
13   * $Date:: 2007-08-28 21:37:07 #$<br/>
14   */
15  public class PropertiesProviderTest extends TestCase
16  {
17      /**
18       * Logger instance.
19       */
20      final public static Logger LOG = Logger.getLogger(PropertiesProviderTest.class);
21  
22      /**
23       * Instance of the covered class to be used below in the tests
24       */
25      private PropertiesProvider m_propertiesProvider = null;
26  
27      /**
28       * <code>STRING_PROPERTY_KEY</code>
29       */
30      private final static String STRING_PROPERTY_KEY = "StringPropertyKey";
31  
32      /**
33       * <code>STRING_PROPERTY_VALUE</code>
34       */
35      private final static String STRING_PROPERTY_VALUE = "StringPropertyValue";
36  
37      /**
38       * Contructor with name of test attribute.
39       *
40       * @param name name of the test
41       */
42      public PropertiesProviderTest(String name)
43      {
44          super(name);
45      }
46  
47      /**
48       * Perform the set up functionality for the test.
49       *
50       * @throws Exception may occur in the case of some problems
51       */
52      public void setUp() throws Exception
53      {
54          super.setUp();
55      }
56  
57      /**
58       * Perform the tear down functionality for the test
59       *
60       * @throws Exception may occur in the case of some problems
61       */
62      public void tearDown() throws Exception
63      {
64          super.tearDown();
65      }
66  
67      /**
68       * Test suite method
69       *
70       * @return the built test suite
71       */
72      public static Test suite()
73      {
74          return new TestSuite(PropertiesProviderTest.class);
75      }
76  
77      /**
78       * Tests {@link PropertiesProvider#checkIfInitialized}
79       *
80       * @throws Exception in the case smth. wrong occuried.
81       * @forversion 1.0
82       */
83      public void testCheckIfInitialized() throws Exception
84      {
85          // normal sub-case: no exception: just to check that the initialization process was succesfull.
86          try
87          {
88              JPatternsTestUtils.setJVMPropsParams(PropertiesProviderTest.class, false, null, null, null, true);
89              JPatternsTestUtils.setJVMXmlParams(PropertiesProviderTest.class, false, null, null, false);
90              m_propertiesProvider = PropertiesProvider.getInstance(PropertiesBasedFactory.getInstance().getPropertiesManager(), true);
91  
92              m_propertiesProvider.checkIfInitialized();
93  
94              JPatternsTestUtils.resetJVMProps();
95              JPatternsTestUtils.resetJVMXmlParams();
96          }
97          catch (Exception ex)
98          {
99              LoggingUtils.logException(LOG, ex, null, null);
100             fail("Exception should not occur for that case params" + ex.getMessage());
101         }
102 
103     }
104 
105     /**
106      * Tests {@link PropertiesProvider#getStringProperty}
107      *
108      * @throws Exception in the case smth. wrong occuried.
109      * @forversion 1.0
110      */
111     public void testGetStringProperty() throws Exception
112     {
113         JPatternsTestUtils.setJVMPropsParams(PropertiesProviderTest.class, false, null, null, null, true);
114         JPatternsTestUtils.setJVMXmlParams(PropertiesProviderTest.class, false, null, null, false);
115         m_propertiesProvider = PropertiesProvider.getInstance(PropertiesBasedFactory.getInstance().getPropertiesManager(), true);
116         // failed sub-case: exception occuried: invalid parameter
117         try
118         {
119 
120             m_propertiesProvider.getStringProperty(null);
121             fail("Exception should have been occuried before this line.");
122         }
123         catch (Exception ex)
124         {
125             assertTrue(ex instanceof IllegalArgumentException);
126         }
127         // failed sub-case: exception occuried: invalid parameter
128         try
129         {
130 
131             m_propertiesProvider.getStringProperty("");
132             fail("Exception should have been occuried before this line.");
133         }
134         catch (Exception ex)
135         {
136             assertTrue(ex instanceof IllegalArgumentException);
137         }
138         // normal sub-case: no exception: check the case when string properties do not contain the properties related to the classes
139         // creation.
140         try
141         {
142             assertNull(m_propertiesProvider.getStringProperty("IJPConfigurator"));
143             assertNull(m_propertiesProvider.getStringProperty("IJPConfigsMerger"));
144             assertNull(m_propertiesProvider.getStringProperty(PropertiesProvider.JPProperties.USE_ONLY_CUSTOM_CONSUMER_XML));
145             assertNull(m_propertiesProvider.getStringProperty(PropertiesProvider.JPProperties.USE_ONLY_CUSTOM_FRAMEWORK_PROPERTIES));
146             assertNull(m_propertiesProvider.getStringProperty(PropertiesProvider.JPProperties.USE_ONLY_CUSTOM_FRAMEWORK_XML));
147 
148             // they are not null cause they define the implementation incorrectly
149             assertNotNull(m_propertiesProvider.getStringProperty("TestCase"));
150             assertNotNull(m_propertiesProvider.getStringProperty("PropertiesBasedFactory"));
151 
152             // string property
153             assertNotNull(m_propertiesProvider.getStringProperty(STRING_PROPERTY_KEY));
154             assertEquals(m_propertiesProvider.getStringProperty(STRING_PROPERTY_KEY), STRING_PROPERTY_VALUE);
155         }
156         catch (Exception ex)
157         {
158             LoggingUtils.logException(LOG, ex, null, null);
159             fail("Exception should not occur for that case params" + ex.getMessage());
160         }
161         JPatternsTestUtils.resetJVMProps();
162         JPatternsTestUtils.resetJVMXmlParams();
163     }
164 
165     /**
166      * Tests {@link PropertiesProvider#getBooleanProperty}
167      *
168      * @throws Exception in the case smth. wrong occuried.
169      * @forversion 1.0
170      */
171     public void testGetBooleanProperty() throws Exception
172     {
173         JPatternsTestUtils.setJVMPropsParams(PropertiesProviderTest.class, false, null, null, null, true);
174         JPatternsTestUtils.setJVMXmlParams(PropertiesProviderTest.class, false, null, null, false);
175         m_propertiesProvider = PropertiesProvider.getInstance(PropertiesBasedFactory.getInstance().getPropertiesManager(), true);
176         // failed sub-case: exception occuried: invalid parameter
177         try
178         {
179 
180             m_propertiesProvider.getBooleanProperty(null);
181             fail("Exception should have been occuried before this line.");
182         }
183         catch (Exception ex)
184         {
185             assertTrue(ex instanceof IllegalArgumentException);
186         }
187         // failed sub-case: exception occuried: invalid parameter
188         try
189         {
190 
191             m_propertiesProvider.getBooleanProperty("");
192             fail("Exception should have been occuried before this line.");
193         }
194         catch (Exception ex)
195         {
196             assertTrue(ex instanceof IllegalArgumentException);
197         }
198         // normal sub-case: no exception: check the case when string properties do not contain the properties related to the classes
199         // creation.
200         try
201         {
202             assertNull(m_propertiesProvider.getBooleanProperty("IJPConfigurator"));
203             assertNull(m_propertiesProvider.getBooleanProperty("IJPConfigsMerger"));
204             assertNotNull(m_propertiesProvider.getBooleanProperty(PropertiesProvider.JPProperties.USE_ONLY_CUSTOM_CONSUMER_XML));
205             assertNotNull(m_propertiesProvider.getBooleanProperty(PropertiesProvider.JPProperties.USE_ONLY_CUSTOM_FRAMEWORK_PROPERTIES));
206             assertNotNull(m_propertiesProvider.getBooleanProperty(PropertiesProvider.JPProperties.USE_ONLY_CUSTOM_FRAMEWORK_XML));
207 
208             // they are not null cause they define the implementation incorrectly
209             assertNull(m_propertiesProvider.getBooleanProperty("TestCase"));
210             assertNull(m_propertiesProvider.getBooleanProperty("PropertiesBasedFactory"));
211 
212             // string property
213             assertNull(m_propertiesProvider.getBooleanProperty(STRING_PROPERTY_KEY));
214         }
215         catch (Exception ex)
216         {
217             LoggingUtils.logException(LOG, ex, null, null);
218             fail("Exception should not occur for that case params" + ex.getMessage());
219         }
220         JPatternsTestUtils.resetJVMProps();
221         JPatternsTestUtils.resetJVMXmlParams();
222     }
223 
224     /**
225      * Tests {@link PropertiesProvider#getInstance}
226      *
227      * @throws Exception in the case smth. wrong occuried.
228      * @forversion 1.0
229      */
230     public void testGetInstance() throws Exception
231     {
232         JPatternsTestUtils.setJVMPropsParams(PropertiesProviderTest.class, false, null, null, null, true);
233         JPatternsTestUtils.setJVMXmlParams(PropertiesProviderTest.class, false, null, null, false);
234         m_propertiesProvider = PropertiesProvider.getInstance(PropertiesBasedFactory.getInstance().getPropertiesManager(), true);
235 
236         assertNotNull(PropertiesBasedFactory.getInstance());
237         final IPropertiesManager manager = PropertiesBasedFactory.getInstance().getPropertiesManager();
238         assertNotNull(manager);
239         assertNotNull(PropertiesProvider.getInstance(manager, true));
240         JPatternsTestUtils.resetJVMProps();
241         JPatternsTestUtils.resetJVMXmlParams();
242     }
243 
244     /**
245      * Tests {@link PropertiesProvider#init}
246      * <br/>
247      * Just to call. The real test - is in the methods getStringProperty and getBooleanProperty
248      *
249      * @throws Exception in the case smth. wrong occuried.
250      * @forversion 1.0
251      */
252     public void testInit() throws Exception
253     {
254         JPatternsTestUtils.setJVMPropsParams(PropertiesProviderTest.class, false, null, null, null, true);
255         JPatternsTestUtils.setJVMXmlParams(PropertiesProviderTest.class, false, null, null, false);
256         m_propertiesProvider = PropertiesProvider.getInstance(PropertiesBasedFactory.getInstance().getPropertiesManager(), true);
257         // normal sub-case: no exception:
258         try
259         {
260             assertNotNull(PropertiesBasedFactory.getInstance());
261             final IPropertiesManager manager = PropertiesBasedFactory.getInstance().getPropertiesManager();
262             assertNotNull(manager);
263             m_propertiesProvider.init(manager);
264         }
265         catch (Exception ex)
266         {
267             LoggingUtils.logException(LOG, ex, null, null);
268             fail("Exception should not occur for that case params" + ex.getMessage());
269         }
270         JPatternsTestUtils.resetJVMProps();
271         JPatternsTestUtils.resetJVMXmlParams();
272     }
273 
274     /**
275      * Perform the check for the overriding depth enum value
276      */
277     public void testOverridingDepthValues()
278     {
279         // normal sub-case: no exception: check all the values.
280         try
281         {
282             assertEquals(PropertiesProvider.OverridingDepths.OVERRIDING_LEVEL_ITEM.toString(), "OVERRIDING_LEVEL_ITEM");
283             assertEquals(PropertiesProvider.OverridingDepths.OVERRIDING_LEVEL_SECTION.toString(), "OVERRIDING_LEVEL_SECTION");       
284         }
285         catch (Exception ex)
286         {
287             LoggingUtils.logException(LOG, ex, null, null);
288             fail("Exception should not occur for that case " + ex.getMessage());
289         }
290     }
291 }