Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/Introspector/AnnotationTest.java
38838 views
/*1* Copyright (c) 2005, 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 6221321 629586726* @summary Test that annotations in Standard MBean interfaces27* correctly produce Descriptor entries28* @author Eamonn McManus29* @run clean AnnotationTest30* @run build AnnotationTest31* @run main AnnotationTest32*/3334import java.lang.annotation.Retention;35import java.lang.annotation.RetentionPolicy;36import javax.management.Descriptor;37import javax.management.DescriptorRead;38import javax.management.DescriptorKey;39import javax.management.ImmutableDescriptor;40import javax.management.MBeanAttributeInfo;41import javax.management.MBeanConstructorInfo;42import javax.management.MBeanInfo;43import javax.management.MBeanOperationInfo;44import javax.management.MBeanServer;45import javax.management.ObjectName;4647/*48This test checks that annotations produce Descriptor entries as49specified in javax.management.DescriptorKey. It does two things:5051- An annotation consisting of an int and a String, each with an52appropriate @DescriptorKey annotation, is placed on every program53element where it can map to a Descriptor, namely:5455. on an MBean interface56. on a getter for a read-only attribute57. on a setter for a write-only attribute58. on the getter but not the setter for a read/write attribute59. on the setter but not the getter for a read/write attribute60. on both the getter and the setter for a read/write attribute61. on an operation62. on each parameter of an operation63. on a public constructor with no parameters64. on a public constructor with a parameter65. on the parameter of that public constructor66. on all of the above for an MXBean instead of an MBean6768The test checks that in each case the corresponding Descriptor69appears in the appropriate place inside the MBean's MBeanInfo.7071- An annotation consisting of enough other types to ensure coverage72is placed on a getter. The test checks that the generated73MBeanAttributeInfo contains the corresponding Descriptor. The tested74types are the following:7576. Class77. an enumeration type (java.lang.annotation.RetentionPolicy)78. boolean79. String[]80. Class[]81. int[]82. an array of enumeration type (RetentionPolicy[])83. boolean[]84*/85public class AnnotationTest {86private static String failed = null;8788@Retention(RetentionPolicy.RUNTIME)89public static @interface Pair {90@DescriptorKey("x")91int x();92@DescriptorKey("y")93String y();94}9596@Retention(RetentionPolicy.RUNTIME)97public static @interface Full {98@DescriptorKey("class")99Class classValue();100@DescriptorKey("enum")101RetentionPolicy enumValue();102@DescriptorKey("boolean")103boolean booleanValue();104@DescriptorKey("stringArray")105String[] stringArrayValue();106@DescriptorKey("classArray")107Class[] classArrayValue();108@DescriptorKey("intArray")109int[] intArrayValue();110@DescriptorKey("enumArray")111RetentionPolicy[] enumArrayValue();112@DescriptorKey("booleanArray")113boolean[] booleanArrayValue();114}115116/* We use the annotation @Pair(x = 3, y = "foo") everywhere, and this is117the Descriptor that it should produce: */118private static Descriptor expectedDescriptor =119new ImmutableDescriptor(new String[] {"x", "y"},120new Object[] {3, "foo"});121122private static Descriptor expectedFullDescriptor =123new ImmutableDescriptor(new String[] {124"class", "enum", "boolean", "stringArray",125"classArray", "intArray", "enumArray",126"booleanArray",127},128new Object[] {129Full.class.getName(),130RetentionPolicy.RUNTIME.name(),131false,132new String[] {"foo", "bar"},133new String[] {Full.class.getName()},134new int[] {1, 2},135new String[] {RetentionPolicy.RUNTIME.name()},136new boolean[] {false, true},137});138139@Pair(x = 3, y = "foo")140public static interface ThingMBean {141@Pair(x = 3, y = "foo")142@Full(classValue=Full.class,143enumValue=RetentionPolicy.RUNTIME,144booleanValue=false,145stringArrayValue={"foo", "bar"},146classArrayValue={Full.class},147intArrayValue={1, 2},148enumArrayValue={RetentionPolicy.RUNTIME},149booleanArrayValue={false, true})150int getReadOnly();151152@Pair(x = 3, y = "foo")153void setWriteOnly(int x);154155@Pair(x = 3, y = "foo")156int getReadWrite1();157void setReadWrite1(int x);158159@Pair(x = 3, y = "foo")160int getReadWrite2();161@Pair(x = 3, y = "foo")162void setReadWrite2(int x);163164int getReadWrite3();165@Pair(x = 3, y = "foo")166void setReadWrite3(int x);167168@Pair(x = 3, y = "foo")169int operation(@Pair(x = 3, y = "foo") int p1,170@Pair(x = 3, y = "foo") int p2);171}172173public static class Thing implements ThingMBean {174@Pair(x = 3, y = "foo")175public Thing() {}176177@Pair(x = 3, y = "foo")178public Thing(@Pair(x = 3, y = "foo") int p1) {}179180public int getReadOnly() {return 0;}181182public void setWriteOnly(int x) {}183184public int getReadWrite1() {return 0;}185public void setReadWrite1(int x) {}186187public int getReadWrite2() {return 0;}188public void setReadWrite2(int x) {}189190public int getReadWrite3() {return 0;}191public void setReadWrite3(int x) {}192193public int operation(int p1, int p2) {return 0;}194}195196@Pair(x = 3, y = "foo")197public static interface ThingMXBean extends ThingMBean {}198199public static class ThingImpl implements ThingMXBean {200@Pair(x = 3, y = "foo")201public ThingImpl() {}202203@Pair(x = 3, y = "foo")204public ThingImpl(@Pair(x = 3, y = "foo") int p1) {}205206public int getReadOnly() {return 0;}207208public void setWriteOnly(int x) {}209210public int getReadWrite1() {return 0;}211public void setReadWrite1(int x) {}212213public int getReadWrite2() {return 0;}214public void setReadWrite2(int x) {}215216public int getReadWrite3() {return 0;}217public void setReadWrite3(int x) {}218219public int operation(int p1, int p2) {return 0;}220}221222public static void main(String[] args) throws Exception {223System.out.println("Testing that annotations are correctly " +224"reflected in Descriptor entries");225226MBeanServer mbs =227java.lang.management.ManagementFactory.getPlatformMBeanServer();228ObjectName on = new ObjectName("a:b=c");229230Thing thing = new Thing();231mbs.registerMBean(thing, on);232check(mbs, on);233mbs.unregisterMBean(on);234235ThingImpl thingImpl = new ThingImpl();236mbs.registerMBean(thingImpl, on);237Descriptor d = mbs.getMBeanInfo(on).getDescriptor();238if (!d.getFieldValue("mxbean").equals("true")) {239System.out.println("NOT OK: expected MXBean");240failed = "Expected MXBean";241}242check(mbs, on);243244if (failed == null)245System.out.println("Test passed");246else247throw new Exception("TEST FAILED: " + failed);248}249250private static void check(MBeanServer mbs, ObjectName on) throws Exception {251MBeanInfo mbi = mbs.getMBeanInfo(on);252253// check the MBean itself254check(mbi);255256// check attributes257MBeanAttributeInfo[] attrs = mbi.getAttributes();258for (MBeanAttributeInfo attr : attrs) {259check(attr);260if (attr.getName().equals("ReadOnly"))261check("@Full", attr.getDescriptor(), expectedFullDescriptor);262}263264// check operations265MBeanOperationInfo[] ops = mbi.getOperations();266for (MBeanOperationInfo op : ops) {267check(op);268check(op.getSignature());269}270271MBeanConstructorInfo[] constrs = mbi.getConstructors();272for (MBeanConstructorInfo constr : constrs) {273check(constr);274check(constr.getSignature());275}276}277278private static void check(DescriptorRead x) {279check(x, x.getDescriptor(), expectedDescriptor);280}281282private static void check(Object x, Descriptor d, Descriptor expect) {283String fail = null;284try {285Descriptor u = ImmutableDescriptor.union(d, expect);286if (!u.equals(d))287fail = "should contain " + expect + "; is " + d;288} catch (IllegalArgumentException e) {289fail = e.getMessage();290}291if (fail == null) {292System.out.println("OK: " + x);293} else {294failed = "NOT OK: Incorrect descriptor for: " + x;295System.out.println(failed);296System.out.println("..." + fail);297}298}299300private static void check(DescriptorRead[] xx) {301for (DescriptorRead x : xx)302check(x);303}304}305306307