1 /*
2  * Copyright (c) 2017, 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  /*
25  * @test
26  * @bug 8024352
27  * @modules java.management
28  * @run main MBeanOperationInfoImpactRangeTest
29  * @summary Check that MBeanOperationInfo throws an IllegalArgumentException when impact
30  * value is not among INFO,ACTION,ACTION_INFO,UNKNOWN
31  */
32 import javax.management.MBeanOperationInfo;
33 
34 public class MBeanOperationInfoImpactRangeTest {
35 
checkInRange(int impact)36     private void checkInRange(int impact) {
37         int impactValue;
38 
39         System.out.println("checking that no exception is thrown when a "
40                 + "value in range is passed, impact value is :" + impact );
41         MBeanOperationInfo mbi = new MBeanOperationInfo("IRC", "impact Range"
42                 + " check", null, null, impact);
43         impactValue = mbi.getImpact();
44         if(impactValue != impact)
45             throw new RuntimeException("unexpected impact value :" + impactValue);
46         System.out.println("given value is :" + impactValue);
47         System.out.println("Success no exception thrown");
48         System.out.println(mbi.toString());
49 
50     }
51 
checkOutOfRange(int impact)52     private void checkOutOfRange(int impact) {
53         int impactValue;
54 
55         try {
56             System.out.println("checking that exception is thrown when a value"
57                     + " out of range is passed, impact value is :" + impact);
58             MBeanOperationInfo mbi = new MBeanOperationInfo("IRC", "impact Range"
59                     + " check", null, null, impact);
60             impactValue = mbi.getImpact();
61             System.out.println("IllegalArgumentException not thrown"
62                     + " when a value out of range is passed ,"
63                     + " given value is :" + impactValue);
64             throw new RuntimeException("Test failed !!");
65             // throwing RuntimeException for notifying the unusual behaviour
66         } catch (IllegalArgumentException e) {
67             System.out.println("IllegalArgumentException thrown as expected, "
68                     + "illegal value given as impact :" + impact);
69             System.out.println("success");
70         }
71 
72     }
73 
main(String Args[])74     public static void main(String Args[]) {
75 
76         // valid range for impact is {INFO=0,ACTION=1,ACTION_INFO=2,UNKNOWN=3}
77         /* MBeanOperationInfo should throw IllegalArgumentException when impact
78         value is given out of range*/
79         MBeanOperationInfoImpactRangeTest impactRangeTest = new MBeanOperationInfoImpactRangeTest();
80 
81         impactRangeTest.checkInRange(MBeanOperationInfo.INFO);
82         impactRangeTest.checkInRange(MBeanOperationInfo.ACTION);
83         impactRangeTest.checkInRange(MBeanOperationInfo.ACTION_INFO);
84         impactRangeTest.checkInRange(MBeanOperationInfo.UNKNOWN);
85         impactRangeTest.checkOutOfRange(-1);
86         impactRangeTest.checkOutOfRange(4);
87 
88         System.out.println("Test Passed");
89 
90 
91     }
92 }