1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4954062
  27  * @summary Test that a custom ModelMBean implementation can have custom
  28  * targetType values in its ModelMBeanOperationInfo descriptors.
  29  * @author Eamonn McManus
  30  *
  31  * @run clean ExoticTargetTypeTest
  32  * @run build ExoticTargetTypeTest
  33  * @run main ExoticTargetTypeTest
  34  */
  35 
  36 import javax.management.*;
  37 import javax.management.modelmbean.*;
  38 
  39 public class ExoticTargetTypeTest {
  40     public static void main(String[] args) throws Exception {
  41         System.out.println("Testing that ModelMBeanOperationInfo can contain" +
  42                            " a nonstandard targetType provided that the " +
  43                            "ModelMBean understands it");
  44 
  45         boolean ok = true;
  46 
  47         final Descriptor noddyDescr =
  48             new DescriptorSupport(new String[] {
  49                 "name=noddy",
  50                 "descriptorType=operation",
  51                 "role=operation",
  52                 "targetType=noddy",
  53             });
  54         final ModelMBeanOperationInfo opInfo =
  55             new ModelMBeanOperationInfo("noddy",
  56                                         "noddy description",
  57                                         new MBeanParameterInfo[0],
  58                                         "void",
  59                                         ModelMBeanOperationInfo.ACTION,
  60                                         noddyDescr);
  61         final ModelMBeanInfo mmbInfo =
  62             new ModelMBeanInfoSupport(ExoticModelMBean.class.getName(),
  63                                       "Noddy ModelMBean description",
  64                                       null, null,
  65                                       new ModelMBeanOperationInfo[] {opInfo},
  66                                       null);
  67 
  68         System.out.println("Testing nonstandard ModelMBean with nonstandard " +
  69                            "ModelMBeanOperationInfo...");
  70         final ExoticModelMBean exoticMMB = new ExoticModelMBean(mmbInfo);
  71         try {
  72             test(exoticMMB);
  73             if (exoticMMB.noddyCalled)
  74                 System.out.println("...OK");
  75             else {
  76                 System.out.println("...TEST FAILS: invoke worked but did " +
  77                                    "not invoke method?!");
  78                 ok = false;
  79             }
  80         } catch (Exception e) {
  81             System.out.println("...TEST FAILS: exception:");
  82             e.printStackTrace(System.out);
  83             ok = false;
  84         }
  85 
  86         System.out.println("Testing standard ModelMBean with nonstandard " +
  87                            "ModelMBeanOperationInfo...");
  88         final ModelMBean standardMMB = new RequiredModelMBean(mmbInfo);
  89         // RequiredModelMBean's constructor could legitimately throw
  90         // an exception here because of the nonstandard descriptor.
  91         // If some day it does, we will have to update this test.
  92         try {
  93             test(standardMMB);
  94             System.out.println("...TEST FAILS: invoke worked but should not");
  95             ok = false;
  96         } catch (MBeanException e) {
  97             Throwable cause = e.getCause();
  98             if (cause instanceof InvalidTargetObjectTypeException) {
  99                 System.out.println("...OK: got exception MBeanException/" +
 100                                    "InvalidTargetObjectTypeException: " +
 101                                    cause.getMessage());
 102             } else {
 103                 System.out.println("...TEST FAILS: got MBeanException with " +
 104                                    "wrong cause: " + cause);
 105                 ok = false;
 106             }
 107         } catch (Exception e) {
 108             System.out.println("...TEST FAILS: exception:");
 109             e.printStackTrace(System.out);
 110             ok = false;
 111         }
 112 
 113         if (ok)
 114             System.out.println("Test passed");
 115         else {
 116             System.out.println("TEST FAILED");
 117             System.exit(1);
 118         }
 119     }
 120 
 121     private static void test(ModelMBean mmb) throws Exception {
 122         final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
 123         final ObjectName name = new ObjectName("d:k=v");
 124         mbs.registerMBean(mmb, name);
 125         try {
 126             mbs.invoke(name, "noddy", null, null);
 127         } finally {
 128             mbs.unregisterMBean(name);
 129         }
 130     }
 131 
 132     public static class ExoticModelMBean extends RequiredModelMBean {
 133         public ExoticModelMBean(ModelMBeanInfo mmbInfo) throws MBeanException {
 134             super();
 135             this.mmbInfo = mmbInfo;
 136         }
 137 
 138         public Object invoke(String opName, Object[] opArgs, String[] sig)
 139                 throws MBeanException, ReflectionException {
 140             if (opName.equals("noddy")) {
 141                 noddyCalled = true;
 142                 return null;
 143             } else
 144                 throw new IllegalArgumentException("Not noddy: " + opName);
 145         }
 146 
 147         private boolean noddyCalled;
 148         private final ModelMBeanInfo mmbInfo;
 149     }
 150 }