Path: blob/master/test/jdk/javax/management/mxbean/Utils.java
41149 views
/*1* Copyright (c) 2005, 2015, 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*/2223import java.util.Map;24import java.util.HashMap;25import java.util.Properties;26import java.lang.reflect.Method;27import javax.management.remote.JMXConnectorServerMBean;2829// utility class for MXBean* tests coming from JMX Tonga test suite30class Utils {3132// DEBUG is printed depending on the DEBUG and DEBUG_LEVEL JAVA property33private static final String DEBUG_HEADER = "[debug] ";3435// DEBUG levels36private static int selectedDebugLevel = 0;37static final int DEBUG_STANDARD = 1;38static final int DEBUG_VERBOSE = 2; // Mainly used for stress tests39static final int DEBUG_ALL = DEBUG_STANDARD | DEBUG_VERBOSE;4041static void parseDebugProperties() {42int level = 0;43Properties p = System.getProperties();4445// get selected levels46if (p.getProperty("DEBUG_STANDARD") != null) {47level |= DEBUG_STANDARD;48}4950if (p.getProperty("DEBUG_VERBOSE") != null) {51level |= DEBUG_VERBOSE;52}5354if (p.getProperty("DEBUG_ALL") != null) {55level |= DEBUG_ALL;56}5758selectedDebugLevel = level;59}6061/**62* Reproduces the original parsing and collection of test parameters63* from the DTonga JMX test suite.64*65* Collects passed args and returns them in a map(argname, value) structure,66* which will be then propagated as necessary to various called methods.67*/68static Map<String, Object> parseParameters(String args[])69throws Exception {70Utils.debug(DEBUG_STANDARD, "TestRoot::parseParameters: Start");71HashMap<String, Object> map = new HashMap<>();7273for ( int i = 0; i < args.length; i++ ) {74if ( args[i].trim().startsWith("-") ) {75if ((i+1) < args.length && !args[i+1].startsWith("-") ) {76Utils.debug(DEBUG_STANDARD,77"TestRoot::parseParameters: added in map = " +78args[i] +79" with value " +80args[i+1]) ;81map.put(args[i].trim(), args[i+1].trim()) ;82} else if ((i+1) < args.length && args[i+1].startsWith("-") ||83(i+1) == args.length ) {84Utils.debug(DEBUG_STANDARD,85"TestRoot::parseParameters: added in map = " +86args[i] +87" with null value") ;88map.put(args[i].trim(), null) ;89} else {90System.out.println(91"TestRoot::parseParameters: (WARNING) not added in map = " +92args[i]) ;93}94}95}9697Utils.debug(DEBUG_STANDARD, "TestRoot::parseParameters: Done") ;98return map ;99}100101/**102* This method is to be used in all tests to print anything103* that is temporary.104* Printing is done only when debug is activated by the property DEBUG.105* Printing depends also on the DEBUG_LEVEL property.106* Here it encapsulates a System.out.println.107*/108public static void debug(int level, String line) {109if ((selectedDebugLevel & level) != 0) {110System.out.println(DEBUG_HEADER + line);111}112}113114/**115* Do print stack trace when withStack is true.116* Does try to call getTargetException() and getTargetError() then117* print embedded stacks in the case of an Exception wrapping118* another Exception or an Error. Recurse until no more wrapping119* is found.120*/121public static void printThrowable(Throwable theThro, boolean withStack) {122try {123if (withStack) {124theThro.printStackTrace(System.out);125}126if (theThro instanceof Exception) {127Exception t = (Exception) theThro;128Method target = null;129String blank = " ";130try {131target = t.getClass().getMethod("getTargetException",132(java.lang.Class<?>[]) null);133} catch (Exception ee) {134// OK: getTargetException method could be there or not135}136System.out.println(blank + t.getClass() + "==>" + t.getMessage());137while (target != null) {138try {139t = (Exception) target.invoke(t,140(java.lang.Object[]) null);141} catch (Exception ee) {142t = null;143}144try {145if (t != null) {146blank = blank + " ";147System.out.println(blank + t.getClass() + "==>" +148t.getMessage());149try {150target =151t.getClass().getMethod("getTargetException",152(java.lang.Class<?>[]) null);153} catch (Exception ee) {154// OK: getTargetException method could be there or not }155}156} else {157target = null;158}159} catch (Exception ee) {160target = null;161}162}163164// We may have exceptions wrapping an Error then it is165// getTargetError that is likely to be called166try {167target = ((Exception) theThro).getClass().getMethod("getTargetError",168(java.lang.Class<?>[]) null);169} catch (Exception ee) {170// OK: getTargetError method could be there or not171}172Throwable err = theThro;173while (target != null) {174try {175err = (Error) target.invoke(err,176(java.lang.Object[]) null);177} catch (Exception ee) {178err = null;179}180try {181if (err != null) {182blank = blank + " ";183System.out.println(blank + err.getClass() + "==>" +184err.getMessage());185if (withStack) {186err.printStackTrace(System.out);187}188try {189target = err.getClass().getMethod("getTargetError",190(java.lang.Class<?>[]) null);191} catch (Exception ee) {192// OK: getTargetError method could be there or not193}194} else {195target = null;196}197} catch (Exception ee) {198target = null;199}200}201} else {202System.out.println("Throwable is : " + theThro);203}204} catch (Throwable x) {205System.out.println("Exception : raised in printException : " + x);206}207}208209/**210* Wait up to maxTimeInSeconds second(s) the given JMX connector server211* comes up (which means isActive returns true).212* If it fails to do so we throw a RunTime exception.213*/214public static void waitReady(JMXConnectorServerMBean server,215int maxTimeInSeconds) throws Exception {216int elapsed = 0;217218while (!server.isActive() && elapsed < maxTimeInSeconds) {219Thread.sleep(1000);220elapsed++;221}222223if (server.isActive()) {224String message = "Utils::waitReady: JMX connector server came up";225if ( elapsed == 0) {226message += " immediately";227} else {228message += " after " + elapsed + " seconds";229}230message += " [" + server.getAddress() + "]";231Utils.debug(DEBUG_STANDARD, message);232} else {233String message = "Utils::waitReady: (ERROR) JMX connector" +234" server didn't come up after " + elapsed + " seconds [" +235server.getAddress() + "]";236System.out.println(message);237throw new RuntimeException(message);238}239}240}241242243