1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
/*
* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 6255956
* @summary Test equals and hashCode for descriptors
* @author Eamonn McManus
*
* @run clean EqualsHashCodeTest
* @run build EqualsHashCodeTest
* @run main EqualsHashCodeTest
*/
import java.util.Arrays;
import javax.management.*;
import javax.management.modelmbean.DescriptorSupport;
public class EqualsHashCodeTest {
public static void main(String[] args) throws Exception {
int[] squares = {1, 4, 9, 16};
int[] serauqs = {16, 9, 4, 1};
int[][] numbers = {squares, serauqs};
Descriptor sq1 =
new ImmutableDescriptor(new String[] {"name", "rank", "squares",
"null", "numbers"},
new Object[] {"Foo McBar", "lowly",
squares.clone(), null,
numbers});
Descriptor sq2 =
new DescriptorSupport(new String[] {"Name", "Rank", "SquareS",
"NULL", "NuMbErS"},
new Object[] {"Foo McBar", "lowly",
squares.clone(), null,
numbers});
Descriptor sq3 = (Descriptor) sq2.clone();
Descriptor sq4 = ImmutableDescriptor.union(sq1, sq2);
String[] names = sq1.getFieldNames();
Object[] values = sq1.getFieldValues((String[]) null);
Object[] values2 = sq1.getFieldValues(names);
if (!Arrays.deepEquals(values, values2)) {
throw new Exception("Arrays not equal: " +
Arrays.deepToString(values) + Arrays.deepToString(values2));
}
int expectedHashCode = 0;
for (int i = 0; i < names.length; i++) {
Object value = values[i];
int h;
if (value == null)
h = 0;
else if (value instanceof int[])
h = Arrays.hashCode((int[]) value);
else if (value instanceof Object[])
h = Arrays.deepHashCode((Object[]) value);
else
h = value.hashCode();
expectedHashCode += names[i].toLowerCase().hashCode() ^ h;
}
for (Descriptor d : new Descriptor[] {sq1, sq2, sq3, sq4}) {
System.out.println("Testing hashCode for " +
d.getClass().getName() + ": " + d);
if (d.hashCode() != expectedHashCode) {
throw new Exception("Bad hashCode: expected " +
expectedHashCode + ", got " + d.hashCode() +
", for " + d);
}
}
int i;
for (i = 0; i < names.length; i++) {
if (names[i].equals("squares")) {
values[i] = serauqs.clone();
break;
}
}
if (i >= names.length)
throw new Exception("Internal error: no squares name");
Descriptor qs1 = new ImmutableDescriptor(names, values);
values[i] = serauqs.clone();
Descriptor qs2 = new DescriptorSupport(names, values);
System.out.println("Testing equality...");
Object[][] equivalenceClasses = {
{sq1, sq2, sq3, sq4},
{qs1, qs2},
};
for (Object[] equivClass : equivalenceClasses) {
for (Object a : equivClass) {
for (Object b : equivClass) {
if (!a.equals(b)) {
throw new Exception("Should be equal but not: " +
a + " :: " + b);
}
}
for (Object[] equivClass2 : equivalenceClasses) {
if (equivClass2 == equivClass)
continue;
for (Object b : equivClass2) {
if (a.equals(b)) {
throw new Exception("Should not be equal: " +
a + " :: " + b);
}
}
}
}
}
System.out.println("TEST PASSED");
}
}
|