0N/A/*
2362N/A * Copyright (c) 2005, 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 4997033
0N/A * @summary Test the following in RequiredModelMBean.setAttribute():
0N/A * MBeanException wrapping a ServiceNotFoundException is thrown is setAttribute
0N/A * called but no setMethod field has been provided.
0N/A * @author Jean-Francois Denise
0N/A * @run clean RequiredModelMBeanSetAttributeTest
0N/A * @run build RequiredModelMBeanSetAttributeTest
0N/A * @run main RequiredModelMBeanSetAttributeTest
0N/A */
0N/A
0N/Aimport javax.management.Descriptor;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.MBeanServerFactory;
0N/Aimport javax.management.ObjectName;
0N/Aimport javax.management.Attribute;
0N/Aimport javax.management.MBeanException;
0N/Aimport javax.management.ServiceNotFoundException;
0N/Aimport javax.management.modelmbean.DescriptorSupport;
0N/Aimport javax.management.modelmbean.ModelMBean;
0N/Aimport javax.management.modelmbean.ModelMBeanAttributeInfo;
0N/Aimport javax.management.modelmbean.ModelMBeanInfo;
0N/Aimport javax.management.modelmbean.ModelMBeanInfoSupport;
0N/Aimport javax.management.modelmbean.ModelMBeanOperationInfo;
0N/Aimport javax.management.modelmbean.RequiredModelMBean;
0N/A
0N/Apublic class RequiredModelMBeanSetAttributeTest {
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A boolean ok = true;
0N/A
0N/A MBeanServer mbs = MBeanServerFactory.createMBeanServer();
0N/A
0N/A // ModelMBeanAttributeInfo
0N/A
0N/A Descriptor somethingAttributeDescriptor =
0N/A new DescriptorSupport(new String[] {
0N/A "name=Something",
0N/A "descriptorType=attribute",
0N/A "getMethod=getSomething"
0N/A });
0N/A ModelMBeanAttributeInfo somethingAttributeInfo =
0N/A new ModelMBeanAttributeInfo("Something",
0N/A "java.lang.String",
0N/A "Something attribute",
0N/A true,
0N/A true,
0N/A false,
0N/A somethingAttributeDescriptor);
0N/A
0N/A Descriptor somethingCachedAttributeDescriptor =
0N/A new DescriptorSupport(new String[] {
0N/A "name=SomethingCached",
0N/A "descriptorType=attribute",
0N/A "getMethod=getSomethingCached",
0N/A "currencyTimeLimit=5000"
0N/A });
0N/A ModelMBeanAttributeInfo somethingCachedAttributeInfo =
0N/A new ModelMBeanAttributeInfo("SomethingCached",
0N/A "java.lang.String",
0N/A "Something cached attribute",
0N/A true,
0N/A true,
0N/A false,
0N/A somethingCachedAttributeDescriptor);
0N/A // ModelMBeanInfo
0N/A
0N/A ModelMBeanInfo mmbi = new ModelMBeanInfoSupport(
0N/A Resource.class.getName(),
0N/A "Resource MBean",
0N/A new ModelMBeanAttributeInfo[] { somethingAttributeInfo, somethingCachedAttributeInfo },
0N/A null,
0N/A new ModelMBeanOperationInfo[] {},
0N/A null);
0N/A
0N/A // RequiredModelMBean
0N/A
0N/A ModelMBean mmb = new RequiredModelMBean(mmbi);
0N/A mmb.setManagedResource(resource, "ObjectReference");
0N/A ObjectName mmbName = new ObjectName(":type=ResourceMBean");
0N/A mbs.registerMBean(mmb, mmbName);
0N/A
0N/A // Run tests
0N/A
0N/A System.out.println("\nTest that we receive ServiceNotFoundException");
0N/A try {
0N/A Attribute attr = new Attribute("Something", "Some string");
0N/A mbs.setAttribute(mmbName, attr);
0N/A System.out.println("TEST FAILED: Didn't caught exception");
0N/A ok = false;
0N/A } catch(MBeanException mbex) {
0N/A Exception e = mbex.getTargetException();
0N/A if(e == null || !(e instanceof ServiceNotFoundException)) {
0N/A System.out.println("TEST FAILED: Caught wrong exception:" + e);
0N/A ok = false;
0N/A } else
0N/A System.out.println("Received expected ServiceNotFoundException");
0N/A
0N/A } catch (Exception e) {
0N/A System.out.println("TEST FAILED: Caught wrong exception: " + e);
0N/A e.printStackTrace(System.out);
0N/A ok = false;
0N/A }
0N/A
0N/A //Now check that when caching is enabled, setAttribute is working
0N/A System.out.println("\nTest that we are not receiving ServiceNotFoundException");
0N/A try {
0N/A Attribute attr = new Attribute("SomethingCached", "Some string");
0N/A mbs.setAttribute(mmbName, attr);
0N/A System.out.println("No exception thrown");
0N/A } catch (Exception e) {
0N/A System.out.println("TEST FAILED: Caught an exception: " + e);
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 throw new Exception("TEST FAILED");
0N/A }
0N/A }
0N/A
0N/A public static class Resource {
0N/A public String getSomething() {
0N/A return "Something value";
0N/A }
0N/A public String getSomethingCached() {
0N/A return "Something cached value";
0N/A }
0N/A }
0N/A
0N/A private static Resource resource = new Resource();
0N/A}