0N/A/*
2362N/A * Copyright (c) 2004, 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 5015663
0N/A * @summary Test ObjectInstance(name,null).hashCode() and .equals()
0N/A * @author Daniel Fuchs
0N/A * @run clean ObjectInstanceNullTest
0N/A * @run build ObjectInstanceNullTest
0N/A * @run main ObjectInstanceNullTest
0N/A */
0N/A
0N/Aimport javax.management.*;
0N/A
0N/Apublic class ObjectInstanceNullTest {
0N/A
0N/A public static void testEquals(ObjectInstance n1, ObjectInstance n2) {
0N/A try {
0N/A if (!n1.equals(n2) || !n2.equals(n1)) {
0N/A System.err.println("Equals yields false for: "+
0N/A "["+n1.getObjectName()+" , "+
0N/A n1.getClassName()+"] ["+
0N/A n2.getObjectName()+" , "+
0N/A n2.getClassName()+"]");
0N/A System.exit(1);
0N/A }
0N/A } catch (Exception x) {
0N/A System.err.println("Equals failed for: "+
0N/A "["+n1.getObjectName()+" , "+
0N/A n1.getClassName()+"] ["+
0N/A n2.getObjectName()+" , "+
0N/A n2.getClassName()+"]: " + x);
0N/A x.printStackTrace();
0N/A System.exit(2);
0N/A }
0N/A try {
0N/A if (n1.hashCode() != n2.hashCode()) {
0N/A System.err.println("Different hashCode() for: "+
0N/A "["+n1.getObjectName()+" , "+
0N/A n1.getClassName()+"] ["+
0N/A n2.getObjectName()+" , "+
0N/A n2.getClassName()+"]");
0N/A System.exit(3);
0N/A }
0N/A } catch (Exception x) {
0N/A System.err.println("Hashcode failed for: "+
0N/A "["+n1.getObjectName()+" , "+
0N/A n1.getClassName()+"] ["+
0N/A n2.getObjectName()+" , "+
0N/A n2.getClassName()+"]: " + x);
0N/A x.printStackTrace();
0N/A System.exit(4);
0N/A }
0N/A }
0N/A
0N/A public static void testNotEquals(ObjectInstance n1, ObjectInstance n2) {
0N/A try {
0N/A if (n1.equals(n2) || n2.equals(n1)) {
0N/A System.err.println("Equals yields true for: "+
0N/A "["+n1.getObjectName()+" , "+
0N/A n1.getClassName()+"] ["+
0N/A n2.getObjectName()+" , "+
0N/A n2.getClassName()+"]");
0N/A System.exit(5);
0N/A }
0N/A } catch (Exception x) {
0N/A System.err.println("!Equals failed for: "+
0N/A "["+n1.getObjectName()+" , "+
0N/A n1.getClassName()+"] ["+
0N/A n2.getObjectName()+" , "+
0N/A n2.getClassName()+"]: " + x);
0N/A x.printStackTrace();
0N/A System.exit(6);
0N/A }
0N/A try {
0N/A if (n1.hashCode() == n2.hashCode()) {
0N/A System.out.println("Warning: Same hashCode() for: "+
0N/A "["+n1.getObjectName()+" , "+
0N/A n1.getClassName()+"] ["+
0N/A n2.getObjectName()+" , "+
0N/A n2.getClassName()+"]");
0N/A }
0N/A } catch (Exception x) {
0N/A System.err.println("Hashcode failed for: "+
0N/A "["+n1.getObjectName()+" , "+
0N/A n1.getClassName()+"] ["+
0N/A n2.getObjectName()+" , "+
0N/A n2.getClassName()+"]: " + x);
0N/A x.printStackTrace();
0N/A System.exit(7);
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A System.out.println("Test ObjectInstance(name,null).equals() and " +
0N/A "ObjectInstance(name,null).hashCode()");
0N/A try {
0N/A ObjectName toto1 = new ObjectName("Toto:foo=bar");
0N/A ObjectName toto2 = new ObjectName("Toto:bar=foo");
0N/A ObjectName clone1 = new ObjectName("Toto:bar=foo,cloned=yes");
0N/A ObjectName clone2 = new ObjectName("Toto:cloned=yes,bar=foo");
0N/A ObjectInstance n1 = new ObjectInstance(toto1,null);
0N/A ObjectInstance n2 = new ObjectInstance(toto1,null);
0N/A testEquals(n1,n1);
0N/A testEquals(n1,n2);
0N/A ObjectInstance n3 = new ObjectInstance(toto1,"Object");
0N/A ObjectInstance n4 = new ObjectInstance(toto1,"Object");
0N/A testEquals(n3,n3);
0N/A testEquals(n3,n4);
0N/A testNotEquals(n1,n3);
0N/A ObjectInstance n5 = new ObjectInstance(toto2,null);
0N/A ObjectInstance n6 = new ObjectInstance(toto2,"Object");
0N/A testEquals(n5,n5);
0N/A testEquals(n6,n6);
0N/A testNotEquals(n5,n1);
0N/A testNotEquals(n5,n3);
0N/A testNotEquals(n6,n1);
0N/A testNotEquals(n6,n3);
0N/A testNotEquals(n5,n6);
0N/A ObjectInstance n7 = new ObjectInstance(clone1,null);
0N/A ObjectInstance n8 = new ObjectInstance(clone2,null);
0N/A testEquals(n7,n8);
0N/A testNotEquals(n7,n1);
0N/A testNotEquals(n7,n5);
0N/A ObjectInstance n9 = new ObjectInstance(clone1,"Object");
0N/A ObjectInstance n10 = new ObjectInstance(clone2,"Object");
0N/A testEquals(n9,n10);
0N/A testNotEquals(n9,n1);
0N/A testNotEquals(n9,n5);
0N/A testNotEquals(n9,n7);
0N/A testNotEquals(n9,n8);
0N/A testNotEquals(n10,n1);
0N/A testNotEquals(n10,n5);
0N/A testNotEquals(n10,n7);
0N/A testNotEquals(n10,n8);
0N/A } catch( Exception x) {
0N/A System.err.println("Unexpected exception: " + x);
0N/A x.printStackTrace();
0N/A System.exit(8);
0N/A }
0N/A }
0N/A
0N/A}