File: MBeanInfoEqualsTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (380 lines) | stat: -rw-r--r-- 17,166 bytes parent folder | download | duplicates (16)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 4719923
 * @summary Test that MBeanInfo.equals works even for mutable subclasses
 * @author Eamonn McManus
 *
 * @run clean MBeanInfoEqualsTest
 * @run build MBeanInfoEqualsTest
 * @run main MBeanInfoEqualsTest
 */

/* Test that MBeanInfo and its referenced classes implement the equals
   and hashCode methods correctly.  These classes include some magic
   to improve performance based on the classes' immutability.  The
   logic checks that any subclasses encountered remain immutable, and
   falls back on less performant code if not.  */

import javax.management.*;
import java.lang.reflect.*;

public class MBeanInfoEqualsTest {
    // Class just used for reflection
    private static class Toy {
        public Toy() {}
        public Toy(int a, String b) {}
        public int getA() {return 0;}
        public void setA(int a) {}
        public boolean isB() {return false;}
        public void setB(boolean b) {}
        public void run() {}
        public void blah(int a, String b) {}
    }

    static final Class toy = Toy.class;
    static final Constructor con1, con2;
    static final Method getA, setA, isB, setB, run, blah;
    static {
        try {
            con1 = toy.getConstructor(new Class[] {});
            con2 = toy.getConstructor(new Class[] {Integer.TYPE,
                                                   String.class});
            getA = toy.getMethod("getA", new Class[] {});
            setA = toy.getMethod("setA", new Class[] {Integer.TYPE});
            isB  = toy.getMethod("isB",  new Class[] {});
            setB = toy.getMethod("setB", new Class[] {Boolean.TYPE});
            run  = toy.getMethod("run",  new Class[] {});
            blah = toy.getMethod("blah", new Class[] {Integer.TYPE,
                                                      String.class});
        } catch (Exception e) {
            throw new Error(e.getMessage());
        }
    }

    private static final MBeanAttributeInfo
        newMBeanAttributeInfo(String name, String description,
                              Method getter, Method setter) {
        try {
            return new MBeanAttributeInfo(name, description, getter, setter);
        } catch (IntrospectionException e) {
            throw new Error(e.getMessage());
        }
    }

    static final MBeanAttributeInfo
        a1a = new MBeanAttributeInfo("thing", "java.foo.bar", "an attribute",
                                     true, true, false),
        a1b = new MBeanAttributeInfo("thing", "java.foo.bar", "an attribute",
                                     true, true, false),
        a2a = new MBeanAttributeInfo("splob", "java.foo.bar", "an attribute",
                                     true, true, false),
        a2b = new MBeanAttributeInfo(a2a.getName(), a2a.getType(),
                                     a2a.getDescription(), a2a.isReadable(),
                                     a2a.isWritable(), a2a.isIs()),
        a3  = new MBeanAttributeInfo("splob", "java.foo.bar", "a whatsit",
                                     true, true, false),
        a4  = new MBeanAttributeInfo("splob", "java.foo.bar", "a whatsit",
                                     false, true, false),
        a5a = newMBeanAttributeInfo("a", "an attribute", getA, setA),
        a5b = new MBeanAttributeInfo("a", "int", "an attribute",
                                     true, true, false),
        a6a = newMBeanAttributeInfo("a", "an attribute", getA, null),
        a6b = new MBeanAttributeInfo("a", "int", "an attribute",
                                     true, false, false),
        a7a = newMBeanAttributeInfo("a", "an attribute", null, setA),
        a7b = new MBeanAttributeInfo("a", "int", "an attribute",
                                     false, true, false),
        a8a = newMBeanAttributeInfo("b", "an attribute", isB, setB),
        a8b = new MBeanAttributeInfo("b", "boolean", "an attribute",
                                     true, true, true),
        a9a = newMBeanAttributeInfo("b", "an attribute", isB, null),
        a9b = new MBeanAttributeInfo("b", "boolean", "an attribute",
                                     true, false, true);

    static final MBeanParameterInfo
        p1a = new MBeanParameterInfo("thing", "java.foo.bar", "a parameter"),
        p1b = new MBeanParameterInfo("thing", "java.foo.bar", "a parameter"),
        p2  = new MBeanParameterInfo("splob", "java.foo.bar", "a parameter"),
        p3  = new MBeanParameterInfo("thing", "java.foo.bax", "a parameter"),
        p4  = new MBeanParameterInfo("thing", "java.foo.bar", "a whatsit");

