Path: blob/master/test/jdk/javax/management/mxbean/MXBeanExceptionHandlingTest.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*/2223/*24* @test25* @bug 805886526* @summary Checks correct exception and error events from NotificationListener27* @author Olivier Lagneau28* @modules java.management.rmi29* @library /lib/testlibrary30* @compile Basic.java31* @run main/othervm/timeout=300 -DDEBUG_STANDARD MXBeanExceptionHandlingTest -timeForNotificationInSeconds 332*/333435import java.util.Map;36import java.util.HashMap;37import java.util.concurrent.ArrayBlockingQueue;38import java.util.concurrent.BlockingQueue;3940import java.lang.management.ManagementFactory;41import javax.management.MBeanServer;42import javax.management.MBeanException;43import javax.management.MBeanServerDelegate;44import javax.management.Notification;45import javax.management.NotificationListener;46import javax.management.MBeanServerConnection;47import javax.management.ObjectName;48import javax.management.RuntimeErrorException;49import javax.management.remote.JMXConnector;50import javax.management.remote.JMXConnectorFactory;51import javax.management.remote.JMXConnectorServer;52import javax.management.remote.JMXConnectorServerFactory;53import javax.management.remote.JMXServiceURL;5455public class MXBeanExceptionHandlingTest implements NotificationListener {5657private static String BASIC_MXBEAN_CLASS_NAME = "Basic";5859private long timeForNotificationInSeconds = 3L;60private int numOfNotifications = 2;61private BlockingQueue<Notification> notifList = null;626364/*65* First Debug properties and arguments are collect in expected66* map (argName, value) format, then calls original test's run method.67*/68public static void main(String args[]) throws Exception {6970System.out.println("=================================================");7172// Parses parameters73Utils.parseDebugProperties();74Map<String, Object> map = Utils.parseParameters(args) ;7576// Run test77MXBeanExceptionHandlingTest test = new MXBeanExceptionHandlingTest();78test.run(map);7980}8182protected void parseArgs(Map<String, Object> args) throws Exception {8384String arg = null;8586// Init timeForNotificationInSeconds87// It is the maximum time in seconds we wait for a notification.88arg = (String)args.get("-timeForNotificationInSeconds") ;89if (arg != null) {90timeForNotificationInSeconds = (new Long(arg)).longValue();91}9293}9495public void run(Map<String, Object> args) {9697System.out.println("MXBeanExceptionHandlingTest::run: Start") ;98int errorCount = 0 ;99100try {101parseArgs(args);102notifList = new ArrayBlockingQueue<Notification>(numOfNotifications);103104// JMX MbeanServer used inside single VM as if remote.105MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();106107JMXServiceURL url = new JMXServiceURL("rmi", null, 0);108JMXConnectorServer cs =109JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);110cs.start();111112JMXServiceURL addr = cs.getAddress();113JMXConnector cc = JMXConnectorFactory.connect(addr);114MBeanServerConnection mbsc = cc.getMBeanServerConnection();115116// ----117System.out.println("Add me as notification listener");118mbsc.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME,119this, null, null);120System.out.println("---- OK\n") ;121122// ----123System.out.println("Create and register the MBean");124ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi") ;125mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);126System.out.println("---- OK\n") ;127128// ----129System.out.println("Call method throwException on our MXBean");130131try {132mbsc.invoke(objName, "throwException", null, null);133errorCount++;134System.out.println("(ERROR) Did not get awaited MBeanException") ;135} catch (MBeanException mbe) {136System.out.println("(OK) Got awaited MBeanException") ;137Throwable cause = mbe.getCause();138139if ( cause instanceof java.lang.Exception ) {140System.out.println("(OK) Cause is of the right class") ;141String mess = cause.getMessage();142143if ( mess.equals(Basic.EXCEPTION_MESSAGE ) ) {144System.out.println("(OK) Cause message is fine") ;145} else {146errorCount++;147System.out.println("(ERROR) Cause has message "148+ cause.getMessage()149+ " as we expect "150+ Basic.EXCEPTION_MESSAGE) ;151}152} else {153errorCount++;154System.out.println("(ERROR) Cause is of class "155+ cause.getClass().getName()156+ " as we expect java.lang.Exception") ;157}158} catch (Exception e) {159errorCount++;160System.out.println("(ERROR) Did not get awaited MBeanException but "161+ e) ;162Utils.printThrowable(e, true);163}164System.out.println("---- DONE\n") ;165166// ----167System.out.println("Call method throwError on our MXBean");168169try {170mbsc.invoke(objName, "throwError", null, null);171errorCount++;172System.out.println("(ERROR) Did not get awaited RuntimeErrorException") ;173} catch (RuntimeErrorException ree) {174System.out.println("(OK) Got awaited RuntimeErrorException") ;175Throwable cause = ree.getCause();176177if ( cause instanceof java.lang.InternalError ) {178System.out.println("(OK) Cause is of the right class") ;179String mess = cause.getMessage();180181if ( mess.equals(Basic.EXCEPTION_MESSAGE ) ) {182System.out.println("(OK) Cause message is fine") ;183} else {184errorCount++;185System.out.println("(ERROR) Cause has message "186+ cause.getMessage()187+ " as we expect "188+ Basic.EXCEPTION_MESSAGE) ;189}190} else {191errorCount++;192System.out.println("(ERROR) Cause is of class "193+ cause.getClass().getName()194+ " as we expect java.lang.InternalError") ;195}196} catch (Exception e) {197errorCount++;198System.out.println("(ERROR) Did not get awaited RuntimeErrorException but "199+ e) ;200Utils.printThrowable(e, true);201}202System.out.println("---- DONE\n") ;203204// ----205System.out.println("Unregister the MBean");206mbsc.unregisterMBean(objName);207System.out.println("---- OK\n") ;208209Thread.sleep(timeForNotificationInSeconds * 1000);210int numOfReceivedNotif = notifList.size();211212if ( numOfReceivedNotif == numOfNotifications ) {213System.out.println("(OK) We received "214+ numOfNotifications215+ " Notifications") ;216} else {217errorCount++;218System.out.println("(ERROR) We received "219+ numOfReceivedNotif220+ " Notifications in place of "221+ numOfNotifications) ;222}223} catch(Exception e) {224Utils.printThrowable(e, true) ;225throw new RuntimeException(e);226}227228if ( errorCount == 0 ) {229System.out.println("MXBeanExceptionHandlingTest::run: Done without any error") ;230} else {231System.out.println("MXBeanExceptionHandlingTest::run: Done with "232+ errorCount233+ " error(s)") ;234throw new RuntimeException("errorCount = " + errorCount);235}236}237238public void handleNotification(Notification notification, Object handback) {239System.out.println("MXBeanExceptionHandlingTest::handleNotification: Received "240+ notification);241notifList.add(notification);242}243244}245246247