0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4954062
0N/A * @summary Test that a custom ModelMBean implementation can have custom
0N/A * targetType values in its ModelMBeanOperationInfo descriptors.
0N/A * @author Eamonn McManus
0N/A * @run clean ExoticTargetTypeTest
0N/A * @run build ExoticTargetTypeTest
0N/A * @run main ExoticTargetTypeTest
0N/A */
0N/A
0N/Aimport javax.management.*;
0N/Aimport javax.management.modelmbean.*;
0N/A
0N/Apublic class ExoticTargetTypeTest {
0N/A public static void main(String[] args) throws Exception {
0N/A System.out.println("Testing that ModelMBeanOperationInfo can contain" +
0N/A " a nonstandard targetType provided that the " +
0N/A "ModelMBean understands it");
0N/A
0N/A boolean ok = true;
0N/A
0N/A final Descriptor noddyDescr =
0N/A new DescriptorSupport(new String[] {
0N/A "name=noddy",
0N/A "descriptorType=operation",
0N/A "role=operation",
0N/A "targetType=noddy",
0N/A });
0N/A final ModelMBeanOperationInfo opInfo =
0N/A new ModelMBeanOperationInfo("noddy",
0N/A "noddy description",
0N/A new MBeanParameterInfo[0],
0N/A "void",
0N/A ModelMBeanOperationInfo.ACTION,
0N/A noddyDescr);
0N/A final ModelMBeanInfo mmbInfo =
0N/A new ModelMBeanInfoSupport(ExoticModelMBean.class.getName(),
0N/A "Noddy ModelMBean description",
0N/A null, null,
0N/A new ModelMBeanOperationInfo[] {opInfo},
0N/A null);
0N/A
0N/A System.out.println("Testing nonstandard ModelMBean with nonstandard " +
0N/A "ModelMBeanOperationInfo...");
0N/A final ExoticModelMBean exoticMMB = new ExoticModelMBean(mmbInfo);
0N/A try {
0N/A test(exoticMMB);
0N/A if (exoticMMB.noddyCalled)
0N/A System.out.println("...OK");
0N/A else {
0N/A System.out.println("...TEST FAILS: invoke worked but did " +
0N/A "not invoke method?!");
0N/A ok = false;
0N/A }
0N/A } catch (Exception e) {
0N/A System.out.println("...TEST FAILS: exception:");
0N/A e.printStackTrace(System.out);
0N/A ok = false;
0N/A }
0N/A
0N/A System.out.println("Testing standard ModelMBean with nonstandard " +
0N/A "ModelMBeanOperationInfo...");
0N/A final ModelMBean standardMMB = new RequiredModelMBean(mmbInfo);
0N/A // RequiredModelMBean's constructor could legitimately throw
0N/A // an exception here because of the nonstandard descriptor.
0N/A // If some day it does, we will have to update this test.
0N/A try {
0N/A test(standardMMB);
0N/A System.out.println("...TEST FAILS: invoke worked but should not");
0N/A ok = false;
0N/A } catch (MBeanException e) {
0N/A Throwable cause = e.getCause();
0N/A if (cause instanceof InvalidTargetObjectTypeException) {
0N/A System.out.println("...OK: got exception MBeanException/" +
0N/A "InvalidTargetObjectTypeException: " +
0N/A cause.getMessage());
0N/A } else {
0N/A System.out.println("...TEST FAILS: got MBeanException with " +
0N/A "wrong cause: " + cause);
0N/A ok = false;
0N/A }
0N/A } catch (Exception e) {
0N/A System.out.println("...TEST FAILS: exception:");
0N/A e.printStackTrace(System.out);
0N/A ok = false;
0N/A }
0N/A
0N/A if (ok)
0N/A System.out.println("Test passed");
0N/A else {
0N/A System.out.println("TEST FAILED");
0N/A System.exit(1);
0N/A }
0N/A }
0N/A
0N/A private static void test(ModelMBean mmb) throws Exception {
0N/A final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
0N/A final ObjectName name = new ObjectName("d:k=v");
0N/A mbs.registerMBean(mmb, name);
0N/A try {
0N/A mbs.invoke(name, "noddy", null, null);
0N/A } finally {
0N/A mbs.unregisterMBean(name);
0N/A }
0N/A }
0N/A
0N/A public static class ExoticModelMBean extends RequiredModelMBean {
0N/A public ExoticModelMBean(ModelMBeanInfo mmbInfo) throws MBeanException {
0N/A super();
0N/A this.mmbInfo = mmbInfo;
0N/A }
0N/A
0N/A public Object invoke(String opName, Object[] opArgs, String[] sig)
0N/A throws MBeanException, ReflectionException {
0N/A if (opName.equals("noddy")) {
0N/A noddyCalled = true;
0N/A return null;
0N/A } else
0N/A throw new IllegalArgumentException("Not noddy: " + opName);
0N/A }
0N/A
0N/A private boolean noddyCalled;
0N/A private final ModelMBeanInfo mmbInfo;
0N/A }
0N/A}