Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/notif/NotSerializableNotifTest.java
38867 views
/*1* Copyright (c) 2004, 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* @summary Tests to send a not serializable notification.26* @bug 502219627* @author Shanliang JIANG28* @run clean NotSerializableNotifTest29* @run build NotSerializableNotifTest30* @run main NotSerializableNotifTest31*/3233// java imports34//35import java.net.MalformedURLException;36import javax.management.MBeanNotificationInfo;37import javax.management.MBeanServer;38import javax.management.MBeanServerConnection;39import javax.management.MBeanServerFactory;40import javax.management.Notification;41import javax.management.NotificationBroadcasterSupport;42import javax.management.NotificationListener;43import javax.management.ObjectName;44import javax.management.remote.JMXConnector;45import javax.management.remote.JMXConnectorFactory;46import javax.management.remote.JMXConnectorServer;47import javax.management.remote.JMXConnectorServerFactory;48import javax.management.remote.JMXServiceURL;4950public class NotSerializableNotifTest {51private static final MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer();52private static ObjectName emitter;5354private static String[] protocols;5556private static final int sentNotifs = 10;5758public static void main(String[] args) throws Exception {59System.out.println(">>> Test to send a not serializable notification");6061// IIOP fails on JDK1.4, see 503431862final String v = System.getProperty("java.version");63float f = Float.parseFloat(v.substring(0, 3));64if (f<1.5) {65protocols = new String[] {"rmi", "jmxmp"};66} else {67protocols = new String[] {"rmi", "iiop", "jmxmp"};68}6970emitter = new ObjectName("Default:name=NotificationEmitter");71mbeanServer.registerMBean(new NotificationEmitter(), emitter);7273for (int i = 0; i < protocols.length; i++) {74test(protocols[i]);75}7677System.out.println(">>> Test passed");78}798081private static void test(String proto) throws Exception {82System.out.println("\n>>> Test for protocol " + proto);8384JMXServiceURL url = new JMXServiceURL(proto, null, 0);8586System.out.println(">>> Create a server: "+url);8788JMXConnectorServer server = null;89try {90server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbeanServer);91} catch (MalformedURLException e) {92System.out.println("System does not recognize URL: " + url +93"; ignoring");94return;95}9697server.start();9899url = server.getAddress();100101System.out.println(">>> Creating a client connectint to: "+url);102JMXConnector conn = JMXConnectorFactory.connect(url, null);103MBeanServerConnection client = conn.getMBeanServerConnection();104105// add listener from the client side106Listener listener = new Listener();107client.addNotificationListener(emitter, listener, null, null);108109// ask to send one not serializable notif110Object[] params = new Object[] {new Integer(1)};111String[] signatures = new String[] {"java.lang.Integer"};112client.invoke(emitter, "sendNotserializableNotifs", params, signatures);113114// listener clean115client.removeNotificationListener(emitter, listener);116listener = new Listener();117client.addNotificationListener(emitter, listener, null, null);118119//ask to send serializable notifs120params = new Object[] {new Integer(sentNotifs)};121client.invoke(emitter, "sendNotifications", params, signatures);122123// waiting ...124synchronized (listener) {125while (listener.received() < sentNotifs) {126listener.wait(); // either pass or test timeout (killed by test harness)127128}129}130131// clean132client.removeNotificationListener(emitter, listener);133134conn.close();135server.stop();136}137138//--------------------------139// private classes140//--------------------------141142private static class Listener implements NotificationListener {143public void handleNotification(Notification notif, Object handback) {144synchronized (this) {145if(++receivedNotifs == sentNotifs) {146this.notifyAll();147}148}149}150151public int received() {152return receivedNotifs;153}154155private int receivedNotifs = 0;156}157158public static class NotificationEmitter extends NotificationBroadcasterSupport159implements NotificationEmitterMBean {160161public MBeanNotificationInfo[] getNotificationInfo() {162final String[] ntfTypes = {myType};163164final MBeanNotificationInfo[] ntfInfoArray = {165new MBeanNotificationInfo(ntfTypes,166"javax.management.Notification",167"Notifications sent by the NotificationEmitter")};168169return ntfInfoArray;170}171172/**173* Send not serializable Notifications.174*175* @param nb The number of notifications to send176*/177public void sendNotserializableNotifs(Integer nb) {178179Notification notif;180for (int i=1; i<=nb.intValue(); i++) {181notif = new Notification(myType, this, i);182183notif.setUserData(new Object());184sendNotification(notif);185}186}187188/**189* Send Notification objects.190*191* @param nb The number of notifications to send192*/193public void sendNotifications(Integer nb) {194Notification notif;195for (int i=1; i<=nb.intValue(); i++) {196notif = new Notification(myType, this, i);197198sendNotification(notif);199}200}201202private final String myType = "notification.my_notification";203}204205public interface NotificationEmitterMBean {206public void sendNotifications(Integer nb);207208public void sendNotserializableNotifs(Integer nb);209}210}211212213