Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/modelmbean/RequiredModelMBeanMethodTest.java
38840 views
/*1* Copyright (c) 2003, 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 495075626* @summary Test that RequiredModelMBean.invoke will not invoke methods27* from the RequiredModelMBean class itself if they are not in the28* ModelMBeanInfo29* @author Eamonn McManus30* @run clean RequiredModelMBeanMethodTest31* @run build RequiredModelMBeanMethodTest32* @run main RequiredModelMBeanMethodTest33*/3435import java.lang.reflect.*;36import javax.management.*;37import javax.management.modelmbean.*;3839/*40* We do the same test with a number of different operations:41*42* - A plain operation that is directed to the managed resource in the43* usual way. We give it some parameters so we can test that the44* class loading logic for signature parameters is reasonable.45*46* - An operation (removeNotificationListener) that is directed to the47* RequiredModelMBean itself. We use this particular operation because48* it will throw an exception, which allows us to check that it did in49* fact execute.50*51* - An operation (load()) that has the same signature as a52* RequiredModelMBean operation but is directed to the resource53* because of a "class" field in the descriptor.54*55* - An operation (store()) that has the same signature as a56* RequiredModelMBean operation but is directed to the resource57* because of a "targetObject" field in the descriptor.58*59* In each case we check that the operation does not work if it is not60* in the ModelMBeanInfo, and does work if it is.61*/62public class RequiredModelMBeanMethodTest {63public static void main(String[] args) throws Exception {64boolean ok = true;65MBeanServer mbs = MBeanServerFactory.createMBeanServer();6667Descriptor tralalaDescriptor =68new DescriptorSupport(new String[] {69"name=tralala",70"descriptorType=operation",71"role=operation",72"targetType=ObjectReference",73});74Method tralalaMethod =75Resource.class.getMethod("tralala",76new Class[] {int.class, Resource.class});77ModelMBeanOperationInfo tralalaInfo =78new ModelMBeanOperationInfo("tralala descr", tralalaMethod,79tralalaDescriptor);8081Method remACNLMethod =82RequiredModelMBean.class.getMethod("removeAttributeChangeNotificationListener",83new Class[] {84NotificationListener.class,85String.class86});87ModelMBeanOperationInfo remACNLInfo =88new ModelMBeanOperationInfo("remACNL descr", remACNLMethod);8990Descriptor loadDescriptor =91new DescriptorSupport(new String[] {92"name=load",93"descriptorType=operation",94"role=operation",95"targetType=ObjectReference",96"class=" + Resource.class.getName(),97});98ModelMBeanOperationInfo loadInfo =99new ModelMBeanOperationInfo("load", "load descr",100new MBeanParameterInfo[0],101"void", ModelMBeanOperationInfo.ACTION,102loadDescriptor);103104Descriptor storeDescriptor =105new DescriptorSupport(new String[] {106"name=store",107"descriptorType=operation",108"role=operation",109"targetType=ObjectReference",110});111storeDescriptor.setField("targetObject", resource);112ModelMBeanOperationInfo storeInfo =113new ModelMBeanOperationInfo("store", "store descr",114new MBeanParameterInfo[0],115"void", ModelMBeanOperationInfo.ACTION,116storeDescriptor);117118ModelMBeanInfo emptyMMBI =119new ModelMBeanInfoSupport(Resource.class.getName(),120"empty descr",121null, null, null, null);122ModelMBean emptyMMB = new RequiredModelMBean(emptyMMBI);123emptyMMB.setManagedResource(resource, "ObjectReference");124ObjectName emptyMMBName = new ObjectName("test:type=Empty");125mbs.registerMBean(emptyMMB, emptyMMBName);126127System.out.println("Testing that we cannot call methods not in the " +128"ModelMBeanInfo");129try {130boolean thisok = test(mbs, emptyMMBName, false);131if (thisok)132System.out.println("...OK");133else134ok = false;135} catch (Exception e) {136System.out.println("TEST FAILED: Caught exception:");137e.printStackTrace(System.out);138ok = false;139}140141ModelMBeanOperationInfo[] opInfos = {142tralalaInfo, remACNLInfo, loadInfo, storeInfo,143};144ModelMBeanInfo fullMMBI =145new ModelMBeanInfoSupport(Resource.class.getName(),146"full descr",147null, null, opInfos, null);148ModelMBean fullMMB = new RequiredModelMBean(fullMMBI);149fullMMB.setManagedResource(resource, "ObjectReference");150ObjectName fullMMBName = new ObjectName("test:type=Full");151mbs.registerMBean(fullMMB, fullMMBName);152153154System.out.println();155System.out.println("Testing that we can call methods in the " +156"ModelMBeanInfo");157System.out.println(" and that \"class\" or \"targetObject\" in " +158"descriptor directs methods to resource");159try {160boolean thisok = test(mbs, fullMMBName, true);161if (thisok)162System.out.println("...OK");163else164ok = false;165} catch (Exception e) {166System.out.println("TEST FAILED: Caught exception:");167e.printStackTrace(System.out);168ok = false;169}170171if (ok) {172if (!resource.loadCalled || !resource.storeCalled) {173System.out.println("TEST FAILED: not called:" +174(resource.loadCalled ? "" : " load") +175(resource.storeCalled ? "" : " store"));176ok = false;177}178}179180// Test the invoke("class.method") form181if (ok) {182System.out.println("Testing invoke(\"class.method\")");183resource.loadCalled = false;184mbs.invoke(fullMMBName, Resource.class.getName() + ".load",185null, null);186if (!resource.loadCalled) {187System.out.println("TEST FAILED: load not called");188ok = false;189}190try {191mbs.invoke(fullMMBName,192RequiredModelMBean.class.getName() +193".removeAttributeChangeNotificationListener",194new Object[] {boringListener, null},195new String[] {196NotificationListener.class.getName(),197String.class.getName(),198});199System.out.println("TEST FAILED: removeNotificationListener" +200" returned successfully but " +201"should not have");202ok = false;203} catch (MBeanException e) {204final Exception target = e.getTargetException();205if (target instanceof ListenerNotFoundException) {206// OK: there is no such listener207} else208throw e;209}210}211212if (ok)213System.out.println("Test passed");214else {215System.out.println("TEST FAILED");216System.exit(1);217}218}219220private static boolean test(MBeanServer mbs, ObjectName name,221boolean shouldWork)222throws Exception {223224boolean ok = true;225226final String[] names = {227"tralala",228"removeAttributeChangeNotificationListener",229"load",230"store",231};232233for (int i = 0; i < 4; i++) {234boolean thisok = true;235try {236switch (i) {237case 0:238String tralala = (String)239mbs.invoke(name, names[i],240new Object[] {new Integer(5), resource},241new String[] {"int",242Resource.class.getName()});243if (!"tralala".equals(tralala)) {244System.out.println("TEST FAILED: tralala returned: " +245tralala);246thisok = false;247}248break;249case 1:250try {251mbs.invoke(name,252names[i],253new Object[] {boringListener, null},254new String[] {255NotificationListener.class.getName(),256String.class.getName(),257});258System.out.println("TEST FAILED: " + names[i] +259" returned successfully but " +260"should not have");261thisok = false;262} catch (MBeanException e) {263final Exception target = e.getTargetException();264if (target instanceof ListenerNotFoundException) {265// OK: there is no such listener266} else267throw e;268}269break;270case 2:271case 3:272mbs.invoke(name,273names[i],274new Object[0],275new String[0]);276break;277default:278throw new AssertionError();279}280281thisok = shouldWork;282if (!shouldWork) {283System.out.println("TEST FAILED: " + names[i] +284" worked but should not");285}286} catch (MBeanException e) {287if (shouldWork) {288System.out.println("TEST FAILED: " + names[i] + ": " + e);289e.printStackTrace(System.out);290thisok = false;291} else {292Exception target = e.getTargetException();293if (!(target instanceof ServiceNotFoundException)) {294System.out.println("TEST FAILED: " + names[i] +295": wrong exception: " + target);296thisok = false;297}298}299} catch (Exception e) {300System.out.println("TEST FAILED: " + names[i] + ": " + e);301e.printStackTrace(System.out);302thisok = false;303}304305if (thisok)306System.out.println("OK: " + names[i]);307else308ok = false;309}310311return ok;312}313314public static class Resource {315public String tralala(int x, Resource y) {316if (x != 5 || y != this)317return "wrong params: " + x + " " + y;318return "tralala";319}320321public void load() {322loadCalled = true;323}324325public void store() {326storeCalled = true;327}328329boolean loadCalled, storeCalled;330}331332private static Resource resource = new Resource();333334private static NotificationListener boringListener =335new NotificationListener() {336public void handleNotification(Notification n, Object h) {337}338};339}340341342