0N/A/*
2362N/A * Copyright (c) 2003, 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
0N/A * @bug 4719923
0N/A * @summary Test that MBeanInfo.equals works even for mutable subclasses
0N/A * @author Eamonn McManus
0N/A * @run clean MBeanInfoEqualsTest
0N/A * @run build MBeanInfoEqualsTest
0N/A * @run main MBeanInfoEqualsTest
0N/A */
0N/A
0N/A/* Test that MBeanInfo and its referenced classes implement the equals
0N/A and hashCode methods correctly. These classes include some magic
0N/A to improve performance based on the classes' immutability. The
0N/A logic checks that any subclasses encountered remain immutable, and
0N/A falls back on less performant code if not. */
0N/A
0N/Aimport javax.management.*;
0N/Aimport java.lang.reflect.*;
0N/A
0N/Apublic class MBeanInfoEqualsTest {
0N/A // Class just used for reflection
0N/A private static class Toy {
0N/A public Toy() {}
0N/A public Toy(int a, String b) {}
0N/A public int getA() {return 0;}
0N/A public void setA(int a) {}
0N/A public boolean isB() {return false;}
0N/A public void setB(boolean b) {}
0N/A public void run() {}
0N/A public void blah(int a, String b) {}
0N/A }
0N/A
0N/A static final Class toy = Toy.class;
0N/A static final Constructor con1, con2;
0N/A static final Method getA, setA, isB, setB, run, blah;
0N/A static {
0N/A try {
0N/A con1 = toy.getConstructor(new Class[] {});
0N/A con2 = toy.getConstructor(new Class[] {Integer.TYPE,
0N/A String.class});
0N/A getA = toy.getMethod("getA", new Class[] {});
0N/A setA = toy.getMethod("setA", new Class[] {Integer.TYPE});
0N/A isB = toy.getMethod("isB", new Class[] {});
0N/A setB = toy.getMethod("setB", new Class[] {Boolean.TYPE});
0N/A run = toy.getMethod("run", new Class[] {});
0N/A blah = toy.getMethod("blah", new Class[] {Integer.TYPE,
0N/A String.class});
0N/A } catch (Exception e) {
0N/A throw new Error(e.getMessage());
0N/A }
0N/A }
0N/A
0N/A private static final MBeanAttributeInfo
0N/A newMBeanAttributeInfo(String name, String description,
0N/A Method getter, Method setter) {
0N/A try {
0N/A return new MBeanAttributeInfo(name, description, getter, setter);
0N/A } catch (IntrospectionException e) {
0N/A throw new Error(e.getMessage());
0N/A }
0N/A }
0N/A
0N/A static final MBeanAttributeInfo
0N/A a1a = new MBeanAttributeInfo("thing", "java.foo.bar", "an attribute",
0N/A true, true, false),
0N/A a1b = new MBeanAttributeInfo("thing", "java.foo.bar", "an attribute",
0N/A true, true, false),
0N/A a2a = new MBeanAttributeInfo("splob", "java.foo.bar", "an attribute",
0N/A true, true, false),
0N/A a2b = new MBeanAttributeInfo(a2a.getName(), a2a.getType(),
0N/A a2a.getDescription(), a2a.isReadable(),
0N/A a2a.isWritable(), a2a.isIs()),
0N/A a3 = new MBeanAttributeInfo("splob", "java.foo.bar", "a whatsit",
0N/A true, true, false),
0N/A a4 = new MBeanAttributeInfo("splob", "java.foo.bar", "a whatsit",
0N/A false, true, false),
0N/A a5a = newMBeanAttributeInfo("a", "an attribute", getA, setA),
0N/A a5b = new MBeanAttributeInfo("a", "int", "an attribute",
0N/A true, true, false),
0N/A a6a = newMBeanAttributeInfo("a", "an attribute", getA, null),
0N/A a6b = new MBeanAttributeInfo("a", "int", "an attribute",
0N/A true, false, false),
0N/A a7a = newMBeanAttributeInfo("a", "an attribute", null, setA),
0N/A a7b = new MBeanAttributeInfo("a", "int", "an attribute",
0N/A false, true, false),
0N/A a8a = newMBeanAttributeInfo("b", "an attribute", isB, setB),
0N/A a8b = new MBeanAttributeInfo("b", "boolean", "an attribute",
0N/A true, true, true),
0N/A a9a = newMBeanAttributeInfo("b", "an attribute", isB, null),
0N/A a9b = new MBeanAttributeInfo("b", "boolean", "an attribute",
0N/A true, false, true);
0N/A
0N/A static final MBeanParameterInfo
0N/A p1a = new MBeanParameterInfo("thing", "java.foo.bar", "a parameter"),
0N/A p1b = new MBeanParameterInfo("thing", "java.foo.bar", "a parameter"),
0N/A p2 = new MBeanParameterInfo("splob", "java.foo.bar", "a parameter"),
0N/A p3 = new MBeanParameterInfo("thing", "java.foo.bax", "a parameter"),
0N/A p4 = new MBeanParameterInfo("thing", "java.foo.bar", "a whatsit");
0N/A
0N/A static final MBeanConstructorInfo
0N/A c1a = new MBeanConstructorInfo("a constructor", con1),
0N/A c1b = new MBeanConstructorInfo(c1a.getName(), "a constructor",
0N/A new MBeanParameterInfo[0]),
0N/A c1c = new MBeanConstructorInfo(c1a.getName(), c1a.getDescription(),
0N/A c1a.getSignature()),
0N/A c1d = new MBeanConstructorInfo(c1a.getName(), c1a.getDescription(),
0N/A null),
0N/A c2a = new MBeanConstructorInfo("another constructor", con2),
0N/A c2b = new MBeanConstructorInfo(c2a.getName(),
0N/A c2a.getDescription(),
0N/A c2a.getSignature()),
0N/A c3a = new MBeanConstructorInfo("conName", "a constructor",
0N/A new MBeanParameterInfo[] {p2, p3}),
0N/A c3b = new MBeanConstructorInfo("conName", "a constructor",
0N/A new MBeanParameterInfo[] {p2, p3}),
0N/A c4 = new MBeanConstructorInfo("conName", "a constructor",
0N/A new MBeanParameterInfo[] {p3, p2}),
0N/A c5 = new MBeanConstructorInfo("otherName", "a constructor",
0N/A new MBeanParameterInfo[] {p3, p2}),
0N/A c6 = new MBeanConstructorInfo("otherName", "another constructor",
0N/A new MBeanParameterInfo[] {p3, p2});
0N/A
0N/A static final MBeanOperationInfo
0N/A o1a = new MBeanOperationInfo("an operation", run),
0N/A o1b = new MBeanOperationInfo("an operation", run),
0N/A o1c = new MBeanOperationInfo("run", "an operation",
0N/A o1a.getSignature(), "void",
0N/A o1a.getImpact()),
0N/A o1d = new MBeanOperationInfo("run", "an operation",
0N/A new MBeanParameterInfo[0], "void",
0N/A o1a.getImpact()),
0N/A o1e = new MBeanOperationInfo("run", "an operation",
0N/A null, "void",
0N/A o1a.getImpact()),
0N/A o2a = new MBeanOperationInfo("another operation", blah),
0N/A o2b = new MBeanOperationInfo(o2a.getName(), o2a.getDescription(),
0N/A o2a.getSignature(), o2a.getReturnType(),
0N/A o2a.getImpact());
0N/A
0N/A static final MBeanNotificationInfo
0N/A n1a = new MBeanNotificationInfo(new String[] {"a.b", "c.d"},
0N/A "x.y.z",
0N/A "a notification info"),
0N/A n1b = new MBeanNotificationInfo(new String[] {"a.b", "c.d"},
0N/A "x.y.z",
0N/A "a notification info"),
0N/A n2a = new MBeanNotificationInfo(new String[] {"a.b", "c.d"},
0N/A "x.y.z",
0N/A "another notification info"),
0N/A n2b = new MBeanNotificationInfo(n2a.getNotifTypes(),
0N/A n2a.getName(),
0N/A n2a.getDescription()),
0N/A n3 = new MBeanNotificationInfo(new String[] {"a.b", "c.d"},
0N/A "x.y.zz",
0N/A "a notification info"),
0N/A n4 = new MBeanNotificationInfo(new String[] {"c.d", "a.b"},
0N/A "x.y.z",
0N/A "a notification info");
0N/A
0N/A static final MBeanAttributeInfo[]
0N/A xa1a = {a1a, a2a},
0N/A xa1b = {a1b, a2b},
0N/A xa2a = {a2a, a1a};
0N/A
0N/A static final MBeanConstructorInfo[]
0N/A xc1a = {c1a, c2a},
0N/A xc1b = {c1b, c2b},
0N/A xc2a = {c2a, c1a};
0N/A
0N/A static final MBeanOperationInfo[]
0N/A xo1a = {o1a, o2a},
0N/A xo1b = {o1b, o2b},
0N/A xo2a = {o2a, o1a};
0N/A
0N/A static final MBeanNotificationInfo[]
0N/A xn1a = {n1a, n2a},
0N/A xn1b = {n1b, n2b},
0N/A xn2a = {n2a, n1a};
0N/A
0N/A static final MBeanInfo
0N/A i1a = new MBeanInfo("a.b.c", "an MBean info", xa1a, xc1a, xo1a, xn1a),
0N/A i1b = new MBeanInfo("a.b.c", "an MBean info", xa1a, xc1a, xo1a, xn1a),
0N/A i1c = new MBeanInfo("a.b.c", "an MBean info", xa1b, xc1b, xo1b, xn1b),
0N/A i1d = new MutableMBeanInfo("a.b.c", "an MBean info", xa1b, xc1b, xo1b,
0N/A xn1b),
0N/A i1e = new ImmutableMBeanInfo("a.b.c", "an MBean info", xa1b, xc1b,
0N/A xo1b, xn1b),
0N/A i1f = new ImmutableMBeanInfo("a.b.c", "an MBean info", xa1b, xc1b,
0N/A xo1b, xn1b),
0N/A i2a = new MBeanInfo("a.b.cc", "an MBean info", xa1a, xc1a, xo1a, xn1a),
0N/A i2b = new MBeanInfo(i2a.getClassName(), i2a.getDescription(),
0N/A i2a.getAttributes(), i2a.getConstructors(),
0N/A i2a.getOperations(), i2a.getNotifications()),
0N/A i3 = new MBeanInfo("a.b.c", "another MBean info", xa1a, xc1a, xo1a,
0N/A xn1a),
0N/A i4 = new MBeanInfo("a.b.c", "an MBean info", xa2a, xc1a, xo1a, xn1a),
0N/A i5 = new MBeanInfo("a.b.c", "an MBean info", xa1a, xc2a, xo1a, xn1a),
0N/A i6 = new MBeanInfo("a.b.c", "an MBean info", xa1a, xc1a, xo2a, xn1a),
0N/A i7 = new MBeanInfo("a.b.c", "an MBean info", xa1a, xc1a, xo1a, xn2a);
0N/A
0N/A static final Object[][] equivalenceClasses = {
0N/A {a1a, a1b}, {a2a, a2b}, {a3}, {a4}, {a5a, a5b}, {a6a, a6b}, {a7a, a7b},
0N/A {a8a, a8b}, {a9a, a9b},
0N/A {c1a, c1b, c1c, c1d}, {c2a, c2b}, {c3a, c3b}, {c4}, {c5}, {c6},
0N/A {o1a, o1b, o1c, o1d, o1e}, {o2a, o2b},
0N/A {p1a, p1b}, {p2}, {p3}, {p4},
0N/A {n1a, n1b}, {n2a, n2b}, {n3}, {n4},
0N/A {i1a, i1b, i1c, i1d, i1e, i1f}, {i2a, i2b}, {i3}, {i4}, {i5}, {i6},
0N/A {i7},
0N/A };
0N/A
0N/A private static class ImmutableMBeanInfo extends MBeanInfo {
0N/A ImmutableMBeanInfo(String className,
0N/A String description,
0N/A MBeanAttributeInfo[] attributes,
0N/A MBeanConstructorInfo[] constructors,
0N/A MBeanOperationInfo[] operations,
0N/A MBeanNotificationInfo[] notifications) {
0N/A super(className, description, attributes, constructors, operations,
0N/A notifications);
0N/A }
0N/A }
0N/A
0N/A /* This class checks that the MBeanInfo.equals() method really
0N/A does call getClassName() etc rather than referring to its
0N/A private fields. */
0N/A private static class MutableMBeanInfo extends MBeanInfo {
0N/A private final String className;
0N/A private final String description;
0N/A private final MBeanAttributeInfo[] attributes;
0N/A private final MBeanOperationInfo[] operations;
0N/A private final MBeanConstructorInfo[] constructors;
0N/A private final MBeanNotificationInfo[] notifications;
0N/A
0N/A MutableMBeanInfo(String className,
0N/A String description,
0N/A MBeanAttributeInfo[] attributes,
0N/A MBeanConstructorInfo[] constructors,
0N/A MBeanOperationInfo[] operations,
0N/A MBeanNotificationInfo[] notifications) {
0N/A super("bogus", null, null, null, null, null);
0N/A this.className = className;
0N/A this.description = description;
0N/A this.attributes = attributes;
0N/A this.constructors = constructors;
0N/A this.operations = operations;
0N/A this.notifications = notifications;
0N/A }
0N/A
0N/A public String getClassName() {
0N/A return className;
0N/A }
0N/A
0N/A public String getDescription() {
0N/A return description;
0N/A }
0N/A
0N/A public MBeanAttributeInfo[] getAttributes() {
0N/A return attributes;
0N/A }
0N/A
0N/A public MBeanOperationInfo[] getOperations() {
0N/A return operations;
0N/A }
0N/A
0N/A public MBeanConstructorInfo[] getConstructors() {
0N/A return constructors;
0N/A }
0N/A
0N/A public MBeanNotificationInfo[] getNotifications() {
0N/A return notifications;
0N/A }
0N/A }
0N/A
0N/A private static boolean checkEquals(String what, Object[][] equivs) {
0N/A boolean ok = true;
0N/A /* The equivs array is an array of equivalence classes. The members
0N/A of each equivalence class must be equal among themselves.
0N/A Each member of each equivalence class must be different from
0N/A each member of each other equivalence class. */
0N/A for (int ei = 0; ei < equivs.length; ei++) {
0N/A Object[] ec1 = equivs[ei];
0N/A ok &= checkSame(what + " equivalence class " + ei, ec1);
0N/A for (int ej = 0; ej < equivs.length; ej++) {
0N/A if (ei == ej)
0N/A continue;
0N/A Object[] ec2 = equivs[ej];
0N/A ok &= checkDifferent(what + " equivalence classes " +
0N/A ei + " and " + ej, ec1, ec2);
0N/A }
0N/A }
0N/A if (ok)
0N/A System.out.println("equals test for " + what + " passed");
0N/A return ok;
0N/A }
0N/A
0N/A /* We could simplify this test to compare every element with every
0N/A other and choose whether they are supposed to be the same based
0N/A on whether they are in the same equivalence class. A bit
0N/A simpler, but so what. */
0N/A
0N/A private static boolean checkSame(String what, Object[] equiv) {
0N/A boolean ok = true;
0N/A for (int i = 0; i < equiv.length; i++) {
0N/A final Object o1 = equiv[i];
0N/A for (int j = 0; j < equiv.length; j++) {
0N/A final Object o2 = equiv[j];
0N/A if (!o1.equals(o2)) {
0N/A System.out.println("equals test: " + what +
0N/A ": !obj[" + i +
0N/A "].equals(obj[" + j + "])");
0N/A System.out.println("..." + o1 + " " + o2);
0N/A ok = false;
0N/A }
0N/A if (o1.hashCode() != o2.hashCode()) {
0N/A System.out.println("equals test: " + what +
0N/A ": obj[" + i +
0N/A "].hashCode() != obj[" + j +
0N/A "].hashCode()");
0N/A System.out.println("..." + o1 + " " + o2);
0N/A ok = false;
0N/A }
0N/A }
0N/A }
0N/A return ok;
0N/A }
0N/A
0N/A private static boolean checkDifferent(String what, Object[] equiv1,
0N/A Object[] equiv2) {
0N/A boolean ok = true;
0N/A for (int i = 0; i < equiv1.length; i++) {
0N/A final Object o1 = equiv1[i];
0N/A for (int j = 0; j < equiv2.length; j++) {
0N/A final Object o2 = equiv2[j];
0N/A if (o1.equals(o2)) {
0N/A System.out.println("equals test " + what + ": obj[" +
0N/A i + "].equals(obj[" + j + "])");
0N/A System.out.println("..." + o1 + " " + o2);
0N/A ok = false;
0N/A }
0N/A }
0N/A }
0N/A return ok;
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A boolean ok = true;
0N/A ok &= checkEquals("equivalence", equivalenceClasses);
0N/A if (ok) {
0N/A System.out.println("all tests passed");
0N/A } else {
0N/A System.out.println("at least one test failed");
0N/A System.exit(1);
0N/A }
0N/A }
0N/A}