Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/System/PropertyTest.java
41149 views
1
/*
2
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.lang.System;
25
import java.util.Arrays;
26
import java.util.List;
27
import java.util.Objects;
28
import java.util.Properties;
29
import java.util.stream.Collectors;
30
31
import org.testng.Assert;
32
import org.testng.IMethodInstance;
33
import org.testng.IMethodInterceptor;
34
import org.testng.TestListenerAdapter;
35
import org.testng.TestNG;
36
import org.testng.annotations.BeforeTest;
37
import org.testng.annotations.DataProvider;
38
import org.testng.annotations.Test;
39
40
41
/*
42
* @test
43
* @bug 4463345 4244670 8030781 8265989
44
* @summary Simple test of System getProperty, setProperty, clearProperty,
45
* getProperties, and setProperties
46
* @run testng/othervm PropertyTest
47
*/
48
49
@Test
50
public class PropertyTest {
51
52
@DataProvider(name = "requiredProperties")
53
static Object[][] requiredProperties() {
54
return new Object[][]{
55
{"java.version"},
56
{"java.version.date"},
57
{"java.vendor"},
58
{"java.vendor.url"},
59
{"java.home"},
60
{"java.vm.specification.version"},
61
{"java.vm.specification.vendor"},
62
{"java.vm.specification.name"},
63
{"java.vm.version"},
64
{"java.vm.vendor"},
65
{"java.vm.name"},
66
{"java.specification.version"},
67
{"java.specification.vendor"},
68
{"java.specification.name"},
69
{"java.class.version"},
70
{"java.class.path"},
71
{"java.library.path"},
72
{"java.io.tmpdir"},
73
{"os.arch"},
74
{"os.version"},
75
{"file.separator"},
76
{"path.separator"},
77
{"line.separator"},
78
{"user.name"},
79
{"user.home"},
80
{"user.dir"},
81
{"java.runtime.version"},
82
{"java.runtime.name"},
83
{"native.encoding"},
84
};
85
}
86
87
@Test
88
static void getTest() {
89
System.setProperty("blah", "blech");
90
Assert.assertEquals(System.getProperty("blah"), "blech");
91
92
try {
93
System.getProperty(null);
94
Assert.fail("Failed: expected NullPointerException");
95
} catch (NullPointerException npe) {
96
// Correct result
97
}
98
try {
99
System.getProperty("");
100
Assert.fail("Failed: expected IllegalArgumentException");
101
} catch (IllegalArgumentException iae) {
102
// Correct result
103
}
104
}
105
106
@Test
107
static void clearTest() {
108
System.setProperty("blah", "blech");
109
Assert.assertEquals(System.getProperty("blah"), "blech");
110
111
System.clearProperty("blah");
112
Assert.assertNull(System.getProperty("blah"));
113
114
try {
115
System.clearProperty(null);
116
Assert.fail("Failed: expected NullPointerException");
117
} catch (NullPointerException npe) {
118
// Correct result
119
}
120
try {
121
System.clearProperty("");
122
Assert.fail("Failed: expected IllegalArgumentException");
123
} catch (IllegalArgumentException iae) {
124
// Correct result
125
}
126
}
127
128
@Test
129
static void setTest() {
130
System.setProperty("blah", "blech");
131
Assert.assertEquals(System.getProperty("blah"), "blech");
132
133
System.setProperty("blah", "");
134
Assert.assertEquals(System.getProperty("blah"), "");
135
136
try {
137
System.setProperty(null, null);
138
Assert.fail("Failed: expected NullPointerException");
139
} catch (NullPointerException npe) {
140
// Correct result
141
}
142
143
try {
144
System.setProperty("blah", null);
145
Assert.fail("Failed: expected NullPointerException");
146
} catch (NullPointerException npe) {
147
// Correct result
148
}
149
150
try {
151
System.setProperty(null, "blech");
152
Assert.fail("Failed: expected NullPointerException");
153
} catch (NullPointerException npe) {
154
// Correct result
155
}
156
157
try {
158
System.setProperty("", "blech");
159
Assert.fail("Failed: expected IllegalArgumentException");
160
} catch (IllegalArgumentException iae) {
161
// Correct result
162
}
163
try {
164
System.setProperty("", "");
165
Assert.fail("Failed: expected IllegalArgumentException");
166
} catch (IllegalArgumentException iae) {
167
// Correct result
168
}
169
}
170
171
@Test
172
static void replaceSetProperties() {
173
Properties oldProps = System.getProperties();
174
Properties newProps = new Properties();
175
oldProps.forEach( (k,v) -> newProps.put(k,v));
176
System.setProperties(newProps);
177
178
Assert.assertSame(System.getProperties(), newProps,
179
"getProperties not the same as setProperties");
180
181
final String KEY = "blah";
182
final String VALUE = "blech";
183
184
// Set via Property instance; get via System methods
185
newProps.setProperty(KEY, VALUE);
186
Assert.assertEquals(System.getProperty(KEY), VALUE, KEY);
187
188
String s = (String)newProps.remove(KEY);
189
Assert.assertEquals(s, VALUE);
190
Assert.assertNull(System.getProperty(KEY), KEY);
191
192
// Set via System methods; Get via Property instance;
193
System.setProperty(KEY, VALUE);
194
Assert.assertEquals(newProps.getProperty(KEY), VALUE);
195
196
String t = System.clearProperty(KEY);
197
Assert.assertEquals(t, VALUE, KEY);
198
Assert.assertNull(newProps.getProperty(KEY), KEY);
199
}
200
201
@Test
202
static void setNullProperties() {
203
Properties oldProps = System.getProperties();
204
Properties savedProps = new Properties();
205
oldProps.forEach((k,v) -> {
206
if (v == null) {
207
throw new RuntimeException("null value, key: " + k);
208
}
209
savedProps.put(k,v);
210
});
211
212
// Re-initialize properties
213
System.setProperties(null);
214
215
Properties newProps = System.getProperties();
216
Object[][] propnames = requiredProperties();
217
for (Object[] p : propnames) {
218
String name = (String)p[0];
219
Assert.assertEquals(System.getProperty(name), savedProps.getProperty(name), name);
220
221
Assert.assertEquals(newProps.getProperty(name), savedProps.getProperty(name), name);
222
}
223
}
224
225
// Verify all the required properties have values from System.getProperty and
226
// System.getProperties()
227
@Test(dataProvider = "requiredProperties")
228
static void checkRequiredProperties(String name) {
229
Assert.assertNotNull(System.getProperty(name), name);
230
231
Properties props = System.getProperties();
232
Assert.assertNotNull(props.getProperty(name), name);
233
}
234
235
@SuppressWarnings("raw_types")
236
@Test(enabled=false)
237
public static void main(String[] args) {
238
TestListenerAdapter tla = new TestListenerAdapter();
239
240
Class<?>[] testclass = {PropertyTest.class};
241
TestNG testng = new TestNG();
242
testng.setTestClasses(testclass);
243
testng.addListener(tla);
244
if (args.length > 0) {
245
IMethodInterceptor intercept = (m, c) -> {
246
List<IMethodInstance> x = m.stream()
247
.filter(m1 -> m1.getMethod().getMethodName().contains(args[0]))
248
.collect(Collectors.toList());
249
return x;
250
};
251
testng.setMethodInterceptor(intercept);
252
}
253
testng.run();
254
tla.getPassedTests()
255
.stream().forEach(t -> System.out.printf("Passed: %s%s%n", t.getName(),
256
List.of(t.getParameters())));
257
tla.getFailedTests()
258
.stream().forEach(t -> System.out.printf("Failed: %s%s%n", t.getName(),
259
List.of(t.getParameters())));
260
}
261
}
262
263