1 /* 2 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 5015663 27 * @summary Test ObjectInstance(name,null).hashCode() and .equals() 28 * @author Daniel Fuchs 29 * @run clean ObjectInstanceNullTest 30 * @run build ObjectInstanceNullTest 31 * @run main ObjectInstanceNullTest 32 */ 33 34 import javax.management.*; 35 36 public class ObjectInstanceNullTest { 37 38 public static void testEquals(ObjectInstance n1, ObjectInstance n2) { 39 try { 40 if (!n1.equals(n2) || !n2.equals(n1)) { 41 System.err.println("Equals yields false for: "+ 42 "["+n1.getObjectName()+" , "+ 43 n1.getClassName()+"] ["+ 44 n2.getObjectName()+" , "+ 45 n2.getClassName()+"]"); 46 System.exit(1); 47 } 48 } catch (Exception x) { 49 System.err.println("Equals failed for: "+ 50 "["+n1.getObjectName()+" , "+ 51 n1.getClassName()+"] ["+ 52 n2.getObjectName()+" , "+ 53 n2.getClassName()+"]: " + x); 54 x.printStackTrace(); 55 System.exit(2); 56 } 57 try { 58 if (n1.hashCode() != n2.hashCode()) { 59 System.err.println("Different hashCode() for: "+ 60 "["+n1.getObjectName()+" , "+ 61 n1.getClassName()+"] ["+ 62 n2.getObjectName()+" , "+ 63 n2.getClassName()+"]"); 64 System.exit(3); 65 } 66 } catch (Exception x) { 67 System.err.println("Hashcode failed for: "+ 68 "["+n1.getObjectName()+" , "+ 69 n1.getClassName()+"] ["+ 70 n2.getObjectName()+" , "+ 71 n2.getClassName()+"]: " + x); 72 x.printStackTrace(); 73 System.exit(4); 74 } 75 } 76 77 public static void testNotEquals(ObjectInstance n1, ObjectInstance n2) { 78 try { 79 if (n1.equals(n2) || n2.equals(n1)) { 80 System.err.println("Equals yields true for: "+ 81 "["+n1.getObjectName()+" , "+ 82 n1.getClassName()+"] ["+ 83 n2.getObjectName()+" , "+ 84 n2.getClassName()+"]"); 85 System.exit(5); 86 } 87 } catch (Exception x) { 88 System.err.println("!Equals failed for: "+ 89 "["+n1.getObjectName()+" , "+ 90 n1.getClassName()+"] ["+ 91 n2.getObjectName()+" , "+ 92 n2.getClassName()+"]: " + x); 93 x.printStackTrace(); 94 System.exit(6); 95 } 96 try { 97 if (n1.hashCode() == n2.hashCode()) { 98 System.out.println("Warning: Same hashCode() for: "+ 99 "["+n1.getObjectName()+" , "+ 100 n1.getClassName()+"] ["+ 101 n2.getObjectName()+" , "+ 102 n2.getClassName()+"]"); 103 } 104 } catch (Exception x) { 105 System.err.println("Hashcode failed for: "+ 106 "["+n1.getObjectName()+" , "+ 107 n1.getClassName()+"] ["+ 108 n2.getObjectName()+" , "+ 109 n2.getClassName()+"]: " + x); 110 x.printStackTrace(); 111 System.exit(7); 112 } 113 } 114 115 public static void main(String[] args) throws Exception { 116 System.out.println("Test ObjectInstance(name,null).equals() and " + 117 "ObjectInstance(name,null).hashCode()"); 118 try { 119 ObjectName toto1 = new ObjectName("Toto:foo=bar"); 120 ObjectName toto2 = new ObjectName("Toto:bar=foo"); 121 ObjectName clone1 = new ObjectName("Toto:bar=foo,cloned=yes"); 122 ObjectName clone2 = new ObjectName("Toto:cloned=yes,bar=foo"); 123 ObjectInstance n1 = new ObjectInstance(toto1,null); 124 ObjectInstance n2 = new ObjectInstance(toto1,null); 125 testEquals(n1,n1); 126 testEquals(n1,n2); 127 ObjectInstance n3 = new ObjectInstance(toto1,"Object"); 128 ObjectInstance n4 = new ObjectInstance(toto1,"Object"); 129 testEquals(n3,n3); 130 testEquals(n3,n4); 131 testNotEquals(n1,n3); 132 ObjectInstance n5 = new ObjectInstance(toto2,null); 133 ObjectInstance n6 = new ObjectInstance(toto2,"Object"); 134 testEquals(n5,n5); 135 testEquals(n6,n6); 136 testNotEquals(n5,n1); 137 testNotEquals(n5,n3); 138 testNotEquals(n6,n1); 139 testNotEquals(n6,n3); 140 testNotEquals(n5,n6); 141 ObjectInstance n7 = new ObjectInstance(clone1,null); 142 ObjectInstance n8 = new ObjectInstance(clone2,null); 143 testEquals(n7,n8); 144 testNotEquals(n7,n1); 145 testNotEquals(n7,n5); 146 ObjectInstance n9 = new ObjectInstance(clone1,"Object"); 147 ObjectInstance n10 = new ObjectInstance(clone2,"Object"); 148 testEquals(n9,n10); 149 testNotEquals(n9,n1); 150 testNotEquals(n9,n5); 151 testNotEquals(n9,n7); 152 testNotEquals(n9,n8); 153 testNotEquals(n10,n1); 154 testNotEquals(n10,n5); 155 testNotEquals(n10,n7); 156 testNotEquals(n10,n8); 157 } catch( Exception x) { 158 System.err.println("Unexpected exception: " + x); 159 x.printStackTrace(); 160 System.exit(8); 161 } 162 } 163 164 }