SnmpOidHashCode.java revision 2362
0N/A/*
0N/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
2362N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
2362N/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 *
0N/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.
2362N/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 */
0N/Aimport java.lang.reflect.Constructor;
0N/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+
50N/A ".39."+0xFFFFFFFFL
0N/A };
50N/A
50N/A // We use an SnmpOidBuilder in order to adapt this test case to a
50N/A // configuration where the SNMP packages are not present in rt.jar.
50N/A //
50N/A public static final class SnmpOidBuilder {
50N/A public static final String SNMP_OID_CLASS_NAME =
0N/A "com.sun.jmx.snmp.SnmpOid";
50N/A private static final Class<?> SNMP_OID_CLASS;
50N/A private static final Constructor<?> SNMP_OID_CTOR;
50N/A static {
50N/A Class<?> snmpOidClass;
50N/A try {
50N/A snmpOidClass =
50N/A Class.forName(SNMP_OID_CLASS_NAME, true, null);
50N/A } catch (ClassNotFoundException x) {
50N/A snmpOidClass = null;
0N/A System.err.println("WARNING: can't load "+SNMP_OID_CLASS_NAME);
50N/A } catch (NoClassDefFoundError x) {
50N/A snmpOidClass = null;
50N/A System.err.println("WARNING: can't load "+SNMP_OID_CLASS_NAME);
50N/A }
50N/A SNMP_OID_CLASS = snmpOidClass;
50N/A if (SNMP_OID_CLASS != null) {
0N/A try {
50N/A SNMP_OID_CTOR = snmpOidClass.getConstructor(String.class);
50N/A } catch (Exception x) {
50N/A throw new ExceptionInInitializerError(x);
0N/A }
50N/A } else {
50N/A SNMP_OID_CTOR = null;
50N/A }
0N/A }
50N/A
50N/A public static boolean isSnmpPresent() {
50N/A System.out.println(SnmpOidHashCode.class.getName()+
50N/A ": Testing for SNMP Packages...");
50N/A return SNMP_OID_CLASS != null;
50N/A }
50N/A
0N/A public static Object newSnmpOid(String oid)
50N/A throws InstantiationException,
50N/A IllegalAccessException,
50N/A InvocationTargetException {
50N/A return SNMP_OID_CTOR.newInstance(oid);
50N/A }
50N/A
50N/A }
50N/A
50N/A private static Object newSnmpOid(String oid) throws Exception {
50N/A try {
50N/A return SnmpOidBuilder.newSnmpOid(oid);
50N/A } catch (InvocationTargetException x) {
50N/A final Throwable cause = x.getCause();
0N/A if (cause instanceof Exception) throw (Exception)cause;
50N/A if (cause instanceof Error) throw (Error)cause;
50N/A throw x;
50N/A }
50N/A }
50N/A
50N/A public static void main(String args[]) {
50N/A if (!SnmpOidBuilder.isSnmpPresent()) {
50N/A System.err.println("WARNING: "+
50N/A SnmpOidBuilder.SNMP_OID_CLASS_NAME+" not present.");
50N/A System.err.println(SnmpOidHashCode.class.getName()+
50N/A ": test skipped.");
50N/A return;
50N/A }
0N/A try {
50N/A int errCount=0;
50N/A int collisions=0;
50N/A for (int i=0;i<oids.length;i++) {
50N/A System.out.println("Testing " + oids[i]);
50N/A final Object o1 = newSnmpOid(oids[i]);
50N/A final int startCount=errCount;
50N/A for (int j=0;j<oids.length;j++) {
50N/A final Object o2 = newSnmpOid(oids[j]);
0N/A if (o1.equals(o2)) {
50N/A if (!(oids[i].equals(oids[j]))) {
50N/A System.err.println("OIDs differ but " +
50N/A "equals yields true: " +
50N/A "\n\to1="+oids[i]+
50N/A "\n\to2="+oids[j]);
50N/A errCount++;
50N/A }
0N/A if (o1.hashCode() != o2.hashCode()) {
0N/A System.err.println("OIDs are equal but " +
0N/A "hashCode differ:" +
50N/A "\n\thashCode("+o1+")="+
50N/A o1.hashCode()+", "+
50N/A "\n\thashCode("+o2+")="+
50N/A o2.hashCode());
50N/A errCount++;
50N/A }
50N/A } else {
50N/A if (oids[i].equals(oids[j])) {
50N/A System.err.println("OIDs are equal but " +
0N/A "equals yields false: " +
50N/A "\n\to1="+oids[i]+
50N/A "\n\to2="+oids[j]);
50N/A errCount++;
50N/A }
50N/A if (o1.hashCode() == o2.hashCode()) collisions++;
50N/A }
50N/A }
50N/A if (errCount == startCount)
50N/A System.out.println("*** Test Passed for: " + o1);
50N/A else
50N/A System.out.println("*** Test Failed (" +
50N/A (errCount - startCount) + ") for: "
50N/A + o1);
50N/A }
50N/A
50N/A if (errCount == 0) {
0N/A System.out.println("*** -----------------------------------");
System.out.println("*** Test SnmpOidHashCode " +
"succesfully passed (" + collisions +
" collisions).");
System.out.println("*** -----------------------------------");
} else {
System.err.println("*** -----------------------------------");
System.err.println("*** Test SnmpOidHashCode failed: " +
errCount + " failures (" + collisions +
" collisions).");
System.err.println("*** -----------------------------------");
System.exit(1);
}
} catch(Exception x) {
x.printStackTrace();
System.exit(2);
}
}
}