1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.configuration2;
19
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import java.util.Iterator;
26
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29
30 /**
31 * Test class for EnvironmentConfiguration.
32 */
33 public class TestEnvironmentConfiguration {
34 /** Stores the configuration to be tested. */
35 private EnvironmentConfiguration config;
36
37 @BeforeEach
38 public void setUp() throws Exception {
39 config = new EnvironmentConfiguration();
40 }
41
42 /**
43 * Tries to add another property. This should cause an exception.
44 */
45 @Test
46 void testAddProperty() {
47 assertThrows(UnsupportedOperationException.class, () -> config.addProperty("JAVA_HOME", "C:\\java"));
48 }
49
50 /**
51 * Tests removing all properties. This should not be possible.
52 */
53 @Test
54 void testClear() {
55 assertThrows(UnsupportedOperationException.class, config::clear);
56 }
57
58 /**
59 * Tests removing properties. This should not be possible.
60 */
61 @Test
62 void testClearProperty() {
63 final String key = config.getKeys().next();
64 assertThrows(UnsupportedOperationException.class, () -> config.clearProperty(key));
65 }
66
67 /**
68 * Tests whether a newly created configuration contains some properties. (We expect that at least some properties are
69 * set in each environment.)
70 */
71 @Test
72 void testInit() {
73 boolean found = false;
74 assertFalse(config.isEmpty());
75 for (final Iterator<String> it = config.getKeys(); it.hasNext();) {
76 final String key = it.next();
77 assertTrue(config.containsKey(key), "Key not found: " + key);
78 assertNotNull(config.getString(key), "No value for property " + key);
79 found = true;
80 }
81 assertTrue(found);
82 }
83
84 /**
85 * Tries to set the value of a property. This should cause an exception.
86 */
87 @Test
88 void testSetProperty() {
89 assertThrows(UnsupportedOperationException.class, () -> config.setProperty("JAVA_HOME", "C:\\java"));
90 }
91 }