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