File: MiscTest.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 (739 lines) | stat: -rw-r--r-- 24,820 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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/*
 * 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 6175517 6304996
 * @summary General MXBean test: createMBean, registerMBean, immutableInfo,
 *          interfaceClassName, openType, originalType, StandardMBean,
 *          StandardEmitterMBean.
 * @author Luis-Miguel Alventosa
 *
 * @run clean MiscTest
 * @run build MiscTest
 * @run main MiscTest
 */

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

public class MiscTest {

    private static final MBeanNotificationInfo notifs[] =
        new MBeanNotificationInfo[] {
          new MBeanNotificationInfo(
            new String[] {AttributeChangeNotification.ATTRIBUTE_CHANGE},
            AttributeChangeNotification.class.getName(),
            "This notification is emitted when the reset() method is called.")
    };

    private static Class<?> testClasses[] = {
        Test11.class, Test12.class,
        Test21.class, Test22.class,
        Test31.class, Test32.class,
        Test33.class, Test34.class,
        Test41.class, Test42.class,
        Test43.class, Test44.class,
    };

    private static Class<?> testIntfs[] = {
        Test11MBean.class, Test12MBean.class,
        Test21MXBean.class, Test22MXBean.class,
        Test31SMB.class, Test32SMB.class,
        Test33SMB.class, Test34SMB.class,
        Test41SMX.class, Test42SMX.class,
        Test43SMX.class, Test44SMX.class,
    };

    public interface SuperInterface {
        public String getState();
        public void setState(String s);
        public int getNbChanges();
        public void reset();
        public void close(boolean force);
        public MemoryUsage getMemoryUsage();
    }

    public static class BaseTest {

        public String getState() {
            return state;
        }

        public void setState(String s) {
            state = s;
            nbChanges++;
        }

        public int getNbChanges() {
            return nbChanges;
        }

        public void reset() {
            state = "initial state";
            nbChanges = 0;
            nbResets++;
        }

        public String getName() {
            return "name";
        }

        public void setName(String s) {
        }

        public void close(boolean force) {
        }

        public MemoryUsage getMemoryUsage() {
            return new MemoryUsage(10, 20, 30, 40);
        }

        public int getNbResets() {
            return nbResets;
        }

        private String state = "initial state";
        private int nbChanges = 0;
        private int nbResets = 0;
    }

    public static class BaseEmitterTest
        extends NotificationBroadcasterSupport {

        public String getState() {
            return state;
        }

        public void setState(String s) {
            state = s;
            nbChanges++;
        }

        public int getNbChanges() {
            return nbChanges;
        }

        public void reset() {
            state = "initial state";
            nbChanges = 0;
            nbResets++;
        }

        public String getName() {
            return "name";
        }

        public void setName(String s) {
        }

        public void close(boolean force) {
        }

        public MemoryUsage getMemoryUsage() {
            return new MemoryUsage(10, 20, 30, 40);
        }

        public int getNbResets() {
            return nbResets;
        }

        public MBeanNotificationInfo[] getNotificationInfo() {
            return notifs;
        }

        private String state = "initial state";
        private int nbChanges = 0;
        private int nbResets = 0;
    }

    public static interface Test11MBean extends SuperInterface {
    }

    public static interface Test12MBean extends SuperInterface {
    }

    public static interface Test21MXBean extends SuperInterface {
    }

    public static interface Test22MXBean extends SuperInterface {
    }

    public static interface Test31SMB extends SuperInterface {
    }

    public static interface Test32SMB extends SuperInterface {
    }

    public static interface Test33SMB extends SuperInterface {
    }

    public static interface Test34SMB extends SuperInterface {
    }

    public static interface Test41SMX extends SuperInterface {
    }

    public static interface Test42SMX extends SuperInterface {
    }

    public static interface Test43SMX extends SuperInterface {
    }

    public static interface Test44SMX extends SuperInterface {
    }

    public static class Test11 extends BaseTest
        implements Test11MBean {
    }

    public static class Test12 extends BaseEmitterTest
        implements Test12MBean {
    }

