1 /*
2 * Copyright (c) 2014, 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 package com.oracle.java.testlibrary;
24
25 import com.sun.management.HotSpotDiagnosticMXBean;
26 import com.sun.management.VMOption;
27 import java.lang.management.ManagementFactory;
28
29 /**
30 * Simple class to check writability, invalid and valid values for VMOption
31 */
32 public class DynamicVMOptionChecker {
33
34 /**
35 * Reads VM option from PlatformMXBean and parse it to integer value
36 *
37 * @param name of option
38 * @return parsed value
39 */
40 public static int getIntValue(String name) {
41
42 VMOption option = ManagementFactory.
43 getPlatformMXBean(HotSpotDiagnosticMXBean.class).
44 getVMOption(name);
45
46 return Integer.parseInt(option.getValue());
47 }
48
49 /**
50 * Sets VM option value
51 *
52 * @param name of option
53 * @param value to set
54 */
55 public static void setIntValue(String name, int value) {
56 ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class).setVMOption(name, Integer.toString(value));
57 }
58
59 /**
60 * Checks that VM option is dynamically writable
61 *
62 * @param name
63 * @throws RuntimeException if option if not writable
64 * @return always true
65 */
66 public static boolean checkIsWritable(String name) {
67 VMOption option = ManagementFactory.
68 getPlatformMXBean(HotSpotDiagnosticMXBean.class).
69 getVMOption(name);
70
71 if (!option.isWriteable()) {
72 throw new RuntimeException(name + " is not writable");
73 }
74
75 return true;
76 }
77
78 /**
79 * Checks that value cannot be set
80 *
81 * @param name of flag
82 * @param value string representation of value to set
83 * @throws RuntimeException on error - when expected exception hasn't been thrown
84 */
85 public static void checkInvalidValue(String name, String value) {
86 // should throw
87 try {
88 ManagementFactory.
89 getPlatformMXBean(HotSpotDiagnosticMXBean.class).
90 setVMOption(name, value);
91
92 } catch (IllegalArgumentException e) {
93 return;
94 }
95
96 throw new RuntimeException("Expected IllegalArgumentException was not thrown, " + name + "= " + value);
97 }
98
99 /**
100 * Checks that value can be set
101 *
102 * @param name of flag to set
103 * @param value string representation of value to set
104 * @throws RuntimeException on error - when value in VM is not equal to origin
105 */
106 public static void checkValidValue(String name, String value) {
107 ManagementFactory.
108 getPlatformMXBean(HotSpotDiagnosticMXBean.class).
109 setVMOption(name, value);
110
111 VMOption option = ManagementFactory.
112 getPlatformMXBean(HotSpotDiagnosticMXBean.class).
113 getVMOption(name);
114
115 if (!option.getValue().equals(value)) {
116 throw new RuntimeException("Actual value of " + name + " \"" + option.getValue()
117 + "\" not equal origin \"" + value + "\"");
118 }
119 }
120
121 }
--- EOF ---