1   package com.sourceforge.jpatterns.utils;
2   
3   import com.sourceforge.jpatterns.core.JPConstants;
4   import com.sourceforge.jpatterns.core.configuration.IPropertiesManager;
5   import com.sourceforge.jpatterns.core.configuration.PropertiesManagerImpl;
6   import com.sourceforge.jpatterns.core.configuration.exceptions.JPInitializationException;
7   import com.sourceforge.jpatterns.core.configuration.junit.PropertiesManagerMockImpl;
8   import com.sourceforge.jpatterns.core.configuration.junit.PropertiesManagerMockImplTwo;
9   import com.sourceforge.jpatterns.utils.junit.JPatternsTestUtils;
10  import com.zmicer.utils.LoggingUtils;
11  import com.zmicer.utils.junit.SystemPropsUtils;
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:: zmicer             $<br/>
19   * $Rev:: 67                    $<br/> * $Date:: 2007-08-28 21:37:07 #$<br/>
20   *
21   * @version 1.0.1
22   */
23  public class JPatternsPropsUtilsTest extends TestCase
24  {
25      /**
26       * JVM_OVERRIDEN_FILE_NAME_NOT_EXISTED
27       */
28      final private static String JVM_OVERRIDEN_FILE_NAME_NOT_EXISTED = "JVMOverriden";
29  
30      /**
31       * The test cases for this test.
32       */
33      private interface Cases
34      {
35          String onlyDefaultContains = "onlyDefaultContains";
36          String onlyCustomContains = "onlyCustomContains";
37          String noOneContains = "noOneContains";
38          String bothContain = "bothContain";
39          String wrongImplementation = "wrongImplementation";
40      }
41  
42      /**
43       * Logger instance.
44       */
45      final public static Logger LOG = Logger.getLogger(JPatternsPropsUtilsTest.class);
46  
47      /**
48       * Contructor with name of test attribute.
49       *
50       * @param name name of the test
51       */
52      public JPatternsPropsUtilsTest(String name)
53      {
54          super(name);
55      }
56  
57      /**
58       * Perform the set up functionality for the test.
59       *
60       * @throws Exception may occur in the case of some problems
61       */
62      public void setUp() throws Exception
63      {
64          super.setUp();
65      }
66  
67      /**
68       * Perform the tear down functionality for the test
69       *
70       * @throws Exception may occur in the case of some problems
71       */
72      public void tearDown() throws Exception
73      {
74          super.tearDown();
75      }
76  
77      /**
78       * Test suite method
79       *
80       * @return the built test suite
81       */
82      public static Test suite()
83      {
84          return new TestSuite(JPatternsPropsUtilsTest.class);
85      }
86  
87      /**
88       * Tests {@link com.sourceforge.jpatterns.core.configuration.PropertiesManagerImpl#getDefaultBundleName}
89       *
90       * @throws Exception in the case smth. wrong occuried.
91       * @forversion 1.0
92       */
93      public void testGetDefaultBundleName() throws Exception
94      {
95          // normal sub-case: no exception
96          try
97          {
98              JPatternsTestUtils.resetJVMProps();
99              assertNotNull(JPatternsPropsUtils.getDefaultBundleName());
100             assertEquals(JPatternsPropsUtils.getDefaultBundleName(), JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_BASE_NAME);
101 
102             System.setProperty(JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_FILE_NAME_JVM_PARAM, JVM_OVERRIDEN_FILE_NAME_NOT_EXISTED);
103             assertNotNull(JPatternsPropsUtils.getDefaultBundleName());
104             assertEquals(JPatternsPropsUtils.getDefaultBundleName(), JVM_OVERRIDEN_FILE_NAME_NOT_EXISTED);
105             SystemPropsUtils.clearProps(JPConstants.PropertiesConfigFilesConstants.DEFAULT_PROPERTIES_FILE_NAME_JVM_PARAM);
106         }
107         catch (Exception ex)
108         {
109             LoggingUtils.logException(LOG, ex, null, null);
110             fail("Exception should not occur for that case params" + ex.getMessage());
111         }
112     }
113 
114     /**
115      * Tests {@link com.sourceforge.jpatterns.core.configuration.PropertiesManagerImpl#getCustomBundleName}
116      *
117      * @throws Exception in the case smth. wrong occuried.
118      * @forversion 1.0
119      */
120     public void testGetCustomBundleName() throws Exception
121     {
122         // normal sub-case: no exception
123         try
124         {
125             System.clearProperty(JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_FILE_NAME_JVM_PARAM);
126             assertNotNull(JPatternsPropsUtils.getCustomBundleName());
127             assertEquals(JPatternsPropsUtils.getCustomBundleName(), JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_BASE_NAME);
128 
129             System.setProperty(JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_FILE_NAME_JVM_PARAM, JVM_OVERRIDEN_FILE_NAME_NOT_EXISTED);
130             assertNotNull(JPatternsPropsUtils.getCustomBundleName());
131             assertEquals(JPatternsPropsUtils.getCustomBundleName(), JVM_OVERRIDEN_FILE_NAME_NOT_EXISTED);
132             SystemPropsUtils.clearProps(JPConstants.PropertiesConfigFilesConstants.CUSTOM_PROPERTIES_FILE_NAME_JVM_PARAM);
133         }
134         catch (Exception ex)
135         {
136             LoggingUtils.logException(LOG, ex, null, null);
137             fail("Exception should not occur for that case params" + ex.getMessage());
138         }
139     }
140 
141     /**
142      * Tests {@link JPatternsPropsUtils#getPropertiesManagerImplementation}
143      *
144      * @throws Exception in the case smth. wrong occuried.
145      * @forversion 1.1
146      */
147     public void testGetPropertiesManagerImplementation() throws Exception
148     {
149         // normal sub-case: no exception: custom value: two cases allow us to obtain the custom configuration:
150         // a:when only at the custom file it is specified b: when it specified at the both files
151         try
152         {
153             JPatternsTestUtils.setJVMPropsParams(JPatternsPropsUtilsTest.class, false, Cases.bothContain, null, null, false);
154             IPropertiesManager manager = JPatternsPropsUtils.getPropertiesManagerImplementation();
155             assertNotNull(manager);
156             assertTrue(manager instanceof PropertiesManagerMockImplTwo);
157 
158             JPatternsTestUtils.setJVMPropsParams(JPatternsPropsUtilsTest.class, false, Cases.onlyDefaultContains, null, null, false);
159             manager = JPatternsPropsUtils.getPropertiesManagerImplementation();
160             assertNotNull(manager);
161             assertTrue(manager instanceof PropertiesManagerMockImpl);
162 
163             JPatternsTestUtils.setJVMPropsParams(JPatternsPropsUtilsTest.class, false, Cases.onlyCustomContains, null, null, false);
164             manager = JPatternsPropsUtils.getPropertiesManagerImplementation();
165             assertNotNull(manager);
166             assertTrue(manager instanceof PropertiesManagerMockImplTwo);
167 
168             JPatternsTestUtils.setJVMPropsParams(JPatternsPropsUtilsTest.class, false, Cases.noOneContains, null, null, false);
169             manager = JPatternsPropsUtils.getPropertiesManagerImplementation();
170             assertNotNull(manager);
171             assertTrue(manager instanceof PropertiesManagerImpl);
172 
173             JPatternsTestUtils.setJVMPropsParams(JPatternsPropsUtilsTest.class, false, Cases.wrongImplementation, null, null, false);
174             // failed sub-case: exception occuried:
175             try
176             {
177                 manager = JPatternsPropsUtils.getPropertiesManagerImplementation();
178                 fail("Exception should have been occuried before this line.");
179             }
180             catch (Exception ex)
181             {
182                 LoggingUtils.logException(LOG, ex, null, this.getClass());
183                 assertTrue(ex instanceof IllegalStateException);
184             }
185             finally
186             {
187                 JPatternsTestUtils.resetJVMProps();
188             }
189         }
190         catch (Exception ex)
191         {
192             LoggingUtils.logException(LOG, ex, null, null);
193             fail("Exception should not occur for that case " + ex.getMessage());
194         }
195     }
196 }