    static final MBeanConstructorInfo
        c1a = new MBeanConstructorInfo("a constructor", con1),
        c1b = new MBeanConstructorInfo(c1a.getName(), "a constructor",
                                       new MBeanParameterInfo[0]),
        c1c = new MBeanConstructorInfo(c1a.getName(), c1a.getDescription(),
                                       c1a.getSignature()),
        c1d = new MBeanConstructorInfo(c1a.getName(), c1a.getDescription(),
                                       null),
        c2a = new MBeanConstructorInfo("another constructor", con2),
        c2b = new MBeanConstructorInfo(c2a.getName(),
                                       c2a.getDescription(),
                                       c2a.getSignature()),
        c3a = new MBeanConstructorInfo("conName", "a constructor",
                                       new MBeanParameterInfo[] {p2, p3}),
        c3b = new MBeanConstructorInfo("conName", "a constructor",
                                       new MBeanParameterInfo[] {p2, p3}),
        c4  = new MBeanConstructorInfo("conName", "a constructor",
                                       new MBeanParameterInfo[] {p3, p2}),
        c5  = new MBeanConstructorInfo("otherName", "a constructor",
                                       new MBeanParameterInfo[] {p3, p2}),
        c6  = new MBeanConstructorInfo("otherName", "another constructor",
                                       new MBeanParameterInfo[] {p3, p2});

    static final MBeanOperationInfo
        o1a = new MBeanOperationInfo("an operation", run),
        o1b = new MBeanOperationInfo("an operation", run),
        o1c = new MBeanOperationInfo("run", "an operation",
                                     o1a.getSignature(), "void",
                                     o1a.getImpact()),
        o1d = new MBeanOperationInfo("run", "an operation",
                                     new MBeanParameterInfo[0], "void",
                                     o1a.getImpact()),
        o1e = new MBeanOperationInfo("run", "an operation",
                                     null, "void",
                                     o1a.getImpact()),
        o2a = new MBeanOperationInfo("another operation", blah),
        o2b = new MBeanOperationInfo(o2a.getName(), o2a.getDescription(),
                                     o2a.getSignature(), o2a.getReturnType(),
                                     o2a.getImpact());

    static final MBeanNotificationInfo
        n1a = new MBeanNotificationInfo(new String[] {"a.b", "c.d"},
                                        "x.y.z",
                                        "a notification info"),
        n1b = new MBeanNotificationInfo(new String[] {"a.b", "c.d"},
                                        "x.y.z",
                                        "a notification info"),
        n2a = new MBeanNotificationInfo(new String[] {"a.b", "c.d"},
                                        "x.y.z",
                                        "another notification info"),
        n2b = new MBeanNotificationInfo(n2a.getNotifTypes(),
                                        n2a.getName(),
                                        n2a.getDescription()),
        n3  = new MBeanNotificationInfo(new String[] {"a.b", "c.d"},
                                        "x.y.zz",
                                        "a notification info"),
        n4  = new MBeanNotificationInfo(new String[] {"c.d", "a.b"},
                                        "x.y.z",
                                        "a notification info");

    static final MBeanAttributeInfo[]
        xa1a = {a1a, a2a},
        xa1b = {a1b, a2b},
        xa2a = {a2a, a1a};

    static final MBeanConstructorInfo[]
        xc1a = {c1a, c2a},
        xc1b = {c1b, c2b},
        xc2a = {c2a, c1a};

    static final MBeanOperationInfo[]
        xo1a = {o1a, o2a},
        xo1b = {o1b, o2b},
        xo2a = {o2a, o1a};

    static final MBeanNotificationInfo[]
        xn1a = {n1a, n2a},
        xn1b = {n1b, n2b},
        xn2a = {n2a, n1a};

    static final MBeanInfo
        i1a = new MBeanInfo("a.b.c", "an MBean info", xa1a, xc1a, xo1a, xn1a),
        i1b = new MBeanInfo("a.b.c", "an MBean info", xa1a, xc1a, xo1a, xn1a),
        i1c = new MBeanInfo("a.b.c", "an MBean info", xa1b, xc1b, xo1b, xn1b),
        i1d = new MutableMBeanInfo("a.b.c", "an MBean info", xa1b, xc1b, xo1b,
                                   xn1b),
        i1e = new ImmutableMBeanInfo("a.b.c", "an MBean info", xa1b, xc1b,
                                     xo1b, xn1b),
        i1f = new ImmutableMBeanInfo("a.b.c", "an MBean info", xa1b, xc1b,
                                     xo1b, xn1b),
        i2a = new MBeanInfo("a.b.cc", "an MBean info", xa1a, xc1a, xo1a, xn1a),
        i2b = new MBeanInfo(i2a.getClassName(), i2a.getDescription(),
                            i2a.getAttributes(), i2a.getConstructors(),
                            i2a.getOperations(), i2a.getNotifications()),
        i3  = new MBeanInfo("a.b.c", "another MBean info", xa1a, xc1a, xo1a,
                            xn1a),
        i4  = new MBeanInfo("a.b.c", "an MBean info", xa2a, xc1a, xo1a, xn1a),
        i5  = new MBeanInfo("a.b.c", "an MBean info", xa1a, xc2a, xo1a, xn1a),
        i6  = new MBeanInfo("a.b.c", "an MBean info", xa1a, xc1a, xo2a, xn1a),
        i7  = new MBeanInfo("a.b.c", "an MBean info", xa1a, xc1a, xo1a, xn2a);

