EqualsHashCodeTest.java revision 0
0N/A/*
0N/A * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6255956
0N/A * @summary Test equals and hashCode for descriptors
0N/A * @author Eamonn McManus
0N/A * @run clean EqualsHashCodeTest
0N/A * @run build EqualsHashCodeTest
0N/A * @run main EqualsHashCodeTest
0N/A */
0N/A
0N/Aimport java.util.Arrays;
0N/Aimport javax.management.*;
0N/Aimport javax.management.modelmbean.DescriptorSupport;
0N/A
0N/Apublic class EqualsHashCodeTest {
0N/A public static void main(String[] args) throws Exception {
0N/A int[] squares = {1, 4, 9, 16};
0N/A int[] serauqs = {16, 9, 4, 1};
0N/A int[][] numbers = {squares, serauqs};
0N/A
0N/A Descriptor sq1 =
0N/A new ImmutableDescriptor(new String[] {"name", "rank", "squares",
0N/A "null", "numbers"},
0N/A new Object[] {"Foo McBar", "lowly",
0N/A squares.clone(), null,
0N/A numbers});
0N/A Descriptor sq2 =
0N/A new DescriptorSupport(new String[] {"Name", "Rank", "SquareS",
0N/A "NULL", "NuMbErS"},
0N/A new Object[] {"Foo McBar", "lowly",
0N/A squares.clone(), null,
0N/A numbers});
0N/A Descriptor sq3 = (Descriptor) sq2.clone();
0N/A Descriptor sq4 = ImmutableDescriptor.union(sq1, sq2);
0N/A
0N/A String[] names = sq1.getFieldNames();
0N/A Object[] values = sq1.getFieldValues((String[]) null);
0N/A Object[] values2 = sq1.getFieldValues(names);
0N/A if (!Arrays.deepEquals(values, values2)) {
0N/A throw new Exception("Arrays not equal: " +
0N/A Arrays.deepToString(values) + Arrays.deepToString(values2));
0N/A }
0N/A
0N/A int expectedHashCode = 0;
0N/A for (int i = 0; i < names.length; i++) {
0N/A Object value = values[i];
0N/A int h;
0N/A if (value == null)
0N/A h = 0;
0N/A else if (value instanceof int[])
0N/A h = Arrays.hashCode((int[]) value);
0N/A else if (value instanceof Object[])
0N/A h = Arrays.deepHashCode((Object[]) value);
0N/A else
0N/A h = value.hashCode();
0N/A expectedHashCode += names[i].toLowerCase().hashCode() ^ h;
0N/A }
0N/A for (Descriptor d : new Descriptor[] {sq1, sq2, sq3, sq4}) {
0N/A System.out.println("Testing hashCode for " +
0N/A d.getClass().getName() + ": " + d);
0N/A if (d.hashCode() != expectedHashCode) {
0N/A throw new Exception("Bad hashCode: expected " +
0N/A expectedHashCode + ", got " + d.hashCode() +
0N/A ", for " + d);
0N/A }
0N/A }
0N/A
0N/A int i;
0N/A for (i = 0; i < names.length; i++) {
0N/A if (names[i].equals("squares")) {
0N/A values[i] = serauqs.clone();
0N/A break;
0N/A }
0N/A }
0N/A if (i >= names.length)
0N/A throw new Exception("Internal error: no squares name");
0N/A Descriptor qs1 = new ImmutableDescriptor(names, values);
0N/A values[i] = serauqs.clone();
0N/A Descriptor qs2 = new DescriptorSupport(names, values);
0N/A
0N/A System.out.println("Testing equality...");
0N/A
0N/A Object[][] equivalenceClasses = {
0N/A {sq1, sq2, sq3, sq4},
0N/A {qs1, qs2},
0N/A };
0N/A for (Object[] equivClass : equivalenceClasses) {
0N/A for (Object a : equivClass) {
0N/A for (Object b : equivClass) {
0N/A if (!a.equals(b)) {
0N/A throw new Exception("Should be equal but not: " +
0N/A a + " :: " + b);
0N/A }
0N/A }
0N/A for (Object[] equivClass2 : equivalenceClasses) {
0N/A if (equivClass2 == equivClass)
0N/A continue;
0N/A for (Object b : equivClass2) {
0N/A if (a.equals(b)) {
0N/A throw new Exception("Should not be equal: " +
0N/A a + " :: " + b);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A System.out.println("TEST PASSED");
0N/A }
0N/A}