diff options
Diffstat (limited to 'same/src')
3 files changed, 70 insertions, 0 deletions
diff --git a/same/src/main/java/com/orbekk/same/config/Configuration.java b/same/src/main/java/com/orbekk/same/config/Configuration.java new file mode 100644 index 0000000..dce9dee --- /dev/null +++ b/same/src/main/java/com/orbekk/same/config/Configuration.java @@ -0,0 +1,52 @@ +package com.orbekk.same.config; + +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.Enumeration; +import java.util.Properties; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Configuration { + public final static String configurationProperty = + "com.orbekk.same.config.file"; + + Logger logger = LoggerFactory.getLogger(getClass()); + Properties configuration = new Properties(); + + private Configuration() { + // Use factory methods. + } + + public static Configuration load() { + Configuration configuration = new Configuration(); + configuration.loadDefault(); + return configuration; + } + + public void loadDefault() { + String filename = System.getProperty(configurationProperty); + if (filename != null) { + try { + configuration.load(new FileReader(filename)); + } catch (FileNotFoundException e) { + logger.error("Failed to load configuration. {}", e); + logger.error("Failed to load configuration. {}={}", + configurationProperty, filename); + } catch (IOException e) { + logger.error("Failed to load configuration. {}", e); + logger.error("Failed to load configuration. {}={}", + configurationProperty, filename); + } + } else { + logger.error("Failed to load configuration. {}={}", + configurationProperty, filename); + } + } + + public String getProperty(String name) { + return configuration.getProperty(name); + } +} diff --git a/same/src/test/java/com/orbekk/same/config/ConfigurationTest.java b/same/src/test/java/com/orbekk/same/config/ConfigurationTest.java new file mode 100644 index 0000000..e5b42aa --- /dev/null +++ b/same/src/test/java/com/orbekk/same/config/ConfigurationTest.java @@ -0,0 +1,17 @@ +package com.orbekk.same.config; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class ConfigurationTest { + @Test public void testLoadConfiguration() { + String resources = "src/test/resources/"; + System.setProperty(Configuration.configurationProperty, + resources + "com/orbekk/same/config/test.properties"); + Configuration configuration = Configuration.load(); + assertEquals("Test Value", + configuration.getProperty("testProperty")); + + } +} diff --git a/same/src/test/resources/com/orbekk/same/config/test.properties b/same/src/test/resources/com/orbekk/same/config/test.properties new file mode 100644 index 0000000..45e1cc4 --- /dev/null +++ b/same/src/test/resources/com/orbekk/same/config/test.properties @@ -0,0 +1 @@ +testProperty=Test Value |