1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
37
38 public class TestSystemConfiguration {
39
40 @TempDir
41 public File tempFolder;
42
43
44
45
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
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
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 }