Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/descriptor/EqualsHashCodeTest.java
38839 views
/*1* Copyright (c) 2005, 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* @run clean EqualsHashCodeTest29* @run build EqualsHashCodeTest30* @run main EqualsHashCodeTest31*/3233import java.util.Arrays;34import javax.management.*;35import javax.management.modelmbean.DescriptorSupport;3637public class EqualsHashCodeTest {38public static void main(String[] args) throws Exception {39int[] squares = {1, 4, 9, 16};40int[] serauqs = {16, 9, 4, 1};41int[][] numbers = {squares, serauqs};4243Descriptor sq1 =44new ImmutableDescriptor(new String[] {"name", "rank", "squares",45"null", "numbers"},46new Object[] {"Foo McBar", "lowly",47squares.clone(), null,48numbers});49Descriptor sq2 =50new DescriptorSupport(new String[] {"Name", "Rank", "SquareS",51"NULL", "NuMbErS"},52new Object[] {"Foo McBar", "lowly",53squares.clone(), null,54numbers});55Descriptor sq3 = (Descriptor) sq2.clone();56Descriptor sq4 = ImmutableDescriptor.union(sq1, sq2);5758String[] names = sq1.getFieldNames();59Object[] values = sq1.getFieldValues((String[]) null);60Object[] values2 = sq1.getFieldValues(names);61if (!Arrays.deepEquals(values, values2)) {62throw new Exception("Arrays not equal: " +63Arrays.deepToString(values) + Arrays.deepToString(values2));64}6566int expectedHashCode = 0;67for (int i = 0; i < names.length; i++) {68Object value = values[i];69int h;70if (value == null)71h = 0;72else if (value instanceof int[])73h = Arrays.hashCode((int[]) value);74else if (value instanceof Object[])75h = Arrays.deepHashCode((Object[]) value);76else77h = value.hashCode();78expectedHashCode += names[i].toLowerCase().hashCode() ^ h;79}80for (Descriptor d : new Descriptor[] {sq1, sq2, sq3, sq4}) {81System.out.println("Testing hashCode for " +82d.getClass().getName() + ": " + d);83if (d.hashCode() != expectedHashCode) {84throw new Exception("Bad hashCode: expected " +85expectedHashCode + ", got " + d.hashCode() +86", for " + d);87}88}8990int i;91for (i = 0; i < names.length; i++) {92if (names[i].equals("squares")) {93values[i] = serauqs.clone();94break;95}96}97if (i >= names.length)98throw new Exception("Internal error: no squares name");99Descriptor qs1 = new ImmutableDescriptor(names, values);100values[i] = serauqs.clone();101Descriptor qs2 = new DescriptorSupport(names, values);102103System.out.println("Testing equality...");104105Object[][] equivalenceClasses = {106{sq1, sq2, sq3, sq4},107{qs1, qs2},108};109for (Object[] equivClass : equivalenceClasses) {110for (Object a : equivClass) {111for (Object b : equivClass) {112if (!a.equals(b)) {113throw new Exception("Should be equal but not: " +114a + " :: " + b);115}116}117for (Object[] equivClass2 : equivalenceClasses) {118if (equivClass2 == equivClass)119continue;120for (Object b : equivClass2) {121if (a.equals(b)) {122throw new Exception("Should not be equal: " +123a + " :: " + b);124}125}126}127}128}129130System.out.println("TEST PASSED");131}132}133134135