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
19
20
21
22
23 public class JPatternsPropsUtilsTest extends TestCase
24 {
25
26
27
28 final private static String JVM_OVERRIDEN_FILE_NAME_NOT_EXISTED = "JVMOverriden";
29
30
31
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
44
45 final public static Logger LOG = Logger.getLogger(JPatternsPropsUtilsTest.class);
46
47
48
49
50
51
52 public JPatternsPropsUtilsTest(String name)
53 {
54 super(name);
55 }
56
57
58
59
60
61
62 public void setUp() throws Exception
63 {
64 super.setUp();
65 }
66
67
68
69
70
71
72 public void tearDown() throws Exception
73 {
74 super.tearDown();
75 }
76
77
78
79
80
81
82 public static Test suite()
83 {
84 return new TestSuite(JPatternsPropsUtilsTest.class);
85 }
86
87
88
89
90
91
92
93 public void testGetDefaultBundleName() throws Exception
94 {
95
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
116
117
118
119
120 public void testGetCustomBundleName() throws Exception
121 {
122
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
143
144
145
146
147 public void testGetPropertiesManagerImplementation() throws Exception
148 {
149
150
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
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 }