1 /* 2 * Copyright (c) 2003, 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 4943248 27 * @summary Tests that NullPointerException is thrown when listener is null. 28 * @author Daniel Fuchs 29 * @modules java.management.rmi 30 * @run clean ConnectionListenerNullTest 31 * @run build ConnectionListenerNullTest 32 * @run main ConnectionListenerNullTest 33 */ 34 import javax.management.remote.JMXConnector; 35 import javax.management.remote.JMXServiceURL; 36 import javax.management.remote.JMXConnectorFactory; 37 import javax.management.Notification; 38 import javax.management.NotificationFilter; 39 import javax.management.NotificationListener; 40 import java.util.Map; 41 public class ConnectionListenerNullTest { 42 43 static boolean isPresent(String cn) { 44 try { 45 Class.forName(cn); 46 return true; 47 } catch (ClassNotFoundException x) { 48 return false; 49 } 50 } 51 52 public static int test(String... urls) { 53 int errCount = 0; 54 for (int i=0;i<urls.length;i++) { 55 try { 56 final JMXServiceURL url = new JMXServiceURL(urls[i]); 57 final JMXConnector c = 58 JMXConnectorFactory.newJMXConnector(url,(Map<String,String>)null); 59 final NotificationListener nl = null; 60 final NotificationFilter nf = null; 61 final Object h = null; 62 System.out.println("Testing " + c.getClass().getName()); 63 try { 64 System.out.println( 65 "addConnectionNotificationListener(null,null,null)"); 66 c.addConnectionNotificationListener(nl,nf,h); 67 throw new AssertionError("No exception raised"); 68 } catch (NullPointerException npe) { 69 // OK. 70 } 71 final NotificationListener listener = new 72 NotificationListener() { 73 public void handleNotification(Notification notification, 74 Object handback) { 75 } 76 }; 77 c.addConnectionNotificationListener(listener,nf,h); 78 try { 79 System.out.println( 80 "removeConnectionNotificationListener(null)"); 81 c.removeConnectionNotificationListener(nl); 82 throw new AssertionError("No exception raised"); 83 } catch (NullPointerException npe) { 84 // OK. 85 } 86 try { 87 System.out.println( 88 "removeConnectionNotificationListener(null,null,null)"); 89 c.removeConnectionNotificationListener(nl,nf,h); 90 throw new AssertionError("No exception raised"); 91 } catch (NullPointerException npe) { 92 // OK. 93 } 94 c.removeConnectionNotificationListener(listener); 95 System.out.println(c.getClass().getName() + 96 " successfully tested."); 97 } catch (Exception x) { 98 System.err.println("Unexpected exception for " + 99 urls[i] + ": " + x); 100 x.printStackTrace(); 101 errCount++; 102 } catch (AssertionError e) { 103 System.err.println("Unexpected assertion error for " + 104 urls[i] + ": " + e); 105 e.printStackTrace(); 106 errCount++; 107 } 108 } 109 return errCount; 110 } 111 112 public static void main(String args[]) { 113 int errCount = 0; 114 115 // mandatory 116 errCount += test("service:jmx:rmi://"); 117 118 // optional 119 if (isPresent("javax.management.remote.rmi._RMIConnectionImpl_Tie")) 120 errCount += test("service:jmx:iiop://"); 121 if (isPresent("javax.management.remote.generic.GenericConnector")) 122 errCount += test("service:jmx:jmxmp://"); 123 124 if (errCount > 0) { 125 throw new RuntimeException("ConnectionListenerNullTest failed: " + 126 errCount + " error(s) reported."); 127 } 128 System.out.println("ConnectionListenerNullTest passed."); 129 } 130 }