EqualsHashCodeTest.java revision 2362
5630N/A/*
5630N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
5630N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5630N/A *
5630N/A * This code is free software; you can redistribute it and/or modify it
5630N/A * under the terms of the GNU General Public License version 2 only, as
5630N/A * published by the Free Software Foundation.
7034N/A *
7034N/A * This code is distributed in the hope that it will be useful, but WITHOUT
7034N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7034N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
7034N/A * version 2 for more details (a copy is included in the LICENSE file that
7034N/A * accompanied this code).
7034N/A *
5630N/A * You should have received a copy of the GNU General Public License version
5630N/A * 2 along with this work; if not, write to the Free Software Foundation,
5630N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5630N/A *
5630N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5630N/A * or visit www.oracle.com if you need additional information or have any
5630N/A * questions.
5630N/A */
5630N/A
5630N/A/*
5630N/A * @test
5630N/A * @bug 6255956
7034N/A * @summary Test equals and hashCode for descriptors
7034N/A * @author Eamonn McManus
7034N/A * @run clean EqualsHashCodeTest
7034N/A * @run build EqualsHashCodeTest
5630N/A * @run main EqualsHashCodeTest
5630N/A */
5630N/A
5630N/Aimport java.util.Arrays;
5630N/Aimport javax.management.*;
5630N/Aimport javax.management.modelmbean.DescriptorSupport;
7034N/A
7034N/Apublic class EqualsHashCodeTest {
7034N/A public static void main(String[] args) throws Exception {
7034N/A int[] squares = {1, 4, 9, 16};
7034N/A int[] serauqs = {16, 9, 4, 1};
7034N/A int[][] numbers = {squares, serauqs};
5630N/A
5630N/A 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");
}
}