KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > ext > logging > LoggingTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 package com.sun.enterprise.management.ext.logging;
25
26 import java.util.Set JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.logging.Level JavaDoc;
33 import java.lang.reflect.Method JavaDoc;
34
35 import java.io.Serializable JavaDoc;
36
37 import javax.management.NotificationListener JavaDoc;
38 import javax.management.Notification JavaDoc;
39 import javax.management.Attribute JavaDoc;
40 import javax.management.ObjectName JavaDoc;
41 import javax.management.ListenerNotFoundException JavaDoc;
42
43 import com.sun.appserv.management.base.AMX;
44 import com.sun.appserv.management.base.Util;
45 import com.sun.appserv.management.base.XTypes;
46 import com.sun.appserv.management.base.QueryMgr;
47
48 import com.sun.appserv.management.j2ee.J2EEServer;
49
50 import com.sun.appserv.management.ext.logging.Logging;
51 import static com.sun.appserv.management.ext.logging.Logging.*;
52 import com.sun.appserv.management.ext.logging.LogModuleNames;
53 import com.sun.appserv.management.ext.logging.LogQueryResult;
54 import com.sun.appserv.management.ext.logging.LogQueryResultImpl;
55 import com.sun.appserv.management.ext.logging.LogQueryEntry;
56
57 import com.sun.appserv.management.util.misc.GSetUtil;
58 import com.sun.appserv.management.util.misc.StringUtil;
59 import com.sun.appserv.management.util.misc.CollectionUtil;
60 import com.sun.appserv.management.util.stringifier.CollectionStringifier;
61
62
63 import com.sun.enterprise.management.AMXTestBase;
64 import com.sun.enterprise.management.Capabilities;
65
66
67 /**
68     Test the {@link Logging}.
69  */

