author | ykantser |
Thu, 07 May 2015 09:11:49 +0200 | |
changeset 30376 | 2ccf2cf7ea48 |
parent 5506 | 202f599c92aa |
child 43503 | bc7f8619ab70 |
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) 2004, 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 4982668 |
|
27 |
* @summary Test that a map containing 'null' values is handled |
|
28 |
* properly when converting the map into a hashtable, |
|
29 |
* i.e. all 'null' values should be removed before creating |
|
30 |
* the hashtable in order to avoid a NullPointerException. |
|
31 |
* Check also that null values for keys are not allowed in |
|
32 |
* the maps passed to the JMXConnector[Server] factories. |
|
33 |
* @author Luis-Miguel Alventosa |
|
30376
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
5506
diff
changeset
|
34 |
* @modules java.management/com.sun.jmx.remote.util |
2 | 35 |
* @run clean MapNullValuesTest |
36 |
* @run build MapNullValuesTest |
|
37 |
* @run main MapNullValuesTest |
|
38 |
*/ |
|
39 |
||
40 |
import java.rmi.RemoteException; |
|
41 |
import java.rmi.registry.Registry; |
|
42 |
import java.rmi.registry.LocateRegistry; |
|
43 |
import java.util.HashMap; |
|
44 |
import java.util.Hashtable; |
|
45 |
import java.util.Iterator; |
|
46 |
import java.util.Map; |
|
47 |
import java.util.Set; |
|
48 |
import javax.management.MBeanServer; |
|
49 |
import javax.management.MBeanServerFactory; |
|
50 |
import javax.management.remote.JMXConnector; |
|
51 |
import javax.management.remote.JMXConnectorFactory; |
|
52 |
import javax.management.remote.JMXConnectorServer; |
|
53 |
import javax.management.remote.JMXConnectorServerFactory; |
|
54 |
import javax.management.remote.JMXServiceURL; |
|
55 |
import com.sun.jmx.remote.util.EnvHelp; |
|
56 |
||
57 |
public class MapNullValuesTest { |
|
58 |
||
59 |
private static int port; |
|
60 |
private Map map0; |
|
61 |
private Map map1; |
|
62 |
private Map map2; |
|
63 |
private Map map3; |
|
64 |
private Map maps[]; |
|
65 |
||
66 |
public MapNullValuesTest() { |
|
67 |
// Map 0 |
|
68 |
// |
|
69 |
map0 = new HashMap(); |
|
70 |
||
71 |
// Map 1 |
|
72 |
// |
|
73 |
map1 = new HashMap(); |
|
74 |
map1.put("key1", "value1"); |
|
75 |
map1.put("key2", "value2"); |
|
76 |
map1.put("key3", "value3"); |
|
77 |
||
78 |
// Map 2 |
|
79 |
// |
|
80 |
map2 = new HashMap(); |
|
81 |
map2.put("key1", "value1"); |
|
82 |
map2.put("key2", null); |
|
83 |
map2.put("key3", "value3"); |
|
84 |
map2.put("key4", null); |
|
85 |
map2.put("key5", "value5"); |
|
86 |
||
87 |
// Map 3 |
|
88 |
// |
|
89 |
map3 = new HashMap(); |
|
90 |
map3.put("key1", "value1"); |
|
91 |
map3.put(null, "value2"); |
|
92 |
map3.put("key3", "value3"); |
|
93 |
||
94 |
// Map Array |
|
95 |
// |
|
96 |
maps = new Map[] { map0, map1, map2, map3 }; |
|
97 |
} |
|
98 |
||
99 |
private void checkContents(Map m, Hashtable t) |
|
100 |
throws IllegalArgumentException { |
|
101 |
int size = m.size(); |
|
102 |
Set s = m.entrySet(); |
|
103 |
for (Iterator i = s.iterator(); i.hasNext(); ) { |
|
104 |
Map.Entry e = (Map.Entry) i.next(); |
|
105 |
Object key = e.getKey(); |
|
106 |
Object value = e.getValue(); |
|
107 |
if (key == null || value == null) { // Null value |
|
108 |
size--; |
|
109 |
} else { // Check for equality |
|
110 |
if (t.get(key) == null) |
|
111 |
throw new IllegalArgumentException("Unknown key!"); |
|
112 |
else if (!t.get(key).equals(value)) |
|
113 |
throw new IllegalArgumentException("Value mismatch!"); |
|
114 |
} |
|
115 |
} |
|
116 |
if (t.size() != size) |
|
117 |
throw new IllegalArgumentException("Size mismatch!"); |
|
118 |
} |
|
119 |
||
120 |
private int mapToHashtableTests() { |
|
121 |
int errorCount = 0; |
|
122 |
echo(""); |
|
123 |
echo(dashedMessage("Run MapToHashtable Tests")); |
|
124 |
for (int i = 0; i < maps.length; i++) { |
|
125 |
echo("\n>>> MapToHashtable Test [" + i + "]"); |
|
126 |
try { |
|
127 |
echo("\tMap = " + maps[i]); |
|
128 |
Hashtable t = EnvHelp.mapToHashtable(maps[i]); |
|
129 |
echo("\tHashtable = " + t); |
|
130 |
checkContents(maps[i], t); |
|
131 |
echo("\tTest [" + i + "] PASSED!"); |
|
132 |
} catch (Exception e) { |
|
133 |
errorCount++; |
|
134 |
echo("\tTest [" + i + "] FAILED!"); |
|
135 |
e.printStackTrace(System.out); |
|
136 |
} |
|
137 |
} |
|
138 |
if (errorCount == 0) { |
|
139 |
echo(""); |
|
140 |
echo(dashedMessage("MapToHashtable Tests PASSED!")); |
|
141 |
} else { |
|
142 |
echo(""); |
|
143 |
echo(dashedMessage("MapToHashtable Tests FAILED!")); |
|
144 |
} |
|
145 |
return errorCount; |
|
146 |
} |
|
147 |
||
148 |
private int jmxConnectorServerFactoryTests() { |
|
149 |
int errorCount = 0; |
|
150 |
echo(""); |
|
151 |
echo(dashedMessage("Run JMXConnectorServerFactory Tests")); |
|
152 |
for (int i = 0; i < maps.length - 1; i++) { |
|
153 |
echo("\n>>> JMXConnectorServerFactory Test [" + i + "]"); |
|
154 |
try { |
|
155 |
echo("\tMap = " + maps[i]); |
|
156 |
echo("\tCreate the MBean server"); |
|
157 |
MBeanServer mbs = MBeanServerFactory.createMBeanServer(); |
|
158 |
echo("\tCreate the RMI connector server"); |
|
159 |
JMXServiceURL url = |
|
160 |
new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:" + |
|
161 |
port + "/JMXConnectorServerFactory" + i); |
|
162 |
JMXConnectorServer jmxcs = |
|
163 |
JMXConnectorServerFactory.newJMXConnectorServer(url, |
|
164 |
maps[i], |
|
165 |
mbs); |
|
166 |
echo("\tStart the RMI connector server"); |
|
167 |
jmxcs.start(); |
|
168 |
echo("\tCall RMIConnectorServer.toJMXConnector(Map)"); |
|
169 |
jmxcs.toJMXConnector(maps[i]); |
|
170 |
echo("\tStop the RMI connector server"); |
|
171 |
jmxcs.stop(); |
|
172 |
echo("\tTest [" + i + "] PASSED!"); |
|
173 |
} catch (Exception e) { |
|
174 |
errorCount++; |
|
175 |
echo("\tTest [" + i + "] FAILED!"); |
|
176 |
e.printStackTrace(System.out); |
|
177 |
} |
|
178 |
} |
|
179 |
if (errorCount == 0) { |
|
180 |
echo(""); |
|
181 |
echo(dashedMessage("JMXConnectorServerFactory Tests PASSED!")); |
|
182 |
} else { |
|
183 |
echo(""); |
|
184 |
echo(dashedMessage("JMXConnectorServerFactory Tests FAILED!")); |
|
185 |
} |
|
186 |
return errorCount; |
|
187 |
} |
|
188 |
||
189 |
private int jmxConnectorFactoryTests() { |
|
190 |
int errorCount = 0; |
|
191 |
echo(""); |
|
192 |
echo(dashedMessage("Run JMXConnectorFactory Tests")); |
|
193 |
for (int i = 0; i < maps.length - 1; i++) { |
|
194 |
echo("\n>>> JMXConnectorFactory Test [" + i + "]"); |
|
195 |
try { |
|
196 |
echo("\tMap = " + maps[i]); |
|
197 |
echo("\tCreate the MBean server"); |
|
198 |
MBeanServer mbs = MBeanServerFactory.createMBeanServer(); |
|
199 |
echo("\tCreate the RMI connector server"); |
|
200 |
JMXServiceURL url = |
|
201 |
new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:" + |
|
202 |
port + "/JMXConnectorFactory" + i); |
|
203 |
JMXConnectorServer jmxcs = |
|
204 |
JMXConnectorServerFactory.newJMXConnectorServer(url, |
|
205 |
null, |
|
206 |
mbs); |
|
207 |
echo("\tStart the RMI connector server"); |
|
208 |
jmxcs.start(); |
|
209 |
echo("\tCreate and connect the RMI connector"); |
|
210 |
JMXConnector jmxc = |
|
211 |
JMXConnectorFactory.connect(jmxcs.getAddress(), maps[i]); |
|
212 |
echo("\tClose the RMI connector"); |
|
213 |
jmxc.close(); |
|
214 |
echo("\tTest [" + i + "] PASSED!"); |
|
215 |
} catch (Exception e) { |
|
216 |
errorCount++; |
|
217 |
echo("\tTest [" + i + "] FAILED!"); |
|
218 |
e.printStackTrace(System.out); |
|
219 |
} |
|
220 |
} |
|
221 |
if (errorCount == 0) { |
|
222 |
echo(""); |
|
223 |
echo(dashedMessage("JMXConnectorFactory Tests PASSED!")); |
|
224 |
} else { |
|
225 |
echo(""); |
|
226 |
echo(dashedMessage("JMXConnectorFactory Tests FAILED!")); |
|
227 |
} |
|
228 |
return errorCount; |
|
229 |
} |
|
230 |
||
231 |
private int nullKeyFactoryTests() { |
|
232 |
int errorCount = 0; |
|
233 |
echo(""); |
|
234 |
echo(dashedMessage("Run Null Key Factory Tests")); |
|
235 |
echo("\tMap = " + map3); |
|
236 |
try { |
|
237 |
String urlStr = |
|
238 |
"service:jmx:rmi:///jndi/rmi://:" + port + "/NullKeyFactory"; |
|
239 |
MBeanServer mbs = MBeanServerFactory.createMBeanServer(); |
|
240 |
JMXServiceURL url = null; |
|
241 |
JMXConnectorServer jmxcs = null; |
|
242 |
JMXConnector jmxc = null; |
|
243 |
||
244 |
echo("\tJMXConnectorServerFactory.newJMXConnectorServer()"); |
|
245 |
try { |
|
246 |
url = new JMXServiceURL(urlStr + "1"); |
|
247 |
jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url, |
|
248 |
map3, |
|
249 |
mbs); |
|
250 |
errorCount++; |
|
251 |
echo("\tTest FAILED!"); |
|
252 |
} catch (Exception e) { |
|
253 |
echo("\tException Message: " + e.getMessage()); |
|
254 |
echo("\tTest PASSED!"); |
|
255 |
} |
|
256 |
||
257 |
echo("\tJMXConnectorServerFactory.toJMXConnector()"); |
|
258 |
try { |
|
259 |
url = new JMXServiceURL(urlStr + "2"); |
|
260 |
jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url, |
|
261 |
null, |
|
262 |
mbs); |
|
263 |
jmxcs.start(); |
|
264 |
jmxcs.toJMXConnector(map3); |
|
265 |
errorCount++; |
|
266 |
echo("\tTest FAILED!"); |
|
267 |
} catch (Exception e) { |
|
268 |
echo("\tException Message: " + e.getMessage()); |
|
269 |
echo("\tTest PASSED!"); |
|
270 |
} finally { |
|
271 |
jmxcs.stop(); |
|
272 |
} |
|
273 |
||
274 |
echo("\tJMXConnectorFactory.newJMXConnector()"); |
|
275 |
try { |
|
276 |
url = new JMXServiceURL(urlStr + "3"); |
|
277 |
jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url, |
|
278 |
null, |
|
279 |
mbs); |
|
280 |
jmxcs.start(); |
|
281 |
jmxc = JMXConnectorFactory.newJMXConnector(jmxcs.getAddress(), |
|
282 |
map3); |
|
283 |
errorCount++; |
|
284 |
echo("\tTest FAILED!"); |
|
285 |
} catch (Exception e) { |
|
286 |
echo("\tException Message: " + e.getMessage()); |
|
287 |
echo("\tTest PASSED!"); |
|
288 |
} finally { |
|
289 |
jmxcs.stop(); |
|
290 |
} |
|
291 |
||
292 |
echo("\tJMXConnectorFactory.connect()"); |
|
293 |
try { |
|
294 |
url = new JMXServiceURL(urlStr + "4"); |
|
295 |
jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url, |
|
296 |
null, |
|
297 |
mbs); |
|
298 |
jmxcs.start(); |
|
299 |
jmxc = JMXConnectorFactory.connect(jmxcs.getAddress(), map3); |
|
300 |
errorCount++; |
|
301 |
echo("\tTest FAILED!"); |
|
302 |
} catch (Exception e) { |
|
303 |
echo("\tException Message: " + e.getMessage()); |
|
304 |
echo("\tTest PASSED!"); |
|
305 |
} finally { |
|
306 |
jmxcs.stop(); |
|
307 |
} |
|
308 |
||
309 |
echo("\tJMXConnector.connect()"); |
|
310 |
try { |
|
311 |
url = new JMXServiceURL(urlStr + "5"); |
|
312 |
jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url, |
|
313 |
null, |
|
314 |
mbs); |
|
315 |
jmxcs.start(); |
|
316 |
jmxc = JMXConnectorFactory.newJMXConnector(jmxcs.getAddress(), |
|
317 |
null); |
|
318 |
jmxc.connect(map3); |
|
319 |
errorCount++; |
|
320 |
echo("\tTest FAILED!"); |
|
321 |
} catch (Exception e) { |
|
322 |
echo("\tException Message: " + e.getMessage()); |
|
323 |
echo("\tTest PASSED!"); |
|
324 |
} finally { |
|
325 |
jmxcs.stop(); |
|
326 |
} |
|
327 |
||
328 |
} catch (Exception e) { |
|
329 |
echo("\tGot unexpected exception!"); |
|
330 |
e.printStackTrace(System.out); |
|
331 |
errorCount = 1; |
|
332 |
} |
|
333 |
||
334 |
if (errorCount == 0) { |
|
335 |
echo(""); |
|
336 |
echo(dashedMessage("Null Key Factory Tests PASSED!")); |
|
337 |
} else { |
|
338 |
echo(""); |
|
339 |
echo(dashedMessage("Null Key Factory Tests FAILED!")); |
|
340 |
} |
|
341 |
return errorCount; |
|
342 |
} |
|
343 |
||
344 |
private static String dashedMessage(String message) { |
|
345 |
final int MAX_LINE = 80; |
|
346 |
StringBuffer sb = new StringBuffer(message); |
|
347 |
sb.append(" "); |
|
348 |
for (int i = MAX_LINE; i > message.length() + 1; i--) |
|
349 |
sb.append("-"); |
|
350 |
return sb.toString(); |
|
351 |
} |
|
352 |
||
353 |
private static void echo(String message) { |
|
354 |
System.out.println(message); |
|
355 |
} |
|
356 |
||
357 |
public static void main(String[] args) throws Exception { |
|
358 |
||
359 |
int errorCount = 0; |
|
360 |
||
361 |
MapNullValuesTest test = new MapNullValuesTest(); |
|
362 |
||
363 |
// Create an RMI registry |
|
364 |
// |
|
365 |
echo(""); |
|
366 |
echo(dashedMessage("Start RMI registry")); |
|
367 |
Registry reg = null; |
|
368 |
port = 7500; |
|
369 |
while (port++ < 7550) { |
|
370 |
try { |
|
371 |
reg = LocateRegistry.createRegistry(port); |
|
372 |
echo("\nRMI registry running on port " + port); |
|
373 |
break; |
|
374 |
} catch (RemoteException e) { |
|
375 |
// Failed to create RMI registry... |
|
376 |
// |
|
377 |
echo("\nFailed to create RMI registry on port " + port); |
|
378 |
e.printStackTrace(System.out); |
|
379 |
} |
|
380 |
} |
|
381 |
if (reg == null) { |
|
382 |
System.exit(1); |
|
383 |
} |
|
384 |
||
385 |
// Run tests |
|
386 |
// |
|
387 |
errorCount += test.mapToHashtableTests(); |
|
388 |
errorCount += test.jmxConnectorServerFactoryTests(); |
|
389 |
errorCount += test.jmxConnectorFactoryTests(); |
|
390 |
errorCount += test.nullKeyFactoryTests(); |
|
391 |
||
392 |
if (errorCount == 0) { |
|
393 |
echo("\nNull values for key/value pairs in Map Tests PASSED!"); |
|
394 |
} else { |
|
395 |
echo("\nNull values for key/value pairs in Map Tests FAILED!"); |
|
396 |
System.exit(1); |
|
397 |
} |
|
398 |
} |
|
399 |
} |