Path: blob/master/test/jdk/javax/management/Introspector/FeatureOrderTest.java
41149 views
/*1* Copyright (c) 2006, 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 648665526* @summary Test that attributes and operations appear in the same order27* in MBeanInfo as they did in the Standard MBean or MXBean Interface.28* @author Eamonn McManus29*/3031/*32* For more background on this test, see:33* http://weblogs.java.net/blog/emcmanus/archive/2006/11/notes_on_unspec.html34*/3536import java.lang.management.ManagementFactory;37import java.lang.reflect.Method;38import java.math.BigInteger;39import java.util.ArrayList;40import java.util.Collections;41import java.util.List;42import javax.management.MBeanAttributeInfo;43import javax.management.MBeanInfo;44import javax.management.MBeanOperationInfo;45import javax.management.MBeanServer;46import javax.management.ObjectName;47import javax.management.StandardMBean;4849public class FeatureOrderTest {50private static boolean failed;5152public static interface OrderMXBean {53public int getMercury();5455public String getVenus();56public void setVenus(String x);5758public BigInteger getEarth();59public void setEarth(BigInteger x);6061public boolean isMars();6263public double getJupiter();6465public byte getSaturn();6667public short getUranus();68public void setUranus(short x);6970public long getNeptune();7172// No more Pluto! Yay!7374public void neptune();75public void uranus(int x);76public int saturn(int x, int y);77public short jupiter(int x, long y, double z);78public void mars(boolean x);79public BigInteger earth();80public double earth(double x); // http://www.imdb.com/title/tt0064519/81public String venus();82public int mercury();83}8485public static interface OrderMBean extends OrderMXBean {}8687public static class OrderImpl implements OrderMXBean {88public int getMercury() {89return 0;90}9192public String getVenus() {93return null;94}9596public void setVenus(String x) {97}9899public BigInteger getEarth() {100return null;101}102103public void setEarth(BigInteger x) {104}105106public boolean isMars() {107return true;108}109110public double getJupiter() {111return 0;112}113114public byte getSaturn() {115return 0;116}117118public short getUranus() {119return 0;120}121122public void setUranus(short x) {123}124125public long getNeptune() {126return 0;127}128129public void neptune() {130}131132public void uranus(int x) {133}134135public int saturn(int x, int y) {136return 0;137}138139public short jupiter(int x, long y, double z) {140return 0;141}142143public void mars(boolean x) {144}145146public BigInteger earth() {147return null;148}149150public double earth(double x) {151return 0;152}153154public String venus() {155return null;156}157158public int mercury() {159return 0;160}161}162163public static class Order extends OrderImpl implements OrderMBean {}164165private static final boolean[] booleans = {false, true};166167public static void main(String[] args) throws Exception {168// Build the lists of attributes and operations that we would expect169// if they are derived by reflection and preserve the reflection order170List<String> expectedAttributeNames = new ArrayList<String>();171List<String> expectedOperationNames = new ArrayList<String>();172for (Method m : OrderMXBean.class.getMethods()) {173String name = m.getName();174String attrName = null;175if (name.startsWith("get") && !name.equals("get") &&176m.getParameterTypes().length == 0 &&177m.getReturnType() != void.class)178attrName = name.substring(3);179else if (name.startsWith("is") && !name.equals("is") &&180m.getParameterTypes().length == 0 &&181m.getReturnType() == boolean.class)182attrName = name.substring(2);183else if (name.startsWith("set") && !name.equals("set") &&184m.getReturnType() == void.class &&185m.getParameterTypes().length == 1)186attrName = name.substring(3);187if (attrName != null) {188if (!expectedAttributeNames.contains(attrName))189expectedAttributeNames.add(attrName);190} else191expectedOperationNames.add(name);192}193194MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();195for (boolean mxbean : booleans) {196for (boolean withStandardMBean : booleans) {197String testName = "MXBean: " + mxbean + "; " +198"using javax.management.StandardMBean: " +199withStandardMBean;200System.out.println("Test case: " + testName);201Object mbean;202if (mxbean) {203if (withStandardMBean) {204mbean = new StandardMBean(205new OrderImpl(), OrderMXBean.class, true);206} else207mbean = new OrderImpl();208} else {209if (withStandardMBean)210mbean = new StandardMBean(new Order(), OrderMBean.class);211else212mbean = new Order();213}214ObjectName name = new ObjectName(215":mxbean=" + mxbean + "," + "withStandardMBean=" +216withStandardMBean);217mbs.registerMBean(mbean, name);218219/* Make sure we are testing what we think. */220MBeanInfo mbi = mbs.getMBeanInfo(name);221boolean isWithStandardMBean =222mbs.isInstanceOf(name, StandardMBean.class.getName());223System.out.println("classname " +mbi.getClassName());224String mxbeanField =225(String) mbi.getDescriptor().getFieldValue("mxbean");226boolean isMXBean = "true".equalsIgnoreCase(mxbeanField);227228if (mxbean != isMXBean)229throw new Exception("Test error: MXBean mismatch");230if (withStandardMBean != isWithStandardMBean)231throw new Exception("Test error: StandardMBean mismatch");232233// Check that order of attributes and operations matches234MBeanAttributeInfo[] mbais = mbi.getAttributes();235checkEqual(expectedAttributeNames.size(), mbais.length,236"number of attributes");237List<String> attributeNames = new ArrayList<String>();238for (MBeanAttributeInfo mbai : mbais)239attributeNames.add(mbai.getName());240checkEqual(expectedAttributeNames, attributeNames,241"order of attributes");242243MBeanOperationInfo[] mbois = mbi.getOperations();244checkEqual(expectedOperationNames.size(), mbois.length,245"number of operations");246List<String> operationNames = new ArrayList<String>();247for (MBeanOperationInfo mboi : mbois)248operationNames.add(mboi.getName());249checkEqual(expectedOperationNames, operationNames,250"order of operations");251System.out.println();252}253}254255if (failed)256throw new Exception("TEST FAILED");257System.out.println("TEST PASSED");258}259260private static void checkEqual(Object expected, Object actual, String what) {261if (expected.equals(actual))262System.out.println("OK: " + what + " matches");263else {264System.out.println("FAILED: " + what + " differs:");265System.out.println(" expected: " + expected);266System.out.println(" actual: " + actual);267failed = true;268}269}270}271272273