Path: blob/master/test/jdk/javax/management/ObjectInstance/MBeanInfoFailTest.java
41149 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 500185726* @summary Test queryNames() and queryMBeans() with a buggy DynamicMBean27* @author Daniel Fuchs28*29* @run clean MBeanInfoFailTest30* @run build MBeanInfoFailTest31* @run main MBeanInfoFailTest32*/3334import javax.management.*;35import java.util.*;3637public class MBeanInfoFailTest {3839public static class UnspeakableException extends RuntimeException {40public UnspeakableException(String unspeakableMessage) {41super(unspeakableMessage);42}43}4445public interface ThornyDevilMBean {46public boolean isDormant();47public void setDormant(boolean sleep);48}4950public static class ThornyDevil51extends StandardMBean implements ThornyDevilMBean {52private boolean sleep=true;53public ThornyDevil() throws NotCompliantMBeanException {54super(ThornyDevilMBean.class);55}56public boolean isDormant() {57return this.sleep;58}59public void setDormant(boolean sleep) {60this.sleep = sleep;61}62public MBeanInfo getMBeanInfo() {63if (isDormant()) return super.getMBeanInfo();64throw new UnspeakableException("The Thorny Devil has awoken!");65}66}6768public static void printInstances(Set instances) {69for (Iterator it1 = instances.iterator(); it1.hasNext();) {70final ObjectInstance oi = (ObjectInstance)it1.next();71final ObjectName on = oi.getObjectName();72final String cn = oi.getClassName();73System.err.println(String.valueOf(on) + ": class is " + cn);74}75}7677public static void main(String[] args) throws Exception {78System.out.println("Test queryNames() and queryMBeans() with a "+79"buggy DynamicMBean");80try {81final MBeanServer server = MBeanServerFactory.createMBeanServer();8283final ObjectName troubleKeeper =84new ObjectName("ThornyDevil:name=TroubleKeeper");85final ObjectName troubleMaker =86new ObjectName("ThornyDevil:name=TroubleMaker");87final ObjectName thornyPattern =88new ObjectName("ThornyDevil:*");8990server.createMBean(ThornyDevil.class.getName(),91troubleKeeper);92server.createMBean(ThornyDevil.class.getName(),93troubleMaker);9495final MBeanInfo info1 = server.getMBeanInfo(troubleKeeper);96System.out.println(String.valueOf(troubleKeeper)+" is a " +97info1.getClassName());98final MBeanInfo info2 = server.getMBeanInfo(troubleMaker);99System.out.println(String.valueOf(troubleMaker)+" is a " +100info2.getClassName());101final Set thorny1 = server.queryNames(thornyPattern,null);102if (thorny1.size() != 2) {103System.err.println("queryNames(): " +104"Expected to find 2 ThornyDevils before" +105" trouble started! ");106System.err.println("Found "+thorny1.size()+" instead: "+107thorny1);108System.exit(1);109}110final Set thornyM1 = server.queryMBeans(thornyPattern,null);111if (thornyM1.size() != 2) {112System.err.println("queryMBeans(): " +113"Expected to find 2 ThornyDevils before" +114" trouble started! ");115System.err.println("Found "+thornyM1.size()+" instead: ");116printInstances(thornyM1);117System.exit(2);118}119for (Iterator it1 = thornyM1.iterator(); it1.hasNext();) {120final ObjectInstance oi = (ObjectInstance)it1.next();121final ObjectName on = oi.getObjectName();122final String cn = oi.getClassName();123if (cn == null || !ThornyDevil.class.getName().124equals(cn)) {125System.err.println("Expected no trouble yet!");126System.err.println(String.valueOf(on) + ": class is " +127cn);128System.exit(3);129}130System.out.println(String.valueOf(on) + ": class is " + cn);131}132133System.out.println("Starting trouble with "+troubleMaker+" ...");134ThornyDevilMBean troubleMakerproxy =135(ThornyDevilMBean) MBeanServerInvocationHandler.136newProxyInstance(server, troubleMaker, ThornyDevilMBean.class,137false);138troubleMakerproxy.setDormant(false);139140try {141final MBeanInfo mbi = server.getMBeanInfo(troubleMaker);142System.err.println("No trouble started!: " + mbi);143System.exit(2);144} catch (Exception x) {145if (x.getCause() instanceof UnspeakableException)146System.out.println("Trouble started as expected: "147+ x.getCause());148else {149System.err.println("Unexpected trouble: " + x.getCause());150x.printStackTrace();151System.exit(3);152}153}154155final Set thorny2 = server.queryNames(thornyPattern,null);156if (thorny2.size() != 2) {157System.err.println("Expected to find 2 ThornyDevils after" +158" trouble started! ");159System.err.println("Found "+thorny2.size()+" instead: "+160thorny2);161System.exit(4);162}163164final Set thornyM2 = server.queryMBeans(thornyPattern,null);165if (thornyM2.size() != 2) {166System.err.println("queryMBeans(): " +167"Expected to find 2 ThornyDevils after" +168" trouble started! ");169System.err.println("Found "+thornyM2.size()+" instead: ");170printInstances(thornyM2);171System.exit(5);172}173for (Iterator it1 = thornyM2.iterator(); it1.hasNext();) {174final ObjectInstance oi = (ObjectInstance)it1.next();175final ObjectName on = oi.getObjectName();176final String cn = oi.getClassName();177if (!on.equals(troubleMaker)) {178if (cn == null || !ThornyDevil.class.getName().179equals(cn)) {180System.err.println("Expected no trouble for " + on);181System.err.println(String.valueOf(on) + ": class is " +182cn);183System.exit(6);184}185} else {186if (cn != null) {187System.err.println("Expected trouble for " + on);188System.err.println(String.valueOf(on) + ": class is " +189cn);190System.exit(7);191}192}193System.out.println(String.valueOf(on) + ": class is " + cn);194}195196System.out.println("Ahahah! " + troubleMaker +197" successfully thwarted!");198} catch( Exception x) {199System.err.println("Unexpected exception: " + x);200x.printStackTrace();201System.exit(8);202}203}204205}206207208