1 /* 2 * Copyright (c) 2005, 2015, 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 * @bug 8058865 27 * @summary Checks correct exception and error events from NotificationListener 28 * @author Olivier Lagneau 29 * 30 * @library /lib/testlibrary 31 * @modules java.management.rmi 32 * 33 * @compile Basic.java 34 * @run main/othervm/timeout=300 -DDEBUG_STANDARD MXBeanExceptionHandlingTest -timeForNotificationInSeconds 3 35 */ 36 37 38 import java.util.Map; 39 import java.util.HashMap; 40 import java.util.concurrent.ArrayBlockingQueue; 41 import java.util.concurrent.BlockingQueue; 42 43 import java.lang.management.ManagementFactory; 44 import javax.management.MBeanServer; 45 import javax.management.MBeanException; 46 import javax.management.MBeanServerDelegate; 47 import javax.management.Notification; 48 import javax.management.NotificationListener; 49 import javax.management.MBeanServerConnection; 50 import javax.management.ObjectName; 51 import javax.management.RuntimeErrorException; 52 import javax.management.remote.JMXConnector; 53 import javax.management.remote.JMXConnectorFactory; 54 import javax.management.remote.JMXConnectorServer; 55 import javax.management.remote.JMXConnectorServerFactory; 56 import javax.management.remote.JMXServiceURL; 57 58 public class MXBeanExceptionHandlingTest implements NotificationListener { 59 60 private static String BASIC_MXBEAN_CLASS_NAME = "Basic"; 61 62 private long timeForNotificationInSeconds = 3L; 63 private int numOfNotifications = 2; 64 private BlockingQueue<Notification> notifList = null; 65 66 67 /* 68 * First Debug properties and arguments are collect in expected 69 * map (argName, value) format, then calls original test's run method. 70 */ 71 public static void main(String args[]) throws Exception { 72 73 System.out.println("================================================="); 74 75 // Parses parameters 76 Utils.parseDebugProperties(); 77 Map<String, Object> map = Utils.parseParameters(args) ; 78 79 // Run test 80 MXBeanExceptionHandlingTest test = new MXBeanExceptionHandlingTest(); 81 test.run(map); 82 83 } 84 85 protected void parseArgs(Map<String, Object> args) throws Exception { 86 87 String arg = null; 88 89 // Init timeForNotificationInSeconds 90 // It is the maximum time in seconds we wait for a notification. 91 arg = (String)args.get("-timeForNotificationInSeconds") ; 92 if (arg != null) { 93 timeForNotificationInSeconds = (new Long(arg)).longValue(); 94 } 95 96 } 97 98 public void run(Map<String, Object> args) { 99 100 System.out.println("MXBeanExceptionHandlingTest::run: Start") ; 101 int errorCount = 0 ; 102 103 try { 104 parseArgs(args); 105 notifList = new ArrayBlockingQueue<Notification>(numOfNotifications); 106 107 // JMX MbeanServer used inside single VM as if remote. 108 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 109 110 JMXServiceURL url = new JMXServiceURL("rmi", null, 0); 111 JMXConnectorServer cs = 112 JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs); 113 cs.start(); 114 115 JMXServiceURL addr = cs.getAddress(); 116 JMXConnector cc = JMXConnectorFactory.connect(addr); 117 MBeanServerConnection mbsc = cc.getMBeanServerConnection(); 118 119 // ---- 120 System.out.println("Add me as notification listener"); 121 mbsc.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME, 122 this, null, null); 123 System.out.println("---- OK\n") ; 124 125 // ---- 126 System.out.println("Create and register the MBean"); 127 ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi") ; 128 mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName); 129 System.out.println("---- OK\n") ; 130 131 // ---- 132 System.out.println("Call method throwException on our MXBean"); 133 134 try { 135 mbsc.invoke(objName, "throwException", null, null); 136 errorCount++; 137 System.out.println("(ERROR) Did not get awaited MBeanException") ; 138 } catch (MBeanException mbe) { 139 System.out.println("(OK) Got awaited MBeanException") ; 140 Throwable cause = mbe.getCause(); 141 142 if ( cause instanceof java.lang.Exception ) { 143 System.out.println("(OK) Cause is of the right class") ; 144 String mess = cause.getMessage(); 145 146 if ( mess.equals(Basic.EXCEPTION_MESSAGE ) ) { 147 System.out.println("(OK) Cause message is fine") ; 148 } else { 149 errorCount++; 150 System.out.println("(ERROR) Cause has message " 151 + cause.getMessage() 152 + " as we expect " 153 + Basic.EXCEPTION_MESSAGE) ; 154 } 155 } else { 156 errorCount++; 157 System.out.println("(ERROR) Cause is of class " 158 + cause.getClass().getName() 159 + " as we expect java.lang.Exception") ; 160 } 161 } catch (Exception e) { 162 errorCount++; 163 System.out.println("(ERROR) Did not get awaited MBeanException but " 164 + e) ; 165 Utils.printThrowable(e, true); 166 } 167 System.out.println("---- DONE\n") ; 168 169 // ---- 170 System.out.println("Call method throwError on our MXBean"); 171 172 try { 173 mbsc.invoke(objName, "throwError", null, null); 174 errorCount++; 175 System.out.println("(ERROR) Did not get awaited RuntimeErrorException") ; 176 } catch (RuntimeErrorException ree) { 177 System.out.println("(OK) Got awaited RuntimeErrorException") ; 178 Throwable cause = ree.getCause(); 179 180 if ( cause instanceof java.lang.InternalError ) { 181 System.out.println("(OK) Cause is of the right class") ; 182 String mess = cause.getMessage(); 183 184 if ( mess.equals(Basic.EXCEPTION_MESSAGE ) ) { 185 System.out.println("(OK) Cause message is fine") ; 186 } else { 187 errorCount++; 188 System.out.println("(ERROR) Cause has message " 189 + cause.getMessage() 190 + " as we expect " 191 + Basic.EXCEPTION_MESSAGE) ; 192 } 193 } else { 194 errorCount++; 195 System.out.println("(ERROR) Cause is of class " 196 + cause.getClass().getName() 197 + " as we expect java.lang.InternalError") ; 198 } 199 } catch (Exception e) { 200 errorCount++; 201 System.out.println("(ERROR) Did not get awaited RuntimeErrorException but " 202 + e) ; 203 Utils.printThrowable(e, true); 204 } 205 System.out.println("---- DONE\n") ; 206 207 // ---- 208 System.out.println("Unregister the MBean"); 209 mbsc.unregisterMBean(objName); 210 System.out.println("---- OK\n") ; 211 212 Thread.sleep(timeForNotificationInSeconds * 1000); 213 int numOfReceivedNotif = notifList.size(); 214 215 if ( numOfReceivedNotif == numOfNotifications ) { 216 System.out.println("(OK) We received " 217 + numOfNotifications 218 + " Notifications") ; 219 } else { 220 errorCount++; 221 System.out.println("(ERROR) We received " 222 + numOfReceivedNotif 223 + " Notifications in place of " 224 + numOfNotifications) ; 225 } 226 } catch(Exception e) { 227 Utils.printThrowable(e, true) ; 228 throw new RuntimeException(e); 229 } 230 231 if ( errorCount == 0 ) { 232 System.out.println("MXBeanExceptionHandlingTest::run: Done without any error") ; 233 } else { 234 System.out.println("MXBeanExceptionHandlingTest::run: Done with " 235 + errorCount 236 + " error(s)") ; 237 throw new RuntimeException("errorCount = " + errorCount); 238 } 239 } 240 241 public void handleNotification(Notification notification, Object handback) { 242 System.out.println("MXBeanExceptionHandlingTest::handleNotification: Received " 243 + notification); 244 notifList.add(notification); 245 } 246 247 }