Path: blob/master/test/jdk/javax/management/Introspector/AnnotationTest.java
41152 views
/*1* Copyright (c) 2005, 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 6221321 629586726* @summary Test that annotations in Standard MBean interfaces27* correctly produce Descriptor entries28* @author Eamonn McManus29*30* @run clean AnnotationTest31* @run build AnnotationTest32* @run main AnnotationTest33*/3435import java.lang.annotation.Retention;36import java.lang.annotation.RetentionPolicy;37import javax.management.Descriptor;38import javax.management.DescriptorRead;39import javax.management.DescriptorKey;40import javax.management.ImmutableDescriptor;41import javax.management.MBeanAttributeInfo;42import javax.management.MBeanConstructorInfo;43import javax.management.MBeanInfo;44import javax.management.MBeanOperationInfo;45import javax.management.MBeanServer;46import javax.management.ObjectName;4748/*49This test checks that annotations produce Descriptor entries as50specified in javax.management.DescriptorKey. It does two things:5152- An annotation consisting of an int and a String, each with an53appropriate @DescriptorKey annotation, is placed on every program54element where it can map to a Descriptor, namely:5556. on an MBean interface57. on a getter for a read-only attribute58. on a setter for a write-only attribute59. on the getter but not the setter for a read/write attribute60. on the setter but not the getter for a read/write attribute61. on both the getter and the setter for a read/write attribute62. on an operation63. on each parameter of an operation64. on a public constructor with no parameters65. on a public constructor with a parameter66. on the parameter of that public constructor67. on all of the above for an MXBean instead of an MBean6869The test checks that in each case the corresponding Descriptor70appears in the appropriate place inside the MBean's MBeanInfo.7172- An annotation consisting of enough other types to ensure coverage73is placed on a getter. The test checks that the generated74MBeanAttributeInfo contains the corresponding Descriptor. The tested75types are the following:7677. Class78. an enumeration type (java.lang.annotation.RetentionPolicy)79. boolean80. String[]81. Class[]82. int[]83. an array of enumeration type (RetentionPolicy[])84. boolean[]85*/86public class AnnotationTest {87private static String failed = null;8889@Retention(RetentionPolicy.RUNTIME)90public static @interface Pair {91@DescriptorKey("x")92int x();93@DescriptorKey("y")94String y();95}9697@Retention(RetentionPolicy.RUNTIME)98public static @interface Full {99@DescriptorKey("class")100Class classValue();101@DescriptorKey("enum")102RetentionPolicy enumValue();103@DescriptorKey("boolean")104boolean booleanValue();105@DescriptorKey("stringArray")106String[] stringArrayValue();107@DescriptorKey("classArray")108Class[] classArrayValue();109@DescriptorKey("intArray")110int[] intArrayValue();111@DescriptorKey("enumArray")112RetentionPolicy[] enumArrayValue();113@DescriptorKey("booleanArray")114boolean[] booleanArrayValue();115}116117/* We use the annotation @Pair(x = 3, y = "foo") everywhere, and this is118the Descriptor that it should produce: */119private static Descriptor expectedDescriptor =120new ImmutableDescriptor(new String[] {"x", "y"},121new Object[] {3, "foo"});122123private static Descriptor expectedFullDescriptor =124new ImmutableDescriptor(new String[] {125"class", "enum", "boolean", "stringArray",126"classArray", "intArray", "enumArray",127"booleanArray",128},129new Object[] {130Full.class.getName(),131RetentionPolicy.RUNTIME.name(),132false,133new String[] {"foo", "bar"},134new String[] {Full.class.getName()},135new int[] {1, 2},136new String[] {RetentionPolicy.RUNTIME.name()},137new boolean[] {false, true},138});139140@Pair(x = 3, y = "foo")141public static interface ThingMBean {142@Pair(x = 3, y = "foo")143@Full(classValue=Full.class,144enumValue=RetentionPolicy.RUNTIME,145booleanValue=false,146stringArrayValue={"foo", "bar"},147classArrayValue={Full.class},148intArrayValue={1, 2},149enumArrayValue={RetentionPolicy.RUNTIME},150booleanArrayValue={false, true})151int getReadOnly();152153@Pair(x = 3, y = "foo")154void setWriteOnly(int x);155156@Pair(x = 3, y = "foo")157int getReadWrite1();158void setReadWrite1(int x);159160@Pair(x = 3, y = "foo")161int getReadWrite2();162@Pair(x = 3, y = "foo")163void setReadWrite2(int x);164165int getReadWrite3();166@Pair(x = 3, y = "foo")167void setReadWrite3(int x);168169@Pair(x = 3, y = "foo")170int operation(@Pair(x = 3, y = "foo") int p1,171@Pair(x = 3, y = "foo") int p2);172}173174public static class Thing implements ThingMBean {175@Pair(x = 3, y = "foo")176public Thing() {}177178@Pair(x = 3, y = "foo")179public Thing(@Pair(x = 3, y = "foo") int p1) {}180181public int getReadOnly() {return 0;}182183public void setWriteOnly(int x) {}184185public int getReadWrite1() {return 0;}186public void setReadWrite1(int x) {}187188public int getReadWrite2() {return 0;}189public void setReadWrite2(int x) {}190191public int getReadWrite3() {return 0;}192public void setReadWrite3(int x) {}193194public int operation(int p1, int p2) {return 0;}195}196197@Pair(x = 3, y = "foo")198public static interface ThingMXBean extends ThingMBean {}199200public static class ThingImpl implements ThingMXBean {201@Pair(x = 3, y = "foo")202public ThingImpl() {}203204@Pair(x = 3, y = "foo")205public ThingImpl(@Pair(x = 3, y = "foo") int p1) {}206207public int getReadOnly() {return 0;}208209public void setWriteOnly(int x) {}210211public int getReadWrite1() {return 0;}212public void setReadWrite1(int x) {}213214public int getReadWrite2() {return 0;}215public void setReadWrite2(int x) {}216217public int getReadWrite3() {return 0;}218public void setReadWrite3(int x) {}219220public int operation(int p1, int p2) {return 0;}221}222223public static void main(String[] args) throws Exception {224System.out.println("Testing that annotations are correctly " +225"reflected in Descriptor entries");226227MBeanServer mbs =228java.lang.management.ManagementFactory.getPlatformMBeanServer();229ObjectName on = new ObjectName("a:b=c");230231Thing thing = new Thing();232mbs.registerMBean(thing, on);233check(mbs, on);234mbs.unregisterMBean(on);235236ThingImpl thingImpl = new ThingImpl();237mbs.registerMBean(thingImpl, on);238Descriptor d = mbs.getMBeanInfo(on).getDescriptor();239if (!d.getFieldValue("mxbean").equals("true")) {240System.out.println("NOT OK: expected MXBean");241failed = "Expected MXBean";242}243check(mbs, on);244245if (failed == null)246System.out.println("Test passed");247else248throw new Exception("TEST FAILED: " + failed);249}250251private static void check(MBeanServer mbs, ObjectName on) throws Exception {252MBeanInfo mbi = mbs.getMBeanInfo(on);253254// check the MBean itself255check(mbi);256257// check attributes258MBeanAttributeInfo[] attrs = mbi.getAttributes();259for (MBeanAttributeInfo attr : attrs) {260check(attr);261if (attr.getName().equals("ReadOnly"))262check("@Full", attr.getDescriptor(), expectedFullDescriptor);263}264265// check operations266MBeanOperationInfo[] ops = mbi.getOperations();267for (MBeanOperationInfo op : ops) {268check(op);269check(op.getSignature());270}271272MBeanConstructorInfo[] constrs = mbi.getConstructors();273for (MBeanConstructorInfo constr : constrs) {274check(constr);275check(constr.getSignature());276}277}278279private static void check(DescriptorRead x) {280check(x, x.getDescriptor(), expectedDescriptor);281}282283private static void check(Object x, Descriptor d, Descriptor expect) {284String fail = null;285try {286Descriptor u = ImmutableDescriptor.union(d, expect);287if (!u.equals(d))288fail = "should contain " + expect + "; is " + d;289} catch (IllegalArgumentException e) {290fail = e.getMessage();291}292if (fail == null) {293System.out.println("OK: " + x);294} else {295failed = "NOT OK: Incorrect descriptor for: " + x;296System.out.println(failed);297System.out.println("..." + fail);298}299}300301private static void check(DescriptorRead[] xx) {302for (DescriptorRead x : xx)303check(x);304}305}306307308