J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any 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 | * @run clean ConnectionListenerNullTest |
| 30 | * @run build ConnectionListenerNullTest |
| 31 | * @run main ConnectionListenerNullTest |
| 32 | */ |
| 33 | import javax.management.remote.JMXConnector; |
| 34 | import javax.management.remote.JMXServiceURL; |
| 35 | import javax.management.remote.JMXConnectorFactory; |
| 36 | import javax.management.Notification; |
| 37 | import javax.management.NotificationFilter; |
| 38 | import javax.management.NotificationListener; |
| 39 | import java.util.Map; |
| 40 | public class ConnectionListenerNullTest { |
| 41 | |
| 42 | static final boolean optionalFlag; |
| 43 | static { |
| 44 | Class genericClass = null; |
| 45 | try { |
| 46 | genericClass = |
| 47 | Class.forName("javax.management.remote.generic.GenericConnector"); |
| 48 | } catch (ClassNotFoundException x) { |
| 49 | // NO optional package |
| 50 | } |
| 51 | optionalFlag = (genericClass != null); |
| 52 | } |
| 53 | |
| 54 | final static String[] mandatoryList = { |
| 55 | "service:jmx:rmi://", "service:jmx:iiop://" |
| 56 | }; |
| 57 | |
| 58 | final static String[] optionalList = { |
| 59 | "service:jmx:jmxmp://" |
| 60 | }; |
| 61 | |
| 62 | public static int test(String[] urls) { |
| 63 | int errCount = 0; |
| 64 | for (int i=0;i<urls.length;i++) { |
| 65 | try { |
| 66 | final JMXServiceURL url = new JMXServiceURL(urls[i]); |
| 67 | final JMXConnector c = |
| 68 | JMXConnectorFactory.newJMXConnector(url,(Map)null); |
| 69 | final NotificationListener nl = null; |
| 70 | final NotificationFilter nf = null; |
| 71 | final Object h = null; |
| 72 | System.out.println("Testing " + c.getClass().getName()); |
| 73 | try { |
| 74 | System.out.println( |
| 75 | "addConnectionNotificationListener(null,null,null)"); |
| 76 | c.addConnectionNotificationListener(nl,nf,h); |
| 77 | throw new AssertionError("No exception raised"); |
| 78 | } catch (NullPointerException npe) { |
| 79 | // OK. |
| 80 | } |
| 81 | final NotificationListener listener = new |
| 82 | NotificationListener() { |
| 83 | public void handleNotification(Notification notification, |
| 84 | Object handback) { |
| 85 | } |
| 86 | }; |
| 87 | c.addConnectionNotificationListener(listener,nf,h); |
| 88 | try { |
| 89 | System.out.println( |
| 90 | "removeConnectionNotificationListener(null)"); |
| 91 | c.removeConnectionNotificationListener(nl); |
| 92 | throw new AssertionError("No exception raised"); |
| 93 | } catch (NullPointerException npe) { |
| 94 | // OK. |
| 95 | } |
| 96 | try { |
| 97 | System.out.println( |
| 98 | "removeConnectionNotificationListener(null,null,null)"); |
| 99 | c.removeConnectionNotificationListener(nl,nf,h); |
| 100 | throw new AssertionError("No exception raised"); |
| 101 | } catch (NullPointerException npe) { |
| 102 | // OK. |
| 103 | } |
| 104 | c.removeConnectionNotificationListener(listener); |
| 105 | System.out.println(c.getClass().getName() + |
| 106 | " successfully tested."); |
| 107 | } catch (Exception x) { |
| 108 | System.err.println("Unexpected exception for " + |
| 109 | urls[i] + ": " + x); |
| 110 | x.printStackTrace(); |
| 111 | errCount++; |
| 112 | } catch (AssertionError e) { |
| 113 | System.err.println("Unexpected assertion error for " + |
| 114 | urls[i] + ": " + e); |
| 115 | e.printStackTrace(); |
| 116 | errCount++; |
| 117 | } |
| 118 | } |
| 119 | return errCount; |
| 120 | } |
| 121 | |
| 122 | public static void main(String args[]) { |
| 123 | int errCount = 0; |
| 124 | errCount += test(mandatoryList); |
| 125 | if (optionalFlag) errCount += test(optionalList); |
| 126 | if (errCount > 0) { |
| 127 | System.err.println("ConnectionListenerNullTest failed: " + |
| 128 | errCount + " error(s) reported."); |
| 129 | System.exit(1); |
| 130 | } |
| 131 | System.out.println("ConnectionListenerNullTest passed."); |
| 132 | } |
| 133 | } |