KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > client > ProxyTest


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 /*
25  * $Header: /cvs/glassfish/admin/mbeanapi-impl/tests/com/sun/enterprise/management/client/ProxyTest.java,v 1.5 2006/03/09 20:30:52 llc Exp $
26  * $Revision: 1.5 $
27  * $Date: 2006/03/09 20:30:52 $
28  */

29 package com.sun.enterprise.management.client;
30
31 import java.util.Set JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.io.IOException JavaDoc;
37
38 import java.lang.reflect.Method JavaDoc;
39
40 import javax.management.ObjectName JavaDoc;
41 import javax.management.AttributeList JavaDoc;
42 import javax.management.MBeanServerConnection JavaDoc;
43 import javax.management.NotCompliantMBeanException JavaDoc;
44 import javax.management.AttributeNotFoundException JavaDoc;
45 import javax.management.InstanceNotFoundException JavaDoc;
46
47 import com.sun.appserv.management.j2ee.J2EEDomain;
48
49 import com.sun.appserv.management.client.ProxyFactory;
50 import com.sun.appserv.management.base.Container;
51 import com.sun.appserv.management.config.Description;
52 import com.sun.appserv.management.base.XTypes;
53 import com.sun.appserv.management.DomainRoot;
54 import com.sun.appserv.management.base.QueryMgr;
55 import com.sun.appserv.management.base.AMX;
56 import com.sun.appserv.management.base.Util;
57 import com.sun.appserv.management.config.ResourceRefConfig;
58 import com.sun.appserv.management.config.DeployedItemRefConfig;
59 import com.sun.appserv.management.config.ResourceConfig;
60 import com.sun.enterprise.management.support.TypeInfos;
61 import com.sun.enterprise.management.support.TypeInfo;
62
63 import com.sun.appserv.management.util.jmx.JMXUtil;
64 import com.sun.appserv.management.util.misc.ExceptionUtil;
65 import com.sun.appserv.management.util.misc.StringUtil;
66 import com.sun.appserv.management.util.misc.ClassUtil;
67 import com.sun.appserv.management.util.stringifier.CollectionStringifier;
68
69
70
71 import com.sun.enterprise.management.AMXTestBase;
72 import com.sun.enterprise.management.Capabilities;
73
74 /**
75  */

