Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jmx/snmp/SnmpOidHashCode.java
38855 views
/*1* Copyright (c) 2003, 2008, 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* @summary Test that SnmpOid hashCode is consistent with equals.26* @bug 495510527* @build SnmpOidHashCode28* @run main SnmpOidHashCode29*/30import java.lang.reflect.Constructor;31import java.lang.reflect.InvocationTargetException;3233public class SnmpOidHashCode {34public static final String[] oids = {35"1.1.1.1.1.1.1.1.1",36"1.1.1.1.1.1.1.1",37"1.1.1.1.2.1.1.1.1",38"1.1.1.1.1.2.1.1.1",39"1.3.2",40"2.3.1",41"1.2.67."+Integer.MAX_VALUE+"."+Integer.MAX_VALUE+42"."+Integer.MAX_VALUE+"."+Integer.MAX_VALUE+"."+Integer.MAX_VALUE+43"1",44"1.2."+0xFFFFFFFFL+".3."+0xFFFFFFFFL+".4."+0xFFFFFFFFL+45".5."+0xFFFFFFFFL+".6."+0xFFFFFFFFL+".7."+0xFFFFFFFFL+46".8."+0xFFFFFFFFL+".9."+0xFFFFFFFFL+".10."+0xFFFFFFFFL+47".11."+0xFFFFFFFFL+".12."+0xFFFFFFFFL+".13."+0xFFFFFFFFL+48".14."+0xFFFFFFFFL+".15."+0xFFFFFFFFL+".16."+0xFFFFFFFFL+49".17."+0xFFFFFFFFL+".18."+0xFFFFFFFFL+".19."+0xFFFFFFFFL+50".20."+0xFFFFFFFFL+".21."+0xFFFFFFFFL+".22."+0xFFFFFFFFL+51".23."+0xFFFFFFFFL+".24."+0xFFFFFFFFL+".25."+0xFFFFFFFFL+52".26."+0xFFFFFFFFL+".27."+0xFFFFFFFFL+".28."+0xFFFFFFFFL+53".29."+0xFFFFFFFFL+54".30."+0xFFFFFFFFL+".31."+0xFFFFFFFFL+".32."+0xFFFFFFFFL+55".33."+0xFFFFFFFFL+".34."+0xFFFFFFFFL+".35."+0xFFFFFFFFL+56".36."+0xFFFFFFFFL+".37."+0xFFFFFFFFL+".38."+0xFFFFFFFFL+57".39."+0xFFFFFFFFL58};5960// We use an SnmpOidBuilder in order to adapt this test case to a61// configuration where the SNMP packages are not present in rt.jar.62//63public static final class SnmpOidBuilder {64public static final String SNMP_OID_CLASS_NAME =65"com.sun.jmx.snmp.SnmpOid";66private static final Class<?> SNMP_OID_CLASS;67private static final Constructor<?> SNMP_OID_CTOR;68static {69Class<?> snmpOidClass;70try {71snmpOidClass =72Class.forName(SNMP_OID_CLASS_NAME, true, null);73} catch (ClassNotFoundException x) {74snmpOidClass = null;75System.err.println("WARNING: can't load "+SNMP_OID_CLASS_NAME);76} catch (NoClassDefFoundError x) {77snmpOidClass = null;78System.err.println("WARNING: can't load "+SNMP_OID_CLASS_NAME);79}80SNMP_OID_CLASS = snmpOidClass;81if (SNMP_OID_CLASS != null) {82try {83SNMP_OID_CTOR = snmpOidClass.getConstructor(String.class);84} catch (Exception x) {85throw new ExceptionInInitializerError(x);86}87} else {88SNMP_OID_CTOR = null;89}90}9192public static boolean isSnmpPresent() {93System.out.println(SnmpOidHashCode.class.getName()+94": Testing for SNMP Packages...");95return SNMP_OID_CLASS != null;96}9798public static Object newSnmpOid(String oid)99throws InstantiationException,100IllegalAccessException,101InvocationTargetException {102return SNMP_OID_CTOR.newInstance(oid);103}104105}106107private static Object newSnmpOid(String oid) throws Exception {108try {109return SnmpOidBuilder.newSnmpOid(oid);110} catch (InvocationTargetException x) {111final Throwable cause = x.getCause();112if (cause instanceof Exception) throw (Exception)cause;113if (cause instanceof Error) throw (Error)cause;114throw x;115}116}117118public static void main(String args[]) {119if (!SnmpOidBuilder.isSnmpPresent()) {120System.err.println("WARNING: "+121SnmpOidBuilder.SNMP_OID_CLASS_NAME+" not present.");122System.err.println(SnmpOidHashCode.class.getName()+123": test skipped.");124return;125}126try {127int errCount=0;128int collisions=0;129for (int i=0;i<oids.length;i++) {130System.out.println("Testing " + oids[i]);131final Object o1 = newSnmpOid(oids[i]);132final int startCount=errCount;133for (int j=0;j<oids.length;j++) {134final Object o2 = newSnmpOid(oids[j]);135if (o1.equals(o2)) {136if (!(oids[i].equals(oids[j]))) {137System.err.println("OIDs differ but " +138"equals yields true: " +139"\n\to1="+oids[i]+140"\n\to2="+oids[j]);141errCount++;142}143if (o1.hashCode() != o2.hashCode()) {144System.err.println("OIDs are equal but " +145"hashCode differ:" +146"\n\thashCode("+o1+")="+147o1.hashCode()+", "+148"\n\thashCode("+o2+")="+149o2.hashCode());150errCount++;151}152} else {153if (oids[i].equals(oids[j])) {154System.err.println("OIDs are equal but " +155"equals yields false: " +156"\n\to1="+oids[i]+157"\n\to2="+oids[j]);158errCount++;159}160if (o1.hashCode() == o2.hashCode()) collisions++;161}162}163if (errCount == startCount)164System.out.println("*** Test Passed for: " + o1);165else166System.out.println("*** Test Failed (" +167(errCount - startCount) + ") for: "168+ o1);169}170171if (errCount == 0) {172System.out.println("*** -----------------------------------");173System.out.println("*** Test SnmpOidHashCode " +174"succesfully passed (" + collisions +175" collisions).");176System.out.println("*** -----------------------------------");177} else {178System.err.println("*** -----------------------------------");179System.err.println("*** Test SnmpOidHashCode failed: " +180errCount + " failures (" + collisions +181" collisions).");182System.err.println("*** -----------------------------------");183System.exit(1);184}185} catch(Exception x) {186x.printStackTrace();187System.exit(2);188}189}190}191192193