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