KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > config > ComSunAppservConfigTest


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.config;
24
25 import javax.management.ObjectName JavaDoc;
26
27 import java.util.Collection JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.HashMap JavaDoc;
34
35 import java.io.IOException JavaDoc;
36
37 import javax.management.*;
38
39 import com.sun.appserv.management.util.j2ee.J2EEUtil;
40 import com.sun.appserv.management.util.misc.CollectionUtil;
41 import com.sun.appserv.management.util.misc.ExceptionUtil;
42 import com.sun.appserv.management.util.misc.GSetUtil;
43 import com.sun.appserv.management.util.misc.StringUtil;
44 import com.sun.appserv.management.util.misc.TypeCast;
45 import com.sun.appserv.management.util.stringifier.ArrayStringifier;
46 import com.sun.appserv.management.util.jmx.JMXUtil;
47
48 import com.sun.appserv.management.base.Util;
49
50 import com.sun.enterprise.management.support.ComSunAppservTest;
51
52 /**
53     Unit test for the com.sun.appserv monitoring MBeans relied
54     upon by AMX.
55  */

56 public final class ComSunAppservConfigTest extends ComSunAppservTest
57 {
58         public
59     ComSunAppservConfigTest()
60     {
61     }
62
63
64         private boolean
65     checkConfig(final ObjectName JavaDoc objectName )
66         throws Exception JavaDoc
67     {
68         boolean worksOK = true;
69         
70         final MBeanServerConnection conn = getMBeanServerConnection();
71         
72         final MBeanInfo mbeanInfo = conn.getMBeanInfo( objectName );
73         assert( mbeanInfo != null );
74         final MBeanAttributeInfo[] attrInfos = mbeanInfo.getAttributes();
75         for( final MBeanAttributeInfo attrInfo : attrInfos )
76         {
77             try
78             {
79                 final Object JavaDoc value = conn.getAttribute( objectName, attrInfo.getName() );
80             }
81             catch( Exception JavaDoc e )
82             {
83                 final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( e );
84             
85                 warning( "MBean " + StringUtil.quote( objectName ) +
86                     " threw an exception trying to get Attribute " +
87                     StringUtil.quote( attrInfo.getName() ) + ": " + rootCause );
88                 worksOK = false;
89             }
90         }
91         
92         final String JavaDoc[] attrNames = JMXUtil.getAttributeNames( attrInfos );
93         try
94         {
95            // System.out.println( objectName + " {" + ArrayStringifier.stringify( attrNames, ", " ) + "}");
96
final AttributeList attrs = conn.getAttributes( objectName, attrNames );
97             assert( attrs.size() == attrInfos.length );
98             
99             // verify that Attribute names match what was returned
100
final Set JavaDoc<String JavaDoc> attrNamesSet = GSetUtil.newSet( attrNames );
101             assert( attrNamesSet.size() == attrNames.length ); // should all be unique
102
final List JavaDoc<Attribute> typed = TypeCast.asList( attrs );
103             for( final Attribute attr : typed )
104             {
105                 assert( attrNamesSet.contains( attr.getName() ) );
106             }
107         }
108         catch( Exception JavaDoc e )
109         {
110             final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( e );
111                 
112             warning( "MBean " + StringUtil.quote( objectName ) +
113                 " threw an exception trying to get getAttributes(" +
114                 ArrayStringifier.stringify( attrNames, ", " ) + "): " + rootCause );
115             worksOK = false;
116         }
117         
118         return( worksOK );
119     }
120     
121     private static final Set JavaDoc<String JavaDoc> COM_SUN_APPSERV_EXCLUDED_TYPES =
122         Collections.unmodifiableSet( GSetUtil.newSet( new String JavaDoc[]
123         {
124             "servers",
125             "applications",
126             "configs",
127             "resources",
128             "transactions-recovery",
129             "synchronization",
130         } ));
131         
132     
133     
134         public void
135     testAllConfig()
136         throws Exception JavaDoc
137     {
138         final Map JavaDoc<String JavaDoc,ObjectName JavaDoc> m = getAllComSunAppservConfig();
139         
140         final Collection JavaDoc<ObjectName JavaDoc> objectNames = m.values();
141         final Set JavaDoc<ObjectName JavaDoc> defective = new HashSet JavaDoc<ObjectName JavaDoc>();
142         int testedCount = 0;
143         for( final ObjectName JavaDoc objectName : objectNames )
144         {
145             final String JavaDoc type = objectName.getKeyProperty( "type" );
146             if ( COM_SUN_APPSERV_EXCLUDED_TYPES.contains( type ) )
147             {
148                 continue;
149             }
150             
151             ++testedCount;
152             if ( ! checkConfig( objectName ) )
153             {
154                 defective.add( objectName );
155             }
156         }
157         
158         printVerbose( "ComSunAppservConfigTest.testAllConfig: checked " +
159             testedCount + " com.sun.appserv:category=config MBeans for basic functionality, " +
160             defective.size() + " failures." );
161         
162         if ( defective.size() != 0 )
163         {
164             failure( "The following MBeans are defective:\n" +
165                 CollectionUtil.toString( defective, "\n") );
166         }
167     }
168     
169   
170     
171 }
172
173
174
175
176
177
178
Popular Tags