Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jmx/snmp/TimeTicksWrapping.java
38855 views
/*1* Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Test that SnmpTimeTicks wraps around when it is passed a long26* value27* @bug 495510528* @build TimeTicksWrapping29* @run main TimeTicksWrapping30*/31import java.lang.reflect.Constructor;32import java.lang.reflect.InvocationTargetException;33import java.lang.reflect.Method;3435public class TimeTicksWrapping {36// We use an SnmpTimeticksBuilder in order to adapt this test case to a37// configuration where the SNMP packages are not present in rt.jar.38//39public static final class SnmpTimeticksBuilder {40public static final long MAX_VALUE = 0x0ffffffffL;41public static final String SNMP_TIME_TICKS_CLASS_NAME =42"com.sun.jmx.snmp.SnmpTimeticks";43private static final Class<?> SNMP_TIME_TICKS_CLASS;44private static final Constructor<?> SNMP_long_CTOR;45private static final Constructor<?> SNMP_LONG_CTOR;46private static final Method SNMP_LONG_VALUE;47static {48Class<?> snmpTimeTicksClass;49try {50snmpTimeTicksClass =51Class.forName(SNMP_TIME_TICKS_CLASS_NAME, true, null);52} catch (ClassNotFoundException x) {53snmpTimeTicksClass = null;54System.err.println("WARNING: can't load "+55SNMP_TIME_TICKS_CLASS_NAME);56} catch (NoClassDefFoundError x) {57snmpTimeTicksClass = null;58System.err.println("WARNING: can't load "+59SNMP_TIME_TICKS_CLASS_NAME);60}61SNMP_TIME_TICKS_CLASS = snmpTimeTicksClass;62if (SNMP_TIME_TICKS_CLASS != null) {63try {64SNMP_long_CTOR =65SNMP_TIME_TICKS_CLASS.getConstructor(long.class);66} catch (Exception x) {67throw new ExceptionInInitializerError(x);68}69} else {70SNMP_long_CTOR = null;71}72if (SNMP_TIME_TICKS_CLASS != null) {73try {74SNMP_LONG_CTOR =75SNMP_TIME_TICKS_CLASS.getConstructor(Long.class);76} catch (Exception x) {77throw new ExceptionInInitializerError(x);78}79} else {80SNMP_LONG_CTOR = null;81}82if (SNMP_TIME_TICKS_CLASS != null) {83try {84SNMP_LONG_VALUE =85SNMP_TIME_TICKS_CLASS.getMethod("longValue");86} catch (Exception x) {87throw new ExceptionInInitializerError(x);88}89} else {90SNMP_LONG_VALUE = null;91}9293}9495private final Object timeticks;9697public SnmpTimeticksBuilder(long ticks) throws Exception {98timeticks = newSnmpTimeticks(ticks);99}100public SnmpTimeticksBuilder(Long ticks) throws Exception {101timeticks = newSnmpTimeticks(ticks);102}103public long longValue() throws Exception {104return longValue(timeticks);105}106107public static boolean isSnmpPresent() {108System.out.println(TimeTicksWrapping.class.getName()+109": Testing for SNMP Packages...");110return SNMP_TIME_TICKS_CLASS != null;111}112113private static Object newSnmpTimeticks(long time)114throws Exception {115try {116return SNMP_long_CTOR.newInstance(time);117} catch (InvocationTargetException x) {118final Throwable cause = x.getCause();119if (cause instanceof Exception) throw (Exception) cause;120if (cause instanceof Error) throw (Error) cause;121throw x;122}123}124125private static Object newSnmpTimeticks(Long time)126throws Exception {127try {128return SNMP_LONG_CTOR.newInstance(time);129} catch (InvocationTargetException x) {130final Throwable cause = x.getCause();131if (cause instanceof Exception) throw (Exception) cause;132if (cause instanceof Error) throw (Error) cause;133throw x;134}135}136137private static long longValue(Object o)138throws Exception {139try {140return ((Long)SNMP_LONG_VALUE.invoke(o)).longValue();141} catch (InvocationTargetException x) {142final Throwable cause = x.getCause();143if (cause instanceof Exception) throw (Exception) cause;144if (cause instanceof Error) throw (Error) cause;145throw x;146}147}148149}150151public static final long[] oks = {1520L, 1L, (long)Integer.MAX_VALUE, (long)Integer.MAX_VALUE*2,153(long)Integer.MAX_VALUE*2+1L, (long)Integer.MAX_VALUE*2+2L,154(long)Integer.MAX_VALUE*3,155SnmpTimeticksBuilder.MAX_VALUE, SnmpTimeticksBuilder.MAX_VALUE+1L,156SnmpTimeticksBuilder.MAX_VALUE*3-1L, Long.MAX_VALUE157};158159public static final long[] kos = {160-1L, (long)Integer.MIN_VALUE, (long)Integer.MIN_VALUE*2,161(long)Integer.MIN_VALUE*2-1L, (long)Integer.MIN_VALUE*3,162-SnmpTimeticksBuilder.MAX_VALUE, -(SnmpTimeticksBuilder.MAX_VALUE+1L),163-(SnmpTimeticksBuilder.MAX_VALUE*3-1L), Long.MIN_VALUE164};165166167public static void main(String args[]) {168if (!SnmpTimeticksBuilder.isSnmpPresent()) {169System.err.println("WARNING: "+170SnmpTimeticksBuilder.SNMP_TIME_TICKS_CLASS_NAME+171" not present.");172System.err.println(TimeTicksWrapping.class.getName()+173": test skipped.");174return;175}176try {177SnmpTimeticksBuilder t = null;178179for (int i=0;i<oks.length;i++) {180final long t1,t2,t3;181t1 = (new SnmpTimeticksBuilder(oks[i])).longValue();182t2 = (new SnmpTimeticksBuilder(new Long(oks[i]))).longValue();183t3 = oks[i]%0x0100000000L;184if (t1 != t3)185throw new Exception("Value should have wrapped: " +186oks[i] + " expected: " + t3);187if (t2 != t3)188throw new Exception("Value should have wrapped: " +189"Long("+oks[i]+") expected: " + t3);190191if (t1 > SnmpTimeticksBuilder.MAX_VALUE)192throw new Exception("Value should have wrapped " +193"for " + oks[i] + ": " +194t1 + " exceeds max: " +195SnmpTimeticksBuilder.MAX_VALUE);196if (t2 > SnmpTimeticksBuilder.MAX_VALUE)197throw new Exception("Value should have wrapped " +198"for " + oks[i] + ": " +199t2 + " exceeds max: " +200SnmpTimeticksBuilder.MAX_VALUE);201202if (t1 < 0)203throw new Exception("Value should have wrapped: " +204"for " + oks[i] + ": " +205t1 + " is negative");206if (t2 < 0)207throw new Exception("Value should have wrapped: " +208"for " + oks[i] + ": " +209t2 + " is negative");210211System.out.println("TimeTicks[" + oks[i] +212"] rightfully accepted: " + t3);213}214215for (int i=0;i<kos.length;i++) {216try {217t = new SnmpTimeticksBuilder(kos[i]);218throw new Exception("Value should have been rejected: " +219kos[i]);220} catch (IllegalArgumentException x) {221// OK!222}223try {224t = new SnmpTimeticksBuilder(new Long(kos[i]));225throw new Exception("Value should have been rejected: " +226"Long("+kos[i]+")");227} catch (IllegalArgumentException x) {228// OK!229}230231System.out.println("TimeTicks[" + kos[i] +232"] rightfully rejected.");233}234235} catch(Exception x) {236x.printStackTrace();237System.exit(1);238}239}240}241242243