1 /*
2  * Copyright (c) 2011, 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.ThreadMXBean.GetThreadCpuTime;
25 
26 import nsk.share.*;
27 import nsk.monitoring.share.*;
28 import nsk.monitoring.ThreadMXBean.ThreadMXBeanTestBase;
29 
30 
31 /**
32  * Tests  getThreadCpuTime(long[] ids) and getThreadUserTime(long[] ids)
33  * functions of com.sun.management.ThreadMXBean
34  * <p>
35  *  - any method called with null argument should throw NullPointerException
36  * for direct and proxy MBean delivery methods and RuntimeException for
37  * server MBean delivery method (MBeanServer)
38  * <br>
39  *  - any method called with zero (0 for long, { 0 } for long[]) argument
40  * should throw IllegalArgumentException for direct and proxy MBean delivery
41  * methods and RuntimeException for server MBean delivery method (MBeanServer)
42  */
43 public class IllegalArgumentsTest extends ThreadMXBeanTestBase {
44 
45     /**
46      * Actually runs the test
47      */
run()48     public void run() {
49         if (threadMXBean == null)
50             return;
51         int exceptions = 0;
52         // getThreadCpuTime(long[]) with null
53         try {
54             threadMXBean.getThreadCpuTime(null);
55         } catch (NullPointerException e) {
56             log.info("Caught expected NPE : " + e.getMessage());
57             exceptions++;
58         } catch (RuntimeException e1) {
59             log.info("Caught expected RuntimeException : " + e1.getMessage());
60             exceptions++;
61         }
62         // getThreadCpuTime(long[]) with { 0 }
63         try {
64             threadMXBean.getThreadCpuTime(new long[] { 0 });
65         } catch (IllegalArgumentException e) {
66             log.info("Caught expected IllegalArgumentException : " + e.getMessage());
67             exceptions++;
68         } catch (RuntimeException e1) {
69             log.info("Caught expected RuntimeException : " + e1.getMessage());
70             exceptions++;
71         }
72         // getThreadUserTime(long[]) with null
73         try {
74             threadMXBean.getThreadUserTime(null);
75         } catch (NullPointerException e) {
76             log.info("Caught expected NPE : " + e.getMessage());
77             exceptions++;
78         } catch (RuntimeException e1) {
79             log.info("Caught expected RuntimeException : " + e1.getMessage());
80             exceptions++;
81         }
82         // getThreadCpuTime(long[]) with { 0 }
83         try {
84             threadMXBean.getThreadUserTime(new long[] { 0 });
85         } catch (IllegalArgumentException e) {
86             log.info("Caught expected IllegalArgumentException : " + e.getMessage());
87             exceptions++;
88         } catch (RuntimeException e1) {
89             log.info("Caught expected RuntimeException : " + e1.getMessage());
90             exceptions++;
91         }
92         // 4 exceptions should have been caught
93         if (exceptions != 4)
94             throw new TestFailure("Failure! Expected to catch 4 exceptions, "
95                     + "actually caught : " + exceptions);
96         log.info("IllegalArgumentsTest passed.");
97     }
98 
99     /**
100      * Entry point for java program
101      * @param args sets the test configuration
102      */
main(String[] args)103     public static void main(String[] args) {
104         Monitoring.runTest(new IllegalArgumentsTest(), args);
105     }
106 }
107