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