    public static class Test21 extends BaseTest
        implements Test21MXBean {
    }

    public static class Test22 extends BaseEmitterTest
        implements Test22MXBean {
    }

    public static class Test31 extends BaseTest
        implements Test31SMB {
    }

    public static class Test32 extends BaseEmitterTest
        implements Test32SMB {
    }

    public static class Test33 extends StandardMBean
        implements Test33SMB {

        public Test33() {
            super(Test33SMB.class, false);
        }

        public String getState() {
            return state;
        }

        public void setState(String s) {
            state = s;
            nbChanges++;
        }

        public int getNbChanges() {
            return nbChanges;
        }

        public void reset() {
            state = "initial state";
            nbChanges = 0;
            nbResets++;
        }

        public String getName() {
            return "name";
        }

        public void setName(String s) {
        }

        public void close(boolean force) {
        }

        public MemoryUsage getMemoryUsage() {
            return new MemoryUsage(10, 20, 30, 40);
        }

        public int getNbResets() {
            return nbResets;
        }

        private String state = "initial state";
        private int nbChanges = 0;
        private int nbResets = 0;
    }

    public static class Test34 extends StandardEmitterMBean
        implements Test34SMB {

        public Test34() {
            super(Test34SMB.class, false,
                  new NotificationBroadcasterSupport(notifs));
        }

        public String getState() {
            return state;
        }

        public void setState(String s) {
            state = s;
            nbChanges++;
        }

        public int getNbChanges() {
            return nbChanges;
        }

        public void reset() {
            state = "initial state";
            nbChanges = 0;
            nbResets++;
        }

        public String getName() {
            return "name";
        }

        public void setName(String s) {
        }

        public void close(boolean force) {
        }

        public MemoryUsage getMemoryUsage() {
            return new MemoryUsage(10, 20, 30, 40);
        }

        public int getNbResets() {
            return nbResets;
        }

        private String state = "initial state";
        private int nbChanges = 0;
        private int nbResets = 0;
    }

    public static class Test41 extends BaseTest
        implements Test41SMX {
    }

    public static class Test42 extends BaseEmitterTest
        implements Test42SMX {
    }

   public static class Test43 extends StandardMBean
        implements Test43SMX {

        public Test43() {
            super(Test43SMX.class, true);
        }

        public String getState() {
            return state;
        }

        public void setState(String s) {
            state = s;
            nbChanges++;
        }

        public int getNbChanges() {
            return nbChanges;
        }

        public void reset() {
            state = "initial state";
            nbChanges = 0;
            nbResets++;
        }

        public String getName() {
            return "name";
        }

        public void setName(String s) {
        }

        public void close(boolean force) {
        }

        public MemoryUsage getMemoryUsage() {
            return new MemoryUsage(10, 20, 30, 40);
        }

        public int getNbResets() {
            return nbResets;
        }

        private String state = "initial state";
        private int nbChanges = 0;
        private int nbResets = 0;
    }

    public static class Test44 extends StandardEmitterMBean
        implements Test44SMX {

        public Test44() {
            super(Test44SMX.class, true,
                  new NotificationBroadcasterSupport(notifs));
        }

        public String getState() {
            return state;
        }

        public void setState(String s) {
            state = s;
            nbChanges++;
        }

        public int getNbChanges() {
            return nbChanges;
        }

        public void reset() {
            state = "initial state";
            nbChanges = 0;
            nbResets++;
        }

        public String getName() {
            return "name";
        }

        public void setName(String s) {
        }

        public void close(boolean force) {
        }

        public MemoryUsage getMemoryUsage() {
            return new MemoryUsage(10, 20, 30, 40);
        }

        public int getNbResets() {
            return nbResets;
        }

        private String state = "initial state";
        private int nbChanges = 0;
        private int nbResets = 0;
    }

