1 /*
   2  * Copyright (c) 2004, 2015, 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  *
  30  * @run clean ObjectInstanceNullTest
  31  * @run build ObjectInstanceNullTest
  32  * @run main ObjectInstanceNullTest
  33  */
  34 
  35 import javax.management.*;
  36 
  37 public class ObjectInstanceNullTest {
  38 
  39     public static void testEquals(ObjectInstance n1, ObjectInstance n2) {
  40         try {
  41             if (!n1.equals(n2) || !n2.equals(n1)) {
  42                 System.err.println("Equals yields false for: "+
  43                                    "["+n1.getObjectName()+" , "+
  44                                    n1.getClassName()+"] ["+
  45                                    n2.getObjectName()+" , "+
  46                                    n2.getClassName()+"]");
  47                 System.exit(1);
  48             }
  49         } catch (Exception x) {
  50             System.err.println("Equals failed for: "+
  51                                "["+n1.getObjectName()+" , "+
  52                                n1.getClassName()+"] ["+
  53                                n2.getObjectName()+" , "+
  54                                n2.getClassName()+"]: " + x);
  55             x.printStackTrace();
  56             System.exit(2);
  57         }
  58         try {
  59             if (n1.hashCode() != n2.hashCode()) {
  60                 System.err.println("Different hashCode() for: "+
  61                                    "["+n1.getObjectName()+" , "+
  62                                    n1.getClassName()+"] ["+
  63                                    n2.getObjectName()+" , "+
  64                                    n2.getClassName()+"]");
  65                 System.exit(3);
  66             }
  67         } catch (Exception x) {
  68             System.err.println("Hashcode failed for: "+
  69                                "["+n1.getObjectName()+" , "+
  70                                n1.getClassName()+"] ["+
  71                                n2.getObjectName()+" , "+
  72                                n2.getClassName()+"]: " + x);
  73             x.printStackTrace();
  74             System.exit(4);
  75         }
  76     }
  77 
  78     public static void testNotEquals(ObjectInstance n1, ObjectInstance n2) {
  79         try {
  80             if (n1.equals(n2) || n2.equals(n1)) {
  81                 System.err.println("Equals yields true for: "+
  82                                    "["+n1.getObjectName()+" , "+
  83                                    n1.getClassName()+"] ["+
  84                                    n2.getObjectName()+" , "+
  85                                    n2.getClassName()+"]");
  86                 System.exit(5);
  87             }
  88         } catch (Exception x) {
  89             System.err.println("!Equals failed for: "+
  90                                "["+n1.getObjectName()+" , "+
  91                                n1.getClassName()+"] ["+
  92                                n2.getObjectName()+" , "+
  93                                n2.getClassName()+"]: " + x);
  94             x.printStackTrace();
  95             System.exit(6);
  96         }
  97         try {
  98             if (n1.hashCode() == n2.hashCode()) {
  99                 System.out.println("Warning: Same hashCode() for: "+
 100                                    "["+n1.getObjectName()+" , "+
 101                                    n1.getClassName()+"] ["+
 102                                    n2.getObjectName()+" , "+
 103                                    n2.getClassName()+"]");
 104             }
 105         } catch (Exception x) {
 106             System.err.println("Hashcode failed for: "+
 107                                "["+n1.getObjectName()+" , "+
 108                                n1.getClassName()+"] ["+
 109                                n2.getObjectName()+" , "+
 110                                n2.getClassName()+"]: " + x);
 111             x.printStackTrace();
 112             System.exit(7);
 113         }
 114     }
 115 
 116     public static void main(String[] args) throws Exception {
 117         System.out.println("Test ObjectInstance(name,null).equals() and " +
 118                            "ObjectInstance(name,null).hashCode()");
 119         try {
 120             ObjectName toto1  = new ObjectName("Toto:foo=bar");
 121             ObjectName toto2  = new ObjectName("Toto:bar=foo");
 122             ObjectName clone1 = new ObjectName("Toto:bar=foo,cloned=yes");
 123             ObjectName clone2 = new ObjectName("Toto:cloned=yes,bar=foo");
 124             ObjectInstance n1 = new ObjectInstance(toto1,null);
 125             ObjectInstance n2 = new ObjectInstance(toto1,null);
 126             testEquals(n1,n1);
 127             testEquals(n1,n2);
 128             ObjectInstance n3 = new ObjectInstance(toto1,"Object");
 129             ObjectInstance n4 = new ObjectInstance(toto1,"Object");
 130             testEquals(n3,n3);
 131             testEquals(n3,n4);
 132             testNotEquals(n1,n3);
 133             ObjectInstance n5 = new ObjectInstance(toto2,null);
 134             ObjectInstance n6 = new ObjectInstance(toto2,"Object");
 135             testEquals(n5,n5);
 136             testEquals(n6,n6);
 137             testNotEquals(n5,n1);
 138             testNotEquals(n5,n3);
 139             testNotEquals(n6,n1);
 140             testNotEquals(n6,n3);
 141             testNotEquals(n5,n6);
 142             ObjectInstance n7 = new ObjectInstance(clone1,null);
 143             ObjectInstance n8 = new ObjectInstance(clone2,null);
 144             testEquals(n7,n8);
 145             testNotEquals(n7,n1);
 146             testNotEquals(n7,n5);
 147             ObjectInstance n9  = new ObjectInstance(clone1,"Object");
 148             ObjectInstance n10 = new ObjectInstance(clone2,"Object");
 149             testEquals(n9,n10);
 150             testNotEquals(n9,n1);
 151             testNotEquals(n9,n5);
 152             testNotEquals(n9,n7);
 153             testNotEquals(n9,n8);
 154             testNotEquals(n10,n1);
 155             testNotEquals(n10,n5);
 156             testNotEquals(n10,n7);
 157             testNotEquals(n10,n8);
 158         } catch( Exception x) {
 159             System.err.println("Unexpected exception: " + x);
 160             x.printStackTrace();
 161             System.exit(8);
 162         }
 163     }
 164 
 165 }