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
35 /** Stores the configuration to be tested. */
36 private EnvironmentConfiguration config;
37
38 @BeforeEach
39 public void setUp() throws Exception {
40 config = new EnvironmentConfiguration();
41 }
42
43 /**
44 * Tries to add another property. This should cause an exception.
45 */
46 @Test
47 void testAddProperty() {
48 assertThrows(UnsupportedOperationException.class, () -> config.addProperty("JAVA_HOME", "C:\\java"));
49 }
50
51 /**
52 * Tests removing all properties. This should not be possible.
53 */
54 @Test
55 void testClear() {
56 assertThrows(UnsupportedOperationException.class, config::clear);
57 }
58
59 /**
60 * Tests removing properties. This should not be possible.
61 */
62 @Test
63 void testClearProperty() {
64 final String key = config.getKeys().next();
65 assertThrows(UnsupportedOperationException.class, () -> config.clearProperty(key));
66 }
67
68 /**
69 * Tests whether a newly created configuration contains some properties. (We expect that at least some properties are
70 * set in each environment.)
71 */
72 @Test
73 void testInit() {
74 boolean found = false;
75 assertFalse(config.isEmpty());
76 for (final Iterator<String> it = config.getKeys(); it.hasNext();) {
77 final String key = it.next();
78 assertTrue(config.containsKey(key), "Key not found: " + key);
79 assertNotNull(config.getString(key), "No value for property " + key);
80 found = true;
81 }
82 assertTrue(found);
83 }
84
85 /**
86 * Tries to set the value of a property. This should cause an exception.
87 */
88 @Test
89 void testSetProperty() {
90 assertThrows(UnsupportedOperationException.class, () -> config.setProperty("JAVA_HOME", "C:\\java"));
91 }
92 }