22
33import org .apache .commons .configuration .Configuration ;
44import org .apache .commons .configuration .ConversionException ;
5+ import org .apache .commons .io .FileUtils ;
6+ import org .apache .commons .lang3 .SystemUtils ;
57import org .junit .Assert ;
68import org .junit .Before ;
79import org .junit .Test ;
@@ -77,7 +79,7 @@ public class DefaultConfigurationTest {
7779 @ Before
7880 public void loadProperties () {
7981 config = new DefaultConfiguration (
80- "com/github/bordertech/config/DefaultConfigurationTest.properties" );
82+ "com/github/bordertech/config/DefaultConfigurationTest.properties" );
8183 }
8284
8385 @ Test
@@ -136,7 +138,7 @@ public void testGetSubProperties() {
136138 assertPropertyEquals (BOOLEAN_FALSE_PROPERTY_KEY , "false" , props );
137139 assertPropertyEquals ("simple.listPropertyKey" , "item1,item2,item3" , props );
138140 assertPropertyEquals ("simple.propertiesPropertyKey" , "key1=value1,key2=value2,key3=value3" ,
139- props );
141+ props );
140142
141143 // Now test with the prefix truncated
142144 props = config .getSubProperties ("simple." , true );
@@ -212,7 +214,7 @@ public void testGetLong() {
212214 Assert .assertEquals ("Incorrect long value for missing key" , 0 , config .getLong (MISSING_PROPERTY_KEY ));
213215
214216 Assert .assertEquals ("Incorrect default long value for missing key" , MISSING_PROPERTY_VAL ,
215- config .getLong (MISSING_PROPERTY_KEY , MISSING_PROPERTY_VAL ));
217+ config .getLong (MISSING_PROPERTY_KEY , MISSING_PROPERTY_VAL ));
216218
217219 Assert .assertEquals ("Incorrect default long value for missing key" ,
218220 Long .valueOf (MISSING_PROPERTY_VAL ), config .getLong (MISSING_PROPERTY_KEY , Long .valueOf (MISSING_PROPERTY_VAL )));
@@ -325,7 +327,7 @@ public void testGetBigDecimal() {
325327 BigDecimal .valueOf (0.0 ), config .getBigDecimal (MISSING_PROPERTY_KEY ));
326328
327329 Assert .assertEquals ("Incorrect default BigDecimal value for missing key" ,
328- BigDecimal .valueOf (MISSING_PROPERTY_VAL ), config .getBigDecimal (MISSING_PROPERTY_KEY , BigDecimal .
330+ BigDecimal .valueOf (MISSING_PROPERTY_VAL ), config .getBigDecimal (MISSING_PROPERTY_KEY , BigDecimal .
329331 valueOf (MISSING_PROPERTY_VAL )));
330332 }
331333
@@ -337,13 +339,13 @@ public void testGetInvalidBigDecimal() {
337339 @ Test
338340 public void testGetBigInteger () {
339341 Assert .assertEquals ("Incorrect BigInteger value for " + INT_PROPERTY_KEY ,
340- BigInteger .valueOf (INT_PROPERTY_VAL ), config .getBigInteger (INT_PROPERTY_KEY ));
342+ BigInteger .valueOf (INT_PROPERTY_VAL ), config .getBigInteger (INT_PROPERTY_KEY ));
341343
342344 Assert .assertEquals ("Incorrect BigInteger value for missing key" ,
343- BigInteger .valueOf (0 ), config .getBigInteger (MISSING_PROPERTY_KEY ));
345+ BigInteger .valueOf (0 ), config .getBigInteger (MISSING_PROPERTY_KEY ));
344346
345347 Assert .assertEquals ("Incorrect default BigInteger value for missing key" ,
346- BigInteger .valueOf (MISSING_PROPERTY_VAL ), config .getBigInteger (MISSING_PROPERTY_KEY , BigInteger .
348+ BigInteger .valueOf (MISSING_PROPERTY_VAL ), config .getBigInteger (MISSING_PROPERTY_KEY , BigInteger .
347349 valueOf (MISSING_PROPERTY_VAL )));
348350 }
349351
@@ -369,13 +371,13 @@ public void testGetBoolean() {
369371 public void testGetFloat () {
370372 final float expectedVal = 234.0f ;
371373 Assert .assertEquals ("Incorrect float value for " + INT_PROPERTY_KEY ,
372- Float .parseFloat ("123" ), config .getFloat (INT_PROPERTY_KEY ), 0.0 );
374+ Float .parseFloat ("123" ), config .getFloat (INT_PROPERTY_KEY ), 0.0 );
373375
374376 Assert .assertEquals ("Incorrect float value for missing key" ,
375- 0.0f , config .getFloat (MISSING_PROPERTY_KEY ), 0.0 );
377+ 0.0f , config .getFloat (MISSING_PROPERTY_KEY ), 0.0 );
376378
377379 Assert .assertEquals ("Incorrect default float value for missing key" ,
378- expectedVal , config .getFloat (MISSING_PROPERTY_KEY , expectedVal ), 0.0 );
380+ expectedVal , config .getFloat (MISSING_PROPERTY_KEY , expectedVal ), 0.0 );
379381
380382 Assert .assertEquals ("Incorrect default float value for missing key" ,
381383 Float .valueOf (MISSING_PROPERTY_VAL ), config .getFloat (MISSING_PROPERTY_KEY , Float .valueOf (MISSING_PROPERTY_VAL )));
@@ -401,7 +403,7 @@ public void testGetDouble() {
401403 0.0 , config .getDouble (MISSING_PROPERTY_KEY ), 0.0 );
402404
403405 Assert .assertEquals ("Incorrect default double value for missing key" ,
404- expectedVal , config .getDouble (MISSING_PROPERTY_KEY , MISSING_PROPERTY_VAL ), 0.0 );
406+ expectedVal , config .getDouble (MISSING_PROPERTY_KEY , MISSING_PROPERTY_VAL ), 0.0 );
405407
406408 Assert .assertEquals ("Incorrect default double value for missing key" ,
407409 Double .valueOf (MISSING_PROPERTY_VAL ), config .getDouble (MISSING_PROPERTY_KEY , Double .valueOf (MISSING_PROPERTY_VAL )));
@@ -704,6 +706,37 @@ public void testLoadWithPhysicalFile() throws Exception {
704706 Assert .assertEquals ("physicalFileIncludeValue" , config .getString ("physical.file.include" ));
705707 }
706708
709+ @ Test
710+ public void testLoadFileFromUserHome () throws Exception {
711+
712+ Properties props = new Properties ();
713+ props .setProperty ("user.home.file.include" , "userHomeIncludeValue" );
714+ props .store (new FileWriter (FileUtils .getFile (SystemUtils .getUserHome (), "DefaultConfigTestIncludeUserHome.properties" )), null );
715+
716+ DefaultConfiguration config = new DefaultConfiguration (
717+ "com/github/bordertech/config/DefaultConfigurationTest_include.properties" );
718+
719+ Assert .assertEquals ("userHomeIncludeValue" , config .getString ("user.home.file.include" ));
720+ }
721+
722+ @ Test
723+ public void testLoadFileFromMultiplePlaces () throws Exception {
724+
725+ Properties props = new Properties ();
726+ props .setProperty ("user.home.file.include.multi" , "userHomeIncludeValue" );
727+ props .store (new FileWriter (FileUtils .getFile (SystemUtils .getUserHome (), "DefaultConfigTestIncludeMulti.properties" )), null );
728+ props .clear ();
729+
730+ props = new Properties ();
731+ props .setProperty ("user.home.file.include.multi" , "userDirIncludeValue" );
732+ props .store (new FileWriter (FileUtils .getFile (SystemUtils .getUserHome (), "DefaultConfigTestIncludeMulti.properties" )), null );
733+
734+ DefaultConfiguration config = new DefaultConfiguration (
735+ "com/github/bordertech/config/DefaultConfigurationTest_include.properties" );
736+
737+ Assert .assertEquals ("userDirIncludeValue" , config .getString ("user.home.file.include.multi" ));
738+ }
739+
707740 /**
708741 * Asserts that the configuration contains the given key/value.
709742 *
@@ -717,12 +750,12 @@ private void assertPropertyEquals(final String key, final Object expected) {
717750 /**
718751 * Asserts that the given properties contains the given key/value.
719752 *
720- * @param key the property key
753+ * @param key the property key
721754 * @param expected the expected property value.
722- * @param props the properties to search in.
755+ * @param props the properties to search in.
723756 */
724757 private void assertPropertyEquals (final String key , final Object expected ,
725- final Properties props ) {
758+ final Properties props ) {
726759 Assert .assertEquals ("Incorrect value for " + key , expected , props .get (key ));
727760 }
728761}
0 commit comments