Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/ObjectInstance/MBeanInfoFailTest.java
38838 views
/*1* Copyright (c) 2004, 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 500185726* @summary Test queryNames() and queryMBeans() with a buggy DynamicMBean27* @author Daniel Fuchs28* @run clean MBeanInfoFailTest29* @run build MBeanInfoFailTest30* @run main MBeanInfoFailTest31*/3233import javax.management.*;34import java.util.*;3536public class MBeanInfoFailTest {3738public static class UnspeakableException extends RuntimeException {39public UnspeakableException(String unspeakableMessage) {40super(unspeakableMessage);41}42}4344public interface ThornyDevilMBean {45public boolean isDormant();46public void setDormant(boolean sleep);47}4849public static class ThornyDevil50extends StandardMBean implements ThornyDevilMBean {51private boolean sleep=true;52public ThornyDevil() throws NotCompliantMBeanException {53super(ThornyDevilMBean.class);54}55public boolean isDormant() {56return this.sleep;57}58public void setDormant(boolean sleep) {59this.sleep = sleep;60}61public MBeanInfo getMBeanInfo() {62if (isDormant()) return super.getMBeanInfo();63throw new UnspeakableException("The Thorny Devil has awoken!");64}65}6667public static void printInstances(Set instances) {68for (Iterator it1 = instances.iterator(); it1.hasNext();) {69final ObjectInstance oi = (ObjectInstance)it1.next();70final ObjectName on = oi.getObjectName();71final String cn = oi.getClassName();72System.err.println(String.valueOf(on) + ": class is " + cn);73}74}7576public static void main(String[] args) throws Exception {77System.out.println("Test queryNames() and queryMBeans() with a "+78"buggy DynamicMBean");79try {80final MBeanServer server = MBeanServerFactory.createMBeanServer();8182final ObjectName troubleKeeper =83new ObjectName("ThornyDevil:name=TroubleKeeper");84final ObjectName troubleMaker =85new ObjectName("ThornyDevil:name=TroubleMaker");86final ObjectName thornyPattern =87new ObjectName("ThornyDevil:*");8889server.createMBean(ThornyDevil.class.getName(),90troubleKeeper);91server.createMBean(ThornyDevil.class.getName(),92troubleMaker);9394final MBeanInfo info1 = server.getMBeanInfo(troubleKeeper);95System.out.println(String.valueOf(troubleKeeper)+" is a " +96info1.getClassName());97final MBeanInfo info2 = server.getMBeanInfo(troubleMaker);98System.out.println(String.valueOf(troubleMaker)+" is a " +99info2.getClassName());100final Set thorny1 = server.queryNames(thornyPattern,null);101if (thorny1.size() != 2) {102System.err.println("queryNames(): " +103"Expected to find 2 ThornyDevils before" +104" trouble started! ");105System.err.println("Found "+thorny1.size()+" instead: "+106thorny1);107System.exit(1);108}109final Set thornyM1 = server.queryMBeans(thornyPattern,null);110if (thornyM1.size() != 2) {111System.err.println("queryMBeans(): " +112"Expected to find 2 ThornyDevils before" +113" trouble started! ");114System.err.println("Found "+thornyM1.size()+" instead: ");115printInstances(thornyM1);116System.exit(2);117}118for (Iterator it1 = thornyM1.iterator(); it1.hasNext();) {119final ObjectInstance oi = (ObjectInstance)it1.next();120final ObjectName on = oi.getObjectName();121final String cn = oi.getClassName();122if (cn == null || !ThornyDevil.class.getName().123equals(cn)) {124System.err.println("Expected no trouble yet!");125System.err.println(String.valueOf(on) + ": class is " +126cn);127System.exit(3);128}129System.out.println(String.valueOf(on) + ": class is " + cn);130}131132System.out.println("Starting trouble with "+troubleMaker+" ...");133ThornyDevilMBean troubleMakerproxy =134(ThornyDevilMBean) MBeanServerInvocationHandler.135newProxyInstance(server, troubleMaker, ThornyDevilMBean.class,136false);137troubleMakerproxy.setDormant(false);138139try {140final MBeanInfo mbi = server.getMBeanInfo(troubleMaker);141System.err.println("No trouble started!: " + mbi);142System.exit(2);143} catch (Exception x) {144if (x.getCause() instanceof UnspeakableException)145System.out.println("Trouble started as expected: "146+ x.getCause());147else {148System.err.println("Unexpected trouble: " + x.getCause());149x.printStackTrace();150System.exit(3);151}152}153154final Set thorny2 = server.queryNames(thornyPattern,null);155if (thorny2.size() != 2) {156System.err.println("Expected to find 2 ThornyDevils after" +157" trouble started! ");158System.err.println("Found "+thorny2.size()+" instead: "+159thorny2);160System.exit(4);161}162163final Set thornyM2 = server.queryMBeans(thornyPattern,null);164if (thornyM2.size() != 2) {165System.err.println("queryMBeans(): " +166"Expected to find 2 ThornyDevils after" +167" trouble started! ");168System.err.println("Found "+thornyM2.size()+" instead: ");169printInstances(thornyM2);170System.exit(5);171}172for (Iterator it1 = thornyM2.iterator(); it1.hasNext();) {173final ObjectInstance oi = (ObjectInstance)it1.next();174final ObjectName on = oi.getObjectName();175final String cn = oi.getClassName();176if (!on.equals(troubleMaker)) {177if (cn == null || !ThornyDevil.class.getName().178equals(cn)) {179System.err.println("Expected no trouble for " + on);180System.err.println(String.valueOf(on) + ": class is " +181cn);182System.exit(6);183}184} else {185if (cn != null) {186System.err.println("Expected trouble for " + on);187System.err.println(String.valueOf(on) + ": class is " +188cn);189System.exit(7);190}191}192System.out.println(String.valueOf(on) + ": class is " + cn);193}194195System.out.println("Ahahah! " + troubleMaker +196" successfully thwarted!");197} catch( Exception x) {198System.err.println("Unexpected exception: " + x);199x.printStackTrace();200System.exit(8);201}202}203204}205206207