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