QueryDottedAttrTest.java 6.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
/*
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

/*
 * @test QueryDottedAttrTest
 * @bug 6602310
 * @summary Test that Query.attr can understand a.b etc.
 * @author Eamonn McManus
 */

import java.beans.ConstructorProperties;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collections;
import java.util.Set;
import javax.management.AttributeNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import javax.management.Query;
import javax.management.QueryExp;
import javax.management.ReflectionException;
import javax.management.StandardMBean;

public class QueryDottedAttrTest {
    public static class Complex {
        private final double re, im;

        @ConstructorProperties({"real", "imaginary"})
        public Complex(double re, double im) {
            this.re = re;
            this.im = im;
        }

        public double getRe() {
            return re;
        }

        public double getIm() {
            return im;
        }
    }

    public static interface Intf {
        Complex getComplex();
        int[] getIntArray();
        String[] getStringArray();
    }

    public static class Impl implements Intf {
        public Complex getComplex() {
            return new Complex(1.0, 1.0);
        }

        public int[] getIntArray() {
            return new int[] {1, 2, 3};
        }

        public String[] getStringArray() {
            return new String[] {"one", "two", "three"};
        }
    }

    public static interface TestMBean extends Intf {}

    public static class Test extends Impl implements TestMBean {}

    public static interface TestMXBean extends Intf {}

    public static class TestMX extends Impl implements TestMXBean {}

    public static class AttrWithDot extends StandardMBean {
        public <T> AttrWithDot(Object impl, Class<T> intf) {
            super(intf.cast(impl), intf, (intf == TestMXBean.class));
        }

        public Object getAttribute(String attribute)
        throws AttributeNotFoundException, MBeanException, ReflectionException {
            if (attribute.equals("Complex.re"))
                return 2.0;
            else
                return super.getAttribute(attribute);
        }
    }

    private static final boolean[] booleans = {false, true};

    private static final QueryExp[] alwaysTrueQueries = {
        Query.eq(Query.attr("IntArray.length"), Query.value(3)),
        Query.eq(Query.attr("StringArray.length"), Query.value(3)),
        Query.eq(Query.attr("Complex.im"), Query.value(1.0)),
    };

    private static final QueryExp[] alwaysFalseQueries = {
        Query.eq(Query.attr("IntArray.length"), Query.value("3")),
        Query.eq(Query.attr("IntArray.length"), Query.value(2)),
        Query.eq(Query.attr("Complex.im"), Query.value(-1.0)),
        Query.eq(Query.attr("Complex.xxx"), Query.value(0)),
    };

    private static final QueryExp[] attrWithDotTrueQueries = {
        Query.eq(Query.attr("Complex.re"), Query.value(2.0)),
    };

    private static final QueryExp[] attrWithDotFalseQueries = {
        Query.eq(Query.attr("Complex.re"), Query.value(1.0)),
    };

    private static String failure;

    public static void main(String[] args) throws Exception {
        ObjectName name = new ObjectName("a:b=c");
        for (boolean attrWithDot : booleans) {
            for (boolean mx : booleans) {
                String what =
                        (mx ? "MXBean" : "Standard MBean") +
                        (attrWithDot ? " having attribute with dot in its name" : "");
                System.out.println("Testing " + what);
                Class<?> intf = mx ? TestMXBean.class : TestMBean.class;
                Object impl = mx ? new TestMX() : new Test();
                if (attrWithDot)
                    impl = new AttrWithDot(impl, intf);
                MBeanServer mbs = MBeanServerFactory.newMBeanServer();
                mbs.registerMBean(impl, name);
                boolean ismx = "true".equals(
                        mbs.getMBeanInfo(name).getDescriptor().getFieldValue("mxbean"));
                if (mx != ismx)
                    fail("MBean should " + (mx ? "" : "not ") + "be MXBean");
                test(mbs, name, alwaysTrueQueries, true);
                test(mbs, name, alwaysFalseQueries, false);
                test(mbs, name, attrWithDotTrueQueries, attrWithDot);
                test(mbs, name, attrWithDotFalseQueries, !attrWithDot);
            }
        }
        if (failure != null)
            throw new Exception("TEST FAILED: " + failure);
    }

    private static void test(
            MBeanServer mbs, ObjectName name, QueryExp[] queries, boolean expect)
            throws Exception {
        for (QueryExp query : queries) {
            // Serialize and deserialize the query to ensure that its
            // serialization is correct
            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();
            Set<ObjectName> names = mbs.queryNames(null, query);
            if (names.isEmpty()) {
                if (expect)
                    fail("Query is false but should be true: " + query);
            } else if (names.equals(Collections.singleton(name))) {
                if (!expect)
                    fail("Query is true but should be false: " + query);
            } else {
                fail("Query returned unexpected set: " + names);
            }
        }
    }

    private static void fail(String msg) {
        failure = msg;
        System.out.println("..." + msg);
    }
}