0N/A/*
2362N/A * Copyright (c) 2004, 2008, 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
470N/A * @bug 6211220 6616825
0N/A * @summary Test that jmx.serial.form=1.0 works for ObjectName
470N/A * @author Eamonn McManus, Daniel Fuchs
0N/A * @run clean SerialCompatTest
0N/A * @run build SerialCompatTest
0N/A * @run main/othervm SerialCompatTest
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport javax.management.ObjectName;
0N/A
0N/Apublic class SerialCompatTest {
0N/A
470N/A public static void check6211220() throws Exception {
0N/A
0N/A ObjectName on = new ObjectName("a:b=c");
0N/A ByteArrayOutputStream bos = new ByteArrayOutputStream();
0N/A ObjectOutputStream oos = new ObjectOutputStream(bos);
0N/A oos.writeObject(on);
0N/A oos.close();
0N/A byte[] bytes = bos.toByteArray();
0N/A ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
0N/A ObjectInputStream ois = new ObjectInputStream(bis);
0N/A ObjectName on1 = (ObjectName) ois.readObject();
0N/A
0N/A // if the bug is present, these will get NullPointerException
0N/A for (int i = 0; i <= 11; i++) {
470N/A String msg = "6211220 case(" + i + ")";
0N/A try {
0N/A switch (i) {
470N/A case 0:
470N/A check(msg, on1.getDomain().equals("a"));
470N/A break;
470N/A case 1:
470N/A check(msg, on1.getCanonicalName().equals("a:b=c"));
470N/A break;
470N/A case 2:
470N/A check(msg, on1.getKeyPropertyListString()
470N/A .equals("b=c"));
470N/A break;
470N/A case 3:
470N/A check(msg, on1.getCanonicalKeyPropertyListString()
470N/A .equals("b=c"));
470N/A break;
470N/A case 4:
470N/A check(msg, on1.getKeyProperty("b").equals("c"));
470N/A break;
470N/A case 5:
470N/A check(msg, on1.getKeyPropertyList()
470N/A .equals(Collections.singletonMap("b", "c")));
470N/A break;
470N/A case 6:
470N/A check(msg, !on1.isDomainPattern());
470N/A break;
470N/A case 7:
470N/A check(msg, !on1.isPattern());
470N/A break;
470N/A case 8:
470N/A check(msg, !on1.isPropertyPattern());
470N/A break;
470N/A case 9:
470N/A check(msg, on1.equals(on));
470N/A break;
470N/A case 10:
470N/A check(msg, on.equals(on1));
470N/A break;
470N/A case 11:
470N/A check(msg, on1.apply(on));
470N/A break;
470N/A default:
470N/A throw new Exception(msg + ": Test incorrect");
0N/A }
0N/A } catch (Exception e) {
470N/A System.out.println(msg + ": Test failed with exception:");
0N/A e.printStackTrace(System.out);
0N/A failed = true;
0N/A }
0N/A }
0N/A
470N/A if (failed) {
470N/A throw new Exception("Some tests for 6211220 failed");
470N/A } else {
470N/A System.out.println("All tests for 6211220 passed");
470N/A }
0N/A }
0N/A
470N/A static void checkName(String testname, ObjectName on)
470N/A throws Exception {
470N/A ByteArrayOutputStream bos = new ByteArrayOutputStream();
470N/A ObjectOutputStream oos = new ObjectOutputStream(bos);
470N/A oos.writeObject(on);
470N/A oos.close();
470N/A byte[] bytes = bos.toByteArray();
470N/A ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
470N/A ObjectInputStream ois = new ObjectInputStream(bis);
470N/A ObjectName on1 = (ObjectName) ois.readObject();
470N/A // if the bug is present, these will get NullPointerException
470N/A for (int i = 0; i <= 11; i++) {
470N/A String msg = testname + " case(" + i + ")";
470N/A try {
470N/A switch (i) {
470N/A case 0:
470N/A check(msg, on1.getDomain().equals(on.getDomain()));
470N/A break;
470N/A case 1:
470N/A check(msg, on1.getCanonicalName().
470N/A equals(on.getCanonicalName()));
470N/A break;
470N/A case 2:
470N/A check(msg, on1.getKeyPropertyListString().
470N/A equals(on.getKeyPropertyListString()));
470N/A break;
470N/A case 3:
470N/A check(msg, on1.getCanonicalKeyPropertyListString().
470N/A equals(on.getCanonicalKeyPropertyListString()));
470N/A break;
470N/A case 4:
470N/A for (Object ko : on1.getKeyPropertyList().keySet()) {
470N/A final String key = (String) ko;
470N/A check(msg, on1.getKeyProperty(key).
470N/A equals(on.getKeyProperty(key)));
470N/A }
470N/A for (Object ko : on.getKeyPropertyList().keySet()) {
470N/A final String key = (String) ko;
470N/A check(msg, on1.getKeyProperty(key).
470N/A equals(on.getKeyProperty(key)));
470N/A }
470N/A case 5:
470N/A check(msg, on1.getKeyPropertyList()
470N/A .equals(on.getKeyPropertyList()));
470N/A break;
470N/A case 6:
470N/A check(msg, on1.isDomainPattern()==on.isDomainPattern());
470N/A break;
470N/A case 7:
470N/A check(msg, on1.isPattern() == on.isPattern());
470N/A break;
470N/A case 8:
470N/A check(msg,
470N/A on1.isPropertyPattern()==on.isPropertyPattern());
470N/A break;
470N/A case 9:
470N/A check(msg, on1.equals(on));
470N/A break;
470N/A case 10:
470N/A check(msg, on.equals(on1));
470N/A break;
470N/A case 11:
470N/A if (!on.isPattern()) {
470N/A check(msg, on1.apply(on));
470N/A }
470N/A break;
470N/A default:
470N/A throw new Exception("Test incorrect: case: " + i);
470N/A }
470N/A } catch (Exception e) {
470N/A System.out.println("Test (" + i + ") failed with exception:");
470N/A e.printStackTrace(System.out);
470N/A failed = true;
470N/A }
470N/A }
470N/A
470N/A }
470N/A private static String[] names6616825 = {
470N/A "a:b=c", "a:b=c,*", "*:*", ":*", ":b=c", ":b=c,*",
470N/A "a:*,b=c", ":*", ":*,b=c", "*x?:k=\"x\\*z\"", "*x?:k=\"x\\*z\",*",
470N/A "*x?:*,k=\"x\\*z\"", "*x?:k=\"x\\*z\",*,b=c"
470N/A };
470N/A
470N/A static void check6616825() throws Exception {
470N/A System.out.println("Testing 616825");
470N/A for (String n : names6616825) {
470N/A final ObjectName on;
470N/A try {
470N/A on = new ObjectName(n);
470N/A } catch (Exception x) {
470N/A failed = true;
470N/A System.out.println("Unexpected failure for 6616825 [" + n +
470N/A "]: " + x);
470N/A x.printStackTrace(System.out);
470N/A continue;
470N/A }
470N/A try {
470N/A checkName("616825 " + n, on);
470N/A } catch (Exception x) {
470N/A failed = true;
470N/A System.out.println("6616825 failed for [" + n + "]: " + x);
470N/A x.printStackTrace(System.out);
470N/A }
470N/A }
470N/A
470N/A if (failed) {
470N/A throw new Exception("Some tests for 6616825 failed");
470N/A } else {
470N/A System.out.println("All tests for 6616825 passed");
470N/A }
470N/A }
470N/A
470N/A public static void main(String[] args) throws Exception {
470N/A System.setProperty("jmx.serial.form", "1.0");
470N/A
470N/A /* Check that we really are in jmx.serial.form=1.0 mode.
470N/A The property is frozen the first time the ObjectName class
470N/A is referenced so checking that it is set to the correct
470N/A value now is not enough. */
470N/A ObjectStreamClass osc = ObjectStreamClass.lookup(ObjectName.class);
470N/A if (osc.getFields().length != 6) {
470N/A throw new Exception("Not using old serial form: fields: " +
470N/A Arrays.asList(osc.getFields()));
470N/A // new serial form has no fields, uses writeObject
470N/A }
470N/A
470N/A try {
470N/A check6211220();
470N/A } catch (Exception x) {
470N/A System.err.println(x.getMessage());
470N/A }
470N/A try {
470N/A check6616825();
470N/A } catch (Exception x) {
470N/A System.err.println(x.getMessage());
470N/A }
470N/A
470N/A if (failed) {
470N/A throw new Exception("Some tests failed");
470N/A } else {
470N/A System.out.println("All tests passed");
470N/A }
470N/A }
470N/A
470N/A private static void check(String msg, boolean condition) {
0N/A if (!condition) {
470N/A new Throwable("Test failed " + msg).printStackTrace(System.out);
0N/A failed = true;
0N/A }
0N/A }
0N/A private static boolean failed;
0N/A}