0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4982668
0N/A * @summary Test that a map containing 'null' values is handled
0N/A * properly when converting the map into a hashtable,
0N/A * i.e. all 'null' values should be removed before creating
0N/A * the hashtable in order to avoid a NullPointerException.
0N/A * Check also that null values for keys are not allowed in
0N/A * the maps passed to the JMXConnector[Server] factories.
0N/A * @author Luis-Miguel Alventosa
0N/A * @run clean MapNullValuesTest
0N/A * @run build MapNullValuesTest
0N/A * @run main MapNullValuesTest
0N/A */
0N/A
0N/Aimport java.rmi.RemoteException;
0N/Aimport java.rmi.registry.Registry;
0N/Aimport java.rmi.registry.LocateRegistry;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.Iterator;
0N/Aimport java.util.Map;
0N/Aimport java.util.Set;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.MBeanServerFactory;
0N/Aimport javax.management.remote.JMXConnector;
0N/Aimport javax.management.remote.JMXConnectorFactory;
0N/Aimport javax.management.remote.JMXConnectorServer;
0N/Aimport javax.management.remote.JMXConnectorServerFactory;
0N/Aimport javax.management.remote.JMXServiceURL;
0N/Aimport com.sun.jmx.remote.util.EnvHelp;
0N/A
0N/Apublic class MapNullValuesTest {
0N/A
0N/A private static int port;
0N/A private Map map0;
0N/A private Map map1;
0N/A private Map map2;
0N/A private Map map3;
0N/A private Map maps[];
0N/A
0N/A public MapNullValuesTest() {
0N/A // Map 0
0N/A //
0N/A map0 = new HashMap();
0N/A
0N/A // Map 1
0N/A //
0N/A map1 = new HashMap();
0N/A map1.put("key1", "value1");
0N/A map1.put("key2", "value2");
0N/A map1.put("key3", "value3");
0N/A
0N/A // Map 2
0N/A //
0N/A map2 = new HashMap();
0N/A map2.put("key1", "value1");
0N/A map2.put("key2", null);
0N/A map2.put("key3", "value3");
0N/A map2.put("key4", null);
0N/A map2.put("key5", "value5");
0N/A
0N/A // Map 3
0N/A //
0N/A map3 = new HashMap();
0N/A map3.put("key1", "value1");
0N/A map3.put(null, "value2");
0N/A map3.put("key3", "value3");
0N/A
0N/A // Map Array
0N/A //
0N/A maps = new Map[] { map0, map1, map2, map3 };
0N/A }
0N/A
0N/A private void checkContents(Map m, Hashtable t)
0N/A throws IllegalArgumentException {
0N/A int size = m.size();
0N/A Set s = m.entrySet();
0N/A for (Iterator i = s.iterator(); i.hasNext(); ) {
0N/A Map.Entry e = (Map.Entry) i.next();
0N/A Object key = e.getKey();
0N/A Object value = e.getValue();
0N/A if (key == null || value == null) { // Null value
0N/A size--;
0N/A } else { // Check for equality
0N/A if (t.get(key) == null)
0N/A throw new IllegalArgumentException("Unknown key!");
0N/A else if (!t.get(key).equals(value))
0N/A throw new IllegalArgumentException("Value mismatch!");
0N/A }
0N/A }
0N/A if (t.size() != size)
0N/A throw new IllegalArgumentException("Size mismatch!");
0N/A }
0N/A
0N/A private int mapToHashtableTests() {
0N/A int errorCount = 0;
0N/A echo("");
0N/A echo(dashedMessage("Run MapToHashtable Tests"));
0N/A for (int i = 0; i < maps.length; i++) {
0N/A echo("\n>>> MapToHashtable Test [" + i + "]");
0N/A try {
0N/A echo("\tMap = " + maps[i]);
0N/A Hashtable t = EnvHelp.mapToHashtable(maps[i]);
0N/A echo("\tHashtable = " + t);
0N/A checkContents(maps[i], t);
0N/A echo("\tTest [" + i + "] PASSED!");
0N/A } catch (Exception e) {
0N/A errorCount++;
0N/A echo("\tTest [" + i + "] FAILED!");
0N/A e.printStackTrace(System.out);
0N/A }
0N/A }
0N/A if (errorCount == 0) {
0N/A echo("");
0N/A echo(dashedMessage("MapToHashtable Tests PASSED!"));
0N/A } else {
0N/A echo("");
0N/A echo(dashedMessage("MapToHashtable Tests FAILED!"));
0N/A }
0N/A return errorCount;
0N/A }
0N/A
0N/A private int jmxConnectorServerFactoryTests() {
0N/A int errorCount = 0;
0N/A echo("");
0N/A echo(dashedMessage("Run JMXConnectorServerFactory Tests"));
0N/A for (int i = 0; i < maps.length - 1; i++) {
0N/A echo("\n>>> JMXConnectorServerFactory Test [" + i + "]");
0N/A try {
0N/A echo("\tMap = " + maps[i]);
0N/A echo("\tCreate the MBean server");
0N/A MBeanServer mbs = MBeanServerFactory.createMBeanServer();
0N/A echo("\tCreate the RMI connector server");
0N/A JMXServiceURL url =
0N/A new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:" +
0N/A port + "/JMXConnectorServerFactory" + i);
0N/A JMXConnectorServer jmxcs =
0N/A JMXConnectorServerFactory.newJMXConnectorServer(url,
0N/A maps[i],
0N/A mbs);
0N/A echo("\tStart the RMI connector server");
0N/A jmxcs.start();
0N/A echo("\tCall RMIConnectorServer.toJMXConnector(Map)");
0N/A jmxcs.toJMXConnector(maps[i]);
0N/A echo("\tStop the RMI connector server");
0N/A jmxcs.stop();
0N/A echo("\tTest [" + i + "] PASSED!");
0N/A } catch (Exception e) {
0N/A errorCount++;
0N/A echo("\tTest [" + i + "] FAILED!");
0N/A e.printStackTrace(System.out);
0N/A }
0N/A }
0N/A if (errorCount == 0) {
0N/A echo("");
0N/A echo(dashedMessage("JMXConnectorServerFactory Tests PASSED!"));
0N/A } else {
0N/A echo("");
0N/A echo(dashedMessage("JMXConnectorServerFactory Tests FAILED!"));
0N/A }
0N/A return errorCount;
0N/A }
0N/A
0N/A private int jmxConnectorFactoryTests() {
0N/A int errorCount = 0;
0N/A echo("");
0N/A echo(dashedMessage("Run JMXConnectorFactory Tests"));
0N/A for (int i = 0; i < maps.length - 1; i++) {
0N/A echo("\n>>> JMXConnectorFactory Test [" + i + "]");
0N/A try {
0N/A echo("\tMap = " + maps[i]);
0N/A echo("\tCreate the MBean server");
0N/A MBeanServer mbs = MBeanServerFactory.createMBeanServer();
0N/A echo("\tCreate the RMI connector server");
0N/A JMXServiceURL url =
0N/A new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:" +
0N/A port + "/JMXConnectorFactory" + i);
0N/A JMXConnectorServer jmxcs =
0N/A JMXConnectorServerFactory.newJMXConnectorServer(url,
0N/A null,
0N/A mbs);
0N/A echo("\tStart the RMI connector server");
0N/A jmxcs.start();
0N/A echo("\tCreate and connect the RMI connector");
0N/A JMXConnector jmxc =
0N/A JMXConnectorFactory.connect(jmxcs.getAddress(), maps[i]);
0N/A echo("\tClose the RMI connector");
0N/A jmxc.close();
0N/A echo("\tTest [" + i + "] PASSED!");
0N/A } catch (Exception e) {
0N/A errorCount++;
0N/A echo("\tTest [" + i + "] FAILED!");
0N/A e.printStackTrace(System.out);
0N/A }
0N/A }
0N/A if (errorCount == 0) {
0N/A echo("");
0N/A echo(dashedMessage("JMXConnectorFactory Tests PASSED!"));
0N/A } else {
0N/A echo("");
0N/A echo(dashedMessage("JMXConnectorFactory Tests FAILED!"));
0N/A }
0N/A return errorCount;
0N/A }
0N/A
0N/A private int nullKeyFactoryTests() {
0N/A int errorCount = 0;
0N/A echo("");
0N/A echo(dashedMessage("Run Null Key Factory Tests"));
0N/A echo("\tMap = " + map3);
0N/A try {
0N/A String urlStr =
0N/A "service:jmx:rmi:///jndi/rmi://:" + port + "/NullKeyFactory";
0N/A MBeanServer mbs = MBeanServerFactory.createMBeanServer();
0N/A JMXServiceURL url = null;
0N/A JMXConnectorServer jmxcs = null;
0N/A JMXConnector jmxc = null;
0N/A
0N/A echo("\tJMXConnectorServerFactory.newJMXConnectorServer()");
0N/A try {
0N/A url = new JMXServiceURL(urlStr + "1");
0N/A jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url,
0N/A map3,
0N/A mbs);
0N/A errorCount++;
0N/A echo("\tTest FAILED!");
0N/A } catch (Exception e) {
0N/A echo("\tException Message: " + e.getMessage());
0N/A echo("\tTest PASSED!");
0N/A }
0N/A
0N/A echo("\tJMXConnectorServerFactory.toJMXConnector()");
0N/A try {
0N/A url = new JMXServiceURL(urlStr + "2");
0N/A jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url,
0N/A null,
0N/A mbs);
0N/A jmxcs.start();
0N/A jmxcs.toJMXConnector(map3);
0N/A errorCount++;
0N/A echo("\tTest FAILED!");
0N/A } catch (Exception e) {
0N/A echo("\tException Message: " + e.getMessage());
0N/A echo("\tTest PASSED!");
0N/A } finally {
0N/A jmxcs.stop();
0N/A }
0N/A
0N/A echo("\tJMXConnectorFactory.newJMXConnector()");
0N/A try {
0N/A url = new JMXServiceURL(urlStr + "3");
0N/A jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url,
0N/A null,
0N/A mbs);
0N/A jmxcs.start();
0N/A jmxc = JMXConnectorFactory.newJMXConnector(jmxcs.getAddress(),
0N/A map3);
0N/A errorCount++;
0N/A echo("\tTest FAILED!");
0N/A } catch (Exception e) {
0N/A echo("\tException Message: " + e.getMessage());
0N/A echo("\tTest PASSED!");
0N/A } finally {
0N/A jmxcs.stop();
0N/A }
0N/A
0N/A echo("\tJMXConnectorFactory.connect()");
0N/A try {
0N/A url = new JMXServiceURL(urlStr + "4");
0N/A jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url,
0N/A null,
0N/A mbs);
0N/A jmxcs.start();
0N/A jmxc = JMXConnectorFactory.connect(jmxcs.getAddress(), map3);
0N/A errorCount++;
0N/A echo("\tTest FAILED!");
0N/A } catch (Exception e) {
0N/A echo("\tException Message: " + e.getMessage());
0N/A echo("\tTest PASSED!");
0N/A } finally {
0N/A jmxcs.stop();
0N/A }
0N/A
0N/A echo("\tJMXConnector.connect()");
0N/A try {
0N/A url = new JMXServiceURL(urlStr + "5");
0N/A jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url,
0N/A null,
0N/A mbs);
0N/A jmxcs.start();
0N/A jmxc = JMXConnectorFactory.newJMXConnector(jmxcs.getAddress(),
0N/A null);
0N/A jmxc.connect(map3);
0N/A errorCount++;
0N/A echo("\tTest FAILED!");
0N/A } catch (Exception e) {
0N/A echo("\tException Message: " + e.getMessage());
0N/A echo("\tTest PASSED!");
0N/A } finally {
0N/A jmxcs.stop();
0N/A }
0N/A
0N/A } catch (Exception e) {
0N/A echo("\tGot unexpected exception!");
0N/A e.printStackTrace(System.out);
0N/A errorCount = 1;
0N/A }
0N/A
0N/A if (errorCount == 0) {
0N/A echo("");
0N/A echo(dashedMessage("Null Key Factory Tests PASSED!"));
0N/A } else {
0N/A echo("");
0N/A echo(dashedMessage("Null Key Factory Tests FAILED!"));
0N/A }
0N/A return errorCount;
0N/A }
0N/A
0N/A private static String dashedMessage(String message) {
0N/A final int MAX_LINE = 80;
0N/A StringBuffer sb = new StringBuffer(message);
0N/A sb.append(" ");
0N/A for (int i = MAX_LINE; i > message.length() + 1; i--)
0N/A sb.append("-");
0N/A return sb.toString();
0N/A }
0N/A
0N/A private static void echo(String message) {
0N/A System.out.println(message);
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A int errorCount = 0;
0N/A
0N/A MapNullValuesTest test = new MapNullValuesTest();
0N/A
0N/A // Create an RMI registry
0N/A //
0N/A echo("");
0N/A echo(dashedMessage("Start RMI registry"));
0N/A Registry reg = null;
0N/A port = 7500;
0N/A while (port++ < 7550) {
0N/A try {
0N/A reg = LocateRegistry.createRegistry(port);
0N/A echo("\nRMI registry running on port " + port);
0N/A break;
0N/A } catch (RemoteException e) {
0N/A // Failed to create RMI registry...
0N/A //
0N/A echo("\nFailed to create RMI registry on port " + port);
0N/A e.printStackTrace(System.out);
0N/A }
0N/A }
0N/A if (reg == null) {
0N/A System.exit(1);
0N/A }
0N/A
0N/A // Run tests
0N/A //
0N/A errorCount += test.mapToHashtableTests();
0N/A errorCount += test.jmxConnectorServerFactoryTests();
0N/A errorCount += test.jmxConnectorFactoryTests();
0N/A errorCount += test.nullKeyFactoryTests();
0N/A
0N/A if (errorCount == 0) {
0N/A echo("\nNull values for key/value pairs in Map Tests PASSED!");
0N/A } else {
0N/A echo("\nNull values for key/value pairs in Map Tests FAILED!");
0N/A System.exit(1);
0N/A }
0N/A }
0N/A}