48664
|
1 |
/*
|
|
2 |
* Copyright (c) 2018, 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 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 6253413 8059361
|
|
27 |
* @summary Test for Properties.stringPropertyNames() if the system
|
|
28 |
* properties contain another list of properties as the defaults.
|
|
29 |
* @author Mandy Chung
|
|
30 |
*
|
|
31 |
* @run build StringPropertyNames
|
|
32 |
* @run main StringPropertyNames
|
|
33 |
*/
|
|
34 |
|
|
35 |
import java.util.Properties;
|
|
36 |
import java.util.Enumeration;
|
|
37 |
import java.util.Iterator;
|
|
38 |
import java.util.Set;
|
|
39 |
|
|
40 |
public class StringPropertyNames {
|
|
41 |
private static int NUM_SHARE_PROPS = 2;
|
|
42 |
private static int NUM_PROPS1 = 3;
|
|
43 |
private static int NUM_PROPS2 = 5;
|
|
44 |
private static String KEY = "good.property.";
|
|
45 |
private static String VALUE = "good.value.";
|
|
46 |
public static void main(String[] argv) throws Exception {
|
|
47 |
Properties props1 = new Properties();
|
|
48 |
Properties props2 = new Properties(props1);
|
|
49 |
|
|
50 |
// add several new properties
|
|
51 |
for (int i = 0; i < NUM_PROPS1; i++) {
|
|
52 |
props1.put(KEY + "1." + i, VALUE + "1." + i);
|
|
53 |
}
|
|
54 |
for (int i = 0; i < NUM_PROPS2; i++) {
|
|
55 |
props2.put(KEY + "2." + i, VALUE + "2." + i);
|
|
56 |
}
|
|
57 |
|
|
58 |
// add the same properties in both props1 and props2
|
|
59 |
for (int i = 0; i < NUM_SHARE_PROPS; i++) {
|
|
60 |
props1.put(KEY + i, VALUE + "1." + i);
|
|
61 |
props2.put(KEY + i, VALUE + "2." + i);
|
|
62 |
}
|
|
63 |
checkProperties(props1,
|
|
64 |
NUM_PROPS1 + NUM_SHARE_PROPS, // size of props1
|
|
65 |
NUM_PROPS1 + NUM_SHARE_PROPS, // num of string keys
|
|
66 |
NUM_PROPS1 + NUM_SHARE_PROPS, // num of keys in propertyName(),
|
|
67 |
false);
|
|
68 |
checkProperties(props2,
|
|
69 |
NUM_PROPS2 + NUM_SHARE_PROPS, // size of props2
|
|
70 |
NUM_PROPS1 + NUM_PROPS2 + NUM_SHARE_PROPS, // num of string keys
|
|
71 |
NUM_PROPS1 + NUM_PROPS2 + NUM_SHARE_PROPS, // num of keys in propertyName(),
|
|
72 |
false);
|
|
73 |
|
|
74 |
// Add non-String value
|
|
75 |
props1.put(KEY + "9", new Integer(4));
|
|
76 |
checkProperties(props1,
|
|
77 |
NUM_PROPS1 + NUM_SHARE_PROPS + 1, // size of props1
|
|
78 |
NUM_PROPS1 + NUM_SHARE_PROPS, // num of string keys
|
|
79 |
NUM_PROPS1 + NUM_SHARE_PROPS + 1, // num of keys in propertyName(),
|
|
80 |
false);
|
|
81 |
checkProperties(props2,
|
|
82 |
NUM_PROPS2 + NUM_SHARE_PROPS, // size of props2
|
|
83 |
NUM_PROPS1 + NUM_PROPS2 + NUM_SHARE_PROPS, // num of string keys
|
|
84 |
NUM_PROPS1 + NUM_PROPS2 + NUM_SHARE_PROPS + 1, // num of keys in propertyName(),
|
|
85 |
false);
|
|
86 |
Object v = props1.remove(KEY + "9");
|
|
87 |
if (v == null) {
|
|
88 |
throw new RuntimeException("Test Failed: " +
|
|
89 |
"Key " + KEY + "9" + " not found");
|
|
90 |
}
|
|
91 |
|
|
92 |
// Add a non-String key
|
|
93 |
props1.put(new Integer(5), "good.value.5");
|
|
94 |
props2.put(new Object(), new Object());
|
|
95 |
checkProperties(props1,
|
|
96 |
NUM_PROPS1 + NUM_SHARE_PROPS + 1, // size of props1
|
|
97 |
NUM_PROPS1 + NUM_SHARE_PROPS, // num of string keys
|
|
98 |
NUM_PROPS1 + NUM_SHARE_PROPS + 1, // num of keys in propertyName(),
|
|
99 |
true);
|
|
100 |
checkProperties(props2,
|
|
101 |
NUM_PROPS2 + NUM_SHARE_PROPS + 1, // size of props2
|
|
102 |
NUM_PROPS1 + NUM_PROPS2 + NUM_SHARE_PROPS, // num of string keys
|
|
103 |
NUM_PROPS1 + NUM_PROPS2 + NUM_SHARE_PROPS + 2, // num of keys in propertyName(),
|
|
104 |
true);
|
|
105 |
System.out.println("Test passed.");
|
|
106 |
}
|
|
107 |
|
|
108 |
private static void checkProperties(Properties props,
|
|
109 |
int propSize,
|
|
110 |
int numStringKeys,
|
|
111 |
int enumerateSize,
|
|
112 |
boolean hasNonStringKeys) {
|
|
113 |
// check the size of the properties
|
|
114 |
if (props.size() != propSize) {
|
|
115 |
throw new RuntimeException("Test Failed: " +
|
|
116 |
"Expected number of properties = " +
|
|
117 |
propSize + " but found = " + props.size());
|
|
118 |
}
|
|
119 |
|
|
120 |
// check the number of properties whose key and value
|
|
121 |
// are both strings
|
|
122 |
Set<String> keys = props.stringPropertyNames();
|
|
123 |
if (keys.size() != numStringKeys) {
|
|
124 |
throw new RuntimeException("Test Failed: " +
|
|
125 |
"Expected number of String keys = " +
|
|
126 |
numStringKeys + " but found = " + keys.size());
|
|
127 |
}
|
|
128 |
boolean cceThrown = false;
|
|
129 |
try {
|
|
130 |
// check the number of properties whose key are strings
|
|
131 |
// but its value can be anything in the current impl
|
|
132 |
int count = 0;
|
|
133 |
Enumeration<?> e = props.propertyNames();
|
|
134 |
for (;e.hasMoreElements(); e.nextElement()) {
|
|
135 |
count++;
|
|
136 |
}
|
|
137 |
if (count != enumerateSize) {
|
|
138 |
throw new RuntimeException("Test Failed: " +
|
|
139 |
"Expected number of enumerated keys = " +
|
|
140 |
enumerateSize + " but found = " + count);
|
|
141 |
}
|
|
142 |
} catch (ClassCastException e) {
|
|
143 |
if (!hasNonStringKeys) {
|
|
144 |
RuntimeException re = new RuntimeException("Test Failed: " +
|
|
145 |
"ClassCastException is expected not to be thrown");
|
|
146 |
re.initCause(e);
|
|
147 |
throw re;
|
|
148 |
}
|
|
149 |
cceThrown = true;
|
|
150 |
}
|
|
151 |
|
|
152 |
if ((hasNonStringKeys && !cceThrown)) {
|
|
153 |
throw new RuntimeException("Test Failed: " +
|
|
154 |
"ClassCastException is expected to be thrown");
|
|
155 |
}
|
|
156 |
|
|
157 |
// make sure the set cannot be modified
|
|
158 |
try {
|
|
159 |
keys.add("xyzzy");
|
|
160 |
throw new RuntimeException("Test Failed: " +
|
|
161 |
"add() should have thrown UnsupportedOperationException");
|
|
162 |
} catch (UnsupportedOperationException ignore) { }
|
|
163 |
|
|
164 |
Iterator<String> it = keys.iterator();
|
|
165 |
if (it.hasNext()) {
|
|
166 |
try {
|
|
167 |
keys.remove(it.next());
|
|
168 |
throw new RuntimeException("Test Failed: " +
|
|
169 |
"remove() should have thrown UnsupportedOperationException");
|
|
170 |
} catch (UnsupportedOperationException ignore) { }
|
|
171 |
}
|
|
172 |
}
|
|
173 |
}
|