Path: blob/master/test/jdk/javax/management/remote/mandatory/notif/NotSerializableNotifTest.java
41155 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* @bug 5022196 813200326* @summary Tests to send a not serializable notification.27* @author Shanliang JIANG28*29* @run clean NotSerializableNotifTest30* @run build NotSerializableNotifTest31* @run main NotSerializableNotifTest32*/3334// java imports35//36import java.net.MalformedURLException;37import javax.management.MBeanNotificationInfo;38import javax.management.MBeanServer;39import javax.management.MBeanServerConnection;40import javax.management.MBeanServerFactory;41import javax.management.Notification;42import javax.management.NotificationBroadcasterSupport;43import javax.management.NotificationListener;44import javax.management.ObjectName;45import javax.management.remote.JMXConnector;46import javax.management.remote.JMXConnectorFactory;47import javax.management.remote.JMXConnectorServer;48import javax.management.remote.JMXConnectorServerFactory;49import javax.management.remote.JMXServiceURL;5051public class NotSerializableNotifTest {52private static final MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer();53private static ObjectName emitter;5455private static String[] protocols = new String[] {"rmi", "iiop", "jmxmp"};5657private static final int sentNotifs = 10;5859public static void main(String[] args) throws Exception {60System.out.println(">>> Test to send a not serializable notification");6162emitter = new ObjectName("Default:name=NotificationEmitter");63mbeanServer.registerMBean(new NotificationEmitter(), emitter);6465for (int i = 0; i < protocols.length; i++) {66test(protocols[i]);67}6869System.out.println(">>> Test passed");70}717273private static void test(String proto) throws Exception {74System.out.println("\n>>> Test for protocol " + proto);7576JMXServiceURL url = new JMXServiceURL(proto, null, 0);7778System.out.println(">>> Create a server: "+url);7980JMXConnectorServer server = null;81try {82server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbeanServer);83} catch (MalformedURLException e) {84System.out.println("System does not recognize URL: " + url +85"; ignoring");86return;87}8889server.start();9091url = server.getAddress();9293System.out.println(">>> Creating a client connectint to: "+url);94JMXConnector conn = JMXConnectorFactory.connect(url, null);95MBeanServerConnection client = conn.getMBeanServerConnection();9697// add listener from the client side98Listener listener = new Listener();99client.addNotificationListener(emitter, listener, null, null);100101// ask to send one not serializable notif102Object[] params = new Object[] {new Integer(1)};103String[] signatures = new String[] {"java.lang.Integer"};104client.invoke(emitter, "sendNotserializableNotifs", params, signatures);105106// listener clean107client.removeNotificationListener(emitter, listener);108listener = new Listener();109client.addNotificationListener(emitter, listener, null, null);110111//ask to send serializable notifs112params = new Object[] {new Integer(sentNotifs)};113client.invoke(emitter, "sendNotifications", params, signatures);114115// waiting ...116synchronized (listener) {117while (listener.received() < sentNotifs) {118listener.wait(); // either pass or test timeout (killed by test harness)119120}121}122123// clean124client.removeNotificationListener(emitter, listener);125126conn.close();127server.stop();128}129130//--------------------------131// private classes132//--------------------------133134private static class Listener implements NotificationListener {135public void handleNotification(Notification notif, Object handback) {136synchronized (this) {137if(++receivedNotifs == sentNotifs) {138this.notifyAll();139}140}141}142143public int received() {144return receivedNotifs;145}146147private int receivedNotifs = 0;148}149150public static class NotificationEmitter extends NotificationBroadcasterSupport151implements NotificationEmitterMBean {152153public MBeanNotificationInfo[] getNotificationInfo() {154final String[] ntfTypes = {myType};155156final MBeanNotificationInfo[] ntfInfoArray = {157new MBeanNotificationInfo(ntfTypes,158"javax.management.Notification",159"Notifications sent by the NotificationEmitter")};160161return ntfInfoArray;162}163164/**165* Send not serializable Notifications.166*167* @param nb The number of notifications to send168*/169public void sendNotserializableNotifs(Integer nb) {170171Notification notif;172for (int i=1; i<=nb.intValue(); i++) {173notif = new Notification(myType, this, i);174175notif.setUserData(new Object());176sendNotification(notif);177}178}179180/**181* Send Notification objects.182*183* @param nb The number of notifications to send184*/185public void sendNotifications(Integer nb) {186Notification notif;187for (int i=1; i<=nb.intValue(); i++) {188notif = new Notification(myType, this, i);189190sendNotification(notif);191}192}193194private final String myType = "notification.my_notification";195}196197public interface NotificationEmitterMBean {198public void sendNotifications(Integer nb);199200public void sendNotserializableNotifs(Integer nb);201}202}203204205