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