KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > util > jmx > JMXTestBase


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.util.jmx;
24
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Collection JavaDoc;
28
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.MBeanServerConnection JavaDoc;
31 import javax.management.MBeanServerInvocationHandler JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.management.MalformedObjectNameException JavaDoc;
34 import javax.management.InstanceAlreadyExistsException JavaDoc;
35 import javax.management.NotCompliantMBeanException JavaDoc;
36 import javax.management.MBeanRegistrationException JavaDoc;
37
38 import com.sun.enterprise.management.PropertyKeys;
39
40 import com.sun.appserv.management.util.stringifier.SmartStringifier;
41 import com.sun.appserv.management.util.jmx.stringifier.AttributeStringifier;
42 import com.sun.appserv.management.util.misc.StringUtil;
43 import com.sun.appserv.management.util.misc.CollectionUtil;
44 import com.sun.appserv.management.util.jmx.JMXUtil;
45
46 /**
47     Base class for AMX unit tests.
48  */

49 public class JMXTestBase extends junit.framework.TestCase
50 {
51     private static MBeanServerConnection JavaDoc sConn;
52     private static Map JavaDoc<String JavaDoc,Object JavaDoc> sEnv;
53     protected final String JavaDoc NEWLINE;
54     
55         public static void
56     setGlobalConnection( final MBeanServerConnection JavaDoc conn )
57     {
58         sConn = conn;
59     }
60     
61         protected <T> T
62     newProxy(
63         final ObjectName JavaDoc target,
64         final Class JavaDoc<T> interfaceClass )
65     {
66         try
67         {
68             assert getMBeanServerConnection().isRegistered( target );
69         }
70         catch( java.io.IOException JavaDoc e )
71         {
72             throw new RuntimeException JavaDoc( e );
73         }
74         
75         return interfaceClass.cast( MBeanServerInvocationHandler.newProxyInstance(
76                     getMBeanServerConnection(), target, interfaceClass, true ) );
77     }
78     
79         public static MBeanServerConnection JavaDoc
80     getGlobalConnection( )
81     {
82         return( getMBeanServerConnection() );
83     }
84     
85         public static MBeanServerConnection JavaDoc
86     getMBeanServerConnection( )
87     {
88         return( sConn );
89     }
90
91         public static synchronized Object JavaDoc
92     getEnvValue( final String JavaDoc key )
93     {
94         return( sEnv == null ? null : sEnv.get( key ) );
95     }
96     
97         public static Integer JavaDoc
98     getEnvInteger( final String JavaDoc key, Integer JavaDoc defaultValue )
99     {
100         final String JavaDoc s = getEnvString( key, null);
101         Integer JavaDoc result = defaultValue;
102         if ( s != null )
103         {
104             result = new Integer JavaDoc( s.trim() );
105         }
106
107         return( result );
108     }
109     
110         public static String JavaDoc
111     getEnvString( final String JavaDoc key, final String JavaDoc defaultValue )
112     {
113         final String JavaDoc s = (String JavaDoc)getEnvValue( key );
114
115         return( s == null ? defaultValue : s );
116     }
117     
118     
119         public static Boolean JavaDoc
120     getEnvBoolean( final String JavaDoc key, final Boolean JavaDoc defaultValue )
121     {
122         Boolean JavaDoc result = defaultValue;
123         final String JavaDoc s = getEnvString( key, null );
124         if ( s != null )
125         {
126             result = Boolean.valueOf( s );
127         }
128
129         return( result );
130     }
131
132
133         private static synchronized void
134     initEnv()
135     {
136         if ( sEnv == null )
137         {
138             sEnv = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
139         }
140     }
141     
142         public static synchronized void
143     setEnvValue(
144         final String JavaDoc key,
145         final Object JavaDoc value )
146     {
147         initEnv();
148         sEnv.put( key, value );
149     }
150     
151         public static synchronized void
152     setEnvValues( final Map JavaDoc<String JavaDoc,Object JavaDoc> m)
153     {
154         initEnv();
155         sEnv.putAll( m );
156     }
157     
158     
159         public
160     JMXTestBase()
161     {
162         super( "JMXTestBase" );
163         
164         NEWLINE = StringUtil.NEWLINE();
165         
166         checkAssertsOn();
167     }
168
169         public
170     JMXTestBase( String JavaDoc name )
171     {
172         super( name );
173         NEWLINE = StringUtil.NEWLINE();
174         checkAssertsOn();
175     }
176     
177             
178
179         protected String JavaDoc
180     toString( final ObjectName JavaDoc objectName )
181     {
182         return JMXUtil.toString( objectName );
183     }
184     
185         protected String JavaDoc
186     toString( final Object JavaDoc o )
187     {
188         String JavaDoc result = null;
189         
190         if ( o instanceof Collection JavaDoc )
191         {
192             result = CollectionUtil.toString( (Collection JavaDoc)o, "\n" );
193         }
194         else
195         {
196             result = SmartStringifier.toString( o );
197         }
198         
199         return( result );
200     }
201     
202     
203         protected static void
204     trace( final Object JavaDoc o )
205     {
206         System.out.println( SmartStringifier.toString( o ) );
207     }
208     
209         protected void
210     println( final Object JavaDoc o )
211     {
212         System.out.println( SmartStringifier.toString( o ) );
213     }
214     
215             protected long
216     now()
217     {
218         return( System.currentTimeMillis() );
219     }
220     
221         protected final void
222     printElapsed( final String JavaDoc msg, final long start )
223     {
224         printVerbose( msg + ": " + (now() - start) + "ms" );
225     }
226     
227         protected final void
228     printElapsedIter( final String JavaDoc msg, final long start, final long iterations)
229     {
230         printVerbose( msg + "(" + iterations + " iterations): " + (now() - start) + "ms" );
231     }
232     
233         protected final void
234     printElapsed(
235         final String JavaDoc msg,
236         final int numItems,
237         final long start )
238     {
239         printVerbose( msg + ", " + numItems + " MBeans: " + (now() - start) + "ms" );
240     }
241
242
243     
244
245
246         protected final String JavaDoc
247     quote( final Object JavaDoc o )
248     {
249         return( StringUtil.quote( SmartStringifier.toString( o ) ) );
250     }
251
252
253
254
255         protected boolean
256     getVerbose()
257     {
258         final String JavaDoc value = (String JavaDoc)getEnvValue( PropertyKeys.VERBOSE_KEY );
259         
260         return( value != null && Boolean.valueOf( value ).booleanValue() );
261     }
262     
263         protected void
264     printVerbose( final Object JavaDoc o )
265     {
266         if ( getVerbose() )
267         {
268             trace( o );
269         }
270     }
271
272
273         protected void
274     warning( final String JavaDoc msg )
275     {
276         trace( "\nWARNING: " + msg + "\n" );
277     }
278     
279         protected void
280     failure( final String JavaDoc msg )
281     {
282         trace( "\nFAILURE: " + msg + "\n" );
283         assert( false ) : msg;
284         throw new Error JavaDoc( msg );
285     }
286
287         protected void
288     checkAssertsOn()
289     {
290         try
291         {
292             assert( false );
293             throw new Error JavaDoc( "Assertions must be enabled for unit tests" );
294         }
295         catch( AssertionError JavaDoc a )
296         {
297         }
298     }
299     
300     
301         protected void
302     registerMBean( Object JavaDoc mbean, String JavaDoc name )
303         throws MalformedObjectNameException JavaDoc, InstanceAlreadyExistsException JavaDoc,
304         NotCompliantMBeanException JavaDoc, MBeanRegistrationException JavaDoc
305     {
306         if ( sConn instanceof MBeanServer JavaDoc )
307         {
308             ((MBeanServer JavaDoc)sConn).registerMBean( mbean, new ObjectName JavaDoc( name ) );
309         }
310         else
311         {
312             throw new IllegalArgumentException JavaDoc( "test connection is not an MBeanServer" );
313         }
314     }
315     
316         public void
317     setUp() throws Exception JavaDoc
318     {
319         checkAssertsOn();
320         assert( sConn != null );
321     }
322     
323         public void
324     tearDown()
325         throws Exception JavaDoc
326     {
327         // do NOT destroy the MBeanServer or its contents
328
}
329
330 };
331
332
Popular Tags