Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/remote/mandatory/notif/DiffHBTest.java
38867 views
/*1* Copyright (c) 2003, 2008, 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* @run clean DiffHBTest29* @run build DiffHBTest30* @run main DiffHBTest31*/323334import javax.management.*;35import javax.management.remote.*;3637/**38* This test registeres an unique listener with two different handbacks,39* it expects to receive a same notification two times.40*/41public class DiffHBTest {42private static final String[] protocols = {"rmi", "iiop", "jmxmp"};4344private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();45private static ObjectName delegateName;46private static ObjectName timerName;4748public static final String[] hbs = new String[] {"0", "1"};4950public static void main(String[] args) throws Exception {51System.out.println(">>> test on one listener with two different handbacks.");5253delegateName = new ObjectName("JMImplementation:type=MBeanServerDelegate");54timerName = new ObjectName("MBean:name=Timer");5556String errors = "";5758for (int i = 0; i < protocols.length; i++) {59final String s = test(protocols[i]);60if (s != null) {61if ("".equals(errors)) {62errors = "Failed to " + protocols[i] + ": "+s;63} else {64errors = "\tFailed to " + protocols[i] + ": "+s;65}66}67}6869if ("".equals(errors)) {70System.out.println(">>> Passed!");71} else {72System.out.println(">>> Failed!");7374throw new RuntimeException(errors);75}76}7778private static String test(String proto) throws Exception {79System.out.println(">>> Test for protocol " + proto);80JMXServiceURL u = new JMXServiceURL(proto, null, 0);81JMXConnectorServer server;82JMXConnector client;8384try {85server =86JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);87server.start();88JMXServiceURL addr = server.getAddress();89client = JMXConnectorFactory.connect(addr, null);90} catch (Exception e) {91// not support92System.out.println(">>> not support: " + proto);93return null;94}9596MBeanServerConnection mserver = client.getMBeanServerConnection();9798System.out.print(">>>\t");99for (int i=0; i<5; i++) {100System.out.print(i + "\t");101final MyListener dummyListener = new MyListener();102mserver.addNotificationListener(103delegateName, dummyListener, null, hbs[0]);104mserver.addNotificationListener(105delegateName, dummyListener, null, hbs[1]);106107mserver.createMBean("javax.management.timer.Timer", timerName);108109long remainingTime = waitingTime;110final long startTime = System.currentTimeMillis();111112try {113synchronized(dummyListener) {114while (!dummyListener.done && remainingTime > 0) {115dummyListener.wait(remainingTime);116remainingTime = waitingTime -117(System.currentTimeMillis() - startTime);118}119120if (dummyListener.errorInfo != null) {121return dummyListener.errorInfo;122}123}124} finally {125//System.out.println("Unregister: "+i);126mserver.unregisterMBean(timerName);127mserver.removeNotificationListener(delegateName, dummyListener);128}129}130131System.out.println("");132client.close();133server.stop();134135return null;136}137138private static class MyListener implements NotificationListener {139public boolean done = false;140public String errorInfo = null;141142private int received = 0;143private MBeanServerNotification receivedNotif = null;144private Object receivedHB = null;145public void handleNotification(Notification n, Object o) {146if (!(n instanceof MBeanServerNotification)) {147failed("Received an unexpected notification: "+n);148return;149}150151if (!hbs[0].equals(o) && !hbs[1].equals(o)) {152failed("Unkown handback: "+o);153return;154}155156// what we need157final MBeanServerNotification msn = (MBeanServerNotification)n;158if (!(MBeanServerNotification.REGISTRATION_NOTIFICATION.equals(159msn.getType())) ||160!msn.getMBeanName().equals(timerName)) {161return;162}163164synchronized(this) {165received++;166167if (received == 1) { // first time168receivedNotif = msn;169receivedHB = o;170171return;172}173174if (received > 2) {175failed("Expect to receive 2 notifs, but get "+received);176177return;178}179180// second time181if (receivedHB.equals(o)) {182failed("Got same handback twice: "+o);183} else if(!hbs[0].equals(o) && !hbs[1].equals(o)) {184failed("Unknown handback: "+o);185} else if (receivedNotif.getSequenceNumber() !=186msn.getSequenceNumber()) {187failed("expected to receive:\n"188+receivedNotif189+"\n but got\n"+msn);190}191192// passed193done = true;194this.notify();195}196}197198private void failed(String errorInfo) {199this.errorInfo = errorInfo;200done = true;201202this.notify();203}204}205206private final static long waitingTime = 2000;207}208209210