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
41 @TempDir
42 public File tempFolder;
43
44
45
46
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
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
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 }