1 | /*
|
---|
2 | * To change this template, choose Tools | Templates
|
---|
3 | * and open the template in the editor.
|
---|
4 | */
|
---|
5 | package gov.hhs.fha.nhinc.common.propaccessor;
|
---|
6 |
|
---|
7 | import gov.hhs.fha.nhinc.common.propertyaccess.DumpPropsToLogRequestType;
|
---|
8 | import gov.hhs.fha.nhinc.common.propertyaccess.DumpPropsToLogResponseType;
|
---|
9 | import gov.hhs.fha.nhinc.common.propertyaccess.ForceRefreshRequestType;
|
---|
10 | import gov.hhs.fha.nhinc.common.propertyaccess.ForceRefreshResponseType;
|
---|
11 | import gov.hhs.fha.nhinc.common.propertyaccess.GetDurationBeforeNextRefreshRequestType;
|
---|
12 | import gov.hhs.fha.nhinc.common.propertyaccess.GetDurationBeforeNextRefreshResponseType;
|
---|
13 | import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertiesRequestType;
|
---|
14 | import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertiesResponseType;
|
---|
15 | import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertyBooleanRequestType;
|
---|
16 | import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertyBooleanResponseType;
|
---|
17 | import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertyRequestType;
|
---|
18 | import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertyNamesRequestType;
|
---|
19 | import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertyNamesResponseType;
|
---|
20 | import gov.hhs.fha.nhinc.common.propertyaccess.GetPropertyResponseType;
|
---|
21 | import gov.hhs.fha.nhinc.common.propertyaccess.GetRefreshDurationRequestType;
|
---|
22 | import gov.hhs.fha.nhinc.common.propertyaccess.GetRefreshDurationResponseType;
|
---|
23 | import gov.hhs.fha.nhinc.common.propertyaccess.PropertyType;
|
---|
24 |
|
---|
25 | import gov.hhs.fha.nhinc.properties.PropertyAccessor;
|
---|
26 |
|
---|
27 | import java.io.File;
|
---|
28 | import java.io.FileReader;
|
---|
29 | import java.io.FileWriter;
|
---|
30 | import java.util.Iterator;
|
---|
31 | import java.util.List;
|
---|
32 | import java.util.Properties;
|
---|
33 | import org.junit.After;
|
---|
34 | import org.junit.AfterClass;
|
---|
35 | import org.junit.Before;
|
---|
36 | import org.junit.BeforeClass;
|
---|
37 | import org.junit.Test;
|
---|
38 | import static org.junit.Assert.*;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | *
|
---|
42 | * @author westbergl
|
---|
43 | */
|
---|
44 | public class PropertyAccessHelperTest
|
---|
45 | {
|
---|
46 | private static String m_sPropertiesDir = "";
|
---|
47 |
|
---|
48 | private static final String CACHE_REFRESH_DURATION_PROPNAME = "CacheRefreshDuration";
|
---|
49 | private static final int iCACHE_REFRESH_DURATION = 10000; // 10 SECONDS
|
---|
50 | private static final String CACHE_REFRESH_DURATION = iCACHE_REFRESH_DURATION + ""; // 10 SECONDS
|
---|
51 |
|
---|
52 | private static final String PROPERTY_FILENAME_NEVER = "testnever";
|
---|
53 | private static final String PROPERTY_FILENAME_ALWAYS = "testalways";
|
---|
54 | private static final String PROPERTY_FILENAME_PERIODIC = "testperiodic";
|
---|
55 | private static final String PROPERTY_FILENAME_PERIODIC_2 = "testperiodic2"; // Used to test Set capabilities.
|
---|
56 |
|
---|
57 | private static final String PROPERTY_NAME_1 = "Property1";
|
---|
58 | private static final String PROPERTY_VALUE_1 = "Value1";
|
---|
59 | private static final String PROPERTY_NAME_2 = "Property2";
|
---|
60 | private static final String PROPERTY_VALUE_2 = "Value2";
|
---|
61 | private static final String PROPERTY_NAME_3 = "Property3";
|
---|
62 | private static final String PROPERTY_VALUE_3 = "Value3";
|
---|
63 | private static final String PROPERTY_NAME_4 = "Property4";
|
---|
64 | private static final String PROPERTY_VALUE_4 = "T";
|
---|
65 | private static final String PROPERTY_NAME_5 = "Property5";
|
---|
66 | private static final String PROPERTY_VALUE_5 = "t";
|
---|
67 | private static final String PROPERTY_NAME_6 = "Property6";
|
---|
68 | private static final String PROPERTY_VALUE_6 = "TrUe";
|
---|
69 |
|
---|
70 | public PropertyAccessHelperTest()
|
---|
71 | {
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * This method is used to load up the property object
|
---|
76 | * with the standard set of properties.
|
---|
77 | *
|
---|
78 | * @param oProps The property object to load up.
|
---|
79 | */
|
---|
80 | private static void loadProps(Properties oProps)
|
---|
81 | {
|
---|
82 | oProps.setProperty(PROPERTY_NAME_1, PROPERTY_VALUE_1);
|
---|
83 | oProps.setProperty(PROPERTY_NAME_2, PROPERTY_VALUE_2);
|
---|
84 | oProps.setProperty(PROPERTY_NAME_3, PROPERTY_VALUE_3);
|
---|
85 | oProps.setProperty(PROPERTY_NAME_4, PROPERTY_VALUE_4);
|
---|
86 | oProps.setProperty(PROPERTY_NAME_5, PROPERTY_VALUE_5);
|
---|
87 | oProps.setProperty(PROPERTY_NAME_6, PROPERTY_VALUE_6);
|
---|
88 | }
|
---|
89 |
|
---|
90 | @BeforeClass
|
---|
91 | public static void setUpClass() throws Exception
|
---|
92 | {
|
---|
93 | m_sPropertiesDir = PropertyAccessor.getPropertyFileLocation();
|
---|
94 |
|
---|
95 | // Create some property files to be used for our testing...
|
---|
96 | //---------------------------------------------------------
|
---|
97 |
|
---|
98 | // Never Refresh
|
---|
99 | //---------------
|
---|
100 | Properties oPropsNever = new Properties();
|
---|
101 | loadProps(oPropsNever);
|
---|
102 |
|
---|
103 | Properties oPropsAlways = new Properties();
|
---|
104 | oPropsAlways.setProperty(CACHE_REFRESH_DURATION_PROPNAME, "0");
|
---|
105 | loadProps(oPropsAlways);
|
---|
106 |
|
---|
107 | Properties oPropsPeriodic = new Properties();
|
---|
108 | oPropsPeriodic.setProperty(CACHE_REFRESH_DURATION_PROPNAME, CACHE_REFRESH_DURATION);
|
---|
109 | loadProps(oPropsPeriodic);
|
---|
110 |
|
---|
111 | Properties oPropsPeriodic2 = new Properties();
|
---|
112 | oPropsPeriodic2.setProperty(CACHE_REFRESH_DURATION_PROPNAME, CACHE_REFRESH_DURATION);
|
---|
113 | loadProps(oPropsPeriodic2);
|
---|
114 |
|
---|
115 | FileWriter fwPropsNever = null;
|
---|
116 | FileWriter fwPropsAlways = null;
|
---|
117 | FileWriter fwPropsPeriodic = null;
|
---|
118 | FileWriter fwPropsPeriodic2 = null;
|
---|
119 | try
|
---|
120 | {
|
---|
121 | fwPropsNever = new FileWriter(m_sPropertiesDir + PROPERTY_FILENAME_NEVER + ".properties");
|
---|
122 | oPropsNever.store(fwPropsNever, "");
|
---|
123 |
|
---|
124 | fwPropsAlways = new FileWriter(m_sPropertiesDir + PROPERTY_FILENAME_ALWAYS + ".properties");
|
---|
125 | oPropsAlways.store(fwPropsAlways, "");
|
---|
126 |
|
---|
127 | fwPropsPeriodic = new FileWriter(m_sPropertiesDir + PROPERTY_FILENAME_PERIODIC + ".properties");
|
---|
128 | oPropsPeriodic.store(fwPropsPeriodic, "");
|
---|
129 |
|
---|
130 | fwPropsPeriodic2 = new FileWriter(m_sPropertiesDir + PROPERTY_FILENAME_PERIODIC_2 + ".properties");
|
---|
131 | oPropsPeriodic2.store(fwPropsPeriodic2, "");
|
---|
132 | }
|
---|
133 | finally
|
---|
134 | {
|
---|
135 | if (fwPropsNever != null)
|
---|
136 | {
|
---|
137 | fwPropsNever.close();
|
---|
138 | fwPropsNever = null;
|
---|
139 | }
|
---|
140 |
|
---|
141 | if (fwPropsAlways != null)
|
---|
142 | {
|
---|
143 | fwPropsAlways.close();
|
---|
144 | fwPropsAlways = null;
|
---|
145 | }
|
---|
146 |
|
---|
147 | if (fwPropsPeriodic != null)
|
---|
148 | {
|
---|
149 | fwPropsPeriodic.close();
|
---|
150 | fwPropsPeriodic = null;
|
---|
151 | }
|
---|
152 |
|
---|
153 | if (fwPropsPeriodic2 != null)
|
---|
154 | {
|
---|
155 | fwPropsPeriodic2.close();
|
---|
156 | fwPropsPeriodic2 = null;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | }
|
---|
161 |
|
---|
162 | @AfterClass
|
---|
163 | public static void tearDownClass() throws Exception
|
---|
164 | {
|
---|
165 | // delete our test property files...
|
---|
166 | //----------------------------------
|
---|
167 | File fPropsNever = new File(m_sPropertiesDir + PROPERTY_FILENAME_NEVER + ".properties");
|
---|
168 | fPropsNever.delete();
|
---|
169 |
|
---|
170 | File fPropsAlways = new File(m_sPropertiesDir + PROPERTY_FILENAME_ALWAYS + ".properties");
|
---|
171 | fPropsAlways.delete();
|
---|
172 |
|
---|
173 | File fPropsPeriodic = new File(m_sPropertiesDir + PROPERTY_FILENAME_PERIODIC + ".properties");
|
---|
174 | fPropsPeriodic.delete();
|
---|
175 |
|
---|
176 | File fPropsPeriodic2 = new File(m_sPropertiesDir + PROPERTY_FILENAME_PERIODIC_2 + ".properties");
|
---|
177 | fPropsPeriodic2.delete();
|
---|
178 | }
|
---|
179 |
|
---|
180 | @Before
|
---|
181 | public void setUp()
|
---|
182 | {
|
---|
183 | }
|
---|
184 |
|
---|
185 | @After
|
---|
186 | public void tearDown()
|
---|
187 | {
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Test of getProperty method, of class PropertyAccessHelper.
|
---|
192 | */
|
---|
193 | @Test
|
---|
194 | public void testGetProperty() throws Exception
|
---|
195 | {
|
---|
196 | System.out.println("getProperty");
|
---|
197 | GetPropertyRequestType oInput = new GetPropertyRequestType();
|
---|
198 | oInput.setPropertyFile(PROPERTY_FILENAME_NEVER);
|
---|
199 | oInput.setPropertyName(PROPERTY_NAME_1);
|
---|
200 | GetPropertyResponseType oOutput = PropertyAccessHelper.getProperty(oInput);
|
---|
201 | assertNotNull(oOutput);
|
---|
202 | assertEquals(PROPERTY_VALUE_1, oOutput.getPropertyValue());
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Test of getPropertyBoolean method, of class PropertyAccessHelper.
|
---|
207 | */
|
---|
208 | @Test
|
---|
209 | public void testGetPropertyBoolean() throws Exception
|
---|
210 | {
|
---|
211 | System.out.println("getPropertyBoolean");
|
---|
212 | GetPropertyBooleanRequestType oInput = new GetPropertyBooleanRequestType();
|
---|
213 | oInput.setPropertyFile(PROPERTY_FILENAME_NEVER);
|
---|
214 |
|
---|
215 | // Test false one first.
|
---|
216 | //----------------------
|
---|
217 | oInput.setPropertyName(PROPERTY_NAME_3);
|
---|
218 | GetPropertyBooleanResponseType oOutput = PropertyAccessHelper.getPropertyBoolean(oInput);
|
---|
219 | assertNotNull(oOutput);
|
---|
220 | assertEquals(false, oOutput.isPropertyValue());
|
---|
221 |
|
---|
222 | // Test "T"
|
---|
223 | //---------
|
---|
224 | oInput.setPropertyName(PROPERTY_NAME_4);
|
---|
225 | oOutput = PropertyAccessHelper.getPropertyBoolean(oInput);
|
---|
226 | assertNotNull(oOutput);
|
---|
227 | assertEquals(true, oOutput.isPropertyValue());
|
---|
228 |
|
---|
229 | // Test "t"
|
---|
230 | //---------
|
---|
231 | oInput.setPropertyName(PROPERTY_NAME_5);
|
---|
232 | oOutput = PropertyAccessHelper.getPropertyBoolean(oInput);
|
---|
233 | assertNotNull(oOutput);
|
---|
234 | assertEquals(true, oOutput.isPropertyValue());
|
---|
235 |
|
---|
236 | // Test "TrUe"
|
---|
237 | //-------------
|
---|
238 | oInput.setPropertyName(PROPERTY_NAME_6);
|
---|
239 | oOutput = PropertyAccessHelper.getPropertyBoolean(oInput);
|
---|
240 | assertNotNull(oOutput);
|
---|
241 | assertEquals(true, oOutput.isPropertyValue());
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Test of getPropertyNames method, of class PropertyAccessHelper.
|
---|
246 | */
|
---|
247 | @Test
|
---|
248 | public void testGetPropertyNames() throws Exception
|
---|
249 | {
|
---|
250 | System.out.println("getPropertyNames");
|
---|
251 | GetPropertyNamesRequestType oInput = new GetPropertyNamesRequestType();
|
---|
252 | oInput.setPropertyFile(PROPERTY_FILENAME_NEVER);
|
---|
253 | GetPropertyNamesResponseType oOutput = PropertyAccessHelper.getPropertyNames(oInput);
|
---|
254 | assertNotNull(oOutput);
|
---|
255 |
|
---|
256 | List<String> olKeys = oOutput.getPropertyName();
|
---|
257 | assertNotNull(olKeys);
|
---|
258 |
|
---|
259 | boolean bFoundProp[] = {false, false, false, false, false, false};
|
---|
260 | Iterator<String> iterKeys = olKeys.iterator();
|
---|
261 | while (iterKeys.hasNext())
|
---|
262 | {
|
---|
263 | String sKey = iterKeys.next();
|
---|
264 | if (sKey.equals(PROPERTY_NAME_1))
|
---|
265 | {
|
---|
266 | bFoundProp[0] = true;
|
---|
267 | }
|
---|
268 | else if (sKey.equals(PROPERTY_NAME_2))
|
---|
269 | {
|
---|
270 | bFoundProp[1] = true;
|
---|
271 | }
|
---|
272 | else if (sKey.equals(PROPERTY_NAME_3))
|
---|
273 | {
|
---|
274 | bFoundProp[2] = true;
|
---|
275 | }
|
---|
276 | else if (sKey.equals(PROPERTY_NAME_4))
|
---|
277 | {
|
---|
278 | bFoundProp[3] = true;
|
---|
279 | }
|
---|
280 | else if (sKey.equals(PROPERTY_NAME_5))
|
---|
281 | {
|
---|
282 | bFoundProp[4] = true;
|
---|
283 | }
|
---|
284 | else if (sKey.equals(PROPERTY_NAME_6))
|
---|
285 | {
|
---|
286 | bFoundProp[5] = true;
|
---|
287 | }
|
---|
288 | else
|
---|
289 | {
|
---|
290 | fail("Found property that was not supposed to be in the file: " + sKey);
|
---|
291 | }
|
---|
292 | } // while (iterKeys.hasNext())
|
---|
293 |
|
---|
294 | assertTrue(bFoundProp[0]);
|
---|
295 | assertTrue(bFoundProp[1]);
|
---|
296 | assertTrue(bFoundProp[2]);
|
---|
297 | assertTrue(bFoundProp[3]);
|
---|
298 | assertTrue(bFoundProp[4]);
|
---|
299 | assertTrue(bFoundProp[5]);
|
---|
300 | }
|
---|
301 |
|
---|
302 | /**
|
---|
303 | * Test of getProperties method, of class PropertyAccessHelper.
|
---|
304 | */
|
---|
305 | @Test
|
---|
306 | public void testGetProperties() throws Exception
|
---|
307 | {
|
---|
308 | System.out.println("getProperties");
|
---|
309 | GetPropertiesRequestType oInput = new GetPropertiesRequestType();
|
---|
310 | oInput.setPropertyFile(PROPERTY_FILENAME_NEVER);
|
---|
311 | GetPropertiesResponseType oOutput = PropertyAccessHelper.getProperties(oInput);
|
---|
312 | assertNotNull(oOutput);
|
---|
313 | assertNotNull(oOutput.getProperties());
|
---|
314 | assertNotNull(oOutput.getProperties().getProperty());
|
---|
315 |
|
---|
316 | List<PropertyType> olProps = oOutput.getProperties().getProperty();
|
---|
317 | Iterator<PropertyType> iterProps = olProps.iterator();
|
---|
318 |
|
---|
319 | boolean bFoundProp[] = {false, false, false, false, false, false};
|
---|
320 |
|
---|
321 | while (iterProps.hasNext())
|
---|
322 | {
|
---|
323 | PropertyType oProp = iterProps.next();
|
---|
324 | if (oProp.getPropertyName().equals(PROPERTY_NAME_1))
|
---|
325 | {
|
---|
326 | assertEquals(PROPERTY_VALUE_1, oProp.getPropertyValue());
|
---|
327 | bFoundProp[0] = true;
|
---|
328 | }
|
---|
329 | else if (oProp.getPropertyName().equals(PROPERTY_NAME_2))
|
---|
330 | {
|
---|
331 | assertEquals(PROPERTY_VALUE_2, oProp.getPropertyValue());
|
---|
332 | bFoundProp[1] = true;
|
---|
333 | }
|
---|
334 | else if (oProp.getPropertyName().equals(PROPERTY_NAME_3))
|
---|
335 | {
|
---|
336 | assertEquals(PROPERTY_VALUE_3, oProp.getPropertyValue());
|
---|
337 | bFoundProp[2] = true;
|
---|
338 | }
|
---|
339 | else if (oProp.getPropertyName().equals(PROPERTY_NAME_4))
|
---|
340 | {
|
---|
341 | assertEquals(PROPERTY_VALUE_4, oProp.getPropertyValue());
|
---|
342 | bFoundProp[3] = true;
|
---|
343 | }
|
---|
344 | else if (oProp.getPropertyName().equals(PROPERTY_NAME_5))
|
---|
345 | {
|
---|
346 | assertEquals(PROPERTY_VALUE_5, oProp.getPropertyValue());
|
---|
347 | bFoundProp[4] = true;
|
---|
348 | }
|
---|
349 | else if (oProp.getPropertyName().equals(PROPERTY_NAME_6))
|
---|
350 | {
|
---|
351 | assertEquals(PROPERTY_VALUE_6, oProp.getPropertyValue());
|
---|
352 | bFoundProp[5] = true;
|
---|
353 | }
|
---|
354 | else
|
---|
355 | {
|
---|
356 | fail("Found property that was not supposed to be in the file: " + oProp.getPropertyName());
|
---|
357 | }
|
---|
358 | } // while (iterKeys.hasNext())
|
---|
359 |
|
---|
360 | assertTrue(bFoundProp[0]);
|
---|
361 | assertTrue(bFoundProp[1]);
|
---|
362 | assertTrue(bFoundProp[2]);
|
---|
363 | assertTrue(bFoundProp[3]);
|
---|
364 | assertTrue(bFoundProp[4]);
|
---|
365 | assertTrue(bFoundProp[5]);
|
---|
366 | }
|
---|
367 |
|
---|
368 | /**
|
---|
369 | * Test of getRefreshDuration method, of class PropertyAccessHelper.
|
---|
370 | */
|
---|
371 | @Test
|
---|
372 | public void testGetRefreshDuration() throws Exception
|
---|
373 | {
|
---|
374 | System.out.println("getRefreshDuration");
|
---|
375 |
|
---|
376 | GetRefreshDurationRequestType oInput = new GetRefreshDurationRequestType();
|
---|
377 | GetRefreshDurationResponseType oOutput = null;
|
---|
378 |
|
---|
379 | oInput.setPropertyFile(PROPERTY_FILENAME_NEVER);
|
---|
380 | oOutput = PropertyAccessHelper.getRefreshDuration(oInput);
|
---|
381 | assertNotNull(oOutput);
|
---|
382 | assertEquals(-1, oOutput.getDurationMillis());
|
---|
383 |
|
---|
384 | oInput.setPropertyFile(PROPERTY_FILENAME_ALWAYS);
|
---|
385 | oOutput = PropertyAccessHelper.getRefreshDuration(oInput);
|
---|
386 | assertNotNull(oOutput);
|
---|
387 | assertEquals(0, oOutput.getDurationMillis());
|
---|
388 |
|
---|
389 | oInput.setPropertyFile(PROPERTY_FILENAME_PERIODIC);
|
---|
390 | oOutput = PropertyAccessHelper.getRefreshDuration(oInput);
|
---|
391 | assertEquals(iCACHE_REFRESH_DURATION, oOutput.getDurationMillis());
|
---|
392 | }
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Test of getDurationBeforeNextRefresh method, of class PropertyAccessHelper.
|
---|
396 | */
|
---|
397 | @Test
|
---|
398 | public void testGetDurationBeforeNextRefresh() throws Exception
|
---|
399 | {
|
---|
400 | System.out.println("getDurationBeforeNextRefresh");
|
---|
401 |
|
---|
402 | GetDurationBeforeNextRefreshRequestType oInput = new GetDurationBeforeNextRefreshRequestType();
|
---|
403 | GetDurationBeforeNextRefreshResponseType oOutput = null;
|
---|
404 |
|
---|
405 | oInput.setPropertyFile(PROPERTY_FILENAME_NEVER);
|
---|
406 | oOutput = PropertyAccessHelper.getDurationBeforeNextRefresh(oInput);
|
---|
407 | assertNotNull(oOutput);
|
---|
408 | assertEquals(-1, oOutput.getDurationMillis());
|
---|
409 |
|
---|
410 | oInput.setPropertyFile(PROPERTY_FILENAME_ALWAYS);
|
---|
411 | oOutput = PropertyAccessHelper.getDurationBeforeNextRefresh(oInput);
|
---|
412 | assertNotNull(oOutput);
|
---|
413 | assertEquals(0, oOutput.getDurationMillis());
|
---|
414 |
|
---|
415 | oInput.setPropertyFile(PROPERTY_FILENAME_PERIODIC);
|
---|
416 | oOutput = PropertyAccessHelper.getDurationBeforeNextRefresh(oInput);
|
---|
417 | assertNotNull(oOutput);
|
---|
418 | assertTrue((oOutput.getDurationMillis() >= 0) && (oOutput.getDurationMillis() <= iCACHE_REFRESH_DURATION));
|
---|
419 | }
|
---|
420 |
|
---|
421 | /**
|
---|
422 | * Test of dumpPropsToLog method, of class PropertyAccessHelper.
|
---|
423 | */
|
---|
424 | @Test
|
---|
425 | public void testDumpPropsToLog() throws Exception
|
---|
426 | {
|
---|
427 | System.out.println("dumpPropsToLog");
|
---|
428 | DumpPropsToLogRequestType oInput = new DumpPropsToLogRequestType();
|
---|
429 | DumpPropsToLogResponseType oOutput = null;
|
---|
430 |
|
---|
431 | oInput.setPropertyFile(PROPERTY_FILENAME_NEVER);
|
---|
432 | oOutput = PropertyAccessHelper.dumpPropsToLog(oInput);
|
---|
433 |
|
---|
434 | // Only real way to verify this is to look at the log/output.
|
---|
435 | //------------------------------------------------------------
|
---|
436 | }
|
---|
437 |
|
---|
438 | /**
|
---|
439 | * Test of forceRefresh method, of class PropertyAccessHelper.
|
---|
440 | */
|
---|
441 | @Test
|
---|
442 | public void testForceRefresh() throws Exception
|
---|
443 | {
|
---|
444 | System.out.println("forceRefresh");
|
---|
445 | ForceRefreshRequestType oInput = new ForceRefreshRequestType();
|
---|
446 | ForceRefreshResponseType oOutput = null;
|
---|
447 |
|
---|
448 | oInput.setPropertyFile(PROPERTY_FILENAME_PERIODIC);
|
---|
449 | oOutput = PropertyAccessHelper.forceRefresh(oInput);
|
---|
450 |
|
---|
451 | // let's sleep for 3 seconds.
|
---|
452 | //---------------------------
|
---|
453 | try
|
---|
454 | {
|
---|
455 | Thread.sleep(3000);
|
---|
456 | }
|
---|
457 | catch (InterruptedException e)
|
---|
458 | {
|
---|
459 | }
|
---|
460 |
|
---|
461 | GetDurationBeforeNextRefreshRequestType oDurInput = new GetDurationBeforeNextRefreshRequestType();
|
---|
462 | oDurInput.setPropertyFile(PROPERTY_FILENAME_PERIODIC);
|
---|
463 | GetDurationBeforeNextRefreshResponseType oFirstDurOutput = null;
|
---|
464 | oFirstDurOutput = PropertyAccessHelper.getDurationBeforeNextRefresh(oDurInput);
|
---|
465 | assertNotNull(oFirstDurOutput);
|
---|
466 |
|
---|
467 | oOutput = PropertyAccessHelper.forceRefresh(oInput);
|
---|
468 |
|
---|
469 | GetDurationBeforeNextRefreshResponseType oSecondDurOutput = null;
|
---|
470 | oSecondDurOutput = PropertyAccessHelper.getDurationBeforeNextRefresh(oDurInput);
|
---|
471 | assertNotNull(oSecondDurOutput);
|
---|
472 |
|
---|
473 | // since we had a delay after the first one and no delay after the second one,
|
---|
474 | // the second one should be larger than the first.
|
---|
475 | //-----------------------------------------------------------------------------
|
---|
476 | assertTrue(oSecondDurOutput.getDurationMillis() > oFirstDurOutput.getDurationMillis());
|
---|
477 | }
|
---|
478 |
|
---|
479 | /**
|
---|
480 | * This method will read in the property file, change the given property and store it back out.
|
---|
481 | *
|
---|
482 | * @param sPropertyFile The name of the property file.
|
---|
483 | * @param sPropertyName The name of the property to be changed.
|
---|
484 | * @param sPropertyValue The value of the property to be changed.
|
---|
485 | * @throws java.lang.Exception Any exception....
|
---|
486 | */
|
---|
487 | private void changePropertyAndStore(String sPropertyFile, String sPropertyName, String sPropertyValue)
|
---|
488 | throws Exception
|
---|
489 | {
|
---|
490 | FileReader frPropFile = null;
|
---|
491 | FileWriter fwPropFile = null;
|
---|
492 |
|
---|
493 | try
|
---|
494 | {
|
---|
495 | frPropFile = new FileReader(m_sPropertiesDir + sPropertyFile + ".properties");
|
---|
496 | Properties oProps = new Properties();
|
---|
497 | oProps.load(frPropFile);
|
---|
498 | frPropFile.close();
|
---|
499 | frPropFile = null;
|
---|
500 |
|
---|
501 | oProps.setProperty(sPropertyName, sPropertyValue);
|
---|
502 | fwPropFile = new FileWriter(m_sPropertiesDir + sPropertyFile + ".properties");
|
---|
503 | oProps.store(fwPropFile, "");
|
---|
504 | fwPropFile.close();
|
---|
505 | frPropFile = null;
|
---|
506 | }
|
---|
507 | finally
|
---|
508 | {
|
---|
509 | if (frPropFile != null)
|
---|
510 | {
|
---|
511 | frPropFile.close();
|
---|
512 | }
|
---|
513 |
|
---|
514 | if (fwPropFile != null)
|
---|
515 | {
|
---|
516 | fwPropFile.close();
|
---|
517 | }
|
---|
518 | }
|
---|
519 | }
|
---|
520 |
|
---|
521 | // NOTE: THIS IS COMMENTED OUT FOR THE BUILD SERVER BECAUSE IT IS EXTREMELY TIME
|
---|
522 | // SENSITIVE - TESTING REFRESHING OF CACHE, ETC. SO IT SHOULD BE UNCOMMENTED WHEN YOU
|
---|
523 | // WANT TO DO THIS LEVEL OF RUN.
|
---|
524 | /**
|
---|
525 | * This method is used to test the caching and refreshing capabilities.
|
---|
526 | * To make sure that it is all working correctly... It is pretty complex.
|
---|
527 | * It will involve changing the property files/waiting to see when they
|
---|
528 | * refresh, etc.
|
---|
529 | */
|
---|
530 | // @Test
|
---|
531 | // public void testCachingAndRefreshing() throws Exception
|
---|
532 | // {
|
---|
533 | // // first let's test against the one that should never refresh...
|
---|
534 | // //---------------------------------------------------------------
|
---|
535 | // GetPropertyRequestType oPropInput = new GetPropertyRequestType();
|
---|
536 | // oPropInput.setPropertyFile(PROPERTY_FILENAME_NEVER);
|
---|
537 | // oPropInput.setPropertyName(PROPERTY_NAME_1);
|
---|
538 | // GetPropertyResponseType oPropOutput = null;
|
---|
539 | // oPropOutput = PropertyAccessHelper.getProperty(oPropInput);
|
---|
540 | // assertNotNull(oPropOutput);
|
---|
541 | // assertEquals(PROPERTY_VALUE_1, oPropOutput.getPropertyValue());
|
---|
542 | //
|
---|
543 | // // Change the value and see if we can force a refresh - it should not change...
|
---|
544 | // //------------------------------------------------------------------------------
|
---|
545 | // changePropertyAndStore(PROPERTY_FILENAME_NEVER, PROPERTY_NAME_1, "ModifiedValue");
|
---|
546 | // ForceRefreshRequestType oRefInput = new ForceRefreshRequestType();
|
---|
547 | // oRefInput.setPropertyFile(PROPERTY_FILENAME_NEVER);
|
---|
548 | // PropertyAccessHelper.forceRefresh(oRefInput);
|
---|
549 | //
|
---|
550 | // oPropOutput = PropertyAccessHelper.getProperty(oPropInput);
|
---|
551 | // assertNotNull(oPropOutput);
|
---|
552 | // assertEquals(PROPERTY_VALUE_1, oPropOutput.getPropertyValue());
|
---|
553 | //
|
---|
554 | // // Now let's test against the one that should always refresh
|
---|
555 | // //----------------------------------------------------------
|
---|
556 | // oPropInput.setPropertyFile(PROPERTY_FILENAME_ALWAYS);
|
---|
557 | // oPropInput.setPropertyName(PROPERTY_NAME_2);
|
---|
558 | // oPropOutput = PropertyAccessHelper.getProperty(oPropInput);
|
---|
559 | // assertNotNull(oPropOutput);
|
---|
560 | // assertEquals(PROPERTY_VALUE_2, oPropOutput.getPropertyValue());
|
---|
561 | //
|
---|
562 | // changePropertyAndStore(PROPERTY_FILENAME_ALWAYS, PROPERTY_NAME_2, "ModVal2");
|
---|
563 | //
|
---|
564 | // // Note it should auto refresh.
|
---|
565 | // //------------------------------
|
---|
566 | // oPropOutput = PropertyAccessHelper.getProperty(oPropInput);
|
---|
567 | // assertNotNull(oPropOutput);
|
---|
568 | // assertEquals("ModVal2", oPropOutput.getPropertyValue());
|
---|
569 | //
|
---|
570 | // // Now let's test against the one that refreshes every 10 seconds...
|
---|
571 | // //------------------------------------------------------------------
|
---|
572 | // oPropInput.setPropertyFile(PROPERTY_FILENAME_PERIODIC);
|
---|
573 | // oPropInput.setPropertyName(PROPERTY_NAME_3);
|
---|
574 | // oPropOutput = PropertyAccessHelper.getProperty(oPropInput);
|
---|
575 | // assertNotNull(oPropOutput);
|
---|
576 | // assertEquals(PROPERTY_VALUE_3, oPropOutput.getPropertyValue());
|
---|
577 | //
|
---|
578 | // // Get a clean time on the clock...
|
---|
579 | // //----------------------------------
|
---|
580 | // oRefInput.setPropertyFile(PROPERTY_FILENAME_PERIODIC);
|
---|
581 | // PropertyAccessHelper.forceRefresh(oRefInput);
|
---|
582 | //
|
---|
583 | // changePropertyAndStore(PROPERTY_FILENAME_PERIODIC, PROPERTY_NAME_3, "ModVal3");
|
---|
584 | //
|
---|
585 | // // We should not have refreshed yet - so should have the old value.
|
---|
586 | // // (Unless our disc is really really slow....
|
---|
587 | // //-----------------------------------------------------------------
|
---|
588 | // oPropOutput = PropertyAccessHelper.getProperty(oPropInput);
|
---|
589 | // assertNotNull(oPropOutput);
|
---|
590 | // assertEquals(PROPERTY_VALUE_3, oPropOutput.getPropertyValue());
|
---|
591 | //
|
---|
592 | // // Wait the cache duration time
|
---|
593 | // //-----------------------------
|
---|
594 | // try
|
---|
595 | // {
|
---|
596 | // Thread.sleep(iCACHE_REFRESH_DURATION);
|
---|
597 | // }
|
---|
598 | // catch (InterruptedException e)
|
---|
599 | // {
|
---|
600 | // }
|
---|
601 | //
|
---|
602 | // // Now we should have the new value.
|
---|
603 | // //---------------------------------
|
---|
604 | // oPropOutput = PropertyAccessHelper.getProperty(oPropInput);
|
---|
605 | // assertNotNull(oPropOutput);
|
---|
606 | // assertEquals("ModVal3", oPropOutput.getPropertyValue());
|
---|
607 | //
|
---|
608 | // }
|
---|
609 |
|
---|
610 | }
|
---|