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/TimeTicksWrapping.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 SnmpTimeTicks wraps around when it is passed a long
27
* value
28
* @bug 4955105
29
* @build TimeTicksWrapping
30
* @run main TimeTicksWrapping
31
*/
32
import java.lang.reflect.Constructor;
33
import java.lang.reflect.InvocationTargetException;
34
import java.lang.reflect.Method;
35
36
public class TimeTicksWrapping {
37
// We use an SnmpTimeticksBuilder in order to adapt this test case to a
38
// configuration where the SNMP packages are not present in rt.jar.
39
//
40
public static final class SnmpTimeticksBuilder {
41
public static final long MAX_VALUE = 0x0ffffffffL;
42
public static final String SNMP_TIME_TICKS_CLASS_NAME =
43
"com.sun.jmx.snmp.SnmpTimeticks";
44
private static final Class<?> SNMP_TIME_TICKS_CLASS;
45
private static final Constructor<?> SNMP_long_CTOR;
46
private static final Constructor<?> SNMP_LONG_CTOR;
47
private static final Method SNMP_LONG_VALUE;
48
static {
49
Class<?> snmpTimeTicksClass;
50
try {
51
snmpTimeTicksClass =
52
Class.forName(SNMP_TIME_TICKS_CLASS_NAME, true, null);
53
} catch (ClassNotFoundException x) {
54
snmpTimeTicksClass = null;
55
System.err.println("WARNING: can't load "+
56
SNMP_TIME_TICKS_CLASS_NAME);
57
} catch (NoClassDefFoundError x) {
58
snmpTimeTicksClass = null;
59
System.err.println("WARNING: can't load "+
60
SNMP_TIME_TICKS_CLASS_NAME);
61
}
62
SNMP_TIME_TICKS_CLASS = snmpTimeTicksClass;
63
if (SNMP_TIME_TICKS_CLASS != null) {
64
try {
65
SNMP_long_CTOR =
66
SNMP_TIME_TICKS_CLASS.getConstructor(long.class);
67
} catch (Exception x) {
68
throw new ExceptionInInitializerError(x);
69
}
70
} else {
71
SNMP_long_CTOR = null;
72
}
73
if (SNMP_TIME_TICKS_CLASS != null) {
74
try {
75
SNMP_LONG_CTOR =
76
SNMP_TIME_TICKS_CLASS.getConstructor(Long.class);
77
} catch (Exception x) {
78
throw new ExceptionInInitializerError(x);
79
}
80
} else {
81
SNMP_LONG_CTOR = null;
82
}
83
if (SNMP_TIME_TICKS_CLASS != null) {
84
try {
85
SNMP_LONG_VALUE =
86
SNMP_TIME_TICKS_CLASS.getMethod("longValue");
87
} catch (Exception x) {
88
throw new ExceptionInInitializerError(x);
89
}
90
} else {
91
SNMP_LONG_VALUE = null;
92
}
93
94
}
95
96
private final Object timeticks;
97
98
public SnmpTimeticksBuilder(long ticks) throws Exception {
99
timeticks = newSnmpTimeticks(ticks);
100
}
101
public SnmpTimeticksBuilder(Long ticks) throws Exception {
102
timeticks = newSnmpTimeticks(ticks);
103
}
104
public long longValue() throws Exception {
105
return longValue(timeticks);
106
}
107
108
public static boolean isSnmpPresent() {
109
System.out.println(TimeTicksWrapping.class.getName()+
110
": Testing for SNMP Packages...");
111
return SNMP_TIME_TICKS_CLASS != null;
112
}
113
114
private static Object newSnmpTimeticks(long time)
115
throws Exception {
116
try {
117
return SNMP_long_CTOR.newInstance(time);
118
} catch (InvocationTargetException x) {
119
final Throwable cause = x.getCause();
120
if (cause instanceof Exception) throw (Exception) cause;
121
if (cause instanceof Error) throw (Error) cause;
122
throw x;
123
}
124
}
125
126
private static Object newSnmpTimeticks(Long time)
127
throws Exception {
128
try {
129
return SNMP_LONG_CTOR.newInstance(time);
130
} catch (InvocationTargetException x) {
131
final Throwable cause = x.getCause();
132
if (cause instanceof Exception) throw (Exception) cause;
133
if (cause instanceof Error) throw (Error) cause;
134
throw x;
135
}
136
}
137
138
private static long longValue(Object o)
139
throws Exception {
140
try {
141
return ((Long)SNMP_LONG_VALUE.invoke(o)).longValue();
142
} catch (InvocationTargetException x) {
143
final Throwable cause = x.getCause();
144
if (cause instanceof Exception) throw (Exception) cause;
145
if (cause instanceof Error) throw (Error) cause;
146
throw x;
147
}
148
}
149
150
}
151
152
public static final long[] oks = {
153
0L, 1L, (long)Integer.MAX_VALUE, (long)Integer.MAX_VALUE*2,
154
(long)Integer.MAX_VALUE*2+1L, (long)Integer.MAX_VALUE*2+2L,
155
(long)Integer.MAX_VALUE*3,
156
SnmpTimeticksBuilder.MAX_VALUE, SnmpTimeticksBuilder.MAX_VALUE+1L,
157
SnmpTimeticksBuilder.MAX_VALUE*3-1L, Long.MAX_VALUE
158
};
159
160
public static final long[] kos = {
161
-1L, (long)Integer.MIN_VALUE, (long)Integer.MIN_VALUE*2,
162
(long)Integer.MIN_VALUE*2-1L, (long)Integer.MIN_VALUE*3,
163
-SnmpTimeticksBuilder.MAX_VALUE, -(SnmpTimeticksBuilder.MAX_VALUE+1L),
164
-(SnmpTimeticksBuilder.MAX_VALUE*3-1L), Long.MIN_VALUE
165
};
166
167
168
public static void main(String args[]) {
169
if (!SnmpTimeticksBuilder.isSnmpPresent()) {
170
System.err.println("WARNING: "+
171
SnmpTimeticksBuilder.SNMP_TIME_TICKS_CLASS_NAME+
172
" not present.");
173
System.err.println(TimeTicksWrapping.class.getName()+
174
": test skipped.");
175
return;
176
}
177
try {
178
SnmpTimeticksBuilder t = null;
179
180
for (int i=0;i<oks.length;i++) {
181
final long t1,t2,t3;
182
t1 = (new SnmpTimeticksBuilder(oks[i])).longValue();
183
t2 = (new SnmpTimeticksBuilder(new Long(oks[i]))).longValue();
184
t3 = oks[i]%0x0100000000L;
185
if (t1 != t3)
186
throw new Exception("Value should have wrapped: " +
187
oks[i] + " expected: " + t3);
188
if (t2 != t3)
189
throw new Exception("Value should have wrapped: " +
190
"Long("+oks[i]+") expected: " + t3);
191
192
if (t1 > SnmpTimeticksBuilder.MAX_VALUE)
193
throw new Exception("Value should have wrapped " +
194
"for " + oks[i] + ": " +
195
t1 + " exceeds max: " +
196
SnmpTimeticksBuilder.MAX_VALUE);
197
if (t2 > SnmpTimeticksBuilder.MAX_VALUE)
198
throw new Exception("Value should have wrapped " +
199
"for " + oks[i] + ": " +
200
t2 + " exceeds max: " +
201
SnmpTimeticksBuilder.MAX_VALUE);
202
203
if (t1 < 0)
204
throw new Exception("Value should have wrapped: " +
205
"for " + oks[i] + ": " +
206
t1 + " is negative");
207
if (t2 < 0)
208
throw new Exception("Value should have wrapped: " +
209
"for " + oks[i] + ": " +
210
t2 + " is negative");
211
212
System.out.println("TimeTicks[" + oks[i] +
213
"] rightfully accepted: " + t3);
214
}
215
216
for (int i=0;i<kos.length;i++) {
217
try {
218
t = new SnmpTimeticksBuilder(kos[i]);
219
throw new Exception("Value should have been rejected: " +
220
kos[i]);
221
} catch (IllegalArgumentException x) {
222
// OK!
223
}
224
try {
225
t = new SnmpTimeticksBuilder(new Long(kos[i]));
226
throw new Exception("Value should have been rejected: " +
227
"Long("+kos[i]+")");
228
} catch (IllegalArgumentException x) {
229
// OK!
230
}
231
232
System.out.println("TimeTicks[" + kos[i] +
233
"] rightfully rejected.");
234
}
235
236
} catch(Exception x) {
237
x.printStackTrace();
238
System.exit(1);
239
}
240
}
241
}
242
243