source: ccr/trunk/nhin-vista/projects/NHINC/Current/Product/Production/Common/NhincLib/test/gov/hhs/fha/nhinc/properties/PropertyAccessorTest.java@ 507

Last change on this file since 507 was 507, checked in by George Lilly, 15 years ago

NHIN gateway and adaptor for use on linux with VistA EHR and RPMS

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