KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > base > QueryMgrTest


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.base;
24
25 import java.util.Set JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import java.io.IOException JavaDoc;
30
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.MBeanServerConnection JavaDoc;
33 import javax.management.MBeanException JavaDoc;
34 import javax.management.AttributeNotFoundException JavaDoc;
35 import javax.management.InstanceNotFoundException JavaDoc;
36 import javax.management.JMException JavaDoc;
37
38
39 import com.sun.appserv.management.DomainRoot;
40 import com.sun.appserv.management.base.AMX;
41 import com.sun.appserv.management.base.AMXAttributes;
42 import com.sun.appserv.management.base.QueryMgr;
43 import com.sun.appserv.management.base.Util;
44 import com.sun.appserv.management.base.XTypes;
45
46 import com.sun.appserv.management.util.jmx.JMXUtil;
47
48 import com.sun.appserv.management.util.misc.GSetUtil;
49 import com.sun.appserv.management.util.misc.CollectionUtil;
50
51 import com.sun.appserv.management.util.stringifier.SmartStringifier;
52
53
54
55 import com.sun.enterprise.management.AMXTestBase;
56 import com.sun.enterprise.management.Capabilities;
57
58 /**
59  */

60 public final class QueryMgrTest extends AMXTestBase
61 {
62     final QueryMgr mQM;
63     
64         public
65     QueryMgrTest( )
66     {
67         mQM = getQueryMgr();
68     }
69         public static Capabilities
70     getCapabilities()
71     {
72         return getOfflineCapableCapabilities( true );
73     }
74     
75
76         private <T> void
77     checkSetsEqual(
78         final String JavaDoc msg,
79         final Set JavaDoc<T> set1,
80         final Set JavaDoc<T> set2 )
81     {
82         if ( ! set1.equals( set2 ) )
83         {
84             final Set JavaDoc<T> conflict = GSetUtil.newNotCommonSet( set1, set2 );
85             final String JavaDoc values = CollectionUtil.toString( conflict, "\n" );
86             failure( msg + ", mismatch =\n" + values );
87         }
88     }
89     
90     
91         private void
92     checkAMXWithObjectNames(
93         final String JavaDoc msg,
94         final Set JavaDoc<AMX> set1,
95         final Set JavaDoc<ObjectName JavaDoc> set2 )
96     {
97         if ( set1.size() != set2.size() )
98         {
99             failure( msg + "set sizes don't match: " + set1.size() + " != " + set2.size() );
100         }
101         
102         final Set JavaDoc<ObjectName JavaDoc> set1ObjectNames = new HashSet JavaDoc<ObjectName JavaDoc>();
103         for( final AMX item : set1 )
104         {
105             final ObjectName JavaDoc objectName = Util.getObjectName( item );
106             
107             if ( set1ObjectNames.contains( objectName ) )
108             {
109                 failure( "set1 contains the same proxy twice with ObjectName: " + objectName );
110             }
111             set1ObjectNames.add( objectName );
112         }
113         
114         checkSetsEqual( msg, set1ObjectNames, set2 );
115     }
116     
117     
118         public void
119     testQueryJ2EETypePvsON()
120         throws ClassNotFoundException JavaDoc
121     {
122         final long start = now();
123         
124         final String JavaDoc testType1 = XTypes.DOMAIN_CONFIG;
125         checkAMXWithObjectNames(
126                 "queryJ2EETypeSet(XTypes.DOMAIN_CONFIG) != queryJ2EETypeObjectNameSet(XTypes.DOMAIN_CONFIG)",
127                 mQM.queryJ2EETypeSet( testType1 ),
128                 mQM.queryJ2EETypeObjectNameSet( testType1 ) );
129         printElapsed( "testObjectNameVersusProxy", start );
130     }
131     
132         public void
133     testQueryJ2EENamePvsON()
134         throws ClassNotFoundException JavaDoc
135     {
136         final long start = now();
137
138         final String JavaDoc name = getDomainRoot().getName();
139         checkAMXWithObjectNames(
140             "queryJ2EENameSet() != queryJ2EENameObjectNameSet()",
141             mQM.queryJ2EENameSet( name ),
142             mQM.queryJ2EENameObjectNameSet( name ) );
143         printElapsed( "testObjectNameVersusProxy", start );
144     }
145     
146         public void
147     testQueryPatternPvsON()
148         throws ClassNotFoundException JavaDoc
149     {
150         final long start = now();
151         
152         final String JavaDoc domain = Util.getObjectName( mQM ).getDomain();
153         final ObjectName JavaDoc pat = JMXUtil.newObjectNamePattern( domain, "*" );
154         checkAMXWithObjectNames(
155             "queryPatternSet() != queryPatternObjectNameSet()",
156             mQM.queryPatternSet( pat ),
157             mQM.queryPatternObjectNameSet( pat ) );
158                     
159         checkAMXWithObjectNames(
160             "queryPatternSet() != queryPatternObjectNameSet()",
161             mQM.queryPatternSet( domain, "*" ),
162             mQM.queryPatternObjectNameSet( domain, "*" ) );
163         printElapsed( "testObjectNameVersusProxy", start );
164     }
165     
166         public void
167     testQueryPropsPvsON() throws ClassNotFoundException JavaDoc
168     {
169         final long start = now();
170
171         final String JavaDoc domain = Util.getObjectName( mQM ).getDomain();
172         final String JavaDoc props = Util.makeRequiredProps( XTypes.DOMAIN_ROOT, domain );
173         checkAMXWithObjectNames(
174             "queryPropsSet() != queryPropsObjectNameSet()",
175             mQM.queryPropsSet( props ),
176             mQM.queryPropsObjectNameSet( props ) );
177         printElapsed( "testObjectNameVersusProxy", start );
178     }
179     
180         public void
181     testQueryWildPvsON()
182     {
183         final long start = now();
184         
185         final String JavaDoc[] wildNames = new String JavaDoc[] { "*" };
186         final String JavaDoc[] wildValues = new String JavaDoc[] { "*" };
187         checkAMXWithObjectNames(
188             "queryWildSet() != queryWildObjectNameSet()",
189             mQM.queryWildSet( wildNames, wildValues ),
190             mQM.queryWildObjectNameSet( wildNames, wildValues ) );
191         printElapsed( "testObjectNameVersusProxy", start );
192     }
193     
194         public void
195     testQueryInterfacePvsON()
196         throws ClassNotFoundException JavaDoc
197     {
198         final long start = now();
199
200         final Set JavaDoc<AMX> candidates = mQM.queryAllSet();
201         final Set JavaDoc<ObjectName JavaDoc> candidateObjectNames = Util.toObjectNames( candidates );
202         final String JavaDoc interfaceName = DomainRoot.class.getName();
203         checkAMXWithObjectNames(
204             "queryInterfaceSet() != queryInterfaceObjectNameSet()",
205             mQM.queryInterfaceSet( interfaceName, candidateObjectNames ),
206             mQM.queryInterfaceObjectNameSet( interfaceName, candidateObjectNames ) );
207         printElapsed( "testObjectNameVersusProxy", start );
208     }
209     
210         public void
211     testQueryAll_AMXMatchesObjectName()
212         throws ClassNotFoundException JavaDoc
213     {
214         final long start = now();
215
216         final Set JavaDoc<AMX> allSet = mQM.queryAllSet();
217         final Set JavaDoc<ObjectName JavaDoc> allObjectNameSet = mQM.queryAllObjectNameSet();
218         
219         assert( allSet.size() == allObjectNameSet.size() );
220         
221         checkAMXWithObjectNames(
222             "queryAllSet() != queryAllObjectNameSet()",
223             allSet, allObjectNameSet );
224         
225         printElapsed( "testObjectNameVersusProxy", start );
226     }
227     
228
229         public void
230     testGetQueryMgr()
231     {
232         assert( getQueryMgr() != null );
233     }
234     
235         public void
236     testQueryAll()
237     {
238         final long start = now();
239         final Set JavaDoc result = getQueryMgr().queryAllSet();
240         
241         assert( result.size() > 1 );
242         
243         printElapsed( "testQueryAll", start );
244     }
245     
246         public void
247     testGetJ2EETypeObjectNames()
248         throws Exception JavaDoc
249     {
250         final long start = now();
251         
252         final Set JavaDoc<String JavaDoc> j2eeTypes = getTestUtil().getAvailJ2EETypes();
253         for( final String JavaDoc j2eeType : j2eeTypes )
254         {
255             final Set JavaDoc<AMX> results =
256                 getQueryMgr().queryJ2EETypeSet(j2eeType );
257             assert( results.size() >= 1 );
258         }
259         
260         printElapsed( "testGetJ2EETypeObjectNames", start );
261     }
262     
263         public void
264     testGetJ2EETypeProxies()
265         throws Exception JavaDoc
266     {
267         final long start = now();
268         final Set JavaDoc<String JavaDoc> j2eeTypes = getTestUtil().getAvailJ2EETypes();
269
270         for( final String JavaDoc j2eeType : j2eeTypes )
271         {
272             final Set JavaDoc<AMX> proxies =
273                 getQueryMgr().queryJ2EETypeSet( j2eeType );
274                 
275             assert( proxies != null );
276         }
277         
278         printElapsed( "testGetJ2EETypeProxies", start );
279     }
280     
281         public void
282     testQueryPatternProxies()
283         throws Exception JavaDoc
284     {
285         final long start = now();
286         
287         final Set JavaDoc proxies = getQueryMgr().queryPropsSet( "*" );
288
289         assert proxies != null;
290         
291         printElapsed( "testQueryPatternProxies", start );
292     }
293     
294         private <T extends AMX> Set JavaDoc<String JavaDoc>
295     getAllInterfaceNames( final Set JavaDoc<T> amxSet )
296     {
297         final Set JavaDoc<String JavaDoc> interfaceNames = new HashSet JavaDoc<String JavaDoc>();
298         for( final T amx : amxSet )
299         {
300             interfaceNames.add( Util.getExtra( amx ).getInterfaceName() );
301         }
302         
303         return( interfaceNames );
304     }
305     
306     /**
307         Final all available interfaces, then for each type of interface, query
308         for all items that have the interface and verify that's the interface
309         they return from getInterfaceName().
310      */

311         public void
312     testQueryInterfaceObjectNames( )
313         throws Exception JavaDoc
314     {
315         final long start = now();
316         
317         final QueryMgr queryMgr = getQueryMgr();
318         final Set JavaDoc<AMX> allAMX = getQueryMgr().queryAllSet();
319         final Set JavaDoc<ObjectName JavaDoc> allAMXObjectNames = Util.toObjectNames( allAMX );
320         final Set JavaDoc<String JavaDoc> interfaceNames = getAllInterfaceNames( allAMX );
321         
322         for( final String JavaDoc interfaceName : interfaceNames )
323         {
324             final Set JavaDoc amxs = queryMgr.queryInterfaceSet( interfaceName, allAMXObjectNames );
325
326             final Iterator JavaDoc amxIter = amxs.iterator();
327             while ( amxIter.hasNext() )
328             {
329                 final AMX amx = Util.asAMX(amxIter.next());
330                 assert( interfaceName.equals( Util.getExtra( amx ).getInterfaceName() ) );
331             }
332         }
333         
334         printElapsed( "testQueryInterfaceObjectNames", start );
335     }
336     
337     
338 }
339
340
341
342
343
344
345
Popular Tags