Path: blob/master/test/jdk/javax/management/remote/mandatory/notif/DiffHBTest.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 491172126* @summary test on add/remove NotificationListener27* @author Shanliang JIANG28*29* @run clean DiffHBTest30* @run build DiffHBTest31* @run main DiffHBTest32*/333435import javax.management.*;36import javax.management.remote.*;3738/**39* This test registeres an unique listener with two different handbacks,40* it expects to receive a same notification two times.41*/42public class DiffHBTest {43private static final String[] protocols = {"rmi", "iiop", "jmxmp"};4445private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();46private static ObjectName delegateName;47private static ObjectName timerName;4849public static final String[] hbs = new String[] {"0", "1"};5051public static void main(String[] args) throws Exception {52System.out.println(">>> test on one listener with two different handbacks.");5354delegateName = new ObjectName("JMImplementation:type=MBeanServerDelegate");55timerName = new ObjectName("MBean:name=Timer");5657String errors = "";5859for (int i = 0; i < protocols.length; i++) {60final String s = test(protocols[i]);61if (s != null) {62if ("".equals(errors)) {63errors = "Failed to " + protocols[i] + ": "+s;64} else {65errors = "\tFailed to " + protocols[i] + ": "+s;66}67}68}6970if ("".equals(errors)) {71System.out.println(">>> Passed!");72} else {73System.out.println(">>> Failed!");7475throw new RuntimeException(errors);76}77}7879private static String test(String proto) throws Exception {80System.out.println(">>> Test for protocol " + proto);81JMXServiceURL u = new JMXServiceURL(proto, null, 0);82JMXConnectorServer server;83JMXConnector client;8485try {86server =87JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);88server.start();89JMXServiceURL addr = server.getAddress();90client = JMXConnectorFactory.connect(addr, null);91} catch (Exception e) {92// not support93System.out.println(">>> not support: " + proto);94return null;95}9697MBeanServerConnection mserver = client.getMBeanServerConnection();9899System.out.print(">>>\t");100for (int i=0; i<5; i++) {101System.out.print(i + "\t");102final MyListener dummyListener = new MyListener();103mserver.addNotificationListener(104delegateName, dummyListener, null, hbs[0]);105mserver.addNotificationListener(106delegateName, dummyListener, null, hbs[1]);107108mserver.createMBean("javax.management.timer.Timer", timerName);109110long remainingTime = waitingTime;111final long startTime = System.currentTimeMillis();112113try {114synchronized(dummyListener) {115while (!dummyListener.done && remainingTime > 0) {116dummyListener.wait(remainingTime);117remainingTime = waitingTime -118(System.currentTimeMillis() - startTime);119}120121if (dummyListener.errorInfo != null) {122return dummyListener.errorInfo;123}124}125} finally {126//System.out.println("Unregister: "+i);127mserver.unregisterMBean(timerName);128mserver.removeNotificationListener(delegateName, dummyListener);129}130}131132System.out.println("");133client.close();134server.stop();135136return null;137}138139private static class MyListener implements NotificationListener {140public boolean done = false;141public String errorInfo = null;142143private int received = 0;144private MBeanServerNotification receivedNotif = null;145private Object receivedHB = null;146public void handleNotification(Notification n, Object o) {147if (!(n instanceof MBeanServerNotification)) {148failed("Received an unexpected notification: "+n);149return;150}151152if (!hbs[0].equals(o) && !hbs[1].equals(o)) {153failed("Unkown handback: "+o);154return;155}156157// what we need158final MBeanServerNotification msn = (MBeanServerNotification)n;159if (!(MBeanServerNotification.REGISTRATION_NOTIFICATION.equals(160msn.getType())) ||161!msn.getMBeanName().equals(timerName)) {162return;163}164165synchronized(this) {166received++;167168if (received == 1) { // first time169receivedNotif = msn;170receivedHB = o;171172return;173}174175if (received > 2) {176failed("Expect to receive 2 notifs, but get "+received);177178return;179}180181// second time182if (receivedHB.equals(o)) {183failed("Got same handback twice: "+o);184} else if(!hbs[0].equals(o) && !hbs[1].equals(o)) {185failed("Unknown handback: "+o);186} else if (receivedNotif.getSequenceNumber() !=187msn.getSequenceNumber()) {188failed("expected to receive:\n"189+receivedNotif190+"\n but got\n"+msn);191}192193// passed194done = true;195this.notify();196}197}198199private void failed(String errorInfo) {200this.errorInfo = errorInfo;201done = true;202203this.notify();204}205}206207private final static long waitingTime = 2000;208}209210211