J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 6255956 |
| 27 | * @summary Test equals and hashCode for descriptors |
| 28 | * @author Eamonn McManus |
| 29 | * @run clean EqualsHashCodeTest |
| 30 | * @run build EqualsHashCodeTest |
| 31 | * @run main EqualsHashCodeTest |
| 32 | */ |
| 33 | |
| 34 | import java.util.Arrays; |
| 35 | import javax.management.*; |
| 36 | import javax.management.modelmbean.DescriptorSupport; |
| 37 | |
| 38 | public class EqualsHashCodeTest { |
| 39 | public static void main(String[] args) throws Exception { |
| 40 | int[] squares = {1, 4, 9, 16}; |
| 41 | int[] serauqs = {16, 9, 4, 1}; |
| 42 | int[][] numbers = {squares, serauqs}; |
| 43 | |
| 44 | Descriptor sq1 = |
| 45 | new ImmutableDescriptor(new String[] {"name", "rank", "squares", |
| 46 | "null", "numbers"}, |
| 47 | new Object[] {"Foo McBar", "lowly", |
| 48 | squares.clone(), null, |
| 49 | numbers}); |
| 50 | Descriptor sq2 = |
| 51 | new DescriptorSupport(new String[] {"Name", "Rank", "SquareS", |
| 52 | "NULL", "NuMbErS"}, |
| 53 | new Object[] {"Foo McBar", "lowly", |
| 54 | squares.clone(), null, |
| 55 | numbers}); |
| 56 | Descriptor sq3 = (Descriptor) sq2.clone(); |
| 57 | Descriptor sq4 = ImmutableDescriptor.union(sq1, sq2); |
| 58 | |
| 59 | String[] names = sq1.getFieldNames(); |
| 60 | Object[] values = sq1.getFieldValues((String[]) null); |
| 61 | Object[] values2 = sq1.getFieldValues(names); |
| 62 | if (!Arrays.deepEquals(values, values2)) { |
| 63 | throw new Exception("Arrays not equal: " + |
| 64 | Arrays.deepToString(values) + Arrays.deepToString(values2)); |
| 65 | } |
| 66 | |
| 67 | int expectedHashCode = 0; |
| 68 | for (int i = 0; i < names.length; i++) { |
| 69 | Object value = values[i]; |
| 70 | int h; |
| 71 | if (value == null) |
| 72 | h = 0; |
| 73 | else if (value instanceof int[]) |
| 74 | h = Arrays.hashCode((int[]) value); |
| 75 | else if (value instanceof Object[]) |
| 76 | h = Arrays.deepHashCode((Object[]) value); |
| 77 | else |
| 78 | h = value.hashCode(); |
| 79 | expectedHashCode += names[i].toLowerCase().hashCode() ^ h; |
| 80 | } |
| 81 | for (Descriptor d : new Descriptor[] {sq1, sq2, sq3, sq4}) { |
| 82 | System.out.println("Testing hashCode for " + |
| 83 | d.getClass().getName() + ": " + d); |
| 84 | if (d.hashCode() != expectedHashCode) { |
| 85 | throw new Exception("Bad hashCode: expected " + |
| 86 | expectedHashCode + ", got " + d.hashCode() + |
| 87 | ", for " + d); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | int i; |
| 92 | for (i = 0; i < names.length; i++) { |
| 93 | if (names[i].equals("squares")) { |
| 94 | values[i] = serauqs.clone(); |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | if (i >= names.length) |
| 99 | throw new Exception("Internal error: no squares name"); |
| 100 | Descriptor qs1 = new ImmutableDescriptor(names, values); |
| 101 | values[i] = serauqs.clone(); |
| 102 | Descriptor qs2 = new DescriptorSupport(names, values); |
| 103 | |
| 104 | System.out.println("Testing equality..."); |
| 105 | |
| 106 | Object[][] equivalenceClasses = { |
| 107 | {sq1, sq2, sq3, sq4}, |
| 108 | {qs1, qs2}, |
| 109 | }; |
| 110 | for (Object[] equivClass : equivalenceClasses) { |
| 111 | for (Object a : equivClass) { |
| 112 | for (Object b : equivClass) { |
| 113 | if (!a.equals(b)) { |
| 114 | throw new Exception("Should be equal but not: " + |
| 115 | a + " :: " + b); |
| 116 | } |
| 117 | } |
| 118 | for (Object[] equivClass2 : equivalenceClasses) { |
| 119 | if (equivClass2 == equivClass) |
| 120 | continue; |
| 121 | for (Object b : equivClass2) { |
| 122 | if (a.equals(b)) { |
| 123 | throw new Exception("Should not be equal: " + |
| 124 | a + " :: " + b); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | System.out.println("TEST PASSED"); |
| 132 | } |
| 133 | } |