0N/A/*
2362N/A * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @summary Test that SnmpOid hashCode is consistent with equals.
0N/A * @bug 4955105
0N/A * @build SnmpOidHashCode
0N/A * @run main SnmpOidHashCode
0N/A */
955N/Aimport java.lang.reflect.Constructor;
955N/Aimport java.lang.reflect.InvocationTargetException;
0N/A
0N/Apublic class SnmpOidHashCode {
0N/A public static final String[] oids = {
0N/A "1.1.1.1.1.1.1.1.1",
0N/A "1.1.1.1.1.1.1.1",
0N/A "1.1.1.1.2.1.1.1.1",
0N/A "1.1.1.1.1.2.1.1.1",
0N/A "1.3.2",
0N/A "2.3.1",
0N/A "1.2.67."+Integer.MAX_VALUE+"."+Integer.MAX_VALUE+
0N/A "."+Integer.MAX_VALUE+"."+Integer.MAX_VALUE+"."+Integer.MAX_VALUE+
0N/A "1",
0N/A "1.2."+0xFFFFFFFFL+".3."+0xFFFFFFFFL+".4."+0xFFFFFFFFL+
0N/A ".5."+0xFFFFFFFFL+".6."+0xFFFFFFFFL+".7."+0xFFFFFFFFL+
0N/A ".8."+0xFFFFFFFFL+".9."+0xFFFFFFFFL+".10."+0xFFFFFFFFL+
0N/A ".11."+0xFFFFFFFFL+".12."+0xFFFFFFFFL+".13."+0xFFFFFFFFL+
0N/A ".14."+0xFFFFFFFFL+".15."+0xFFFFFFFFL+".16."+0xFFFFFFFFL+
0N/A ".17."+0xFFFFFFFFL+".18."+0xFFFFFFFFL+".19."+0xFFFFFFFFL+
0N/A ".20."+0xFFFFFFFFL+".21."+0xFFFFFFFFL+".22."+0xFFFFFFFFL+
0N/A ".23."+0xFFFFFFFFL+".24."+0xFFFFFFFFL+".25."+0xFFFFFFFFL+
0N/A ".26."+0xFFFFFFFFL+".27."+0xFFFFFFFFL+".28."+0xFFFFFFFFL+
0N/A ".29."+0xFFFFFFFFL+
0N/A ".30."+0xFFFFFFFFL+".31."+0xFFFFFFFFL+".32."+0xFFFFFFFFL+
0N/A ".33."+0xFFFFFFFFL+".34."+0xFFFFFFFFL+".35."+0xFFFFFFFFL+
0N/A ".36."+0xFFFFFFFFL+".37."+0xFFFFFFFFL+".38."+0xFFFFFFFFL+
0N/A ".39."+0xFFFFFFFFL
0N/A };
0N/A
955N/A // We use an SnmpOidBuilder in order to adapt this test case to a
955N/A // configuration where the SNMP packages are not present in rt.jar.
955N/A //
955N/A public static final class SnmpOidBuilder {
955N/A public static final String SNMP_OID_CLASS_NAME =
955N/A "com.sun.jmx.snmp.SnmpOid";
955N/A private static final Class<?> SNMP_OID_CLASS;
955N/A private static final Constructor<?> SNMP_OID_CTOR;
955N/A static {
955N/A Class<?> snmpOidClass;
955N/A try {
955N/A snmpOidClass =
955N/A Class.forName(SNMP_OID_CLASS_NAME, true, null);
955N/A } catch (ClassNotFoundException x) {
955N/A snmpOidClass = null;
955N/A System.err.println("WARNING: can't load "+SNMP_OID_CLASS_NAME);
955N/A } catch (NoClassDefFoundError x) {
955N/A snmpOidClass = null;
955N/A System.err.println("WARNING: can't load "+SNMP_OID_CLASS_NAME);
955N/A }
955N/A SNMP_OID_CLASS = snmpOidClass;
955N/A if (SNMP_OID_CLASS != null) {
955N/A try {
955N/A SNMP_OID_CTOR = snmpOidClass.getConstructor(String.class);
955N/A } catch (Exception x) {
955N/A throw new ExceptionInInitializerError(x);
955N/A }
955N/A } else {
955N/A SNMP_OID_CTOR = null;
955N/A }
955N/A }
955N/A
955N/A public static boolean isSnmpPresent() {
955N/A System.out.println(SnmpOidHashCode.class.getName()+
955N/A ": Testing for SNMP Packages...");
955N/A return SNMP_OID_CLASS != null;
955N/A }
955N/A
955N/A public static Object newSnmpOid(String oid)
955N/A throws InstantiationException,
955N/A IllegalAccessException,
955N/A InvocationTargetException {
955N/A return SNMP_OID_CTOR.newInstance(oid);
955N/A }
955N/A
955N/A }
955N/A
955N/A private static Object newSnmpOid(String oid) throws Exception {
955N/A try {
955N/A return SnmpOidBuilder.newSnmpOid(oid);
955N/A } catch (InvocationTargetException x) {
955N/A final Throwable cause = x.getCause();
955N/A if (cause instanceof Exception) throw (Exception)cause;
955N/A if (cause instanceof Error) throw (Error)cause;
955N/A throw x;
955N/A }
955N/A }
955N/A
0N/A public static void main(String args[]) {
955N/A if (!SnmpOidBuilder.isSnmpPresent()) {
955N/A System.err.println("WARNING: "+
955N/A SnmpOidBuilder.SNMP_OID_CLASS_NAME+" not present.");
955N/A System.err.println(SnmpOidHashCode.class.getName()+
955N/A ": test skipped.");
955N/A return;
955N/A }
0N/A try {
0N/A int errCount=0;
0N/A int collisions=0;
0N/A for (int i=0;i<oids.length;i++) {
0N/A System.out.println("Testing " + oids[i]);
955N/A final Object o1 = newSnmpOid(oids[i]);
0N/A final int startCount=errCount;
0N/A for (int j=0;j<oids.length;j++) {
955N/A final Object o2 = newSnmpOid(oids[j]);
0N/A if (o1.equals(o2)) {
0N/A if (!(oids[i].equals(oids[j]))) {
0N/A System.err.println("OIDs differ but " +
0N/A "equals yields true: " +
0N/A "\n\to1="+oids[i]+
0N/A "\n\to2="+oids[j]);
0N/A errCount++;
0N/A }
0N/A if (o1.hashCode() != o2.hashCode()) {
0N/A System.err.println("OIDs are equal but " +
0N/A "hashCode differ:" +
0N/A "\n\thashCode("+o1+")="+
0N/A o1.hashCode()+", "+
0N/A "\n\thashCode("+o2+")="+
0N/A o2.hashCode());
0N/A errCount++;
0N/A }
0N/A } else {
0N/A if (oids[i].equals(oids[j])) {
0N/A System.err.println("OIDs are equal but " +
0N/A "equals yields false: " +
0N/A "\n\to1="+oids[i]+
0N/A "\n\to2="+oids[j]);
0N/A errCount++;
0N/A }
0N/A if (o1.hashCode() == o2.hashCode()) collisions++;
0N/A }
0N/A }
0N/A if (errCount == startCount)
0N/A System.out.println("*** Test Passed for: " + o1);
0N/A else
0N/A System.out.println("*** Test Failed (" +
0N/A (errCount - startCount) + ") for: "
0N/A + o1);
0N/A }
0N/A
0N/A if (errCount == 0) {
0N/A System.out.println("*** -----------------------------------");
0N/A System.out.println("*** Test SnmpOidHashCode " +
0N/A "succesfully passed (" + collisions +
0N/A " collisions).");
0N/A System.out.println("*** -----------------------------------");
0N/A } else {
0N/A System.err.println("*** -----------------------------------");
0N/A System.err.println("*** Test SnmpOidHashCode failed: " +
0N/A errCount + " failures (" + collisions +
0N/A " collisions).");
0N/A System.err.println("*** -----------------------------------");
0N/A System.exit(1);
0N/A }
0N/A } catch(Exception x) {
0N/A x.printStackTrace();
0N/A System.exit(2);
0N/A }
0N/A }
0N/A}