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