author | iignatyev |
Wed, 15 Mar 2017 22:48:59 -0700 | |
changeset 44423 | 306c020eb154 |
parent 30376 | 2ccf2cf7ea48 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
30376
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 4883712 4869006 4894856 5016685 |
|
27 |
* @summary Test that DescriptorSupport correctly validates fields |
|
28 |
* @author Eamonn McManus |
|
44423 | 29 |
* |
2 | 30 |
* @run clean DescriptorSupportTest |
31 |
* @run build DescriptorSupportTest |
|
32 |
* @run main DescriptorSupportTest |
|
33 |
*/ |
|
34 |
||
35 |
import java.io.ByteArrayInputStream; |
|
36 |
import java.io.ByteArrayOutputStream; |
|
37 |
import java.io.ObjectInputStream; |
|
38 |
import java.io.ObjectOutputStream; |
|
39 |
import java.util.ArrayList; |
|
40 |
import java.util.Arrays; |
|
41 |
import java.util.List; |
|
42 |
import javax.management.Descriptor; |
|
43 |
import javax.management.RuntimeOperationsException; |
|
44 |
import javax.management.modelmbean.DescriptorSupport; |
|
45 |
import javax.management.modelmbean.ModelMBeanInfo; |
|
46 |
import javax.management.modelmbean.ModelMBeanInfoSupport; |
|
47 |
||
48 |
public class DescriptorSupportTest { |
|
49 |
private static final Object[] goodFields = { |
|
50 |
"value", "", |
|
51 |
"severity", "0", |
|
52 |
"severity", "6", |
|
53 |
}; |
|
54 |
||
55 |
private static final Object[] badFields = { |
|
56 |
"name", null, |
|
57 |
"name", "", |
|
58 |
"descriptorType", null, |
|
59 |
"descriptorType", "", |
|
60 |
"setMethod", null, |
|
61 |
"getMethod", null, |
|
62 |
"role", null, |
|
63 |
"class", null, |
|
64 |
"visibility", null, |
|
65 |
"visibility", new Integer(0), |
|
66 |
"visibility", "0", |
|
67 |
"visibility", new Integer(5), |
|
68 |
"visibility", "5", |
|
69 |
"severity", null, |
|
70 |
"severity", new Integer(-1), |
|
71 |
"severity", "-1", |
|
72 |
"severity", new Integer(7), |
|
73 |
"severity", "7", |
|
74 |
"persistPolicy", null, |
|
75 |
"persistPolicy", "bogusPersistPolicy", |
|
76 |
"persistPeriod", null, |
|
77 |
"persistPeriod", "not a number", |
|
78 |
"currencyTimeLimit", null, |
|
79 |
"currencyTimeLimit", "not a number", |
|
80 |
"lastUpdatedTimeStamp", null, |
|
81 |
"lastUpdatedTimeStamp", "not a number", |
|
82 |
"lastReturnedTimeStamp", null, |
|
83 |
"lastReturnedTimeStamp", "not a number", |
|
84 |
"log", null, |
|
85 |
"log", "not T or F or true or false", |
|
86 |
"log", new Object[0], |
|
87 |
}; |
|
88 |
||
89 |
||
90 |
public static void main(String[] args) throws Exception { |
|
91 |
boolean ok = true; |
|
92 |
||
93 |
System.out.println("Checking that name and descriptorType are " + |
|
94 |
"mandatory"); |
|
95 |
// Try omitting name and/or descriptorType |
|
96 |
for (int i = 0; i < 3; i++) { |
|
97 |
final boolean addName = ((i & 1) != 0); |
|
98 |
final boolean addDescriptorType = ((i & 2) != 0); |
|
99 |
final List fields = new ArrayList(); |
|
100 |
if (addName) |
|
101 |
fields.add("name=something"); |
|
102 |
if (addDescriptorType) |
|
103 |
fields.add("descriptorType=something-else"); |
|
104 |
final String[] fs = (String[]) fields.toArray(new String[0]); |
|
105 |
final String what = |
|
106 |
"DescriptorSupport with " + |
|
107 |
(addName ? "" : "no ") + "name and " + |
|
108 |
(addDescriptorType ? "" : "no ") + "descriptorType"; |
|
109 |
DescriptorSupport ds = new DescriptorSupport(fs); |
|
110 |
if (ds.isValid()) { |
|
111 |
System.out.println("INCORRECTLY ACCEPTED: " + what); |
|
112 |
ok = false; |
|
113 |
} else |
|
114 |
System.out.println("OK: rejected " + what); |
|
115 |
} |
|
116 |
||
117 |
for (int pass = 0; pass < 2; pass++) { |
|
118 |
boolean shouldAccept = (pass == 0); |
|
119 |
System.out.println("Trying out " + |
|
120 |
(shouldAccept ? "correct" : "bogus") + |
|
121 |
" DescriptorSupport fields"); |
|
122 |
Object[] fields = shouldAccept ? goodFields : badFields; |
|
123 |
for (int i = 0; i < fields.length; i += 2) { |
|
124 |
String[] names = {"name", "descriptorType"}; |
|
125 |
String[] values = {"some-name", "some-type"}; |
|
126 |
DescriptorSupport d = new DescriptorSupport(names, values); |
|
127 |
final String name = (String) fields[i]; |
|
128 |
final Object value = fields[i + 1]; |
|
129 |
final String valueS = |
|
130 |
(value instanceof String) ? ("\"" + value + "\"") : |
|
131 |
(value == null) ? "null" : value.toString(); |
|
132 |
final String what = |
|
133 |
"DescriptorSupport with " + name + " = " + valueS; |
|
134 |
try { |
|
135 |
d.setField(name, value); |
|
136 |
if (shouldAccept) |
|
137 |
System.out.println("OK: accepted " + what); |
|
138 |
else { |
|
139 |
System.out.println("INCORRECTLY ACCEPTED: " + what); |
|
140 |
ok = false; |
|
141 |
} |
|
142 |
} catch (RuntimeOperationsException e) { |
|
143 |
if (shouldAccept) { |
|
144 |
System.out.println("INCORRECTLY REJECTED: " + what + |
|
145 |
": " + e); |
|
146 |
ok = false; |
|
147 |
} else { |
|
148 |
System.out.println("OK: rejected " + what); |
|
149 |
// OK: this is what should happen |
|
150 |
} |
|
151 |
} catch (Exception e) { |
|
152 |
System.out.println("WRONG EXCEPTION: " + what + ": " + e); |
|
153 |
ok = false; |
|
154 |
} |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
// 4894856: ModelMBeanInfoSupport.setDescriptor(d, "mbean") fails |
|
159 |
System.out.println("Checking that setDescriptor(d, \"mbean\") works"); |
|
160 |
ModelMBeanInfo mmbi = |
|
161 |
new ModelMBeanInfoSupport("x", "descr", null, null, null, null); |
|
162 |
Descriptor d = mmbi.getDescriptor("x", "mbean"); |
|
163 |
try { |
|
164 |
mmbi.setDescriptor(d, "mbean"); |
|
165 |
} catch (Exception e) { |
|
166 |
System.out.println("Unexpected exception:"); |
|
167 |
e.printStackTrace(System.out); |
|
168 |
ok = false; |
|
169 |
} |
|
170 |
||
171 |
// 5016685: DescriptorSupport forces field names to lower case |
|
172 |
System.out.println("Checking that field name case is ignored " + |
|
173 |
"but preserved"); |
|
174 |
ok &= caseTest(new DescriptorSupport(new String[] {"NAME=blah"}), |
|
175 |
"DescriptorSupport(String[])"); |
|
176 |
ok &= caseTest(new DescriptorSupport(new String[] {"NAME"}, |
|
177 |
new String[] {"blah"}), |
|
178 |
"DescriptorSupport(String[], Object[])"); |
|
179 |
DescriptorSupport d1 = new DescriptorSupport(); |
|
180 |
d1.setField("NAME", "blah"); |
|
181 |
ok &= caseTest(d1, "DescriptorSupport.setField"); |
|
182 |
d1 = new DescriptorSupport(new String[] {"NAME=blah"}); |
|
183 |
ok &= caseTest(new DescriptorSupport(d1), |
|
184 |
"DescriptorSupport(Descriptor)"); |
|
185 |
d1 = new DescriptorSupport(new String[] {"NAME=blah"}); |
|
186 |
ok &= caseTest(new DescriptorSupport(d1.toXMLString()), |
|
187 |
"DescriptorSupport(String)"); |
|
188 |
d1 = new DescriptorSupport(new String[] {"NAME=blah"}); |
|
189 |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
|
190 |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
|
191 |
oos.writeObject(d1); |
|
192 |
oos.close(); |
|
193 |
bos.close(); |
|
194 |
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); |
|
195 |
ObjectInputStream ois = new ObjectInputStream(bis); |
|
196 |
d1 = (DescriptorSupport) ois.readObject(); |
|
197 |
ok &= caseTest(d1, "serialized DescriptorSupport"); |
|
198 |
||
199 |
if (ok) |
|
200 |
System.out.println("Test passed"); |
|
201 |
else { |
|
202 |
System.out.println("TEST FAILED"); |
|
203 |
System.exit(1); |
|
204 |
} |
|
205 |
} |
|
206 |
||
207 |
private static boolean caseTest(Descriptor d, String what) { |
|
208 |
boolean ok = true; |
|
209 |
||
210 |
System.out.println("..." + what); |
|
211 |
||
212 |
String[] names = d.getFieldNames(); |
|
213 |
if (names.length != 1 || !names[0].equals("NAME")) { |
|
214 |
ok = false; |
|
215 |
System.out.println("...getFieldNames() fails: " + |
|
216 |
Arrays.asList(names)); |
|
217 |
} |
|
218 |
||
219 |
String[] fields = d.getFields(); |
|
220 |
if (fields.length != 1 || !fields[0].equals("NAME=blah")) { |
|
221 |
ok = false; |
|
222 |
System.out.println("...getFields() fails: " + |
|
223 |
Arrays.asList(fields)); |
|
224 |
} |
|
225 |
||
226 |
Object value = d.getFieldValue("namE"); |
|
227 |
if (!"blah".equals(value)) { |
|
228 |
ok = false; |
|
229 |
System.out.println("...getFieldValue(\"namE\") fails: " + value); |
|
230 |
} |
|
231 |
||
232 |
Object[] values = d.getFieldValues(new String[] {"namE"}); |
|
233 |
if (values.length != 1 || !"blah".equals(values[0])) { |
|
234 |
ok = false; |
|
235 |
System.out.println("...getFieldValues({\"namE\"}) fails: " + |
|
236 |
Arrays.asList(values)); |
|
237 |
} |
|
238 |
||
239 |
d.setField("namE", "newblah"); |
|
240 |
Object newblah = d.getFieldValue("Name"); |
|
241 |
if (!"newblah".equals(newblah)) { |
|
242 |
ok = false; |
|
243 |
System.out.println("...setField value not returned: " + newblah); |
|
244 |
} |
|
245 |
||
246 |
d.setFields(new String[] {"NaMe"}, new Object[] {"newerblah"}); |
|
247 |
Object newerblah = d.getFieldValue("naMe"); |
|
248 |
if (!"newerblah".equals(newerblah)) { |
|
249 |
ok = false; |
|
250 |
System.out.println("...setFields value not returned: " + |
|
251 |
newerblah); |
|
252 |
} |
|
253 |
||
254 |
Descriptor d1 = (Descriptor) d.clone(); |
|
255 |
newerblah = d1.getFieldValue("NAMe"); |
|
256 |
if (!"newerblah".equals(newerblah)) { |
|
257 |
ok = false; |
|
258 |
System.out.println("...clone incorrect: " + newerblah); |
|
259 |
} |
|
260 |
||
261 |
d.removeField("NAme"); |
|
262 |
names = d.getFieldNames(); |
|
263 |
if (names.length != 0) { |
|
264 |
ok = false; |
|
265 |
System.out.println("...removeField failed: " + |
|
266 |
Arrays.asList(names)); |
|
267 |
} |
|
268 |
||
269 |
if (ok) |
|
270 |
System.out.println("...succeeded"); |
|
271 |
||
272 |
return ok; |
|
273 |
} |
|
274 |
} |