    public static void main(String[] args) throws Exception {
        // Instantiate the MBean server
        //
        echo("\n>>> Create the MBean server");
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

        // Get default domain
        //
        echo("\n>>> Get the MBean server's default domain");
        String domain = mbs.getDefaultDomain();
        echo("\tDefault Domain = " + domain);

        for (int i = 0; i < testClasses.length; i++) {
            // Create and register the Test MBean
            //
            String cn = testClasses[i].getName();
            String ons = domain + ":type=" + cn;
            echo("\n>>> Create the " + cn +
                 " MBean within the MBeanServer");
            echo("\tObjectName = " + ons);
            ObjectName on = ObjectName.getInstance(ons);
            if (testClasses[i] == Test31.class ||
                testClasses[i] == Test41.class) {
                StandardMBean s = new StandardMBean(
                                      testClasses[i].newInstance(),
                                      (Class) testIntfs[i],
                                      testClasses[i] == Test41.class);
                mbs.registerMBean(s, on);
            } else if (testClasses[i] == Test32.class ||
                       testClasses[i] == Test42.class) {
                Object o = testClasses[i].newInstance();
                StandardEmitterMBean s = new StandardEmitterMBean(
                               o,
                               (Class) testIntfs[i],
                               testClasses[i] == Test42.class,
                               (NotificationEmitter) o);
                mbs.registerMBean(s, on);
            } else {
                mbs.createMBean(cn, on);
            }

            // Check notifs
            //
            MBeanInfo mbi = mbs.getMBeanInfo(on);
            MBeanNotificationInfo mbni[] = mbi.getNotifications();
            if (i % 2 == 0) {
                if (mbni.length != 0) {
                    throw new IllegalArgumentException(
                    "Should not be a NotificationEmitter");
                }
            } else {
                if (mbni.length != 1) {
                    throw new IllegalArgumentException(
                    "Should not a NotificationEmitter with one notification");
                }
            }
            // Manage the Test MBean
            //
            manageMBean(mbs, on, cn);
        }
    }

