Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jmx/snmp/SnmpOidHashCode.java
38855 views
1
/*
2
* Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
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
*
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.
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
*/
31
import java.lang.reflect.Constructor;
32
import java.lang.reflect.InvocationTargetException;
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
61
// We use an SnmpOidBuilder in order to adapt this test case to a
62
// configuration where the SNMP packages are not present in rt.jar.
63
//
64
public static final class SnmpOidBuilder {
65
public static final String SNMP_OID_CLASS_NAME =
66
"com.sun.jmx.snmp.SnmpOid";
67
private static final Class<?> SNMP_OID_CLASS;
68
private static final Constructor<?> SNMP_OID_CTOR;
69
static {
70
Class<?> snmpOidClass;
71
try {
72
snmpOidClass =
73
Class.forName(SNMP_OID_CLASS_NAME, true, null);
74
} catch (ClassNotFoundException x) {
75
snmpOidClass = null;
76
System.err.println("WARNING: can't load "+SNMP_OID_CLASS_NAME);
77
} catch (NoClassDefFoundError x) {
78
snmpOidClass = null;
79
System.err.println("WARNING: can't load "+SNMP_OID_CLASS_NAME);
80
}
81
SNMP_OID_CLASS = snmpOidClass;
82
if (SNMP_OID_CLASS != null) {
83
try {
84
SNMP_OID_CTOR = snmpOidClass.getConstructor(String.class);
85
} catch (Exception x) {
86
throw new ExceptionInInitializerError(x);
87
}
88
} else {
89
SNMP_OID_CTOR = null;
90
}
91
}
92
93
public static boolean isSnmpPresent() {
94
System.out.println(SnmpOidHashCode.class.getName()+
95
": Testing for SNMP Packages...");
96
return SNMP_OID_CLASS != null;
97
}
98
99
public static Object newSnmpOid(String oid)
100
throws InstantiationException,
101
IllegalAccessException,
102
InvocationTargetException {
103
return SNMP_OID_CTOR.newInstance(oid);
104
}
105
106
}
107
108
private static Object newSnmpOid(String oid) throws Exception {
109
try {
110
return SnmpOidBuilder.newSnmpOid(oid);
111
} catch (InvocationTargetException x) {
112
final Throwable cause = x.getCause();
113
if (cause instanceof Exception) throw (Exception)cause;
114
if (cause instanceof Error) throw (Error)cause;
115
throw x;
116
}
117
}
118
119
public static void main(String args[]) {
120
if (!SnmpOidBuilder.isSnmpPresent()) {
121
System.err.println("WARNING: "+
122
SnmpOidBuilder.SNMP_OID_CLASS_NAME+" not present.");
123
System.err.println(SnmpOidHashCode.class.getName()+
124
": test skipped.");
125
return;
126
}
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]);
132
final Object o1 = newSnmpOid(oids[i]);
133
final int startCount=errCount;
134
for (int j=0;j<oids.length;j++) {
135
final Object o2 = newSnmpOid(oids[j]);
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
}
192
193