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
12
13
14
15 public class PropertiesProviderTest extends TestCase
16 {
17
18
19
20 final public static Logger LOG = Logger.getLogger(PropertiesProviderTest.class);
21
22
23
24
25 private PropertiesProvider m_propertiesProvider = null;
26
27
28
29
30 private final static String STRING_PROPERTY_KEY = "StringPropertyKey";
31
32
33
34
35 private final static String STRING_PROPERTY_VALUE = "StringPropertyValue";
36
37
38
39
40
41
42 public PropertiesProviderTest(String name)
43 {
44 super(name);
45 }
46
47
48
49
50
51
52 public void setUp() throws Exception
53 {
54 super.setUp();
55 }
56
57
58
59
60
61
62 public void tearDown() throws Exception
63 {
64 super.tearDown();
65 }
66
67
68
69
70
71
72 public static Test suite()
73 {
74 return new TestSuite(PropertiesProviderTest.class);
75 }
76
77
78
79
80
81
82
83 public void testCheckIfInitialized() throws Exception
84 {
85
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
107
108
109
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
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
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
139
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
149 assertNotNull(m_propertiesProvider.getStringProperty("TestCase"));
150 assertNotNull(m_propertiesProvider.getStringProperty("PropertiesBasedFactory"));
151
152
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
167
168
169
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
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
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
199
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
209 assertNull(m_propertiesProvider.getBooleanProperty("TestCase"));
210 assertNull(m_propertiesProvider.getBooleanProperty("PropertiesBasedFactory"));
211
212
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
226
227
228
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
246
247
248
249
250
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
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
276
277 public void testOverridingDepthValues()
278 {
279
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 }