jdk/test/javax/management/query/CustomQueryTest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 471 422ff0db58d3
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
471
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 471
diff changeset
     2
 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
471
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
     4
 *
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
     7
 * published by the Free Software Foundation.
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
     8
 *
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    13
 * accompanied this code).
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    14
 *
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 471
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 471
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 471
diff changeset
    21
 * questions.
471
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    22
 */
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    23
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    24
/*
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    25
 * @test
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    26
 * @bug 6692027
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    27
 * @summary Check that custom subclasses of QueryEval can be serialized.
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    28
 * @author Eamonn McManus
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    29
 */
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    30
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    31
import java.io.ByteArrayInputStream;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    32
import java.io.ByteArrayOutputStream;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    33
import java.io.ObjectInputStream;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    34
import java.io.ObjectOutputStream;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    35
import java.lang.management.ManagementFactory;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    36
import java.util.Set;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    37
import java.util.concurrent.atomic.AtomicInteger;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    38
import javax.management.MBeanServer;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    39
import javax.management.MalformedObjectNameException;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    40
import javax.management.ObjectName;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    41
import javax.management.QueryEval;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    42
import javax.management.QueryExp;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    43
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    44
public class CustomQueryTest {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    45
    public static interface CountMBean {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    46
        public int getCount();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    47
        public void increment();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    48
    }
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    49
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    50
    public static class Count implements CountMBean {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    51
        private AtomicInteger count = new AtomicInteger();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    52
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    53
        public int getCount() {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    54
            return count.get();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    55
        }
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    56
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    57
        public void increment() {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    58
            count.incrementAndGet();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    59
        }
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    60
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    61
    }
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    62
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    63
    public static final ObjectName countName;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    64
    static {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    65
        try {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    66
            countName = new ObjectName("d:type=Count");
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    67
        } catch (MalformedObjectNameException e) {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    68
            throw new AssertionError(e);
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    69
        }
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    70
    }
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    71
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    72
    /* A query that calls the increment method of the Count MBean every time
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    73
     * it is evaluated.  If there is no ObjectName filter, the query will be
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    74
     * evaluated for every MBean in the MBean Server, so the count will be
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    75
     * incremented by the number of MBeans.
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    76
     */
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    77
    public static class IncrQuery extends QueryEval implements QueryExp {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    78
        public boolean apply(ObjectName name) {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    79
            try {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    80
                getMBeanServer().invoke(countName, "increment", null, null);
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    81
                return true;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    82
            } catch (Throwable t) {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    83
                t.printStackTrace();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    84
                System.exit(1);
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    85
                throw new AssertionError(); // not reached
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    86
            }
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    87
        }
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    88
    }
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    89
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    90
    public static void main(String[] args) throws Exception {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    91
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    92
        mbs.registerMBean(new Count(), countName);
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    93
        int mbeanCount = mbs.getMBeanCount();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    94
        QueryExp query = new IncrQuery();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    95
        Set<ObjectName> names = mbs.queryNames(null, query);
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    96
        assertEquals(mbeanCount, names.size());
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    97
        assertEquals(mbeanCount, mbs.getAttribute(countName, "Count"));
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    98
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    99
        ObjectOutputStream oout = new ObjectOutputStream(bout);
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   100
        oout.writeObject(query);
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   101
        oout.close();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   102
        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   103
        ObjectInputStream oin = new ObjectInputStream(bin);
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   104
        query = (QueryExp) oin.readObject();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   105
        names = mbs.queryNames(null, query);
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   106
        assertEquals(mbeanCount * 2, mbs.getAttribute(countName, "Count"));
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   107
    }
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   108
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   109
    private static void assertEquals(Object expected, Object actual)
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   110
            throws Exception {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   111
        if (!expected.equals(actual)) {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   112
            String failure = "FAILED: expected " + expected + ", got " + actual;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   113
            throw new Exception(failure);
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   114
        }
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   115
    }
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   116
}