Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/modelmbean/RequiredModelMBeanSetAttributeTest.java
38840 views
/*1* Copyright (c) 2005, 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* @run clean RequiredModelMBeanSetAttributeTest31* @run build RequiredModelMBeanSetAttributeTest32* @run main RequiredModelMBeanSetAttributeTest33*/3435import javax.management.Descriptor;36import javax.management.MBeanServer;37import javax.management.MBeanServerFactory;38import javax.management.ObjectName;39import javax.management.Attribute;40import javax.management.MBeanException;41import javax.management.ServiceNotFoundException;42import javax.management.modelmbean.DescriptorSupport;43import javax.management.modelmbean.ModelMBean;44import javax.management.modelmbean.ModelMBeanAttributeInfo;45import javax.management.modelmbean.ModelMBeanInfo;46import javax.management.modelmbean.ModelMBeanInfoSupport;47import javax.management.modelmbean.ModelMBeanOperationInfo;48import javax.management.modelmbean.RequiredModelMBean;4950public class RequiredModelMBeanSetAttributeTest {5152public static void main(String[] args) throws Exception {5354boolean ok = true;5556MBeanServer mbs = MBeanServerFactory.createMBeanServer();5758// ModelMBeanAttributeInfo5960Descriptor somethingAttributeDescriptor =61new DescriptorSupport(new String[] {62"name=Something",63"descriptorType=attribute",64"getMethod=getSomething"65});66ModelMBeanAttributeInfo somethingAttributeInfo =67new ModelMBeanAttributeInfo("Something",68"java.lang.String",69"Something attribute",70true,71true,72false,73somethingAttributeDescriptor);7475Descriptor somethingCachedAttributeDescriptor =76new DescriptorSupport(new String[] {77"name=SomethingCached",78"descriptorType=attribute",79"getMethod=getSomethingCached",80"currencyTimeLimit=5000"81});82ModelMBeanAttributeInfo somethingCachedAttributeInfo =83new ModelMBeanAttributeInfo("SomethingCached",84"java.lang.String",85"Something cached attribute",86true,87true,88false,89somethingCachedAttributeDescriptor);90// ModelMBeanInfo9192ModelMBeanInfo mmbi = new ModelMBeanInfoSupport(93Resource.class.getName(),94"Resource MBean",95new ModelMBeanAttributeInfo[] { somethingAttributeInfo, somethingCachedAttributeInfo },96null,97new ModelMBeanOperationInfo[] {},98null);99100// RequiredModelMBean101102ModelMBean mmb = new RequiredModelMBean(mmbi);103mmb.setManagedResource(resource, "ObjectReference");104ObjectName mmbName = new ObjectName(":type=ResourceMBean");105mbs.registerMBean(mmb, mmbName);106107// Run tests108109System.out.println("\nTest that we receive ServiceNotFoundException");110try {111Attribute attr = new Attribute("Something", "Some string");112mbs.setAttribute(mmbName, attr);113System.out.println("TEST FAILED: Didn't caught exception");114ok = false;115} catch(MBeanException mbex) {116Exception e = mbex.getTargetException();117if(e == null || !(e instanceof ServiceNotFoundException)) {118System.out.println("TEST FAILED: Caught wrong exception:" + e);119ok = false;120} else121System.out.println("Received expected ServiceNotFoundException");122123} catch (Exception e) {124System.out.println("TEST FAILED: Caught wrong exception: " + e);125e.printStackTrace(System.out);126ok = false;127}128129//Now check that when caching is enabled, setAttribute is working130System.out.println("\nTest that we are not receiving ServiceNotFoundException");131try {132Attribute attr = new Attribute("SomethingCached", "Some string");133mbs.setAttribute(mmbName, attr);134System.out.println("No exception thrown");135} catch (Exception e) {136System.out.println("TEST FAILED: Caught an exception: " + e);137e.printStackTrace(System.out);138ok = false;139}140141if (ok)142System.out.println("Test passed");143else {144System.out.println("TEST FAILED");145throw new Exception("TEST FAILED");146}147}148149public static class Resource {150public String getSomething() {151return "Something value";152}153public String getSomethingCached() {154return "Something cached value";155}156}157158private static Resource resource = new Resource();159}160161162