jdk/test/javax/management/query/QueryDottedAttrTest.java
author emcmanus
Mon, 03 Mar 2008 10:32:38 +0100
changeset 34 2d042367885f
permissions -rw-r--r--
6602310: Extensions to Query API for JMX 2.0 6604768: IN queries require their arguments to be constants Summary: New JMX query language and support for dotted attributes in queries. Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
     1
/*
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
     2
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
     4
 *
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
     8
 *
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    13
 * accompanied this code).
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    14
 *
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    18
 *
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    21
 * have any questions.
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    22
 */
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    23
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    24
/*
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    25
 * @test QueryDottedAttrTest
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    26
 * @bug 6602310
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    27
 * @summary Test that Query.attr can understand a.b etc.
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    28
 * @author Eamonn McManus
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    29
 */
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    30
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    31
import java.beans.ConstructorProperties;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    32
import java.io.ByteArrayInputStream;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    33
import java.io.ByteArrayOutputStream;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    34
import java.io.ObjectInputStream;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    35
import java.io.ObjectOutputStream;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    36
import java.util.Collections;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    37
import java.util.Set;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    38
import javax.management.AttributeNotFoundException;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    39
import javax.management.MBeanException;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    40
import javax.management.MBeanServer;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    41
import javax.management.MBeanServerFactory;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    42
import javax.management.ObjectName;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    43
import javax.management.Query;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    44
import javax.management.QueryExp;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    45
import javax.management.ReflectionException;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    46
import javax.management.StandardMBean;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    47
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    48
public class QueryDottedAttrTest {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    49
    public static class Complex {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    50
        private final double re, im;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    51
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    52
        @ConstructorProperties({"real", "imaginary"})
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    53
        public Complex(double re, double im) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    54
            this.re = re;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    55
            this.im = im;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    56
        }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    57
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    58
        public double getRe() {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    59
            return re;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    60
        }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    61
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    62
        public double getIm() {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    63
            return im;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    64
        }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    65
    }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    66
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    67
    public static interface Intf {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    68
        Complex getComplex();
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    69
        int[] getIntArray();
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    70
        String[] getStringArray();
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    71
    }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    72
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    73
    public static class Impl implements Intf {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    74
        public Complex getComplex() {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    75
            return new Complex(1.0, 1.0);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    76
        }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    77
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    78
        public int[] getIntArray() {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    79
            return new int[] {1, 2, 3};
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    80
        }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    81
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    82
        public String[] getStringArray() {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    83
            return new String[] {"one", "two", "three"};
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    84
        }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    85
    }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    86
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    87
    public static interface TestMBean extends Intf {}
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    88
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    89
    public static class Test extends Impl implements TestMBean {}
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    90
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    91
    public static interface TestMXBean extends Intf {}
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    92
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    93
    public static class TestMX extends Impl implements TestMXBean {}
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    94
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    95
    public static class AttrWithDot extends StandardMBean {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    96
        public <T> AttrWithDot(Object impl, Class<T> intf) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    97
            super(intf.cast(impl), intf, (intf == TestMXBean.class));
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    98
        }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
    99
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   100
        public Object getAttribute(String attribute)
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   101
        throws AttributeNotFoundException, MBeanException, ReflectionException {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   102
            if (attribute.equals("Complex.re"))
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   103
                return 2.0;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   104
            else
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   105
                return super.getAttribute(attribute);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   106
        }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   107
    }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   108
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   109
    private static final boolean[] booleans = {false, true};
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   110
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   111
    private static final QueryExp[] alwaysTrueQueries = {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   112
        Query.eq(Query.attr("IntArray.length"), Query.value(3)),
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   113
        Query.eq(Query.attr("StringArray.length"), Query.value(3)),
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   114
        Query.eq(Query.attr("Complex.im"), Query.value(1.0)),
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   115
    };
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   116
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   117
    private static final QueryExp[] alwaysFalseQueries = {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   118
        Query.eq(Query.attr("IntArray.length"), Query.value("3")),
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   119
        Query.eq(Query.attr("IntArray.length"), Query.value(2)),
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   120
        Query.eq(Query.attr("Complex.im"), Query.value(-1.0)),
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   121
        Query.eq(Query.attr("Complex.xxx"), Query.value(0)),
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   122
    };
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   123
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   124
    private static final QueryExp[] attrWithDotTrueQueries = {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   125
        Query.eq(Query.attr("Complex.re"), Query.value(2.0)),
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   126
    };
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   127
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   128
    private static final QueryExp[] attrWithDotFalseQueries = {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   129
        Query.eq(Query.attr("Complex.re"), Query.value(1.0)),
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   130
    };
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   131
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   132
    private static String failure;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   133
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   134
    public static void main(String[] args) throws Exception {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   135
        ObjectName name = new ObjectName("a:b=c");
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   136
        for (boolean attrWithDot : booleans) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   137
            for (boolean mx : booleans) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   138
                String what =
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   139
                        (mx ? "MXBean" : "Standard MBean") +
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   140
                        (attrWithDot ? " having attribute with dot in its name" : "");
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   141
                System.out.println("Testing " + what);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   142
                Class<?> intf = mx ? TestMXBean.class : TestMBean.class;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   143
                Object impl = mx ? new TestMX() : new Test();
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   144
                if (attrWithDot)
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   145
                    impl = new AttrWithDot(impl, intf);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   146
                MBeanServer mbs = MBeanServerFactory.newMBeanServer();
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   147
                mbs.registerMBean(impl, name);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   148
                boolean ismx = "true".equals(
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   149
                        mbs.getMBeanInfo(name).getDescriptor().getFieldValue("mxbean"));
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   150
                if (mx != ismx)
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   151
                    fail("MBean should " + (mx ? "" : "not ") + "be MXBean");
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   152
                test(mbs, name, alwaysTrueQueries, true);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   153
                test(mbs, name, alwaysFalseQueries, false);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   154
                test(mbs, name, attrWithDotTrueQueries, attrWithDot);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   155
                test(mbs, name, attrWithDotFalseQueries, !attrWithDot);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   156
            }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   157
        }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   158
        if (failure != null)
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   159
            throw new Exception("TEST FAILED: " + failure);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   160
    }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   161
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   162
    private static void test(
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   163
            MBeanServer mbs, ObjectName name, QueryExp[] queries, boolean expect)
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   164
            throws Exception {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   165
        for (QueryExp query : queries) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   166
            // Serialize and deserialize the query to ensure that its
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   167
            // serialization is correct
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   168
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   169
            ObjectOutputStream oout = new ObjectOutputStream(bout);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   170
            oout.writeObject(query);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   171
            oout.close();
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   172
            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   173
            ObjectInputStream oin = new ObjectInputStream(bin);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   174
            query = (QueryExp) oin.readObject();
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   175
            Set<ObjectName> names = mbs.queryNames(null, query);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   176
            if (names.isEmpty()) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   177
                if (expect)
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   178
                    fail("Query is false but should be true: " + query);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   179
            } else if (names.equals(Collections.singleton(name))) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   180
                if (!expect)
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   181
                    fail("Query is true but should be false: " + query);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   182
            } else {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   183
                fail("Query returned unexpected set: " + names);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   184
            }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   185
        }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   186
    }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   187
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   188
    private static void fail(String msg) {
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   189
        failure = msg;
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   190
        System.out.println("..." + msg);
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   191
    }
2d042367885f 6602310: Extensions to Query API for JMX 2.0
emcmanus
parents:
diff changeset
   192
}