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