Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/notif/NotificationBufferCreationTest.java
38867 views
/*1* Copyright (c) 2003, 2007, 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* @run clean NotificationBufferCreationTest NotificationSender29* @run build NotificationBufferCreationTest30* @run main NotificationBufferCreationTest31*/32import java.net.MalformedURLException;3334import javax.management.MBeanServerFactory;35import javax.management.MBeanServer;36import javax.management.MBeanServerConnection;37import javax.management.NotificationListener;38import javax.management.ObjectName;39import javax.management.Notification;4041import javax.management.remote.JMXConnector;42import javax.management.remote.JMXConnectorFactory;43import javax.management.remote.JMXConnectorServer;44import javax.management.remote.JMXConnectorServerFactory;45import javax.management.remote.JMXServiceURL;4647public class NotificationBufferCreationTest {48private static final MBeanServer mbs =49MBeanServerFactory.createMBeanServer();50private static final String[] protocols = {"rmi", "iiop", "jmxmp"};51public static void main(String[] args) {52try {53boolean error = false;54ObjectName notifierName =55new ObjectName("TestDomain:type=NotificationSender");5657NotificationSender s = new NotificationSender();58mbs.registerMBean(s, notifierName);5960for(int i = 0; i < protocols.length; i++) {61try {62System.out.println("dotest for " + protocols[i]);63dotest(protocols[i], s, notifierName);64}catch(Exception e) {65e.printStackTrace();66error = true;67}68}6970if(error)71System.exit(1);7273System.out.println("Test OK");7475}catch(Exception e) {76e.printStackTrace();77System.exit(1);78}79}80private static void dotest(String protocol,81NotificationSender s,82ObjectName notifierName) throws Exception {83JMXConnector client = null;84JMXConnectorServer server = null;85JMXServiceURL u = null;86try {87u = new JMXServiceURL(protocol, null, 0);88server =89JMXConnectorServerFactory.newJMXConnectorServer(u,90null,91mbs);92checkNotifier(s, 0, "new ConnectorServer");9394server.start();9596checkNotifier(s, 0, "ConnectorServer start");9798JMXServiceURL addr = server.getAddress();99client = JMXConnectorFactory.newJMXConnector(addr, null);100101checkNotifier(s, 0, "new Connector");102103client.connect(null);104105checkNotifier(s, 0, "Connector connect");106107MBeanServerConnection mbsc = client.getMBeanServerConnection();108109final NotificationListener dummyListener =110new NotificationListener() {111public void handleNotification(Notification n,112Object o) {113// do nothing114return;115}116};117118mbsc.addNotificationListener(notifierName,119dummyListener,120null,121null);122123// 1 Listener is expected to be added by the ServerNotifForwader124checkNotifier(s, 1, "addNotificationListener");125126mbsc.removeNotificationListener(notifierName,127dummyListener);128System.out.println("Test OK for " + protocol);129}catch(MalformedURLException e) {130System.out.println("Skipping URL " + u);131}132finally {133if(client != null)134client.close();135if(server != null)136server.stop();137}138}139140private static void checkNotifier(NotificationSender s,141int expectedListenerCount,142String msg) throws Exception {143int count = s.getListenerCount();144if(count != expectedListenerCount) {145String errorMsg = "Invalid expected listener count [" +146expectedListenerCount + "], real [" + count +"] for " + msg;147System.out.println(errorMsg);148throw new Exception(errorMsg);149}150151}152}153154155