Path: blob/master/test/jdk/javax/management/query/InstanceOfExpTest.java
41149 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 5072174 633584826* @summary test the new method javax.management.Query.isInstanceOf("className")27* @author Shanliang JIANG28*29* @run clean InstanceOfExpTest30* @run build InstanceOfExpTest31* @run main InstanceOfExpTest32*/3334import java.util.*;35import java.lang.management.ManagementFactory;3637import javax.management.*;3839public class InstanceOfExpTest {4041public static class Simple implements SimpleMBean {}42public static interface SimpleMBean {}4344public static void main(String[] args) throws Exception {45System.out.println(">>> Test the method javax.management.Query.isInstanceOf");4647MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();48final String className = "javax.management.NotificationBroadcaster";4950final ObjectName name1 = new ObjectName("test:simple=1");51mbs.createMBean(Simple.class.getName(), name1);5253final ObjectName name2 = new ObjectName("test:timer=1");54mbs.createMBean("javax.management.timer.Timer", name2);5556QueryExp exp = Query.isInstanceOf(Query.value(className));57Set<ObjectName> list = mbs.queryNames(new ObjectName("*:*"), exp);5859if (list.contains(name1) || !list.contains(name2)) {60throw new RuntimeException("InstanceOfExp does not work.");61}6263for (ObjectName on : list) {64if (!mbs.isInstanceOf(on, className)) {65throw new RuntimeException("InstanceOfQueryExp does not work.");66}67}6869Set<ObjectName> all = mbs.queryNames(null, null);70for (ObjectName n : all) {71if (mbs.isInstanceOf(n, className) != list.contains(n))72throw new RuntimeException("InstanceOfExp does not work.");73}7475try {76QueryExp exp1 = Query.isInstanceOf(null);77throw new RuntimeException("Not got an exception with a null class name.");78} catch (IllegalArgumentException iae) {79// OK. Good80}81}82}838485