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