jdk/test/javax/management/Introspector/ParameterNameTest.java
changeset 4159 9e3aae7675f1
parent 4158 0b4d21bc8b5c
parent 4156 acaa49a2768a
child 4160 bda0a85afcb7
equal deleted inserted replaced
4158:0b4d21bc8b5c 4159:9e3aae7675f1
     1 /*
       
     2  * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test %M% %I%
       
    26  * @bug 6323980
       
    27  * @summary Test that parameter names can be specified with @Name.
       
    28  * @author Eamonn McManus
       
    29  */
       
    30 
       
    31 import javax.management.MBean;
       
    32 import javax.management.MBeanInfo;
       
    33 import javax.management.MBeanOperationInfo;
       
    34 import javax.management.MBeanParameterInfo;
       
    35 import javax.management.MBeanServer;
       
    36 import javax.management.MBeanServerFactory;
       
    37 import javax.management.MXBean;
       
    38 import javax.management.ObjectName;
       
    39 
       
    40 import annot.Name;
       
    41 import javax.management.ManagedOperation;
       
    42 
       
    43 public class ParameterNameTest {
       
    44     public static interface NoddyMBean {
       
    45         public int add(int x, @Name("y") int y);
       
    46     }
       
    47 
       
    48     public static class Noddy implements NoddyMBean {
       
    49         public int add(int x, int y) {
       
    50             return x + y;
       
    51         }
       
    52     }
       
    53 
       
    54     public static interface NoddyMXBean {
       
    55         public int add(int x, @Name("y") int y);
       
    56     }
       
    57 
       
    58     public static class NoddyImpl implements NoddyMXBean {
       
    59         public int add(int x, int y) {
       
    60             return x + y;
       
    61         }
       
    62     }
       
    63 
       
    64     @MBean
       
    65     public static class NoddyAnnot {
       
    66         @ManagedOperation
       
    67         public int add(int x, @Name("y") int y) {
       
    68             return x + y;
       
    69         }
       
    70     }
       
    71 
       
    72     @MXBean
       
    73     public static class NoddyAnnotMX {
       
    74         @ManagedOperation
       
    75         public int add(int x, @Name("y") int y) {
       
    76             return x + y;
       
    77         }
       
    78     }
       
    79 
       
    80     private static final Object[] mbeans = {
       
    81         new Noddy(), new NoddyImpl(), new NoddyAnnot(), new NoddyAnnotMX(),
       
    82     };
       
    83 
       
    84     public static void main(String[] args) throws Exception {
       
    85         MBeanServer mbs = MBeanServerFactory.newMBeanServer();
       
    86         ObjectName name = new ObjectName("a:b=c");
       
    87         for (Object mbean : mbeans) {
       
    88             System.out.println("Testing " + mbean.getClass().getName());
       
    89             mbs.registerMBean(mbean, name);
       
    90             MBeanInfo mbi = mbs.getMBeanInfo(name);
       
    91             MBeanOperationInfo[] mbois = mbi.getOperations();
       
    92             assertEquals(1, mbois.length);
       
    93             MBeanParameterInfo[] mbpis = mbois[0].getSignature();
       
    94             assertEquals(2, mbpis.length);
       
    95             boolean mx = Boolean.parseBoolean(
       
    96                     (String) mbi.getDescriptor().getFieldValue("mxbean"));
       
    97             assertEquals(mx ? "p0" : "p1", mbpis[0].getName());
       
    98             assertEquals("y", mbpis[1].getName());
       
    99             mbs.unregisterMBean(name);
       
   100         }
       
   101         System.out.println("TEST PASSED");
       
   102     }
       
   103 
       
   104     private static void assertEquals(Object expect, Object actual)
       
   105     throws Exception {
       
   106         boolean eq;
       
   107         if (expect == null)
       
   108             eq = (actual == null);
       
   109         else
       
   110             eq = expect.equals(actual);
       
   111         if (!eq) {
       
   112             throw new Exception(
       
   113                     "TEST FAILED: expected " + expect + ", found " + actual);
       
   114         }
       
   115     }
       
   116 }