76 public final class ProxyTest extends AMXTestBase
77 {
78         public
79     ProxyTest( )
80     {
81     }
82         public static Capabilities
83     getCapabilities()
84     {
85         return getOfflineCapableCapabilities( false );
86     }
87     
88
89         public void
90     checkCreateProxy( final ObjectName JavaDoc src )
91         throws Exception JavaDoc
92     {
93         final AMX proxy = getProxyFactory().getProxy( src, AMX.class);
94         
95         Util.getObjectName( proxy );
96         proxy.getContainer();
97         proxy.getDomainRoot();
98     }
99     
100         public void
101     testCreateAllProxies()
102         throws Exception JavaDoc
103     {
104         testAll( "checkCreateProxy" );
105     }
106     
107         public void
108     checkProxiesCached( final ObjectName JavaDoc src )
109         throws Exception JavaDoc
110     {
111         final AMX proxy = getProxyFactory().getProxy( src, AMX.class);
112         
113         assert( proxy == getProxyFactory().getProxy( src, AMX.class) );
114         assert( proxy.getContainer() == proxy.getContainer() );
115         assert( proxy.getDomainRoot() == proxy.getDomainRoot() );
116     
117         final Class JavaDoc interfaceClass = getInterfaceClass( proxy );
118         final Method JavaDoc[] proxyMethods = interfaceClass.getMethods();
119         
120         for( int methodIdx = 0; methodIdx < proxyMethods.length; ++methodIdx )
121         {
122             final Method JavaDoc method = proxyMethods[ methodIdx ];
123             final String JavaDoc methodName = method.getName();
124             
125             if ( isProxyGetter( method ) )
126             {
127                 // invoke it twice, and verify that the 2nd call results in the same proxy
128
//trace( "Invoking: " + method );
129
method.invoke( proxy, (Object JavaDoc[])null );
130             }
131         }
132     }
133
134         public void
135     testProxiesCached()
136         throws Exception JavaDoc
137     {
138         testAll( "checkProxiesCached" );
139     }
140     
141     
142     
143     
144         private boolean
145     isProxyGetter( final Method JavaDoc method )
146     {
147         return( method.getName().startsWith( JMXUtil.GET ) &&
148                 method.getParameterTypes().length == 0 &&
149                     AMX.class.isAssignableFrom( method.getReturnType() ) );
150     }
151     
152         private boolean
153     isChildProxyGetter( final Method JavaDoc method )
154     {
155         final Class JavaDoc[] paramTypes = method.getParameterTypes();
156         
157         return( paramTypes.length == 1 &&
158                     paramTypes[ 0 ] == String JavaDoc.class &&
159                     AMX.class.isAssignableFrom( method.getReturnType() ) );
160     }
161     
162         private boolean
163     isProxiesGetter( final Method JavaDoc method )
164     {
165         return( method.getParameterTypes().length == 0 &&
166                     Set JavaDoc.class.isAssignableFrom( method.getReturnType() ) );
167     }
168     
169     
170         private String JavaDoc
171     getProxyGetterName( final String JavaDoc getterName )
172     {
173         final int baseLength = getterName.length() - "ObjectName".length();
174         final String JavaDoc baseName = getterName.substring( 0, baseLength );
175         
176         return( baseName + "Proxy" );
177     }
178     
179     
180         public void
181     testProxyInterfaceIsAMX()
182         throws Exception JavaDoc
183     {
184         final long start = now();
185         final TypeInfos infos = TypeInfos.getInstance();
186         
187         final Iterator JavaDoc iter = infos.getJ2EETypes().iterator();
188         while ( iter.hasNext() )
189         {
190             final TypeInfo info = infos.getInfo( (String JavaDoc)iter.next() );
191             final Class JavaDoc proxyClass = info.getInterface();
192             
193             if ( ! AMX.class.isAssignableFrom( proxyClass ) )
194             {
195                 warning( "Proxy interface does not extend AMX: " + proxyClass.getName() );
196             }
197         }
198         printElapsed( "testProxyInterfaceNameConsistent", start );
199     }
200
201         public void
202     testProxyInterfaceNameConsistent()
203         throws Exception JavaDoc
204     {
205         final long start = now();
206         
207         final TypeInfos infos = TypeInfos.getInstance();
208         
209         final Iterator JavaDoc iter = infos.getJ2EETypes().iterator();
210         while ( iter.hasNext() )
211         {
212             final TypeInfo info = infos.getInfo( (String JavaDoc)iter.next() );
213             
214             final Class JavaDoc proxyClass = info.getInterface();
215             
216             
217             if ( proxyClass.getName().endsWith( "ResourceConfigProxy" ) )
218             {
219                 if ( proxyClass.getName().endsWith( "ConnectorConnectionPoolConfigProxy" ) )
220                 {
221                     // ConnectorConnectionPoolConfig does not extend ResourceConfig,
222
}
223                 else if ( ! ResourceConfig.class.isAssignableFrom( proxyClass ) )
224                 {
225                     warning( "Proxy interface does not extend ResourceConfigProxy: " +
226                         proxyClass.getName() );
227                 }
228             }
229             
230             if ( proxyClass.getName().endsWith( "ResourceRefConfigProxy" ) )
231             {
232                 if ( ! ResourceRefConfig.class.isAssignableFrom( proxyClass ) )
233                 {
234                     warning( "Proxy interface does not extend ResourceRefConfigProxy: " +
235                         proxyClass.getName() );
236                 }
237             }
238             
239             if ( proxyClass.getName().endsWith( "DeployedItemRefConfig" ) )
240             {
241                 if ( ! DeployedItemRefConfig.class.isAssignableFrom( proxyClass ) )
242                 {
243                     trace( "Proxy interface does not extend DeployedItemRefConfig: " +
244                         proxyClass.getName() );
245                 }
246             }
247         }
248         printElapsed( "testProxyInterfaceNameConsistent", start );
249     }
250
251     /**
252         Verify that every getXXX() method can be called (those without parameters).
253      */

254         public void
255     testProxyGetters( final AMX proxy )
256         throws ClassNotFoundException JavaDoc
257     {
258         final Method JavaDoc[] methods = getInterfaceClass( proxy ).getMethods();
259         
260         final List JavaDoc<Method JavaDoc> failedMethods = new ArrayList JavaDoc<Method JavaDoc>();
261         final List JavaDoc<Throwable JavaDoc> exceptions = new ArrayList JavaDoc<Throwable JavaDoc>();
262     
263         final long start = now();
264         
265         for( int methodIdx = 0; methodIdx < methods.length; ++methodIdx )
266         {
267             final Method JavaDoc method = methods[ methodIdx ];
268             final String JavaDoc methodName = method.getName();
269             final Class JavaDoc[] parameterTypes = method.getParameterTypes();
270             
271             if ( methodName.startsWith( JMXUtil.GET ) && parameterTypes.length == 0 )
272             {
273                 try
274                 {
275                     final Object JavaDoc result = method.invoke( proxy, (Object JavaDoc[])null );
276                     //trace( methodName + "=" + result);
277
}
278                 catch( Throwable JavaDoc t )
279                 {
280                     final ObjectName JavaDoc objectName = Util.getObjectName( proxy );
281                     if ( isRemoteIncomplete( objectName ) )
282                     {
283                         trace( "remoteIncomplete: " + objectName );
284                     }
285                     else
286                     {
287                         trace( "failure: " + methodName + " = " + t.getClass().getName() );
288                         failedMethods.add( method );
289                         exceptions.add( t );
290                     }
291                 }
292             }
293         }
294         final long elapsed = now() - start;
295         //printVerbose( "testProxyGetters for: " + Util.getObjectName( proxy ) + " = " + elapsed );
296

297         if ( failedMethods.size() != 0 )
298         {
299             final int numFailed = failedMethods.size();
300             
301             trace( "\nMBean \"" + Util.getObjectName( proxy ) + "\" failed for:" );
302             for( int i = 0; i < numFailed; ++i )
303             {
304                 final Method JavaDoc m = (Method JavaDoc)failedMethods.get( i );
305                 final Throwable JavaDoc t = (Throwable JavaDoc)exceptions.get( i );
306                 
307                 final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( t );
308                 final String JavaDoc rootTrace = ExceptionUtil.getStackTrace( rootCause );
309                 final Class JavaDoc rootCauseClass = rootCause.getClass();
310                 
311                 trace( "testProxyGetters: failure from: " + m.getName() + ": " + rootCauseClass.getName() );
312                 if ( rootCauseClass != AttributeNotFoundException JavaDoc.class )
313                 {
314                     trace( rootTrace + "\n" );
315                 }
316             }
317         }
318     }
319     
320         public void
321     testAllGetters()
322         throws Exception JavaDoc
323     {
324         final long start = now();
325         
326         final Set JavaDoc<AMX> proxies = getAllAMX();
327         for( final AMX amx : proxies )
328         {
329             testProxyGetters( amx );
330         }
331         
332         printElapsed( "testAllGetters", start );
333     }
334     
335     
336         public void
337     testQueryMgr()
338         throws Exception JavaDoc
339     {
340         final QueryMgr proxy = (QueryMgr)getQueryMgr();
341         Util.getObjectName( proxy );
342         proxy.getContainer();
343         proxy.getDomainRoot();
344     }
345     
346         public void
347     testDomainRootCachedProxies()
348         throws Exception JavaDoc
349     {
350         final DomainRoot root = (DomainRoot)getDomainRoot();
351         
352         assert( root.getJ2EEDomain() == root.getJ2EEDomain() );
353         assert( root.getDomainConfig() == root.getDomainConfig() );
354         assert( root.getQueryMgr() == root.getQueryMgr() );
355         assert( root.getBulkAccess() == root.getBulkAccess() );
356         assert( root.getUploadDownloadMgr() == root.getUploadDownloadMgr() );
357         assert( root.getConfigDottedNames() == root.getConfigDottedNames() );
358         assert( root.getMonitoringDottedNames() == root.getMonitoringDottedNames() );
359         
360         assert( root.getJ2EEDomain() == root.getJ2EEDomain() );
361     }
362     
363     /**
364         This test is designed to check that performance is reasonable and/or
365         to detect a change that slows things down drastically.
366         public void
367      */

368         public void
369     testProxyTime()
370         throws Exception JavaDoc
371     {
372         final DomainRoot root = (DomainRoot)getDomainRoot();
373         
374         final long start = now();
375         for( int i = 0; i < 5; ++i )
376         {
377             root.getContainer();
378             root.getDomainRoot();
379             
380             root.getJ2EEDomain();
381             root.getDomainConfig();
382             root.getQueryMgr();
383             root.getBulkAccess();
384             root.getUploadDownloadMgr();
385             root.getConfigDottedNames();
386             root.getMonitoringDottedNames();
387             root.getDeploymentMgr();
388         }
389         final long elapsed = now() - start;
390         
391         // should be < 300 ms, so this is a 10X margin...
392
assert( elapsed < 300 * 10 );
393     }
394 }
395
396
397
398
399
400
Popular Tags