diff options
author | 2017-10-22 19:03:46 +0300 | |
---|---|---|
committer | 2017-10-23 12:38:26 +0300 | |
commit | b118cd814ea0a230486c4179d0bcdc27d39d92b1 (patch) | |
tree | e61517052aab52e3a99e399e979456a2ef60bafd /openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/ConfigurationManagerTest.java | |
parent | 30e46525c1406fb3b10603606c4efcc033ee58c4 (diff) |
Additinal fixes for resources not being released
More fixes for input streams not being properly closed,
with unit tests and other minor improvements (code cleanup and coding conventions).
Change-Id: I6751f924a1469d49b996e4f1d6c61371af6714b1
Issue-ID: SDC-291
Signed-off-by: vempo <vitaliy.emporopulo@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/ConfigurationManagerTest.java')
-rw-r--r-- | openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/ConfigurationManagerTest.java | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/ConfigurationManagerTest.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/ConfigurationManagerTest.java new file mode 100644 index 0000000000..f8d5d2dce7 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/ConfigurationManagerTest.java @@ -0,0 +1,73 @@ +package org.openecomp.core.nosqldb.util; + +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.io.Closeable; +import java.io.IOException; +import java.lang.reflect.Field; + +import static org.testng.Assert.*; + +/** + * @author EVITALIY + * @since 22 Oct 17 + */ +public class ConfigurationManagerTest { + + private static final String NON_EXISTENT = "unexistentfile"; + + @BeforeMethod + public void resetInstance() throws NoSuchFieldException, IllegalAccessException { + Field field = ConfigurationManager.class.getDeclaredField("instance"); + field.setAccessible(true); + field.set(null, null); + } + + @Test(expectedExceptions = IOException.class, + expectedExceptionsMessageRegExp = ".*" + NON_EXISTENT + ".*") + public void testGetInstanceSystemProperty() throws Throwable { + + try (ConfigurationSystemPropertyUpdater updater = new ConfigurationSystemPropertyUpdater(NON_EXISTENT)) { + ConfigurationManager.getInstance(); + } catch (RuntimeException e) { + Throwable cause = e.getCause(); + throw cause == null ? e : cause; + } + } + + @Test() + public void testGetInstanceDefault() throws Exception { + + try (ConfigurationSystemPropertyUpdater property = new ConfigurationSystemPropertyUpdater()) { + ConfigurationManager manager = ConfigurationManager.getInstance(); + assertNotNull(manager.getUsername()); + } + } + + + private static class ConfigurationSystemPropertyUpdater implements Closeable { + + private final String oldValue; + + private ConfigurationSystemPropertyUpdater(String value) { + this.oldValue = System.getProperty(ConfigurationManager.CONFIGURATION_YAML_FILE); + System.setProperty(ConfigurationManager.CONFIGURATION_YAML_FILE, value); + } + + private ConfigurationSystemPropertyUpdater() { + this.oldValue = System.getProperty(ConfigurationManager.CONFIGURATION_YAML_FILE); + System.clearProperty(ConfigurationManager.CONFIGURATION_YAML_FILE); + } + + @Override + public void close() throws IOException { + + if (oldValue == null) { + System.clearProperty(ConfigurationManager.CONFIGURATION_YAML_FILE); + } else { + System.setProperty(ConfigurationManager.CONFIGURATION_YAML_FILE, oldValue); + } + } + } +}
\ No newline at end of file |