Skip to content

Commit

Permalink
Fixed Default property loading issue as reported in #494.
Browse files Browse the repository at this point in the history
  • Loading branch information
amedranogil committed Jun 8, 2018
1 parent 9086103 commit e17e425
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

/**
* @author amedrano
*
*
*/
public abstract class UpdatedPropertiesFile extends Properties {

Expand All @@ -47,22 +47,22 @@ public abstract class UpdatedPropertiesFile extends Properties {

/**
* Get the comments for the Property file.
*
*
* @return
*/
public abstract String getComments();

/**
* to be implemented to add the default values at the start if there is no
* file.
*
*
* @param defaults
*/
protected abstract void addDefaults(Properties defaults);

/**
* Create a {@link Properties} that is linked and updated with the propfile
*
*
* @param propFile
* the properties file
*/
Expand All @@ -73,11 +73,13 @@ public UpdatedPropertiesFile(File propFile) {
/**
* load configuration properties from a file, setting the default for those
* which are not defined.
*
*
* @throws IOException
* @see HTMLUserGenerator#properties
*/
public void loadProperties() throws IOException {
// first load defaults
addDefaults(this);

/*
* Try to load from file, if not create file from defaults.
Expand All @@ -90,7 +92,6 @@ public void loadProperties() throws IOException {
propertiesVersion = propertiesFile.lastModified();
fis.close();
} catch (FileNotFoundException e) {
addDefaults(this);
storeProperties();
propertiesVersion = propertiesFile.lastModified();
} finally {
Expand All @@ -104,13 +105,13 @@ public void loadProperties() throws IOException {

/**
* Checks for updates the properties file, and updates the properties.
*
*
* @return true if the properties have been updated.
*/
protected boolean checkPropertiesVersion() {
if (propertiesFile != null
&& (propertiesVersion != propertiesFile.lastModified())) {
;
&& ((!propertiesFile.exists())
|| propertiesVersion != propertiesFile.lastModified())) {
try {
loadProperties();
return true;
Expand All @@ -122,7 +123,7 @@ protected boolean checkPropertiesVersion() {

/**
* Save the current properties in the file.
*
*
* @throws IOException
*/
protected void storeProperties() throws IOException {
Expand All @@ -136,7 +137,7 @@ protected void storeProperties() throws IOException {

/**
* Access to the property file.
*
*
* @param string
* Key of property to access
* @return String Value of the property
Expand All @@ -149,7 +150,7 @@ public final String getProperty(String key) {

/**
* Access to the property file.
*
*
* @param key
* Key of property to access
* @param defaultValue
Expand All @@ -164,7 +165,7 @@ public final String getProperty(String key, String defaultValue) {

/**
* Update the property file.
*
*
* @param key
* Key of property to access
* @param newValue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.universAAL.ri.gateway.configuration;

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class ConfigurationFileTest {

private static final File test_prop_file = new File("./target/test.properties");

@AfterClass
public static void tearDownAfterClass() throws Exception {
test_prop_file.delete();
}

@Test
public void test0FiledoesNotExist() {
ConfigurationFile cf = new ConfigurationFile(test_prop_file);
checkDefaults(cf, true);
}

@Test
public void test1emptyFileExists() throws IOException {
test_prop_file.delete();
assertTrue(test_prop_file.createNewFile());
ConfigurationFile cf = new ConfigurationFile(test_prop_file);
checkDefaults(cf, true);
}

@Test
public void test2FileExistsWithNonDefaultProperties() throws IOException {
test_prop_file.delete();
assertTrue(test_prop_file.createNewFile());
PrintWriter out = new PrintWriter(test_prop_file);
String testValue = "localhost";
out.println(ConfigurationFile.REMOTE_HOST + "=" +testValue);
out.flush();
out.close();
ConfigurationFile cf = new ConfigurationFile(test_prop_file);
// check defaults are loaded
checkDefaults(cf, true);
// check value
assertEquals(testValue, cf.getProperty(ConfigurationFile.REMOTE_HOST));
}

@Test
public void test3FileExistsWithDefaultPropertiesOverriten() throws IOException {
test_prop_file.delete();
assertTrue(test_prop_file.createNewFile());
PrintWriter out = new PrintWriter(test_prop_file);
String testValue = "5";
out.println(ConfigurationFile.QUEUES + "=" +testValue);
out.flush();
out.close();
ConfigurationFile cf = new ConfigurationFile(test_prop_file);
// check defaults are loaded
checkDefaults(cf, false);
// check value
assertEquals(testValue, cf.getProperty(ConfigurationFile.QUEUES));
}
private void checkDefaults(ConfigurationFile cf, boolean checkValues) {

Properties defaults = new Properties();
cf.addDefaults(defaults);

Set<Entry<Object, Object>> dfaultEntries = defaults.entrySet();
for (Entry<Object, Object> e : dfaultEntries) {
System.out.println(e.getKey()+" -> " + e.getValue());
String actualValue = cf.getProperty((String)e.getKey());
System.out.println(actualValue);
assertTrue( actualValue != null && !actualValue.isEmpty());
if (checkValues) {
assertEquals(e.getValue(), actualValue);
}
}
}
}

0 comments on commit e17e425

Please sign in to comment.