248N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
248N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
248N/A *
248N/A * This code is free software; you can redistribute it and/or modify it
248N/A * under the terms of the GNU General Public License version 2 only, as
248N/A * published by the Free Software Foundation.
248N/A *
248N/A * This code is distributed in the hope that it will be useful, but WITHOUT
248N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
248N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
248N/A * version 2 for more details (a copy is included in the LICENSE file that
248N/A * accompanied this code).
248N/A *
248N/A * You should have received a copy of the GNU General Public License version
248N/A * 2 along with this work; if not, write to the Free Software Foundation,
248N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
248N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
248N/A */
248N/A
248N/A/*
248N/A * @test
248N/A * @bug 6692027
248N/A * @summary Check that custom subclasses of QueryEval can be serialized.
248N/A * @author Eamonn McManus
248N/A */
248N/A
248N/Aimport java.io.ByteArrayInputStream;
248N/Aimport java.io.ByteArrayOutputStream;
248N/Aimport java.io.ObjectInputStream;
248N/Aimport java.io.ObjectOutputStream;
248N/Aimport java.lang.management.ManagementFactory;
248N/Aimport java.util.Set;
248N/Aimport java.util.concurrent.atomic.AtomicInteger;
248N/Aimport javax.management.MBeanServer;
248N/Aimport javax.management.MalformedObjectNameException;
248N/Aimport javax.management.ObjectName;
248N/Aimport javax.management.QueryEval;
248N/Aimport javax.management.QueryExp;
248N/A
248N/Apublic class CustomQueryTest {
248N/A public static interface CountMBean {
248N/A public int getCount();
248N/A public void increment();
248N/A }
248N/A
248N/A public static class Count implements CountMBean {
248N/A private AtomicInteger count = new AtomicInteger();
248N/A
248N/A public int getCount() {
248N/A return count.get();
248N/A }
248N/A
248N/A public void increment() {
248N/A count.incrementAndGet();
248N/A }
248N/A
248N/A }
248N/A
248N/A public static final ObjectName countName;
248N/A static {
248N/A try {
248N/A countName = new ObjectName("d:type=Count");
248N/A } catch (MalformedObjectNameException e) {
248N/A throw new AssertionError(e);
248N/A }
248N/A }
248N/A
248N/A /* A query that calls the increment method of the Count MBean every time
248N/A * it is evaluated. If there is no ObjectName filter, the query will be
248N/A * evaluated for every MBean in the MBean Server, so the count will be
248N/A * incremented by the number of MBeans.
248N/A */
248N/A public static class IncrQuery extends QueryEval implements QueryExp {
248N/A public boolean apply(ObjectName name) {
248N/A try {
248N/A getMBeanServer().invoke(countName, "increment", null, null);
248N/A return true;
248N/A } catch (Throwable t) {
248N/A t.printStackTrace();
248N/A System.exit(1);
248N/A throw new AssertionError(); // not reached
248N/A }
248N/A }
248N/A }
248N/A
248N/A public static void main(String[] args) throws Exception {
248N/A MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
248N/A mbs.registerMBean(new Count(), countName);
248N/A int mbeanCount = mbs.getMBeanCount();
248N/A QueryExp query = new IncrQuery();
248N/A Set<ObjectName> names = mbs.queryNames(null, query);
248N/A assertEquals(mbeanCount, names.size());
248N/A assertEquals(mbeanCount, mbs.getAttribute(countName, "Count"));
248N/A ByteArrayOutputStream bout = new ByteArrayOutputStream();
248N/A ObjectOutputStream oout = new ObjectOutputStream(bout);
248N/A oout.writeObject(query);
248N/A oout.close();
248N/A ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
248N/A ObjectInputStream oin = new ObjectInputStream(bin);
248N/A query = (QueryExp) oin.readObject();
248N/A names = mbs.queryNames(null, query);
248N/A assertEquals(mbeanCount * 2, mbs.getAttribute(countName, "Count"));
248N/A }
248N/A
248N/A private static void assertEquals(Object expected, Object actual)
248N/A throws Exception {
248N/A if (!expected.equals(actual)) {
248N/A String failure = "FAILED: expected " + expected + ", got " + actual;
248N/A throw new Exception(failure);
248N/A }
248N/A }
248N/A}