1 package org.openecomp.core.nosqldb.util;
3 import org.testng.annotations.BeforeMethod;
4 import org.testng.annotations.Test;
6 import java.io.Closeable;
7 import java.io.IOException;
8 import java.lang.reflect.Field;
10 import static org.testng.Assert.assertNotNull;
16 public class ConfigurationManagerTest {
18 private static final String NON_EXISTENT = "unexistentfile";
21 public void resetInstance() throws NoSuchFieldException, IllegalAccessException {
22 Field field = ConfigurationManager.class.getDeclaredField("instance");
23 field.setAccessible(true);
24 field.set(null, null);
27 @Test(expectedExceptions = IOException.class,
28 expectedExceptionsMessageRegExp = ".*" + NON_EXISTENT + ".*")
29 public void testGetInstanceSystemProperty() throws Throwable {
31 try (ConfigurationSystemPropertyUpdater updater = new ConfigurationSystemPropertyUpdater(NON_EXISTENT)) {
32 ConfigurationManager.getInstance();
33 } catch (RuntimeException e) {
34 Throwable cause = e.getCause();
35 throw cause == null ? e : cause;
40 public void testGetInstanceDefault() throws Exception {
42 try (ConfigurationSystemPropertyUpdater property = new ConfigurationSystemPropertyUpdater()) {
43 ConfigurationManager manager = ConfigurationManager.getInstance();
44 assertNotNull(manager.getUsername());
49 private static class ConfigurationSystemPropertyUpdater implements Closeable {
51 private final String oldValue;
53 private ConfigurationSystemPropertyUpdater(String value) {
54 this.oldValue = System.getProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
55 System.setProperty(ConfigurationManager.CONFIGURATION_YAML_FILE, value);
58 private ConfigurationSystemPropertyUpdater() {
59 this.oldValue = System.getProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
60 System.clearProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
64 public void close() throws IOException {
66 if (oldValue == null) {
67 System.clearProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
69 System.setProperty(ConfigurationManager.CONFIGURATION_YAML_FILE, oldValue);