jdk/test/javax/management/query/CustomQueryTest.java
author ykantser
Thu, 07 May 2015 09:11:49 +0200
changeset 30376 2ccf2cf7ea48
parent 5506 202f599c92aa
child 44423 306c020eb154
permissions -rw-r--r--
8078896: Add @modules as needed to the jdk_svc tests Reviewed-by: alanb, mchung
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
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
     2
 * Copyright (c) 2008, 2015, 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
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    29
 * @modules java.management
471
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
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    32
import java.io.ByteArrayInputStream;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    33
import java.io.ByteArrayOutputStream;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    34
import java.io.ObjectInputStream;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    35
import java.io.ObjectOutputStream;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    36
import java.lang.management.ManagementFactory;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    37
import java.util.Set;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    38
import java.util.concurrent.atomic.AtomicInteger;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    39
import javax.management.MBeanServer;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    40
import javax.management.MalformedObjectNameException;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    41
import javax.management.ObjectName;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    42
import javax.management.QueryEval;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    43
import javax.management.QueryExp;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    44
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    45
public class CustomQueryTest {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    46
    public static interface CountMBean {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    47
        public int getCount();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    48
        public void increment();
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
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    51
    public static class Count implements CountMBean {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    52
        private AtomicInteger count = new AtomicInteger();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    53
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    54
        public int getCount() {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    55
            return count.get();
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
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    58
        public void increment() {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    59
            count.incrementAndGet();
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
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    64
    public static final ObjectName countName;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    65
    static {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    66
        try {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    67
            countName = new ObjectName("d:type=Count");
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    68
        } catch (MalformedObjectNameException e) {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    69
            throw new AssertionError(e);
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
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    73
    /* 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
    74
     * 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
    75
     * 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
    76
     * incremented by the number of MBeans.
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    77
     */
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    78
    public static class IncrQuery extends QueryEval implements QueryExp {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    79
        public boolean apply(ObjectName name) {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    80
            try {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    81
                getMBeanServer().invoke(countName, "increment", null, null);
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    82
                return true;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    83
            } catch (Throwable t) {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    84
                t.printStackTrace();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    85
                System.exit(1);
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    86
                throw new AssertionError(); // not reached
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
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    91
    public static void main(String[] args) throws Exception {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    92
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    93
        mbs.registerMBean(new Count(), countName);
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    94
        int mbeanCount = mbs.getMBeanCount();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    95
        QueryExp query = new IncrQuery();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    96
        Set<ObjectName> names = mbs.queryNames(null, query);
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    97
        assertEquals(mbeanCount, names.size());
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    98
        assertEquals(mbeanCount, mbs.getAttribute(countName, "Count"));
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
    99
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   100
        ObjectOutputStream oout = new ObjectOutputStream(bout);
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   101
        oout.writeObject(query);
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   102
        oout.close();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   103
        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   104
        ObjectInputStream oin = new ObjectInputStream(bin);
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   105
        query = (QueryExp) oin.readObject();
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   106
        names = mbs.queryNames(null, query);
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   107
        assertEquals(mbeanCount * 2, mbs.getAttribute(countName, "Count"));
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
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   110
    private static void assertEquals(Object expected, Object actual)
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   111
            throws Exception {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   112
        if (!expected.equals(actual)) {
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   113
            String failure = "FAILED: expected " + expected + ", got " + actual;
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   114
            throw new Exception(failure);
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
    }
422ff0db58d3 6692027: Custom subclasses of QueryEval don't serialize
emcmanus
parents:
diff changeset
   117
}