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