Path: blob/master/test/jdk/javax/management/ObjectName/SerialCompatTest.java
41149 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 6211220 661682526* @summary Test that jmx.serial.form=1.0 works for ObjectName27* @author Eamonn McManus, Daniel Fuchs28*29* @run clean SerialCompatTest30* @run build SerialCompatTest31* @run main/othervm -Djdk.jmx.mbeans.allowNonPublic=true -Djmx.serial.form=1.0 SerialCompatTest32*/3334import java.io.*;35import java.util.*;36import javax.management.ObjectName;3738public class SerialCompatTest {3940public static void check6211220() throws Exception {4142ObjectName on = new ObjectName("a:b=c");43ByteArrayOutputStream bos = new ByteArrayOutputStream();44ObjectOutputStream oos = new ObjectOutputStream(bos);45oos.writeObject(on);46oos.close();47byte[] bytes = bos.toByteArray();48ByteArrayInputStream bis = new ByteArrayInputStream(bytes);49ObjectInputStream ois = new ObjectInputStream(bis);50ObjectName on1 = (ObjectName) ois.readObject();5152// if the bug is present, these will get NullPointerException53for (int i = 0; i <= 11; i++) {54String msg = "6211220 case(" + i + ")";55try {56switch (i) {57case 0:58check(msg, on1.getDomain().equals("a"));59break;60case 1:61check(msg, on1.getCanonicalName().equals("a:b=c"));62break;63case 2:64check(msg, on1.getKeyPropertyListString()65.equals("b=c"));66break;67case 3:68check(msg, on1.getCanonicalKeyPropertyListString()69.equals("b=c"));70break;71case 4:72check(msg, on1.getKeyProperty("b").equals("c"));73break;74case 5:75check(msg, on1.getKeyPropertyList()76.equals(Collections.singletonMap("b", "c")));77break;78case 6:79check(msg, !on1.isDomainPattern());80break;81case 7:82check(msg, !on1.isPattern());83break;84case 8:85check(msg, !on1.isPropertyPattern());86break;87case 9:88check(msg, on1.equals(on));89break;90case 10:91check(msg, on.equals(on1));92break;93case 11:94check(msg, on1.apply(on));95break;96default:97throw new Exception(msg + ": Test incorrect");98}99} catch (Exception e) {100System.out.println(msg + ": Test failed with exception:");101e.printStackTrace(System.out);102failed = true;103}104}105106if (failed) {107throw new Exception("Some tests for 6211220 failed");108} else {109System.out.println("All tests for 6211220 passed");110}111}112113static void checkName(String testname, ObjectName on)114throws Exception {115ByteArrayOutputStream bos = new ByteArrayOutputStream();116ObjectOutputStream oos = new ObjectOutputStream(bos);117oos.writeObject(on);118oos.close();119byte[] bytes = bos.toByteArray();120ByteArrayInputStream bis = new ByteArrayInputStream(bytes);121ObjectInputStream ois = new ObjectInputStream(bis);122ObjectName on1 = (ObjectName) ois.readObject();123// if the bug is present, these will get NullPointerException124for (int i = 0; i <= 11; i++) {125String msg = testname + " case(" + i + ")";126try {127switch (i) {128case 0:129check(msg, on1.getDomain().equals(on.getDomain()));130break;131case 1:132check(msg, on1.getCanonicalName().133equals(on.getCanonicalName()));134break;135case 2:136check(msg, on1.getKeyPropertyListString().137equals(on.getKeyPropertyListString()));138break;139case 3:140check(msg, on1.getCanonicalKeyPropertyListString().141equals(on.getCanonicalKeyPropertyListString()));142break;143case 4:144for (Object ko : on1.getKeyPropertyList().keySet()) {145final String key = (String) ko;146check(msg, on1.getKeyProperty(key).147equals(on.getKeyProperty(key)));148}149for (Object ko : on.getKeyPropertyList().keySet()) {150final String key = (String) ko;151check(msg, on1.getKeyProperty(key).152equals(on.getKeyProperty(key)));153}154case 5:155check(msg, on1.getKeyPropertyList()156.equals(on.getKeyPropertyList()));157break;158case 6:159check(msg, on1.isDomainPattern()==on.isDomainPattern());160break;161case 7:162check(msg, on1.isPattern() == on.isPattern());163break;164case 8:165check(msg,166on1.isPropertyPattern()==on.isPropertyPattern());167break;168case 9:169check(msg, on1.equals(on));170break;171case 10:172check(msg, on.equals(on1));173break;174case 11:175if (!on.isPattern()) {176check(msg, on1.apply(on));177}178break;179default:180throw new Exception("Test incorrect: case: " + i);181}182} catch (Exception e) {183System.out.println("Test (" + i + ") failed with exception:");184e.printStackTrace(System.out);185failed = true;186}187}188189}190private static String[] names6616825 = {191"a:b=c", "a:b=c,*", "*:*", ":*", ":b=c", ":b=c,*",192"a:*,b=c", ":*", ":*,b=c", "*x?:k=\"x\\*z\"", "*x?:k=\"x\\*z\",*",193"*x?:*,k=\"x\\*z\"", "*x?:k=\"x\\*z\",*,b=c"194};195196static void check6616825() throws Exception {197System.out.println("Testing 616825");198for (String n : names6616825) {199final ObjectName on;200try {201on = new ObjectName(n);202} catch (Exception x) {203failed = true;204System.out.println("Unexpected failure for 6616825 [" + n +205"]: " + x);206x.printStackTrace(System.out);207continue;208}209try {210checkName("616825 " + n, on);211} catch (Exception x) {212failed = true;213System.out.println("6616825 failed for [" + n + "]: " + x);214x.printStackTrace(System.out);215}216}217218if (failed) {219throw new Exception("Some tests for 6616825 failed");220} else {221System.out.println("All tests for 6616825 passed");222}223}224225public static void main(String[] args) throws Exception {226/* Check that we really are in jmx.serial.form=1.0 mode.227The property is frozen the first time the ObjectName class228is referenced so checking that it is set to the correct229value now is not enough. */230ObjectStreamClass osc = ObjectStreamClass.lookup(ObjectName.class);231if (osc.getFields().length != 6) {232throw new Exception("Not using old serial form: fields: " +233Arrays.asList(osc.getFields()));234// new serial form has no fields, uses writeObject235}236237try {238check6211220();239} catch (Exception x) {240System.err.println(x.getMessage());241}242try {243check6616825();244} catch (Exception x) {245System.err.println(x.getMessage());246}247248if (failed) {249throw new Exception("Some tests failed");250} else {251System.out.println("All tests passed");252}253}254255private static void check(String msg, boolean condition) {256if (!condition) {257new Throwable("Test failed " + msg).printStackTrace(System.out);258failed = true;259}260}261private static boolean failed;262}263264265