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