CustomQueryTest.java revision 2362
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 * 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 * 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 * 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 * 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 * @bug 6692027
6f3e57ac9d0b054c3169579f3422080b8ba10105mx * @summary Check that custom subclasses of QueryEval can be serialized.
51fc88a818087605a0e5f11eddb8b66576f72c23Winson Wang - Sun Microsystems - Beijing China * @author Eamonn McManus
6f3e57ac9d0b054c3169579f3422080b8ba10105mxpublic class CustomQueryTest {
6f3e57ac9d0b054c3169579f3422080b8ba10105mx public static interface CountMBean {
6f3e57ac9d0b054c3169579f3422080b8ba10105mx public int getCount();
6f3e57ac9d0b054c3169579f3422080b8ba10105mx public void increment();
6f3e57ac9d0b054c3169579f3422080b8ba10105mx public int getCount() {
6f3e57ac9d0b054c3169579f3422080b8ba10105mx public void increment() {
6f3e57ac9d0b054c3169579f3422080b8ba10105mx throw new AssertionError(e);
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 public static class IncrQuery extends QueryEval implements QueryExp {
6f3e57ac9d0b054c3169579f3422080b8ba10105mx getMBeanServer().invoke(countName, "increment", null, null);
6f3e57ac9d0b054c3169579f3422080b8ba10105mx return true;
6f3e57ac9d0b054c3169579f3422080b8ba10105mx } catch (Throwable t) {
6f3e57ac9d0b054c3169579f3422080b8ba10105mx public static void main(String[] args) throws Exception {
6f3e57ac9d0b054c3169579f3422080b8ba10105mx MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
6f3e57ac9d0b054c3169579f3422080b8ba10105mx assertEquals(mbeanCount, mbs.getAttribute(countName, "Count"));
6f3e57ac9d0b054c3169579f3422080b8ba10105mx ByteArrayOutputStream bout = new ByteArrayOutputStream();
6f3e57ac9d0b054c3169579f3422080b8ba10105mx ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
6f3e57ac9d0b054c3169579f3422080b8ba10105mx assertEquals(mbeanCount * 2, mbs.getAttribute(countName, "Count"));
6f3e57ac9d0b054c3169579f3422080b8ba10105mx private static void assertEquals(Object expected, Object actual)