    static final Object[][] equivalenceClasses = {
        {a1a, a1b}, {a2a, a2b}, {a3}, {a4}, {a5a, a5b}, {a6a, a6b}, {a7a, a7b},
        {a8a, a8b}, {a9a, a9b},
        {c1a, c1b, c1c, c1d}, {c2a, c2b}, {c3a, c3b}, {c4}, {c5}, {c6},
        {o1a, o1b, o1c, o1d, o1e}, {o2a, o2b},
        {p1a, p1b}, {p2}, {p3}, {p4},
        {n1a, n1b}, {n2a, n2b}, {n3}, {n4},
        {i1a, i1b, i1c, i1d, i1e, i1f}, {i2a, i2b}, {i3}, {i4}, {i5}, {i6},
        {i7},
    };

    private static class ImmutableMBeanInfo extends MBeanInfo {
        ImmutableMBeanInfo(String className,
                           String description,
                           MBeanAttributeInfo[] attributes,
                           MBeanConstructorInfo[] constructors,
                           MBeanOperationInfo[] operations,
                           MBeanNotificationInfo[] notifications) {
            super(className, description, attributes, constructors, operations,
                  notifications);
        }
    }

    /* This class checks that the MBeanInfo.equals() method really
       does call getClassName() etc rather than referring to its
       private fields.  */
    private static class MutableMBeanInfo extends MBeanInfo {
        private final String className;
        private final String description;
        private final MBeanAttributeInfo[] attributes;
        private final MBeanOperationInfo[] operations;
        private final MBeanConstructorInfo[] constructors;
        private final MBeanNotificationInfo[] notifications;

        MutableMBeanInfo(String className,
                         String description,
                         MBeanAttributeInfo[] attributes,
                         MBeanConstructorInfo[] constructors,
                         MBeanOperationInfo[] operations,
                         MBeanNotificationInfo[] notifications) {
            super("bogus", null, null, null, null, null);
            this.className = className;
            this.description = description;
            this.attributes = attributes;
            this.constructors = constructors;
            this.operations = operations;
            this.notifications = notifications;
        }

        public String getClassName() {
            return className;
        }

        public String getDescription() {
            return description;
        }

        public MBeanAttributeInfo[] getAttributes() {
            return attributes;
        }

        public MBeanOperationInfo[] getOperations() {
            return operations;
        }

        public MBeanConstructorInfo[] getConstructors() {
            return constructors;
        }

        public MBeanNotificationInfo[] getNotifications() {
            return notifications;
        }
    }

    private static boolean checkEquals(String what, Object[][] equivs) {
        boolean ok = true;
        /* The equivs array is an array of equivalence classes.  The members
           of each equivalence class must be equal among themselves.
           Each member of each equivalence class must be different from
           each member of each other equivalence class.  */
        for (int ei = 0; ei < equivs.length; ei++) {
            Object[] ec1 = equivs[ei];
            ok &= checkSame(what + " equivalence class " + ei, ec1);
            for (int ej = 0; ej < equivs.length; ej++) {
                if (ei == ej)
                    continue;
                Object[] ec2 = equivs[ej];
                ok &= checkDifferent(what + " equivalence classes " +
                                     ei + " and " + ej, ec1, ec2);
            }
        }
        if (ok)
            System.out.println("equals test for " + what + " passed");
        return ok;
    }

    /* We could simplify this test to compare every element with every
       other and choose whether they are supposed to be the same based
       on whether they are in the same equivalence class.  A bit
       simpler, but so what.  */

    private static boolean checkSame(String what, Object[] equiv) {
        boolean ok = true;
        for (int i = 0; i < equiv.length; i++) {
            final Object o1 = equiv[i];
            for (int j = 0; j < equiv.length; j++) {
                final Object o2 = equiv[j];
                if (!o1.equals(o2)) {
                    System.out.println("equals test: " + what +
                                       ": !obj[" + i +
                                       "].equals(obj[" + j + "])");
                    System.out.println("..." + o1 + "  " + o2);
                    ok = false;
                }
                if (o1.hashCode() != o2.hashCode()) {
                    System.out.println("equals test: " + what +
                                       ": obj[" + i +
                                       "].hashCode() != obj[" + j +
                                       "].hashCode()");
                    System.out.println("..." + o1 + "  " + o2);
                    ok = false;
                }
            }
        }
        return ok;
    }

    private static boolean checkDifferent(String what, Object[] equiv1,
                                          Object[] equiv2) {
        boolean ok = true;
        for (int i = 0; i < equiv1.length; i++) {
            final Object o1 = equiv1[i];
            for (int j = 0; j < equiv2.length; j++) {
                final Object o2 = equiv2[j];
                if (o1.equals(o2)) {
                    System.out.println("equals test " + what + ": obj[" +
                                       i + "].equals(obj[" + j + "])");
                    System.out.println("..." + o1 + "  " + o2);
                    ok = false;
                }
            }
        }
        return ok;
    }

    public static void main(String[] args) throws Exception {
        boolean ok = true;
        ok &= checkEquals("equivalence", equivalenceClasses);
        if (ok) {
            System.out.println("all tests passed");
        } else {
            System.out.println("at least one test failed");
            System.exit(1);
        }
    }
}