70 public final class LoggingTest extends AMXTestBase
71 {
72         public
73     LoggingTest( )
74     {
75     }
76     
77         public static Capabilities
78     getCapabilities()
79     {
80         return getOfflineCapableCapabilities( false );
81     }
82     
83         final Set JavaDoc<Logging>
84     getAllLogging()
85     {
86         return getQueryMgr().queryJ2EETypeSet( XTypes.LOGGING );
87     }
88     
89     
90         public void
91     testGetModuleLogLevel()
92     {
93         final long start = now();
94         final Set JavaDoc<Logging> loggings = getAllLogging();
95         
96         for( final Logging logging : loggings )
97         {
98             for( final String JavaDoc moduleName : LogModuleNames.ALL_NAMES )
99             {
100                 final String JavaDoc level = logging.getModuleLogLevel( moduleName );
101             }
102         }
103         printElapsed( "testGetModuleLogLevel", loggings.size(), start );
104     }
105     
106        public void
107     testSetModuleLogLevel()
108     {
109         final long start = now();
110         final Set JavaDoc<Logging> loggings = getAllLogging();
111         
112         for( final Logging logging : loggings )
113         {
114             for( final String JavaDoc moduleName : LogModuleNames.ALL_NAMES )
115             {
116                 final String JavaDoc level = logging.getModuleLogLevel( moduleName );
117                 logging.setModuleLogLevel( moduleName, "FINEST" );
118                 logging.setModuleLogLevel( moduleName, level );
119             }
120         }
121         printElapsed( "testSetModuleLogLevel", loggings.size(), start );
122     }
123     
124         public void
125     testGetLogFileNames()
126     {
127         final long start = now();
128         final Set JavaDoc<Logging> loggings = getAllLogging();
129         
130         for( final Logging logging : loggings )
131         {
132             final String JavaDoc[] serverLogs = logging.getLogFileNames( SERVER_KEY );
133             assert( serverLogs.length != 0 );
134             
135             //final String[] accessLogs = logging.getLogFileNames( ACCESS_KEY );
136
//assert( accessLogs.length != 0 );
137
}
138         printElapsed( "testGetLogFileNames", loggings.size(), start );
139     }
140     
141         public void
142     testGetLogFileKeys()
143     {
144         final long start = now();
145         final Set JavaDoc<Logging> loggings = getAllLogging();
146         
147         for( final Logging logging : loggings )
148         {
149             final String JavaDoc[] keys = logging.getLogFileKeys();
150             final Set JavaDoc<String JavaDoc> keysSet = GSetUtil.newSet( keys );
151             assert( keysSet.contains( SERVER_KEY ) );
152             assert( keysSet.contains( ACCESS_KEY ) );
153             assertEquals( 2, keysSet.size() );
154         }
155         printElapsed( "testGetLogFileKeys", loggings.size(), start );
156     }
157     
158     /*
159     !!!
160         Testing these is problematic because if they run
161         before the other tests, they create nearly-empty log files
162         
163         public void
164     testRotateAllLogFiles()
165     {
166        logging.rotateAllLogFiles();
167         how to verify?
168     }
169     
170         public void
171     testRotateLogFile()
172     {
173         warning( "testRotateLogFile: SKIPPING TEST" );
174         logging.rotateLogFile( SERVER_KEY );
175         logging.rotateLogFile( ACCESS_KEY );
176     }
177      */

178     
179         public void
180     testGetLogFile()
181     {
182         final long start = now();
183         final Set JavaDoc<Logging> loggings = getAllLogging();
184         
185         for( final Logging logging : loggings )
186         {
187             final String JavaDoc[] serverLogs = logging.getLogFileNames( SERVER_KEY );
188             assert( serverLogs.length != 0 );
189             
190             for( final String JavaDoc name : serverLogs )
191             {
192                 final long startGet = now();
193                 final String JavaDoc contents = logging.getLogFile( SERVER_KEY, name );
194                 assert( contents != null && contents.length() != 0 );
195                 printVerbose( "Log file " + StringUtil.quote( name ) +
196                 " has size " + (contents.length() / 1024 ) + "K, ms = " + (now() - startGet) );
197             }
198         }
199         printElapsed( "testGetLogFile", loggings.size(), start );
200     }
201     
202     
203         public void
204     testGetErrorInfo()
205     {
206         final long start = now();
207         final Set JavaDoc<Logging> loggings = getAllLogging();
208
209         for( final Logging logging : loggings )
210         {
211             final Map JavaDoc<String JavaDoc,Number JavaDoc>[] infos = logging.getErrorInfo();
212             assert( infos != null );
213             for( final Map JavaDoc<String JavaDoc,Number JavaDoc> info : infos )
214             {
215                assert( info != null && info.keySet().size() >= 3 );
216                assert( info.containsKey( TIMESTAMP_KEY ) );
217                assert( info.containsKey( SEVERE_COUNT_KEY ) );
218                assert( info.containsKey( WARNING_COUNT_KEY ) );
219             }
220         }
221         printElapsed( "testGetErrorInfo", loggings.size(), start );
222     }
223     
224     
225         public void
226     testGetErrorDistribution()
227     {
228         final long start = now();
229         final Set JavaDoc<Logging> loggings = getAllLogging();
230         
231         for( final Logging logging : loggings )
232         {
233             final Map JavaDoc<String JavaDoc,Number JavaDoc>[] infos = logging.getErrorInfo();
234             assert( infos != null );
235             for( final Map JavaDoc<String JavaDoc,Number JavaDoc> info : infos )
236             {
237                final Long JavaDoc timeStamp = (Long JavaDoc)info.get( TIMESTAMP_KEY );
238                assert( timeStamp != null );
239                final Integer JavaDoc severeCount = (Integer JavaDoc)info.get( SEVERE_COUNT_KEY );
240                assert( severeCount != null );
241                final Integer JavaDoc warningCount = (Integer JavaDoc)info.get( WARNING_COUNT_KEY );
242                assert( warningCount != null );
243                
244                final Map JavaDoc<String JavaDoc,Integer JavaDoc> warnings =
245                     logging.getErrorDistribution( timeStamp.longValue(), "WARNING" );
246                assert( warnings != null );
247                for( final String JavaDoc moduleID : warnings.keySet() )
248                {
249                     assert( warnings.get( moduleID) != null );
250                }
251                
252                final Map JavaDoc<String JavaDoc,Integer JavaDoc> severes =
253                     logging.getErrorDistribution( timeStamp, "SEVERE" );
254                assert( severes != null );
255                for( final String JavaDoc moduleID : severes.keySet() )
256                {
257                     assert( severes.get( moduleID) != null );
258                }
259                // verify that there are no illegal keys.
260
final String JavaDoc[] loggerNames = logging.getLoggerNames();
261                final Set JavaDoc<String JavaDoc> illegal = GSetUtil.copySet( severes.keySet() );
262                illegal.removeAll( GSetUtil.newSet( loggerNames ) );
263                assert( illegal.size() == 0 ) :
264                 "Illegal logger names found in Map returned by getErrorDistribution: {" +
265                 CollectionUtil.toString( illegal, ",") + "}";
266              
267                
268                try
269                {
270                     logging.getErrorDistribution( timeStamp, "INFO" );
271                     fail( "expecting failure from getErrorDistribution( timeStamp, 'INFO' )" );
272                }
273                catch( Exception JavaDoc e )
274                {
275                }
276            }
277        }
278        printElapsed( "testGetErrorDistribution", loggings.size(), start );
279     }
280     
281     
282         public void
283     testGetLoggerNamesAndGetLoggerNamesUnder()
284     {
285         final Set JavaDoc<Logging> loggings = getAllLogging();
286         
287         for( final Logging logging : loggings )
288         {
289             final String JavaDoc[] names = logging.getLoggerNames();
290             assert( names != null && names.length != 0 );
291             
292             for( final String JavaDoc loggerName : names )
293             {
294                 final String JavaDoc[] under = logging.getLoggerNamesUnder( loggerName );
295                 assert( under != null );
296             }
297         }
298         
299     }
300     
301         public void
302     testQuery()
303     {
304         final long start = now();
305         final Set JavaDoc<Logging> loggings = getAllLogging();
306         
307         for( final Logging logging : loggings )
308         {
309             final String JavaDoc filename = Logging.MOST_RECENT_NAME;
310             final int startRecord = FIRST_RECORD;
311             final boolean searchForward = true;
312             final int requestedCount = ALL_RECORDS;
313             final Long JavaDoc fromTime = now() - (24 * 60 * 60 * 1000);
314             final Long JavaDoc toTime = now();
315             final Set JavaDoc<String JavaDoc> modules = LogModuleNames.ALL_NAMES; // all
316
final List JavaDoc<Attribute JavaDoc> attrs = null;
317   
318             final LogQueryResult result = logging.queryServerLog(
319                 filename,
320                 startRecord,
321                 searchForward,
322                 requestedCount,
323                 fromTime,
324                 toTime,
325                 Level.WARNING.toString(),
326                 modules,
327                 attrs );
328            
329            final String JavaDoc[] fieldNames = result.getFieldNames();
330            
331            for( final LogQueryEntry entry : result.getEntries() )
332            {
333                 final String JavaDoc messageID = entry.getMessageID();
334                 
335                 final String JavaDoc[] causes = logging.getDiagnosticCauses( messageID );
336
337                 final String JavaDoc[] checks = logging.getDiagnosticChecks( messageID );
338
339                 final String JavaDoc uri = logging.getDiagnosticURI( messageID );
340            }
341        }
342        printElapsed( "testQuery", loggings.size(), start );
343     }
344     
345     
346         private Map JavaDoc<String JavaDoc,J2EEServer>
347     getAllJ2EEServer()
348     {
349         final Map JavaDoc<String JavaDoc,J2EEServer> servers =
350             getDomainRoot().getJ2EEDomain().getJ2EEServerMap();
351         
352         return servers;
353     }
354     
355         public void
356     testHaveLoggingForEachRunningServer()
357     {
358         final long start = now();
359         
360         final Map JavaDoc<String JavaDoc,J2EEServer> servers = getAllJ2EEServer();
361         for( final J2EEServer server : servers.values() )
362         {
363             final int state = server.getstate();
364             final String JavaDoc serverName = server.getName();
365             if ( state != J2EEServer.STATE_RUNNING )
366             {
367                 warning( "testHaveLoggingForEachRunningServer: server " +
368                     serverName + " is not running" );
369             }
370             else
371             {
372                 final Set JavaDoc<Logging> loggings = getAllLogging();
373                 final Set JavaDoc<ObjectName JavaDoc> loggingsObjectNames = Util.toObjectNames( loggings );
374                 final Map JavaDoc<String JavaDoc,Logging> all =
375                     Util.createNameMap( loggings );
376                 assert( all.containsKey( serverName ) ) :
377                 "Can't find Logging for server " + serverName +
378                 ", have: {" +
379                 CollectionStringifier.toString( loggingsObjectNames, ", ") + "}";
380             }
381         }
382         printElapsed( "testHaveLoggingForEachRunningServer", servers.size(), start );
383     }
384     
385     private final class MyListener implements NotificationListener JavaDoc
386     {
387         private final String JavaDoc mID;
388         
389         public MyListener( final String JavaDoc id)
390         {
391             mNotifs = Collections.synchronizedList( new ArrayList JavaDoc<Notification JavaDoc>() );
392             mID = id;
393         }
394         
395         private final List JavaDoc<Notification JavaDoc> mNotifs;
396         
397             public void
398         handleNotification(
399             final Notification JavaDoc notif,
400             final Object JavaDoc handback )
401         {
402            //printVerbose( "Received notif: " + notif );
403

404             if ( notif.getMessage().indexOf( mID ) >= 0 )
405             {
406                 mNotifs.add( notif );
407             }
408         }
409         
410             public List JavaDoc<Notification JavaDoc>
411         getNotifications()
412         {
413             return Collections.unmodifiableList( mNotifs );
414         }
415         
416             public int
417         getNotificationCount()
418         {
419             return mNotifs.size();
420         }
421     }
422     
423     
424         private void
425     testListening( final Logging logging )
426         throws ListenerNotFoundException JavaDoc
427     {
428         final String JavaDoc message= "testListening:" + now();
429             
430         final MyListener listener = new MyListener( message );
431         
432         printVerbose( "Testing Logging=" + logging.getName() );
433         
434         logging.addNotificationListener( listener, null, null);
435         try
436         {
437             logging.testEmitLogMessage( Level.SEVERE.toString(), message);
438             logging.testEmitLogMessage( Level.WARNING.toString(), message);
439             logging.testEmitLogMessage( Level.CONFIG.toString(), message);
440             logging.testEmitLogMessage( Level.INFO.toString(), message);
441             logging.testEmitLogMessage( Level.FINE.toString(), message);
442             logging.testEmitLogMessage( Level.FINER.toString(), message);
443             logging.testEmitLogMessage( Level.FINEST.toString(), message);
444             
445             int count =0;
446             mySleep( 200 );
447             while ( (count = listener.getNotificationCount()) != 7 )
448             {
449                 mySleep( 500 );
450                 trace( "testListening: waiting for 7 Notifications, have: " + count );
451             }
452         }
453         finally
454         {
455             logging.removeNotificationListener( listener);
456         }
457     }
458     
459         public void
460     testListeningForAllLogging()
461         throws ListenerNotFoundException JavaDoc
462     {
463         final long start = now();
464         final Set JavaDoc<Logging> loggings = getAllLogging();
465
466         for( final Logging logging : loggings )
467         {
468             testListening( logging );
469         }
470         printElapsed( "testListeningForAllLogging", loggings.size(), start );
471     }
472     
473     
474         public void
475     testNoStrayLogging()
476         throws ListenerNotFoundException JavaDoc
477     {
478         final Set JavaDoc<String JavaDoc> validNames = getAllJ2EEServer().keySet();
479         final Set JavaDoc<Logging> loggings = getAllLogging();
480
481         assert( validNames.size() >= loggings.size() );
482         
483         for( final Logging logging : loggings )
484         {
485             assert validNames.contains( logging.getName() ) :
486                 "Logging MBean name doesn't match any J2EEServer: " + logging.getName();
487         }
488     }
489
490 }
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
Popular Tags