File: BadConstraintTest.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 (431 lines) | stat: -rw-r--r-- 17,266 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
 * Copyright (c) 2005, 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 6204469
 * @summary Test that Open MBean attributes and parameters cannot have
 * illegal constraints like min greater than max
 * @author Eamonn McManus
 *
 * @run clean BadConstraintTest
 * @run build BadConstraintTest
 * @run main BadConstraintTest
 */

import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import javax.management.*;
import javax.management.openmbean.*;

public class BadConstraintTest {
    private static String failure;

    public static void main(String[] args) throws Exception {
        genericTests();
        descriptorTests();

        if (failure == null)
            System.out.println("Test passed");
        else
            throw new Exception("TEST FAILED: " + failure);
    }

    private static void genericTests() throws Exception {
        for (Object[] test : tests) {
            if (test.length != 5) {
                throw new Exception("Test element has wrong length: " +
                                    toString(test));
            }

            OpenType<?> openType = (OpenType<?>) test[0];
            Object defaultValue = test[1];
            Comparable<?> minValue = (Comparable<?>) test[2];
            Comparable<?> maxValue = (Comparable<?>) test[3];
            Object[] legalValues = (Object[]) test[4];

            System.out.println("test: openType=" + openType +
                               "; defaultValue=" + defaultValue +
                               "; minValue=" + minValue +
                               "; maxValue=" + maxValue +
                               "; legalValues=" + toString(legalValues));

            genericTest(openType, defaultValue, minValue, maxValue,
                        legalValues);
        }
    }

    private static void descriptorTests() throws Exception {
        for (Object[][] test : descriptorTests) {
            if (test.length != 2) {
                throw new Exception("Test element has wrong length: " +
                                    toString(test));
            }

            if (test[0].length != 1) {
                throw new Exception("Test element should have one OpenType: " +
                                    toString(test[0]));
            }

            OpenType<?> openType = (OpenType<?>) test[0][0];
            Descriptor d = descriptor(test[1]);

            System.out.println("test: openType=" + openType +
                               "; descriptor=" + d);

            descriptorTest(openType, d);
        }
    }

    /* Tests that apply to both the Descriptor and the non-Descriptor
       constructors.  We invoke the non-Descriptor constructors by
       reflection, then we make the corresponding Descriptor and call
       the descriptorTest with it.  */
    private static void genericTest(OpenType<?> openType,
                                    Object defaultValue,
                                    Comparable<?> minValue,
                                    Comparable<?> maxValue,
                                    Object[] legalValues)
            throws Exception {

        if (minValue == null && maxValue == null && legalValues == null) {
            if (defaultValue == null)
                throw new Exception("What am I testing?");
            Class[] params1 = new Class[] {
                String.class, String.class, OpenType.class,
                boolean.class, boolean.class, boolean.class,
                Object.class
            };
            Constructor<OpenMBeanAttributeInfoSupport> c1 =
                OpenMBeanAttributeInfoSupport.class.getConstructor(params1);
            Class[] params2 = new Class[] {
                String.class, String.class, OpenType.class,
                Object.class
            };
            Constructor<OpenMBeanParameterInfoSupport> c2 =
                OpenMBeanParameterInfoSupport.class.getConstructor(params2);
            ode(c1, "name", "descr", openType, true, true, false, defaultValue);
            ode(c2, "name", "descr", openType, defaultValue);
            descriptorTest(openType,
                           descriptor("defaultValue", defaultValue));
            descriptorTest(openType,
                           descriptor("defaultValue", string(defaultValue)));
        }

        if (legalValues == null) {
            Class[] params1 = new Class[] {
                String.class, String.class, OpenType.class,
                boolean.class, boolean.class, boolean.class,
                Object.class, Comparable.class, Comparable.class
            };
            Constructor<OpenMBeanAttributeInfoSupport> c1 =
                OpenMBeanAttributeInfoSupport.class.getConstructor(params1);
            Class[] params2 = new Class[] {
                String.class, String.class, OpenType.class,
                Object.class, Comparable.class, Comparable.class
            };
            Constructor<OpenMBeanParameterInfoSupport> c2 =
                OpenMBeanParameterInfoSupport.class.getConstructor(params2);
            ode(c1, "name", "descr", openType, true, true, false, defaultValue,
                minValue, maxValue);
            ode(c2, "name", "descr", openType, defaultValue,
                minValue, maxValue);
            descriptorTest(openType,
                           descriptor("defaultValue", defaultValue,
                                      "minValue", minValue,
                                      "maxValue", maxValue));
            descriptorTest(openType,
                           descriptor("defaultValue", string(defaultValue),
                                      "minValue", string(minValue),
                                      "maxValue", string(maxValue)));
        }

        if (legalValues != null) {
            Class[] params1 = new Class[] {
                String.class, String.class, OpenType.class,
                boolean.class, boolean.class, boolean.class,
                Object.class, Object[].class
            };
            Constructor<OpenMBeanAttributeInfoSupport> c1 =
                OpenMBeanAttributeInfoSupport.class.getConstructor(params1);
            Class[] params2 = new Class[] {
                String.class, String.class, OpenType.class,
                Object.class, Object[].class
            };
            Constructor<OpenMBeanParameterInfoSupport> c2 =
                OpenMBeanParameterInfoSupport.class.getConstructor(params2);
            ode(c1, "name", "descr", openType, true, true, false, defaultValue,
                legalValues);
            ode(c2, "name", "descr", openType, defaultValue,
                legalValues);
            descriptorTest(openType,
                           descriptor("defaultValue", defaultValue,
                                      "legalValues", legalValues));
            descriptorTest(openType,
                           descriptor("defaultValue", defaultValue,
                                      "legalValues", arraySet(legalValues)));
            Set<String> strings = new HashSet<String>();
            for (Object x : legalValues)
                strings.add(x.toString());
            descriptorTest(openType,
                           descriptor("defaultValue", defaultValue,
                                      "legalValues", strings));
            descriptorTest(openType,
                           descriptor("defaultValue", defaultValue,
                                      "legalValues",
                                      strings.toArray(new String[0])));
        }
    }

