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