Path: blob/master/test/jdk/javax/management/modelmbean/RequiredModelMBeanSetAttributeTest.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 499703326* @summary Test the following in RequiredModelMBean.setAttribute():27* MBeanException wrapping a ServiceNotFoundException is thrown is setAttribute28* called but no setMethod field has been provided.29* @author Jean-Francois Denise30*31* @run clean RequiredModelMBeanSetAttributeTest32* @run build RequiredModelMBeanSetAttributeTest33* @run main RequiredModelMBeanSetAttributeTest34*/3536import javax.management.Descriptor;37import javax.management.MBeanServer;38import javax.management.MBeanServerFactory;39import javax.management.ObjectName;40import javax.management.Attribute;41import javax.management.MBeanException;42import javax.management.ServiceNotFoundException;43import javax.management.modelmbean.DescriptorSupport;44import javax.management.modelmbean.ModelMBean;45import javax.management.modelmbean.ModelMBeanAttributeInfo;46import javax.management.modelmbean.ModelMBeanInfo;47import javax.management.modelmbean.ModelMBeanInfoSupport;48import javax.management.modelmbean.ModelMBeanOperationInfo;49import javax.management.modelmbean.RequiredModelMBean;5051public class RequiredModelMBeanSetAttributeTest {5253public static void main(String[] args) throws Exception {5455boolean ok = true;5657MBeanServer mbs = MBeanServerFactory.createMBeanServer();5859// ModelMBeanAttributeInfo6061Descriptor somethingAttributeDescriptor =62new DescriptorSupport(new String[] {63"name=Something",64"descriptorType=attribute",65"getMethod=getSomething"66});67ModelMBeanAttributeInfo somethingAttributeInfo =68new ModelMBeanAttributeInfo("Something",69"java.lang.String",70"Something attribute",71true,72true,73false,74somethingAttributeDescriptor);7576Descriptor somethingCachedAttributeDescriptor =77new DescriptorSupport(new String[] {78"name=SomethingCached",79"descriptorType=attribute",80"getMethod=getSomethingCached",81"currencyTimeLimit=5000"82});83ModelMBeanAttributeInfo somethingCachedAttributeInfo =84new ModelMBeanAttributeInfo("SomethingCached",85"java.lang.String",86"Something cached attribute",87true,88true,89false,90somethingCachedAttributeDescriptor);91// ModelMBeanInfo9293ModelMBeanInfo mmbi = new ModelMBeanInfoSupport(94Resource.class.getName(),95"Resource MBean",96new ModelMBeanAttributeInfo[] { somethingAttributeInfo, somethingCachedAttributeInfo },97null,98new ModelMBeanOperationInfo[] {},99null);100101// RequiredModelMBean102103ModelMBean mmb = new RequiredModelMBean(mmbi);104mmb.setManagedResource(resource, "ObjectReference");105ObjectName mmbName = new ObjectName(":type=ResourceMBean");106mbs.registerMBean(mmb, mmbName);107108// Run tests109110System.out.println("\nTest that we receive ServiceNotFoundException");111try {112Attribute attr = new Attribute("Something", "Some string");113mbs.setAttribute(mmbName, attr);114System.out.println("TEST FAILED: Didn't caught exception");115ok = false;116} catch(MBeanException mbex) {117Exception e = mbex.getTargetException();118if(e == null || !(e instanceof ServiceNotFoundException)) {119System.out.println("TEST FAILED: Caught wrong exception:" + e);120ok = false;121} else122System.out.println("Received expected ServiceNotFoundException");123124} catch (Exception e) {125System.out.println("TEST FAILED: Caught wrong exception: " + e);126e.printStackTrace(System.out);127ok = false;128}129130//Now check that when caching is enabled, setAttribute is working131System.out.println("\nTest that we are not receiving ServiceNotFoundException");132try {133Attribute attr = new Attribute("SomethingCached", "Some string");134mbs.setAttribute(mmbName, attr);135System.out.println("No exception thrown");136} catch (Exception e) {137System.out.println("TEST FAILED: Caught an exception: " + e);138e.printStackTrace(System.out);139ok = false;140}141142if (ok)143System.out.println("Test passed");144else {145System.out.println("TEST FAILED");146throw new Exception("TEST FAILED");147}148}149150public static class Resource {151public String getSomething() {152return "Something value";153}154public String getSomethingCached() {155return "Something cached value";156}157}158159private static Resource resource = new Resource();160}161162163