2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.core.nosqldb.util;
19 import static org.hamcrest.core.StringContains.containsString;
20 import static org.junit.Assert.assertNotNull;
22 import java.io.Closeable;
23 import java.io.IOException;
24 import java.lang.reflect.Field;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
34 public class ConfigurationManagerTest {
37 public ExpectedException expectedException = ExpectedException.none();
39 private static final String NON_EXISTENT = "unexistentfile";
42 public void resetInstance() throws NoSuchFieldException, IllegalAccessException {
43 Field field = ConfigurationManager.class.getDeclaredField("instance");
44 field.setAccessible(true);
45 field.set(null, null);
49 public void testGetInstanceSystemProperty() throws Throwable {
51 expectedException.expect(IOException.class);
52 expectedException.expectMessage(containsString(NON_EXISTENT));
54 try (ConfigurationSystemPropertyUpdater updater = new ConfigurationSystemPropertyUpdater(NON_EXISTENT)) {
55 ConfigurationManager.getInstance();
56 } catch (RuntimeException e) {
57 Throwable cause = e.getCause();
58 throw cause == null ? e : cause;
63 public void testGetInstanceDefault() {
65 try (ConfigurationSystemPropertyUpdater property = new ConfigurationSystemPropertyUpdater()) {
66 ConfigurationManager manager = ConfigurationManager.getInstance();
67 assertNotNull(manager.getUsername());
72 private static class ConfigurationSystemPropertyUpdater implements Closeable {
74 private final String oldValue;
76 private ConfigurationSystemPropertyUpdater(String value) {
77 this.oldValue = System.getProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
78 System.setProperty(ConfigurationManager.CONFIGURATION_YAML_FILE, value);
81 private ConfigurationSystemPropertyUpdater() {
82 this.oldValue = System.getProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
83 System.clearProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
89 if (oldValue == null) {
90 System.clearProperty(ConfigurationManager.CONFIGURATION_YAML_FILE);
92 System.setProperty(ConfigurationManager.CONFIGURATION_YAML_FILE, oldValue);