KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > monitor > MonitorTest


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 package com.sun.enterprise.management.monitor;
24
25 import javax.management.ObjectName JavaDoc;
26
27 import java.util.Set JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Arrays JavaDoc;
33
34 import java.lang.reflect.Method JavaDoc;
35 import java.lang.reflect.InvocationTargetException JavaDoc;
36
37 import javax.management.j2ee.statistics.Statistic JavaDoc;
38 import javax.management.j2ee.statistics.Stats JavaDoc;
39 import javax.management.j2ee.statistics.CountStatistic JavaDoc;
40 import javax.management.j2ee.statistics.BoundaryStatistic JavaDoc;
41 import javax.management.j2ee.statistics.RangeStatistic JavaDoc;
42 import javax.management.j2ee.statistics.BoundedRangeStatistic JavaDoc;
43 import javax.management.j2ee.statistics.TimeStatistic JavaDoc;
44
45 import javax.management.openmbean.CompositeData JavaDoc;
46 import javax.management.openmbean.CompositeDataSupport JavaDoc;
47             
48 import com.sun.appserv.management.base.AMX;
49 import com.sun.appserv.management.base.Util;
50
51 import com.sun.appserv.management.monitor.MonitoringStats;
52 import com.sun.appserv.management.monitor.MonitoringRoot;
53 import com.sun.appserv.management.monitor.ServerRootMonitor;
54 import com.sun.appserv.management.monitor.TransactionServiceMonitor;
55
56 import com.sun.appserv.management.util.misc.GSetUtil;
57 import com.sun.appserv.management.util.misc.ExceptionUtil;
58 import com.sun.appserv.management.util.misc.CollectionUtil;
59 import com.sun.appserv.management.util.misc.StringUtil;
60 import com.sun.appserv.management.util.jmx.JMXUtil;
61 import com.sun.appserv.management.util.j2ee.J2EEUtil;
62
63 import com.sun.appserv.management.config.ModuleMonitoringLevelsConfig;
64
65 import com.sun.appserv.management.j2ee.statistics.*;
66
67
68 public final class MonitorTest
69     extends AMXMonitorTestBase
70 {
71         public
72     MonitorTest()
73     {
74     }
75
76     
77
78         public void
79     checkStatisticNames( final MonitoringStats mon )
80     {
81         final Stats JavaDoc stats = mon.getStats();
82         
83         final Set JavaDoc<String JavaDoc> namesFromMon = GSetUtil.newStringSet( mon.getStatisticNames() );
84         final Set JavaDoc<String JavaDoc> namesFromStats = GSetUtil.newStringSet( stats.getStatisticNames() );
85         
86         assert( namesFromStats.equals( namesFromMon ) ):
87             "statistic names from stats.getStatisticNames() != mon.getStatisticNames(): " +
88             namesFromStats + " != " + namesFromMon;
89     }
90     
91         public void
92     checkNumStatistics( final MonitoringStats mon )
93     {
94         final Stats JavaDoc stats = mon.getStats();
95         assert( stats != null ) : "null Stats from: " + Util.getObjectName( mon );
96         final String JavaDoc[] allNames = mon.getStatisticNames();
97         
98         final Statistic JavaDoc[] statistics = mon.getStatistics( allNames );
99         assert( statistics.length == allNames.length ) :
100             "wrong number of statistics from: " + Util.getObjectName( mon ) +
101             ", got " + statistics.length + ", should be " + allNames.length;
102     }
103     
104         public void
105     checkOpenStats( final MonitoringStats mon )
106     {
107         final CompositeDataSupport JavaDoc openStats = mon.getOpenStats();
108         assert( openStats != null ) : "null OpenStats from: " + Util.getObjectName( mon );
109         
110         final StatsImpl stats = new StatsImpl( openStats );
111         
112         final Set JavaDoc<String JavaDoc> fromOpenStats = GSetUtil.newStringSet( stats.getStatisticNames() );
113         final Set JavaDoc<String JavaDoc> fromStats = GSetUtil.newStringSet( mon.getStats().getStatisticNames() );
114         assert( fromOpenStats.equals( fromStats ) ) :
115             "openStats Statistic names don't match Stats Statistic names: " +
116             fromOpenStats + " != " + fromStats;
117     }
118     
119         private final boolean
120     isLegalStatistic( final Statistic JavaDoc s)
121     {
122         // current, we do not allow MapStatistic as these types cover all
123
return( (s instanceof CountStatistic JavaDoc) ||
124             (s instanceof BoundaryStatistic JavaDoc) ||
125             (s instanceof RangeStatistic JavaDoc) ||
126             (s instanceof BoundedRangeStatistic JavaDoc) ||
127             (s instanceof TimeStatistic JavaDoc ) ||
128             (s instanceof StringStatistic ) );
129     }
130     
131         private final boolean
132     isLegalStatisticImpl( final Statistic JavaDoc s)
133     {
134         boolean isLegal = isLegalStatistic( s );
135         if ( isLegal )
136         {
137             final Class JavaDoc theClass = s.getClass();
138             
139             if( (theClass == CountStatisticImpl.class) ||
140                 (theClass == BoundaryStatisticImpl.class) ||
141                 (theClass == RangeStatisticImpl.class) ||
142                 (theClass == BoundedRangeStatisticImpl.class) ||
143                 (theClass == TimeStatisticImpl.class ) ||
144                 (theClass == StringStatisticImpl.class ) );
145         }
146         return( isLegal );
147     }
148     
149     
150         private void
151     checkLegalStatistic(
152         final ObjectName JavaDoc objectName,
153         final Statistic JavaDoc s )
154     {
155         assert( isLegalStatistic( s ) ) : "Statistic " + s.getName() +
156             " in \"" + objectName +
157             "\" is not a known type of Statistic";
158             
159         assert( isLegalStatisticImpl( s ) ) : "Statistic " + s.getName() +
160             " in \"" + objectName +
161             "\" uses an implementation not intended by the API: " +
162             s.getClass();
163     }
164     
165         public void
166     checkGetStatistic( final MonitoringStats mon )
167     {
168         final Stats JavaDoc stats = mon.getStats();
169         
170         final ObjectName JavaDoc objectName = Util.getObjectName( mon );
171         
172         final String JavaDoc[] names = mon.getStatisticNames();
173         for( int i = 0; i < names.length; ++i )
174         {
175             final String JavaDoc name = names[ i ];
176             final Statistic JavaDoc s = mon.getStatistic( name );
177             assert( s != null );
178             assert( s.getName().equals( name ) );
179             
180             checkLegalStatistic( objectName, s );
181         }
182     }
183     
184         public void
185     checkGetStats( final MonitoringStats mon )
186     {
187         final Stats JavaDoc stats = mon.getStats();
188         
189         final ObjectName JavaDoc objectName = Util.getObjectName( mon );
190         
191         final String JavaDoc[] names = stats.getStatisticNames();
192         for( int i = 0; i < names.length; ++i )
193         {
194             final Statistic JavaDoc s = stats.getStatistic( names[ i ] );
195             assert( s != null );
196             assert( s.getName().equals( names[ i ] ) );
197             
198             checkLegalStatistic( objectName, s );
199         }
200     }
201     
202         public void
203     checkGetOpenStatistic( final MonitoringStats mon )
204     {
205         final Stats JavaDoc stats = mon.getStats();
206         
207         final String JavaDoc[] names = mon.getStatisticNames();
208         for( int i = 0; i < names.length; ++i )
209         {
210             final String JavaDoc name = names[ i ];
211             
212             final CompositeData JavaDoc d = mon.getOpenStatistic( name );
213             final Statistic JavaDoc s = StatisticFactory.create( d );
214             final Statistic JavaDoc s2 = mon.getStatistic( name );
215             
216             assert( s.getName().equals( name ) );
217             // values may have changed, but check the static fields
218
}
219         
220         final CompositeDataSupport JavaDoc[] all = mon.getOpenStatistics( names );
221         assert( all != null );
222         assert( all.length == names.length );
223     }
224     
225         public void
226     checkMonitoringStats( final ObjectName JavaDoc objectName )
227         throws Exception JavaDoc
228     {
229         final MonitoringStats mon = getProxyFactory().getProxy( objectName, MonitoringStats.class);
230         
231         mon.refresh();
232         mon.getStatsInterfaceName();
233         
234         checkNumStatistics( mon );
235         
236         checkStatisticNames( mon );
237         
238         checkGetStatistic( mon );
239         
240         checkGetStats( mon );
241         
242         checkGetOpenStatistic( mon );
243         
244         checkOpenStats( mon );
245     }
246     
247         private Set JavaDoc<MonitoringStats>
248     getAllMonitoringStats()
249         throws ClassNotFoundException JavaDoc
250     {
251         final long start = now();
252         
253         final Set JavaDoc<MonitoringStats> all =
254             getQueryMgr().queryInterfaceSet( MonitoringStats.class.getName(), null );
255             
256         for( final MonitoringStats stats : all )
257         {
258         }
259         
260         printElapsed( "getAllMonitoringStats", all.size(), start );
261         
262         return( all );
263     }
264     
265     
266     
267         private String JavaDoc
268     getterToName( final String JavaDoc getterName )
269     {
270         return StringUtil.stripPrefix( getterName, JMXUtil.GET );
271     }
272     
273         private String JavaDoc[]
274     methodsToNames( final Method JavaDoc[] methods )
275     {
276         final String JavaDoc[] result = new String JavaDoc[ methods.length ];
277         
278         for( int i = 0; i < methods.length; ++i )
279         {
280             result[ i ] = getterToName( methods[ i ].getName() );
281         }
282         
283         Arrays.sort( result );
284         return( result );
285     }
286     
287         public void
288     checkStats(
289         final MonitoringStats mon,
290         final Stats JavaDoc stats )
291         throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc
292     {
293         final ObjectName JavaDoc objectName = Util.getObjectName( mon );
294         
295     trace( "checkStats: " + objectName );
296         
297         final Method JavaDoc[] methodsViaNames = J2EEUtil.getStatisticGetterMethodsUsingNames( stats );
298         
299         final Method JavaDoc[] methods = stats.getClass().getMethods();
300         
301         final Set JavaDoc<String JavaDoc> statisticNames = GSetUtil.newSet( stats.getStatisticNames() );
302         
303         for( int methodIdx = 0; methodIdx < methodsViaNames.length; ++methodIdx )
304         {
305             final Method JavaDoc method = methodsViaNames[ methodIdx ];
306             final String JavaDoc methodName = method.getName();
307             
308             final Class JavaDoc<?> returnType = method.getReturnType();
309             
310             final String JavaDoc statisticName = getterToName( methodName );
311             if ( ! statisticNames.contains( statisticName ) )
312             {
313                 warning( "Statistic " + quote( statisticName ) + " as derived from " + method +
314                     " missing from " + quote( objectName ) +
315                     " available names = " + toString( statisticNames ) );
316             }
317             
318             try
319             {
320                 final Object JavaDoc o = method.invoke( stats, (Object JavaDoc[])null );
321                 assert( o != null );
322                 assert( Statistic JavaDoc.class.isAssignableFrom( o.getClass() ) );
323                 
324                 assert( returnType.isAssignableFrom( o.getClass() ) ) :
325                     "Method " + methodName + " of MBean " + objectName +
326                     " returned object not assignable to " + returnType.getName();
327
328                 final Statistic JavaDoc stat = (Statistic JavaDoc)method.invoke( stats, (Object JavaDoc[])null );
329                 assert( method.getReturnType().isAssignableFrom( stat.getClass() ) );
330                 
331                 final Statistic JavaDoc s = mon.getStatistic( stat.getName() );
332                 assert( returnType.isAssignableFrom( s.getClass() ) ) :
333                     "getStatistic() of MBean " + objectName +
334                     " returned Statistic not assignable to " + returnType.getName();
335                     
336                 //printVerbose( "Verified " + stat.getClass().getName() + " " + stat.getName() );
337
}
338             catch( Exception JavaDoc e )
339             {
340                 final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( e );
341                 
342                 warning(
343                 "Failure calling " + method + " on Stats for " + objectName + " = " +
344                     rootCause.getClass().getName() + "\n" +
345                     "Statistic names = " + toString( stats.getStatisticNames() ) );
346             }
347         }
348     }
349     
350         public void
351     checkAllStats( final ObjectName JavaDoc objectName )
352         throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc
353     {
354         trace( "checkAllStats: " + objectName );
355     
356         final MonitoringStats mon =
357             getProxyFactory().getProxy( objectName, MonitoringStats.class);
358         
359         final Method JavaDoc[] methods = mon.getClass().getMethods();
360         
361         final Method JavaDoc specificStatsMethod = getSpecificStatsGetterMethod( mon );
362
363
364         // the type of Stats returned from getStats() should be the same as the type
365
// returned from the (only) specific getAbcStats()
366
final Stats JavaDoc plainStats = mon.getStats();
367         assert( specificStatsMethod.getReturnType().isAssignableFrom( plainStats.getClass() ) ) :
368             "Stats returned from " + objectName + " getStats() should be assignable to " +
369                 specificStatsMethod.getReturnType().getName();
370         checkStats( mon, plainStats );
371         
372         Stats JavaDoc stats = null;
373         try
374         {
375             // verify that we can get it
376
stats = (Stats JavaDoc)specificStatsMethod.invoke( mon, (Object JavaDoc[])null );
377         }
378         catch( Exception JavaDoc e )
379         {
380             final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( e );
381             
382             failure(
383                 "Failure calling " + specificStatsMethod.getName() + "() on " + objectName + " = " +
384                 rootCause.getClass().getName() );
385         }
386             
387         assert( plainStats.getClass() == stats.getClass() );
388         checkStats( mon, stats );
389     }
390     
391     /**
392         Test the MonitoringStats interface.
393      */

394         public void
395     testMonitoringStats()
396         throws Exception JavaDoc
397     {
398         final long start = now();
399         
400         final Set JavaDoc<MonitoringStats> all = getAllMonitoringStats();
401         
402         testAll( Util.toObjectNames( all ), "checkMonitoringStats" );
403         
404         printElapsed( "testMonitoringStats", all.size(), start );
405     }
406     
407         public void
408     xtestStats()
409         throws Exception JavaDoc
410     {
411         trace( "testStats: ");
412         final long start = now();
413         
414         final Set JavaDoc<MonitoringStats> all = getAllMonitoringStats();
415         assert( all.size() >= 10 ) : "Monitoring is not turned on";
416
417         //final Set all = getQueryMgr().queryInterfaceSet( com.sun.appserv.management.monitor.HTTPServiceVirtualServerMonitor.class.getName(), null );
418

419         testAll( Util.toObjectNames( all ), "checkAllStats" );
420         
421         printElapsed( "testStats", all.size(), start );
422     }
423     
424     
425     /**
426         Get the specific (non-generic) Stats getter. Example:
427         getJVMStats() versus plain getStats().
428      */

429         public Method JavaDoc
430     getSpecificStatsGetterMethod( final MonitoringStats mon )
431         throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc
432     {
433         final Method JavaDoc[] methods = mon.getClass().getMethods();
434         
435         Method JavaDoc result = null;
436         
437         for( int methodIdx = 0; methodIdx < methods.length; ++methodIdx )
438         {
439             final Method JavaDoc method = methods[ methodIdx ];
440             final String JavaDoc methodName = method.getName();
441
442             if ( JMXUtil.isGetter( method ) && ! methodName.equals( "getStats" ) &&
443                 Stats JavaDoc.class.isAssignableFrom( method.getReturnType() ) &&
444                 method.getParameterTypes().length == 0 )
445             {
446                 result = method;
447                 break;
448             }
449         }
450         
451         if ( result == null )
452         {
453             throw new NoSuchMethodException JavaDoc( "Can't find specific Stats getter in " +
454                 quote( Util.getObjectName( mon ) ) );
455         }
456         
457         
458         return( result );
459     }
460     
461       
462     
463         public void
464     checkStatsClassSuppliesAllStatistics( final ObjectName JavaDoc objectName )
465         throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc
466     {
467         //trace( "testStatsClassSuppliesAllStatistics: " + objectName);
468

469         try
470         {
471         final MonitoringStats mon = getProxyFactory().getProxy( objectName, MonitoringStats.class);
472         
473         final Method JavaDoc m = getSpecificStatsGetterMethod( mon );
474         final Stats JavaDoc stats = (Stats JavaDoc)m.invoke( mon, (Object JavaDoc[])null );
475         final Method JavaDoc[] methodsViaIntrospection = J2EEUtil.getStatisticGetterMethodsUsingIntrospection( stats );
476         final Method JavaDoc[] methodsViaNames = J2EEUtil.getStatisticGetterMethodsUsingNames( stats );
477         
478         assert GSetUtil.newSet( methodsViaNames ).equals( GSetUtil.newSet( methodsViaIntrospection ) ) :
479             "Statistic names for " + quote( objectName ) +
480                 " obtained via Statistic names do not match those obtained via introspection: \n" +
481                 "via names:" + toString( methodsToNames(methodsViaNames) ) +
482                 "\nvia introspection: " + toString( methodsToNames(methodsViaIntrospection) );
483                 
484         final String JavaDoc[] namesFromMethods = methodsToNames( methodsViaNames );
485         
486         assert GSetUtil.newSet( namesFromMethods ).equals( GSetUtil.newSet( stats.getStatisticNames() ) ) :
487             "MBean " + quote( objectName ) + " Stats object of class " + stats.getClass().getName() +
488                 " has Statistic methods that don't match getStatisticNames() =>\n" +
489                 toString( namesFromMethods ) + " != " +
490                     toString( stats.getStatisticNames() );
491         }
492         catch( Exception JavaDoc e )
493         {
494             trace( "Caught exception for " + StringUtil.quote(JMXUtil.toString(objectName)) +
495                 " = " + e.getClass().getName() + ": " + StringUtil.quote(e.getMessage()) + "\n" +
496                 ExceptionUtil.getStackTrace( ExceptionUtil.getRootCause(e) ) );
497         }
498     }
499     
500     /**
501         Verify that the Stats class for each MonitoringStats supplies all Statistics
502         found in itself, and that this matches those advertised by MonitoringStats.
503      */

504         public void
505     testStatsClassSuppliesAllStatistics()
506         throws Exception JavaDoc
507     {
508         //trace( "testStatsClassSuppliesAllStatistics: ");
509
final long start = now();
510         
511         final Set JavaDoc<MonitoringStats> all = getAllMonitoringStats();
512         
513         testAll( Util.toObjectNames( all ), "checkStatsClassSuppliesAllStatistics" );
514         
515         printElapsed( "testStatsClassSuppliesAllStatistics", all.size(), start );
516     }
517 }
518
519
520
521
522
523
524
525
526
527
Popular Tags