    private static void manageMBean(MBeanServer mbs,
                                    ObjectName on,
                                    String cn)
        throws Exception {

        echo("\n>>> Manage the " + cn +
             " MBean using its attributes ");
        echo("    and operations exposed for management");

        // Get attribute values
        printAttributes(mbs, on);

        // Change State attribute
        echo("\n    Setting State attribute to value \"new state\"...");
        Attribute stateAttribute = new Attribute("State","new state");
        mbs.setAttribute(on, stateAttribute);

        // Get attribute values
        printAttributes(mbs, on);

        // Invoking reset operation
        echo("\n    Invoking reset operation...");
        mbs.invoke(on, "reset", null, null);

        // Invoking close operation
        echo("\n    Invoking close operation...");
        String type = on.getKeyProperty("type");
        String signature[] = {"boolean"};
        mbs.invoke(on, "close", new Object[] {true}, signature);

        // Get attribute values
        printAttributes(mbs, on);

        // Create proxy
        if (type.equals(Test11.class.getName())) {
            Test11MBean p = JMX.newMBeanProxy(mbs,
                                              on,
                                              Test11MBean.class);
            // Get attribute values
            echo("\n    Getting attribute values through proxies:");
            echo("\tState       = \"" + p.getState() + "\"");
            echo("\tNbChanges   = " + p.getNbChanges());
            echo("\tMemoryUsage = " + p.getMemoryUsage());
            checkDescriptor(mbs, on, "true", Test11MBean.class.getName());
        } else if (type.equals(Test12.class.getName())) {
            Test12MBean p = JMX.newMBeanProxy(mbs,
                                              on,
                                              Test12MBean.class,
                                              true);
            // Get attribute values
            echo("\n    Getting attribute values through proxies:");
            echo("\tState       = \"" + p.getState() + "\"");
            echo("\tNbChanges   = " + p.getNbChanges());
            echo("\tMemoryUsage = " + p.getMemoryUsage());
            checkDescriptor(mbs, on, "false", Test12MBean.class.getName());
        } else if (type.equals(Test21.class.getName())) {
            Test21MXBean p = JMX.newMXBeanProxy(mbs,
                                                on,
                                                Test21MXBean.class);
            // Get attribute values
            echo("\n    Getting attribute values through proxies:");
            echo("\tState       = \"" + p.getState() + "\"");
            echo("\tNbChanges   = " + p.getNbChanges());
            echo("\tMemoryUsage = " + p.getMemoryUsage());
            checkDescriptor(mbs, on, "true", Test21MXBean.class.getName());
        } else if (type.equals(Test22.class.getName())) {
            Test22MXBean p = JMX.newMXBeanProxy(mbs,
                                                on,
                                                Test22MXBean.class,
                                                true);
            // Get attribute values
            echo("\n    Getting attribute values through proxies:");
            echo("\tState       = \"" + p.getState() + "\"");
            echo("\tNbChanges   = " + p.getNbChanges());
            echo("\tMemoryUsage = " + p.getMemoryUsage());
            checkDescriptor(mbs, on, "true", Test22MXBean.class.getName());
        } else if (type.equals(Test31.class.getName())) {
            Test31SMB p = JMX.newMBeanProxy(mbs,
                                            on,
                                            Test31SMB.class);
            // Get attribute values
            echo("\n    Getting attribute values through proxies:");
            echo("\tState       = \"" + p.getState() + "\"");
            echo("\tNbChanges   = " + p.getNbChanges());
            echo("\tMemoryUsage = " + p.getMemoryUsage());
            checkDescriptor(mbs, on, "true", Test31SMB.class.getName());
        } else if (type.equals(Test32.class.getName())) {
            Test32SMB p = JMX.newMBeanProxy(mbs,
                                            on,
                                            Test32SMB.class,
                                            true);
            // Get attribute values
            echo("\n    Getting attribute values through proxies:");
            echo("\tState       = \"" + p.getState() + "\"");
            echo("\tNbChanges   = " + p.getNbChanges());
            echo("\tMemoryUsage = " + p.getMemoryUsage());
            checkDescriptor(mbs, on, "true", Test32SMB.class.getName());
        } else if (type.equals(Test33.class.getName())) {
            Test33SMB p = JMX.newMBeanProxy(mbs,
                                            on,
                                            Test33SMB.class,
                                            true);
            // Get attribute values
            echo("\n    Getting attribute values through proxies:");
            echo("\tState       = \"" + p.getState() + "\"");
            echo("\tNbChanges   = " + p.getNbChanges());
            echo("\tMemoryUsage = " + p.getMemoryUsage());
            checkDescriptor(mbs, on, "true", Test33SMB.class.getName());
        } else if (type.equals(Test34.class.getName())) {
            Test34SMB p = JMX.newMBeanProxy(mbs,
                                            on,
                                            Test34SMB.class,
                                            true);
            // Get attribute values
            echo("\n    Getting attribute values through proxies:");
            echo("\tState       = \"" + p.getState() + "\"");
            echo("\tNbChanges   = " + p.getNbChanges());
            echo("\tMemoryUsage = " + p.getMemoryUsage());
            checkDescriptor(mbs, on, "true", Test34SMB.class.getName());
        } else if (type.equals(Test41.class.getName())) {
            Test41SMX p = JMX.newMXBeanProxy(mbs,
                                             on,
                                             Test41SMX.class);
            // Get attribute values
            echo("\n    Getting attribute values through proxies:");
            echo("\tState       = \"" + p.getState() + "\"");
            echo("\tNbChanges   = " + p.getNbChanges());
            echo("\tMemoryUsage = " + p.getMemoryUsage());
            checkDescriptor(mbs, on, "true", Test41SMX.class.getName());
        } else if (type.equals(Test42.class.getName())) {
            Test42SMX p = JMX.newMXBeanProxy(mbs,
                                             on,
                                             Test42SMX.class,
                                             true);
            // Get attribute values
            echo("\n    Getting attribute values through proxies:");
            echo("\tState       = \"" + p.getState() + "\"");
            echo("\tNbChanges   = " + p.getNbChanges());
            echo("\tMemoryUsage = " + p.getMemoryUsage());
            checkDescriptor(mbs, on, "true", Test42SMX.class.getName());
        } else if (type.equals(Test43.class.getName())) {
            Test43SMX p = JMX.newMXBeanProxy(mbs,
                                             on,
                                             Test43SMX.class);
            // Get attribute values
            echo("\n    Getting attribute values through proxies:");
            echo("\tState       = \"" + p.getState() + "\"");
            echo("\tNbChanges   = " + p.getNbChanges());
            echo("\tMemoryUsage = " + p.getMemoryUsage());
            checkDescriptor(mbs, on, "true", Test43SMX.class.getName());
        } else if (type.equals(Test44.class.getName())) {
            Test44SMX p = JMX.newMXBeanProxy(mbs,
                                             on,
                                             Test44SMX.class,
                                             true);
            // Get attribute values
            echo("\n    Getting attribute values through proxies:");
            echo("\tState       = \"" + p.getState() + "\"");
            echo("\tNbChanges   = " + p.getNbChanges());
            echo("\tMemoryUsage = " + p.getMemoryUsage());
            checkDescriptor(mbs, on, "true", Test44SMX.class.getName());
        } else {
            throw new IllegalArgumentException("Invalid MBean type");
        }
    }

