author | sherman |
Tue, 22 Jun 2010 14:04:36 -0400 | |
changeset 5962 | 0913689fd3a0 |
parent 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @summary Test that SnmpOid hashCode is consistent with equals. |
|
27 |
* @bug 4955105 |
|
28 |
* @build SnmpOidHashCode |
|
29 |
* @run main SnmpOidHashCode |
|
30 |
*/ |
|
2285
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
31 |
import java.lang.reflect.Constructor; |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
32 |
import java.lang.reflect.InvocationTargetException; |
2 | 33 |
|
34 |
public class SnmpOidHashCode { |
|
35 |
public static final String[] oids = { |
|
36 |
"1.1.1.1.1.1.1.1.1", |
|
37 |
"1.1.1.1.1.1.1.1", |
|
38 |
"1.1.1.1.2.1.1.1.1", |
|
39 |
"1.1.1.1.1.2.1.1.1", |
|
40 |
"1.3.2", |
|
41 |
"2.3.1", |
|
42 |
"1.2.67."+Integer.MAX_VALUE+"."+Integer.MAX_VALUE+ |
|
43 |
"."+Integer.MAX_VALUE+"."+Integer.MAX_VALUE+"."+Integer.MAX_VALUE+ |
|
44 |
"1", |
|
45 |
"1.2."+0xFFFFFFFFL+".3."+0xFFFFFFFFL+".4."+0xFFFFFFFFL+ |
|
46 |
".5."+0xFFFFFFFFL+".6."+0xFFFFFFFFL+".7."+0xFFFFFFFFL+ |
|
47 |
".8."+0xFFFFFFFFL+".9."+0xFFFFFFFFL+".10."+0xFFFFFFFFL+ |
|
48 |
".11."+0xFFFFFFFFL+".12."+0xFFFFFFFFL+".13."+0xFFFFFFFFL+ |
|
49 |
".14."+0xFFFFFFFFL+".15."+0xFFFFFFFFL+".16."+0xFFFFFFFFL+ |
|
50 |
".17."+0xFFFFFFFFL+".18."+0xFFFFFFFFL+".19."+0xFFFFFFFFL+ |
|
51 |
".20."+0xFFFFFFFFL+".21."+0xFFFFFFFFL+".22."+0xFFFFFFFFL+ |
|
52 |
".23."+0xFFFFFFFFL+".24."+0xFFFFFFFFL+".25."+0xFFFFFFFFL+ |
|
53 |
".26."+0xFFFFFFFFL+".27."+0xFFFFFFFFL+".28."+0xFFFFFFFFL+ |
|
54 |
".29."+0xFFFFFFFFL+ |
|
55 |
".30."+0xFFFFFFFFL+".31."+0xFFFFFFFFL+".32."+0xFFFFFFFFL+ |
|
56 |
".33."+0xFFFFFFFFL+".34."+0xFFFFFFFFL+".35."+0xFFFFFFFFL+ |
|
57 |
".36."+0xFFFFFFFFL+".37."+0xFFFFFFFFL+".38."+0xFFFFFFFFL+ |
|
58 |
".39."+0xFFFFFFFFL |
|
59 |
}; |
|
60 |
||
2285
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
61 |
// We use an SnmpOidBuilder in order to adapt this test case to a |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
62 |
// configuration where the SNMP packages are not present in rt.jar. |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
63 |
// |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
64 |
public static final class SnmpOidBuilder { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
65 |
public static final String SNMP_OID_CLASS_NAME = |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
66 |
"com.sun.jmx.snmp.SnmpOid"; |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
67 |
private static final Class<?> SNMP_OID_CLASS; |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
68 |
private static final Constructor<?> SNMP_OID_CTOR; |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
69 |
static { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
70 |
Class<?> snmpOidClass; |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
71 |
try { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
72 |
snmpOidClass = |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
73 |
Class.forName(SNMP_OID_CLASS_NAME, true, null); |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
74 |
} catch (ClassNotFoundException x) { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
75 |
snmpOidClass = null; |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
76 |
System.err.println("WARNING: can't load "+SNMP_OID_CLASS_NAME); |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
77 |
} catch (NoClassDefFoundError x) { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
78 |
snmpOidClass = null; |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
79 |
System.err.println("WARNING: can't load "+SNMP_OID_CLASS_NAME); |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
80 |
} |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
81 |
SNMP_OID_CLASS = snmpOidClass; |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
82 |
if (SNMP_OID_CLASS != null) { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
83 |
try { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
84 |
SNMP_OID_CTOR = snmpOidClass.getConstructor(String.class); |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
85 |
} catch (Exception x) { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
86 |
throw new ExceptionInInitializerError(x); |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
87 |
} |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
88 |
} else { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
89 |
SNMP_OID_CTOR = null; |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
90 |
} |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
91 |
} |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
92 |
|
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
93 |
public static boolean isSnmpPresent() { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
94 |
System.out.println(SnmpOidHashCode.class.getName()+ |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
95 |
": Testing for SNMP Packages..."); |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
96 |
return SNMP_OID_CLASS != null; |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
97 |
} |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
98 |
|
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
99 |
public static Object newSnmpOid(String oid) |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
100 |
throws InstantiationException, |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
101 |
IllegalAccessException, |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
102 |
InvocationTargetException { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
103 |
return SNMP_OID_CTOR.newInstance(oid); |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
104 |
} |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
105 |
|
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
106 |
} |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
107 |
|
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
108 |
private static Object newSnmpOid(String oid) throws Exception { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
109 |
try { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
110 |
return SnmpOidBuilder.newSnmpOid(oid); |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
111 |
} catch (InvocationTargetException x) { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
112 |
final Throwable cause = x.getCause(); |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
113 |
if (cause instanceof Exception) throw (Exception)cause; |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
114 |
if (cause instanceof Error) throw (Error)cause; |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
115 |
throw x; |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
116 |
} |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
117 |
} |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
118 |
|
2 | 119 |
public static void main(String args[]) { |
2285
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
120 |
if (!SnmpOidBuilder.isSnmpPresent()) { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
121 |
System.err.println("WARNING: "+ |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
122 |
SnmpOidBuilder.SNMP_OID_CLASS_NAME+" not present."); |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
123 |
System.err.println(SnmpOidHashCode.class.getName()+ |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
124 |
": test skipped."); |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
125 |
return; |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
126 |
} |
2 | 127 |
try { |
128 |
int errCount=0; |
|
129 |
int collisions=0; |
|
130 |
for (int i=0;i<oids.length;i++) { |
|
131 |
System.out.println("Testing " + oids[i]); |
|
2285
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
132 |
final Object o1 = newSnmpOid(oids[i]); |
2 | 133 |
final int startCount=errCount; |
134 |
for (int j=0;j<oids.length;j++) { |
|
2285
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
135 |
final Object o2 = newSnmpOid(oids[j]); |
2 | 136 |
if (o1.equals(o2)) { |
137 |
if (!(oids[i].equals(oids[j]))) { |
|
138 |
System.err.println("OIDs differ but " + |
|
139 |
"equals yields true: " + |
|
140 |
"\n\to1="+oids[i]+ |
|
141 |
"\n\to2="+oids[j]); |
|
142 |
errCount++; |
|
143 |
} |
|
144 |
if (o1.hashCode() != o2.hashCode()) { |
|
145 |
System.err.println("OIDs are equal but " + |
|
146 |
"hashCode differ:" + |
|
147 |
"\n\thashCode("+o1+")="+ |
|
148 |
o1.hashCode()+", "+ |
|
149 |
"\n\thashCode("+o2+")="+ |
|
150 |
o2.hashCode()); |
|
151 |
errCount++; |
|
152 |
} |
|
153 |
} else { |
|
154 |
if (oids[i].equals(oids[j])) { |
|
155 |
System.err.println("OIDs are equal but " + |
|
156 |
"equals yields false: " + |
|
157 |
"\n\to1="+oids[i]+ |
|
158 |
"\n\to2="+oids[j]); |
|
159 |
errCount++; |
|
160 |
} |
|
161 |
if (o1.hashCode() == o2.hashCode()) collisions++; |
|
162 |
} |
|
163 |
} |
|
164 |
if (errCount == startCount) |
|
165 |
System.out.println("*** Test Passed for: " + o1); |
|
166 |
else |
|
167 |
System.out.println("*** Test Failed (" + |
|
168 |
(errCount - startCount) + ") for: " |
|
169 |
+ o1); |
|
170 |
} |
|
171 |
||
172 |
if (errCount == 0) { |
|
173 |
System.out.println("*** -----------------------------------"); |
|
174 |
System.out.println("*** Test SnmpOidHashCode " + |
|
175 |
"succesfully passed (" + collisions + |
|
176 |
" collisions)."); |
|
177 |
System.out.println("*** -----------------------------------"); |
|
178 |
} else { |
|
179 |
System.err.println("*** -----------------------------------"); |
|
180 |
System.err.println("*** Test SnmpOidHashCode failed: " + |
|
181 |
errCount + " failures (" + collisions + |
|
182 |
" collisions)."); |
|
183 |
System.err.println("*** -----------------------------------"); |
|
184 |
System.exit(1); |
|
185 |
} |
|
186 |
} catch(Exception x) { |
|
187 |
x.printStackTrace(); |
|
188 |
System.exit(2); |
|
189 |
} |
|
190 |
} |
|
191 |
} |