    private static void descriptorTest(OpenType<?> openType, Descriptor d)
            throws Exception {
        Class[] params1 = new Class[] {
            String.class, String.class, OpenType.class,
            boolean.class, boolean.class, boolean.class,
            Descriptor.class
        };
        Constructor<OpenMBeanAttributeInfoSupport> c1 =
            OpenMBeanAttributeInfoSupport.class.getConstructor(params1);
        Class[] params2 = new Class[] {
            String.class, String.class, OpenType.class,
            Descriptor.class
        };
        Constructor<OpenMBeanParameterInfoSupport> c2 =
            OpenMBeanParameterInfoSupport.class.getConstructor(params2);
        iae(c1, "name", "descr", openType, true, true, false, d);
        iae(c2, "name", "descr", openType, d);
    }

    /* Check that the given constructor invocation gets an
       IllegalArgumentException. */
    private static void iae(Constructor<?> con, Object... params) {
        checkException(IllegalArgumentException.class, con, params);
    }

    /* Check that the given constructor invocation gets an
       OpenDataException.  */
    private static void ode(Constructor<?> con, Object... params) {
        checkException(OpenDataException.class, con, params);
    }

    private static void checkException(Class<? extends Exception> exc,
                                       Constructor<?> con, Object[] params) {
        try {
            con.newInstance(params);
            fail("Constructor succeeded but should have got " + exc.getName() +
                 " with params " + Arrays.deepToString(params));
        } catch (InvocationTargetException e) {
            Throwable cause = e.getCause();
            if (exc.isInstance(cause))
                return;
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            cause.printStackTrace(pw);
            pw.close();
            fail("Constructor should have got " + exc.getName() +
                 " with params " + Arrays.deepToString(params) + ": " + sw);
        } catch (Exception e) {
            throw new IllegalArgumentException("Reflection failed", e);
        }
    }

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

    private static Descriptor descriptor(Object... entries) {
        if (entries.length % 2 != 0)
            throw new RuntimeException("Odd length descriptor entries");
        String[] names = new String[entries.length / 2];
        Object[] values = new Object[entries.length / 2];
        for (int i = 0; i < entries.length; i += 2) {
            names[i / 2] = (String) entries[i];
            values[i / 2] = entries[i + 1];
        }
        return new ImmutableDescriptor(names, values);
    }

    private static <T> Set<T> arraySet(T[] array) {
        return new HashSet<T>(Arrays.asList(array));
    }

    private static String toString(Object x) {
        if (x == null)
            return "null";
        else if (x.getClass().isArray()) {
            StringBuilder sb = new StringBuilder("[");
            int len = Array.getLength(x);
            for (int i = 0; i < len; i++) {
                if (i > 0)
                    sb.append(", ");
                sb.append(toString(Array.get(x, i)));
            }
            sb.append("]");
            return sb.toString();
        } else
            return x.toString();
    }

    private static String string(Object x) {
        if (x == null)
            return null;
        return toString(x);
    }

