Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/MBeanInfo/TooManyFooTest.java
38841 views
/*1* Copyright (c) 2006, 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 639888426* @summary Test that a method inherited from two different interfaces27* appears only once in MBeanInfo.28* @author dfuchs29* @run clean TooManyFooTest30* @run build TooManyFooTest31* @run main TooManyFooTest32*/3334import java.lang.management.ManagementFactory;35import java.lang.reflect.Method;36import java.util.Arrays;37import java.util.HashMap;38import java.util.HashSet;39import java.util.Map;40import java.util.Set;41import java.util.logging.Logger;42import javax.management.Descriptor;43import javax.management.MBeanInfo;44import javax.management.MBeanOperationInfo;45import javax.management.MBeanServer;46import javax.management.ObjectName;47import javax.management.StandardMBean;48import javax.management.openmbean.OpenMBeanOperationInfo;4950/**51* Class TooManyFooTest52* @author Sun Microsystems, 2005 - All rights reserved.53*/54public class TooManyFooTest {5556/**57* A logger for this class.58**/59private static final Logger LOG =60Logger.getLogger(TooManyFooTest.class.getName());6162public static class NumberHolder {63public Integer getNumber() { return 0;}64public void setNumber(Integer n) {};65}66public static class MyNumberHolder extends NumberHolder {6768}69public interface Parent1 {70public int foo(); // Both in Parent1 and Parent271public Integer barfoo(); // Subtype in Parent1, Super type in Parent272public Long foobar(); // Subtype in Parent1 & MBean, Super type in73// Parent274public Number toofoo(); // Subtype in Parent1, Super type in Parent275// Concrete type in MBean76public Object toofoofoo(); // Super type in Parent1, Subtype in Parent2,77public NumberHolder toobarbar(); // toofoofoo reversed78}7980public interface Parent2 {81public int foo(); // Both in Parent1 and Parent282public Number barfoo();83public Number foobar();84public Object toofoo();85public NumberHolder toofoofoo();86public Object toobarbar();87}8889public interface ChildMBean extends Parent1, Parent2 {90public Long foobar();91public Long toofoo();92}9394public interface ChildMXBean extends Parent1, Parent2 {95public Long foobar();96public Long toofoo();97}9899public interface ChildMixMXBean extends ChildMBean, ChildMXBean {100}101102public static class Child implements ChildMBean {103public int foo() {return 0;}104public Long foobar() {return 0L;}105public Long toofoo() {return 0L;}106public Integer barfoo() {return 0;}107public MyNumberHolder toofoofoo() { return null;}108public MyNumberHolder toobarbar() { return null;}109}110111public static class ChildMix implements ChildMXBean {112public int foo() {return 0;}113public Long foobar() {return 0L;}114public Long toofoo() {return 0L;}115public Integer barfoo() {return 0;}116public MyNumberHolder toofoofoo() { return null;}117public MyNumberHolder toobarbar() { return null;}118}119120public static class ChildMixMix extends Child implements ChildMixMXBean {121}122123124/** Creates a new instance of TooManyFooTest */125public TooManyFooTest() {126}127128private static final int OPCOUNT;129private static final Map<String,String> EXPECTED_TYPES;130private static final String[][] type_array = {131{ "foo", int.class.getName() },132{ "foobar", Long.class.getName()},133{ "toofoo", Long.class.getName()},134{ "barfoo", Integer.class.getName()},135{ "toofoofoo", NumberHolder.class.getName()},136{ "toobarbar", NumberHolder.class.getName()},137};138static {139try {140final Set<String> declared = new HashSet<String>();141for (Method m:Child.class.getDeclaredMethods()) {142declared.add(m.getName()+Arrays.asList(m.getParameterTypes()));143}144final Set<String> exposed = new HashSet<String>();145for (Method m:ChildMBean.class.getMethods()) {146exposed.add(m.getName()+Arrays.asList(m.getParameterTypes()));147}148declared.retainAll(exposed);149OPCOUNT = declared.size();150EXPECTED_TYPES = new HashMap<String,String>();151for (String[] st:type_array) {152EXPECTED_TYPES.put(st[0],st[1]);153}154} catch (Exception x) {155throw new ExceptionInInitializerError(x);156}157}158159private static void test(Object child, String name, boolean mxbean)160throws Exception {161final ObjectName childName =162new ObjectName("test:type=Child,name="+name);163final MBeanServer server =164ManagementFactory.getPlatformMBeanServer();165server.registerMBean(child,childName);166try {167final MBeanInfo info = server.getMBeanInfo(childName);168System.out.println(name+": " + info.getDescriptor());169final int len = info.getOperations().length;170if (len == OPCOUNT) {171System.out.println(name+": OK, only "+OPCOUNT+172" operations here...");173} else {174final String qual = (len>OPCOUNT)?"many":"few";175System.err.println(name+": Too "+qual+" foos! Found "+176len+", expected "+OPCOUNT);177for (MBeanOperationInfo op : info.getOperations()) {178System.err.println("public "+op.getReturnType()+" "+179op.getName()+"();");180}181throw new RuntimeException("Too " + qual +182" foos for "+name);183}184185final Descriptor d = info.getDescriptor();186final String mxstr = String.valueOf(d.getFieldValue("mxbean"));187final boolean mxb =188(mxstr==null)?false:Boolean.valueOf(mxstr).booleanValue();189System.out.println(name+": mxbean="+mxb);190if (mxbean && !mxb)191throw new AssertionError("MXBean is not OpenMBean?");192193for (MBeanOperationInfo mboi : info.getOperations()) {194195// Sanity check196if (mxbean && !mboi.getName().equals("foo")) {197// The spec doesn't guarantee that the MBeanOperationInfo198// of an MXBean will be an OpenMBeanOperationInfo, and in199// some circumstances in our implementation it will not.200// However, in thsi tests, for all methods but foo(),201// it should.202//203if (!(mboi instanceof OpenMBeanOperationInfo))204throw new AssertionError("Operation "+mboi.getName()+205"() is not Open?");206}207208final String exp = EXPECTED_TYPES.get(mboi.getName());209210// For MXBeans, we need to compare 'exp' with the original211// type - because mboi.getReturnType() returns the OpenType212//213String type = (String)mboi.getDescriptor().214getFieldValue("originalType");215if (type == null) type = mboi.getReturnType();216if (type.equals(exp)) continue;217System.err.println("Bad return type for "+218mboi.getName()+"! Found "+type+219", expected "+exp);220throw new RuntimeException("Bad return type for "+221mboi.getName());222}223} finally {224server.unregisterMBean(childName);225}226}227228public static void main(String[] args) throws Exception {229final Child child = new Child();230test(child,"Child[MBean]",false);231final ChildMix childx = new ChildMix();232test(childx,"ChildMix[MXBean]",true);233final ChildMixMix childmx = new ChildMixMix();234test(childmx,"ChildMixMix[MXBean]",false);235final StandardMBean schild = new StandardMBean(child,ChildMBean.class);236test(schild,"Child[StandarMBean(Child)]",false);237final StandardMBean schildx =238new StandardMBean(childx,ChildMXBean.class,true);239test(schildx,"ChildMix[StandarMXBean(ChildMix)]",true);240final StandardMBean schildmx =241new StandardMBean(childmx,ChildMixMXBean.class,true);242test(schildmx,"ChildMixMix[StandarMXBean(ChildMixMix)]",true);243}244245}246247248