Path: blob/master/test/jdk/javax/management/remote/mandatory/notif/NotificationBufferCreationTest.java
41155 views
/*1* Copyright (c) 2003, 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 493423626* @summary Tests that NotificationBuffer is created when used.27* @author jfd@...28*29* @run clean NotificationBufferCreationTest NotificationSender30* @run build NotificationBufferCreationTest31* @run main NotificationBufferCreationTest32*/33import java.net.MalformedURLException;3435import javax.management.MBeanServerFactory;36import javax.management.MBeanServer;37import javax.management.MBeanServerConnection;38import javax.management.NotificationListener;39import javax.management.ObjectName;40import javax.management.Notification;4142import javax.management.remote.JMXConnector;43import javax.management.remote.JMXConnectorFactory;44import javax.management.remote.JMXConnectorServer;45import javax.management.remote.JMXConnectorServerFactory;46import javax.management.remote.JMXServiceURL;4748public class NotificationBufferCreationTest {49private static final MBeanServer mbs =50MBeanServerFactory.createMBeanServer();51private static final String[] protocols = {"rmi", "iiop", "jmxmp"};52public static void main(String[] args) {53try {54boolean error = false;55ObjectName notifierName =56new ObjectName("TestDomain:type=NotificationSender");5758NotificationSender s = new NotificationSender();59mbs.registerMBean(s, notifierName);6061for(int i = 0; i < protocols.length; i++) {62try {63System.out.println("dotest for " + protocols[i]);64dotest(protocols[i], s, notifierName);65}catch(Exception e) {66e.printStackTrace();67error = true;68}69}7071if(error)72System.exit(1);7374System.out.println("Test OK");7576}catch(Exception e) {77e.printStackTrace();78System.exit(1);79}80}81private static void dotest(String protocol,82NotificationSender s,83ObjectName notifierName) throws Exception {84JMXConnector client = null;85JMXConnectorServer server = null;86JMXServiceURL u = null;87try {88u = new JMXServiceURL(protocol, null, 0);89server =90JMXConnectorServerFactory.newJMXConnectorServer(u,91null,92mbs);93checkNotifier(s, 0, "new ConnectorServer");9495server.start();9697checkNotifier(s, 0, "ConnectorServer start");9899JMXServiceURL addr = server.getAddress();100client = JMXConnectorFactory.newJMXConnector(addr, null);101102checkNotifier(s, 0, "new Connector");103104client.connect(null);105106checkNotifier(s, 0, "Connector connect");107108MBeanServerConnection mbsc = client.getMBeanServerConnection();109110final NotificationListener dummyListener =111new NotificationListener() {112public void handleNotification(Notification n,113Object o) {114// do nothing115return;116}117};118119mbsc.addNotificationListener(notifierName,120dummyListener,121null,122null);123124// 1 Listener is expected to be added by the ServerNotifForwader125checkNotifier(s, 1, "addNotificationListener");126127mbsc.removeNotificationListener(notifierName,128dummyListener);129System.out.println("Test OK for " + protocol);130}catch(MalformedURLException e) {131System.out.println("Skipping URL " + u);132}133finally {134if(client != null)135client.close();136if(server != null)137server.stop();138}139}140141private static void checkNotifier(NotificationSender s,142int expectedListenerCount,143String msg) throws Exception {144int count = s.getListenerCount();145if(count != expectedListenerCount) {146String errorMsg = "Invalid expected listener count [" +147expectedListenerCount + "], real [" + count +"] for " + msg;148System.out.println(errorMsg);149throw new Exception(errorMsg);150}151152}153}154155156