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