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