|
1 /* |
|
2 * Copyright (c) 2003, 2016, 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 5007165 |
|
27 * |
|
28 * @summary Basic Test for LoggingMXBean via MBeanServer |
|
29 * @author Ron Mann |
|
30 * @modules java.logging |
|
31 * java.management |
|
32 * @build LoggingMXBeanTest |
|
33 * @run main LoggingMXBeanTest |
|
34 */ |
|
35 |
|
36 import javax.management.*; |
|
37 import java.util.logging.*; |
|
38 |
|
39 |
|
40 public class LoggingMXBeanTest |
|
41 { |
|
42 |
|
43 LoggingMXBean mBean; |
|
44 ObjectName objectName = null; |
|
45 static String LOGGER_NAME_1 = "com.sun.management.Logger1"; |
|
46 static String LOGGER_NAME_2 = "com.sun.management.Logger2"; |
|
47 static Logger logger1; |
|
48 static Logger logger2; |
|
49 |
|
50 public LoggingMXBeanTest() throws Exception { |
|
51 |
|
52 /* |
|
53 * Create the MBeanServeri, register the LoggingMXBean |
|
54 */ |
|
55 System.out.println( "***************************************************" ); |
|
56 System.out.println( "********** LoggingMXBean Unit Test **********" ); |
|
57 System.out.println( "***************************************************" ); |
|
58 System.out.println( "" ); |
|
59 System.out.println( "*******************************" ); |
|
60 System.out.println( "*********** Phase 1 ***********" ); |
|
61 System.out.println( "*******************************" ); |
|
62 System.out.println( " Creating MBeanServer " ); |
|
63 System.out.print( " Register LoggingMXBean: " ); |
|
64 MBeanServer mbs = MBeanServerFactory.createMBeanServer(); |
|
65 String[] list = new String[0]; |
|
66 |
|
67 try { |
|
68 objectName = new ObjectName(LogManager.LOGGING_MXBEAN_NAME); |
|
69 mBean = LogManager.getLoggingMXBean(); |
|
70 mbs.registerMBean( mBean, objectName ); |
|
71 } |
|
72 catch ( Exception e ) { |
|
73 System.out.println( "FAILED" ); |
|
74 throw e; |
|
75 } |
|
76 System.out.println( "PASSED" ); |
|
77 System.out.println(""); |
|
78 |
|
79 /* |
|
80 * Access our MBean to get the current list of Loggers |
|
81 */ |
|
82 System.out.println( "*******************************" ); |
|
83 System.out.println( "*********** Phase 2 ***********" ); |
|
84 System.out.println( "*******************************" ); |
|
85 System.out.println( " Test Logger Name retrieval (getLoggerNames) " ); |
|
86 // check that Level object are returned properly |
|
87 try { |
|
88 list = (String[]) mbs.getAttribute( objectName, "LoggerNames" ); |
|
89 } |
|
90 catch ( Exception e ) { |
|
91 System.out.println(" : FAILED" ); |
|
92 throw e; |
|
93 } |
|
94 |
|
95 /* |
|
96 * Dump the list of Loggers already present, if any |
|
97 */ |
|
98 Object[] params = new Object[1]; |
|
99 String[] signature = new String[1]; |
|
100 Level l; |
|
101 |
|
102 if ( list == null ) { |
|
103 System.out.println(" : PASSED. No Standard Loggers Present" ); |
|
104 System.out.println(""); |
|
105 } |
|
106 else { |
|
107 System.out.println(" : PASSED. There are " + list.length + " Loggers Present" ); |
|
108 System.out.println(""); |
|
109 System.out.println( "*******************************" ); |
|
110 System.out.println( "*********** Phase 2B **********" ); |
|
111 System.out.println( "*******************************" ); |
|
112 System.out.println( " Examine Existing Loggers" ); |
|
113 for ( int i = 0; i < list.length; i++ ) { |
|
114 try { |
|
115 params[0] = list[i]; |
|
116 signature[0] = "java.lang.String"; |
|
117 String levelName = (String) mbs.invoke( objectName, "getLoggerLevel", params, signature ); |
|
118 System.out.println(" : Logger #" + i + " = " + list[i] ); |
|
119 System.out.println(" : Level = " + levelName ); |
|
120 } |
|
121 catch ( Exception e ) { |
|
122 System.out.println(" : FAILED" ); |
|
123 throw e; |
|
124 } |
|
125 } |
|
126 System.out.println(" : PASSED" ); |
|
127 } |
|
128 |
|
129 /* |
|
130 * Create two new loggers to the list of Loggers already present |
|
131 */ |
|
132 System.out.println(""); |
|
133 System.out.println( "*******************************" ); |
|
134 System.out.println( "*********** Phase 3 ***********" ); |
|
135 System.out.println( "*******************************" ); |
|
136 System.out.println( " Create and test new Loggers" ); |
|
137 logger1 = Logger.getLogger( LOGGER_NAME_1 ); |
|
138 logger2 = Logger.getLogger( LOGGER_NAME_2 ); |
|
139 |
|
140 // check that Level object are returned properly |
|
141 try { |
|
142 list = (String[]) mbs.getAttribute( objectName, "LoggerNames" ); |
|
143 } |
|
144 catch ( Exception e ) { |
|
145 System.out.println(" : FAILED" ); |
|
146 throw e; |
|
147 } |
|
148 |
|
149 /* |
|
150 * Check for the existence of our new Loggers |
|
151 */ |
|
152 boolean log1 = false, log2 = false; |
|
153 |
|
154 if ( list == null || list.length < 2 ) { |
|
155 System.out.println(" : FAILED. Could not Detect the presense of the new Loggers" ); |
|
156 throw new RuntimeException( |
|
157 "Could not Detect the presense of the new Loggers"); |
|
158 } |
|
159 else { |
|
160 for ( int i = 0; i < list.length; i++ ) { |
|
161 if ( list[i].equals( LOGGER_NAME_1 ) ) { |
|
162 log1 = true; |
|
163 System.out.println( " : Found new Logger : " + list[i] ); |
|
164 } |
|
165 if ( list[i].equals( LOGGER_NAME_2 ) ) { |
|
166 log2 = true; |
|
167 System.out.println( " : Found new Logger : " + list[i] ); |
|
168 } |
|
169 } |
|
170 if ( log1 && log2 ) |
|
171 System.out.println( " : PASSED." ); |
|
172 else { |
|
173 System.out.println( " : FAILED. Could not Detect the new Loggers." ); |
|
174 throw new RuntimeException( |
|
175 "Could not Detect the presense of the new Loggers"); |
|
176 } |
|
177 } |
|
178 |
|
179 /* |
|
180 * Set a new Logging levels and check that it succeeded |
|
181 */ |
|
182 System.out.println(""); |
|
183 System.out.println( "*******************************" ); |
|
184 System.out.println( "*********** Phase 4 ***********" ); |
|
185 System.out.println( "*******************************" ); |
|
186 System.out.println( " Set and Check the Logger Level" ); |
|
187 log1 = false; |
|
188 log2 = false; |
|
189 try { |
|
190 // Set the level of logger1 to ALL |
|
191 params = new Object[2]; |
|
192 signature = new String[2]; |
|
193 params[0] = LOGGER_NAME_1; |
|
194 params[1] = Level.ALL.getName(); |
|
195 signature[0] = "java.lang.String"; |
|
196 signature[1] = "java.lang.String"; |
|
197 mbs.invoke( objectName, "setLoggerLevel", params, signature ); |
|
198 |
|
199 // Set the level of logger2 to FINER |
|
200 params[0] = LOGGER_NAME_2; |
|
201 params[1] = Level.FINER.getName(); |
|
202 mbs.invoke( objectName, "setLoggerLevel", params, signature ); |
|
203 |
|
204 // Okay read back the Level from Logger1. Should be ALL |
|
205 params = new Object[1]; |
|
206 signature = new String[1]; |
|
207 params[0] = LOGGER_NAME_1; |
|
208 signature[0] = "java.lang.String"; |
|
209 String levelName = (String) mbs.invoke( objectName, "getLoggerLevel", params, signature ); |
|
210 l = Level.parse(levelName); |
|
211 System.out.print(" Logger1: " ); |
|
212 if ( l.equals( Level.ALL ) ) { |
|
213 System.out.println("Level Set to ALL: PASSED" ); |
|
214 log1 = true; |
|
215 } |
|
216 else { |
|
217 System.out.println("Level Set to ALL: FAILED" ); |
|
218 throw new RuntimeException( |
|
219 "Level Set to ALL but returned " + l.toString()); |
|
220 } |
|
221 |
|
222 // Okay read back the Level from Logger2. Should be FINER |
|
223 params = new Object[1]; |
|
224 signature = new String[1]; |
|
225 params[0] = LOGGER_NAME_2; |
|
226 signature[0] = "java.lang.String"; |
|
227 levelName = (String) mbs.invoke( objectName, "getLoggerLevel", params, signature ); |
|
228 l = Level.parse(levelName); |
|
229 System.out.print(" Logger2: " ); |
|
230 if ( l.equals( Level.FINER ) ) { |
|
231 System.out.println("Level Set to FINER: PASSED" ); |
|
232 log2 = true; |
|
233 } |
|
234 else { |
|
235 System.out.println("Level Set to FINER: FAILED" ); |
|
236 throw new RuntimeException( |
|
237 "Level Set to FINER but returned " + l.toString()); |
|
238 } |
|
239 } |
|
240 catch ( Exception e ) { |
|
241 throw e; |
|
242 } |
|
243 |
|
244 System.out.println( "" ); |
|
245 System.out.println( "***************************************************" ); |
|
246 System.out.println( "***************** All Tests Passed ****************" ); |
|
247 System.out.println( "***************************************************" ); |
|
248 } |
|
249 |
|
250 public static void main(String[] argv) throws Exception { |
|
251 new LoggingMXBeanTest(); |
|
252 } |
|
253 } |