1 /*
2  * Copyright (c) 2004, 2018, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package nsk.monitoring.GarbageCollectorMXBean.getCollectionCount;
25 
26 import java.lang.management.*;
27 import java.io.*;
28 import nsk.share.*;
29 import nsk.monitoring.share.*;
30 
31 public class getcollectioncount001 {
32 
33     private static boolean testFailed = false;
34 
main(String[] args)35     public static void main(String[] args) {
36 
37         System.exit(Consts.JCK_STATUS_BASE + run(args, System.out));
38     }
39 
40     private static Log log;
41 
run(String[] args, PrintStream out)42     static int run(String[] args, PrintStream out) {
43 
44         ArgumentHandler argumentHandler = new ArgumentHandler(args);
45         log = new Log(out, argumentHandler);
46 
47         // Test case 1. check that
48         // getCollectionCount() does not throw unexpected exceptions
49 
50         System.gc();
51         System.gc();
52         System.gc();
53 
54         GarbageCollectorMonitor gcMonitor =
55         Monitor.getGarbageCollectorMonitor(log, argumentHandler);
56 
57         Object[] pool = gcMonitor.getGarbageCollectorMXBeans();
58         for (int i=0; i<pool.length; i++) {
59 
60             String beanName = "";
61             long collectionCount = gcMonitor.getCollectionCount(pool[i]);
62 
63             if (pool[i] instanceof javax.management.ObjectName) {
64                 beanName = ((javax.management.ObjectName)pool[i]).toString();
65             } else {
66                 beanName = ((java.lang.management.GarbageCollectorMXBean)
67                     pool[i]).getName();
68             }
69             log.display(beanName+": getCollectionCount() = "+collectionCount);
70 
71             if (collectionCount < -1) {
72                 // value can be non-negative or -1 if if the collection count
73                 // is undefined for this collector.
74                 log.complain("FAILURE 1.");
75                 log.complain("getCollectionCount() returns unexpected value: " +
76                     collectionCount);
77                 testFailed = true;
78             }
79         }
80 
81         if (testFailed)
82             log.complain("TEST FAILED");
83 
84         return (testFailed) ? Consts.TEST_FAILED : Consts.TEST_PASSED;
85     }
86 
87 }
88