View Javadoc
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.apache.commons.configuration2.TempDirUtils.newFile;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.util.Iterator;
27  import java.util.Properties;
28  import java.util.concurrent.atomic.AtomicBoolean;
29  
30  import org.apache.commons.configuration2.ex.ConfigurationException;
31  import org.apache.commons.configuration2.io.FileHandler;
32  import org.junit.jupiter.api.Test;
33  import org.junit.jupiter.api.io.TempDir;
34  
35  /**
36   * Tests for {@code SystemConfiguration}.
37   */
38  public class TestSystemConfiguration {
39  
40      /** A folder for temporary files. */
41      @TempDir
42      public File tempFolder;
43  
44      /**
45       * Tests an append operation with a system configuration while system properties are modified from another thread. This
46       * is related to CONFIGURATION-570.
47       */
48      @Test
49      void testAppendWhileConcurrentAccess() throws InterruptedException {
50          final AtomicBoolean stop = new AtomicBoolean();
51          final String property = SystemConfiguration.class.getName() + ".testProperty";
52          final Thread t = new Thread(() -> {
53              boolean setValue = true;
54              while (!stop.get()) {
55                  if (setValue) {
56                      System.setProperty(property, "true");
57                  } else {
58                      System.clearProperty(property);
59                  }
60                  setValue = !setValue;
61              }
62          });
63          try {
64              t.start();
65  
66              final SystemConfiguration config = new SystemConfiguration();
67              final PropertiesConfiguration props = new PropertiesConfiguration();
68              props.append(config);
69  
70              stop.set(true);
71              t.join();
72              for (final Iterator<String> keys = config.getKeys(); keys.hasNext();) {
73                  final String key = keys.next();
74                  if (!property.equals(key)) {
75                      assertEquals(config.getString(key), props.getString(key), "Wrong value for " + key);
76                  }
77              }
78          } finally {
79              System.clearProperty(property);
80          }
81      }
82  
83      /**
84       * Tests whether the configuration can be used to change system properties.
85       */
86      @Test
87      void testChangeSystemProperties() {
88          final String testProperty = "someTest";
89          final SystemConfiguration config = new SystemConfiguration();
90          config.setProperty(testProperty, "true");
91          assertEquals("true", System.getProperty(testProperty));
92      }
93  
94      @Test
95      void testSetSystemProperties() {
96          final PropertiesConfiguration props = new PropertiesConfiguration();
97          props.addProperty("test.name", "Apache");
98          SystemConfiguration.setSystemProperties(props);
99          assertEquals("Apache", System.getProperty("test.name"));
100     }
101 
102     /**
103      * Tests whether system properties can be set from a configuration file.
104      */
105     @Test
106     void testSetSystemPropertiesFromPropertiesFile() throws ConfigurationException, IOException {
107         final File file = newFile("sys.properties", tempFolder);
108         final PropertiesConfiguration pconfig = new PropertiesConfiguration();
109         final FileHandler handler = new FileHandler(pconfig);
110         pconfig.addProperty("fromFile", Boolean.TRUE);
111         handler.setFile(file);
112         handler.save();
113         SystemConfiguration.setSystemProperties(handler.getBasePath(), handler.getFileName());
114         final SystemConfiguration sconf = new SystemConfiguration();
115         assertTrue(sconf.getBoolean("fromFile"));
116     }
117 
118     @Test
119     void testSystemConfiguration() {
120         final Properties props = System.getProperties();
121         props.put("test.number", "123");
122 
123         final Configuration conf = new SystemConfiguration();
124         assertEquals(123, conf.getInt("test.number"));
125     }
126 }