    private static void printAttributes(MBeanServer mbs,
                                        ObjectName on)
        throws Exception {
        echo("\n    Getting attribute values:");
        String state = (String) mbs.getAttribute(on, "State");
        Integer nbChanges = (Integer) mbs.getAttribute(on,"NbChanges");
        echo("\tState     = \"" + state + "\"");
        echo("\tNbChanges = " + nbChanges);
        String type = on.getKeyProperty("type");
        if (type.indexOf("Test2") != -1 || type.indexOf("Test4") != -1) {
            CompositeData memoryUsage =
                (CompositeData) mbs.getAttribute(on, "MemoryUsage");
            echo("\tMemoryUsage = " + memoryUsage);
        } else {
            MemoryUsage memoryUsage =
                (MemoryUsage) mbs.getAttribute(on, "MemoryUsage");
            echo("\tMemoryUsage = " + memoryUsage);
        }
    }

    public static void checkDescriptor(MBeanServer mbs,
                                       ObjectName on,
                                       String immutable,
                                       String intf)
            throws Exception {

        MBeanInfo mbi = mbs.getMBeanInfo(on);

        Descriptor d = mbi.getDescriptor();
        if (d == null || d.getFieldNames().length == 0)
            throw new IllegalArgumentException("Empty descriptor");
        if (!d.getFieldValue("immutableInfo").equals(immutable)) {
            final String msg =
                "Bad descriptor: expected immutableInfo=" + immutable + ": " + d;
            throw new IllegalArgumentException(msg);
        }
        if (!d.getFieldValue("interfaceClassName").equals(intf)) {
            final String msg =
                "Bad descriptor: expected interfaceClassName=" + intf + ": " + d;
            throw new IllegalArgumentException(msg);
        }

        if (intf.indexOf("MX") != -1) {
            MBeanAttributeInfo attrs[] = mbi.getAttributes();
            if (attrs == null || attrs.length == 0)
                throw new IllegalArgumentException("No attributes");
            boolean nbChangesFound = false;
            for (MBeanAttributeInfo attr : attrs) {
                if (attr.getName().equals("NbChanges")) {
                    nbChangesFound = true;
                    Descriptor ad = attr.getDescriptor();
                    OpenType<?> opty = (OpenType<?>)
                        ad.getFieldValue("openType");
                    if (!opty.equals(SimpleType.INTEGER)) {
                        throw new IllegalArgumentException("Open type should " +
                                                           "be INTEGER: " + opty);
                    }
                    String orty =
                        (String) ad.getFieldValue("originalType");
                    if (!orty.equals(Integer.TYPE.getName())) {
                        throw new IllegalArgumentException("Orig type should " +
                                                           "be int: " + orty);
                    }
                }
            }
            if (!nbChangesFound)
                throw new IllegalArgumentException("Did not find NbChanges");
        }
    }

    private static void echo(String msg) {
        System.out.println(msg);
    }
}