    private static final OpenType<?>
        ostring = SimpleType.STRING,
        oint = SimpleType.INTEGER,
        obool = SimpleType.BOOLEAN,
        olong = SimpleType.LONG,
        obyte = SimpleType.BYTE,
        ofloat = SimpleType.FLOAT,
        odouble = SimpleType.DOUBLE,
        ostringarray, ostringarray2;
    private static final CompositeType ocomposite;
    private static final CompositeData compositeData, compositeData2;
    static {
        try {
            ostringarray = new ArrayType<String[]>(1, ostring);
            ostringarray2 = new ArrayType<String[][]>(2, ostring);
            ocomposite =
                new CompositeType("name", "descr",
                                  new String[] {"s", "i"},
                                  new String[] {"sdesc", "idesc"},
                                  new OpenType[] {ostring, oint});
            compositeData =
                new CompositeDataSupport(ocomposite,
                                         new String[] {"s", "i"},
                                         new Object[] {"foo", 23});
            compositeData2 =
                new CompositeDataSupport(ocomposite,
                                         new String[] {"s", "i"},
                                         new Object[] {"bar", -23});
        } catch (OpenDataException e) { // damn checked exceptions...
            throw new IllegalArgumentException(e.toString(), e);
        }
    }

    private static final Descriptor
        nullD = null,
        emptyD = ImmutableDescriptor.EMPTY_DESCRIPTOR;

    /* Each element of this array contains five Objects:
       - OpenType;
       - defaultValue;
       - minValue;
       - maxValue;
       - legalValues.  The objects are used to construct tests cases
       where all possible constructors that make sense for that
       combination of values are invoked, and all are checked to ensure
       that they throw the right exception.  */
    private static final Object[][] tests = {

        // Values must be of appropriate type

        {oint, "oops", null, null, null},
        {oint, Long.MAX_VALUE, null, null, null},
        {oint, null, "oops", null, null},
        {oint, "oops", 3, null, null},
        {oint, 3, "oops", null, null},
        {oint, null, null, "oops", null},
        {oint, null, 3, "oops", null},
        {oint, null, 3, false, null},
        {oint, null, null, null, new String[] {"x"}},
        {oint, null, null, null, new Object[] {"x"}},
        {oint, null, null, null, new Object[] {3, "x"}},

        // If defaultValue is present then it must satisfy the constraints
        // defined by legalValues, minValue, or maxValue when any of
        // these is also present

        {oint, 3, 4, null, null},
        {oint, 3, null, 2, null},
        {oint, 3, null, null, new Integer[] {2, 4}},

        // If minValue and maxValue are both present then minValue must
        // not be greater than maxValue

        {ostring, null, "z", "a", null},
        {oint, null, 3, 2, null},

        // We don't support default values or legal sets for arrays (yet)

        {ostringarray, new String[] {"x"}, null, null, null},
        {ostringarray, null, null, null, new String[][]{new String[] {"x"}}},
    };

    /* The tests here can only be expressed via Descriptors because an
       attempt to invoke one of the non-Descriptor constructors with
       the implied parameters would not compile (or would fail at the
       reflection stage when reflection is being used).

       Each element of this array contains two subarrays.  The first
       is an array of OpenTypes that must contain exactly one element.
       The second is an array of alternating field names and field
       values that will be used to construct a Descriptor that is supposed
       to fail when given to an OpenMBean*Info constructor with the given
       OpenType.  */
    private static final Object[][][] descriptorTests = {

        // Values must be of appropriate type

        {{oint},
         {"minValue", 25L}},

        {{oint},
         {"minValue", new Object()}}, // not even Comparable
        {{oint},
         {"maxValue", new Object()}}, // not even Comparable
        {{oint},
         {"defaultValue", 3,
          "minValue", new Object()}},
        {{oint},
         {"defaultValue", 3,
          "maxValue", new Object()}},

        {{oint},
         {"legalValues", new int[] {3}}}, // should be new Integer[] to work
        {{oint},
         {"legalValues", 3}},

        // If legalValues is present then neither minValue nor maxValue
        // must be present

        {{oint},
         {"minValue", 3, "legalValues", new Integer[] {3, 4}}},
        {{oint},
         {"maxValue", 3, "legalValues", new Integer[] {2, 3}}},
        {{oint},
         {"defaultValue", 3, "minValue", 3, "legalValues", new Integer[] {3}}},

        // We don't support min or max arrays (what would they mean?)

        {{ostringarray},
         {"minValue", new String[] {"x"}}},
        {{ostringarray},
         {"maxValue", new String[] {"x"}}},

    };
}