Path: blob/master/test/jdk/javax/management/query/CustomQueryTest.java
41149 views
/*1* Copyright (c) 2008, 2020, 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 669202726* @summary Check that custom subclasses of QueryEval can be serialized.27* @author Eamonn McManus28*/2930import java.io.ByteArrayInputStream;31import java.io.ByteArrayOutputStream;32import java.io.ObjectInputStream;33import java.io.ObjectOutputStream;34import java.lang.management.ManagementFactory;35import java.util.Set;36import java.util.concurrent.atomic.AtomicInteger;37import javax.management.MBeanServer;38import javax.management.MalformedObjectNameException;39import javax.management.ObjectName;40import javax.management.QueryEval;41import javax.management.QueryExp;4243public class CustomQueryTest {44public static interface CountMBean {45public int getCount();46public void increment();47}4849public static class Count implements CountMBean {50private AtomicInteger count = new AtomicInteger();5152public int getCount() {53return count.get();54}5556public void increment() {57count.incrementAndGet();58}5960}6162public static final ObjectName countName;63static {64try {65countName = new ObjectName("d:type=Count");66} catch (MalformedObjectNameException e) {67throw new AssertionError(e);68}69}7071/* A query that calls the increment method of the Count MBean every time72* it is evaluated. If there is no ObjectName filter, the query will be73* evaluated for every MBean in the MBean Server, so the count will be74* incremented by the number of MBeans.75*/76public static class IncrQuery extends QueryEval implements QueryExp {77public boolean apply(ObjectName name) {78try {79getMBeanServer().invoke(countName, "increment", null, null);80return true;81} catch (Throwable t) {82t.printStackTrace();83System.exit(1);84throw new AssertionError(); // not reached85}86}87}8889public static void main(String[] args) throws Exception {90MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();91boolean isSecondAttempt = false;92// The test may fail if some new MBean is registered while the test93// is running (e.g. Graal MBean). In this case just retry the test.94while (true) {95mbs.registerMBean(new Count(), countName);96int mbeanCount = mbs.getMBeanCount();97QueryExp query = new IncrQuery();98Set<ObjectName> names = mbs.queryNames(null, query);99assertEquals(mbeanCount, names.size());100assertEquals(mbeanCount, mbs.getAttribute(countName, "Count"));101ByteArrayOutputStream bout = new ByteArrayOutputStream();102ObjectOutputStream oout = new ObjectOutputStream(bout);103oout.writeObject(query);104oout.close();105ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());106ObjectInputStream oin = new ObjectInputStream(bin);107query = (QueryExp) oin.readObject();108names = mbs.queryNames(null, query);109int counterCount = (int)mbs.getAttribute(countName, "Count");110if (mbeanCount * 2 == counterCount) {111break;112}113if (isSecondAttempt) {114assertEquals(mbeanCount * 2, counterCount);115break;116}117isSecondAttempt = true;118System.out.println("New MBean was registered. Retrying...");119mbs.unregisterMBean(countName);120}121}122123private static void assertEquals(Object expected, Object actual)124throws Exception {125if (!expected.equals(actual)) {126String failure = "FAILED: expected " + expected + ", got " + actual;127throw new Exception(failure);128}129}130}131132133