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