1 23 package com.sun.enterprise.management.util.jmx; 24 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.Collection ; 28 29 import javax.management.MBeanServer ; 30 import javax.management.MBeanServerConnection ; 31 import javax.management.MBeanServerInvocationHandler ; 32 import javax.management.ObjectName ; 33 import javax.management.MalformedObjectNameException ; 34 import javax.management.InstanceAlreadyExistsException ; 35 import javax.management.NotCompliantMBeanException ; 36 import javax.management.MBeanRegistrationException ; 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 49 public class JMXTestBase extends junit.framework.TestCase 50 { 51 private static MBeanServerConnection sConn; 52 private static Map <String ,Object > sEnv; 53 protected final String NEWLINE; 54 55 public static void 56 setGlobalConnection( final MBeanServerConnection conn ) 57 { 58 sConn = conn; 59 } 60 61 protected <T> T 62 newProxy( 63 final ObjectName target, 64 final Class <T> interfaceClass ) 65 { 66 try 67 { 68 assert getMBeanServerConnection().isRegistered( target ); 69 } 70 catch( java.io.IOException e ) 71 { 72 throw new RuntimeException ( e ); 73 } 74 75 return interfaceClass.cast( MBeanServerInvocationHandler.newProxyInstance( 76 getMBeanServerConnection(), target, interfaceClass, true ) ); 77 } 78 79 public static MBeanServerConnection 80 getGlobalConnection( ) 81 { 82 return( getMBeanServerConnection() ); 83 } 84 85 public static MBeanServerConnection 86 getMBeanServerConnection( ) 87 { 88 return( sConn ); 89 } 90 91 public static synchronized Object 92 getEnvValue( final String key ) 93 { 94 return( sEnv == null ? null : sEnv.get( key ) ); 95 } 96 97 public static Integer 98 getEnvInteger( final String key, Integer defaultValue ) 99 { 100 final String s = getEnvString( key, null); 101 Integer result = defaultValue; 102 if ( s != null ) 103 { 104 result = new Integer ( s.trim() ); 105 } 106 107 return( result ); 108 } 109 110 public static String 111 getEnvString( final String key, final String defaultValue ) 112 { 113 final String s = (String )getEnvValue( key ); 114 115 return( s == null ? defaultValue : s ); 116 } 117 118 119 public static Boolean 120 getEnvBoolean( final String key, final Boolean defaultValue ) 121 { 122 Boolean result = defaultValue; 123 final String 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 <String ,Object >(); 139 } 140 } 141 142 public static synchronized void 143 setEnvValue( 144 final String key, 145 final Object value ) 146 { 147 initEnv(); 148 sEnv.put( key, value ); 149 } 150 151 public static synchronized void 152 setEnvValues( final Map <String ,Object > 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 name ) 171 { 172 super( name ); 173 NEWLINE = StringUtil.NEWLINE(); 174 checkAssertsOn(); 175 } 176 177 178 179 protected String 180 toString( final ObjectName objectName ) 181 { 182 return JMXUtil.toString( objectName ); 183 } 184 185 protected String 186 toString( final Object o ) 187 { 188 String result = null; 189 190 if ( o instanceof Collection ) 191 { 192 result = CollectionUtil.toString( (Collection )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 o ) 205 { 206 System.out.println( SmartStringifier.toString( o ) ); 207 } 208 209 protected void 210 println( final Object 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 msg, final long start ) 223 { 224 printVerbose( msg + ": " + (now() - start) + "ms" ); 225 } 226 227 protected final void 228 printElapsedIter( final String 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 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 247 quote( final Object o ) 248 { 249 return( StringUtil.quote( SmartStringifier.toString( o ) ) ); 250 } 251 252 253 254 255 protected boolean 256 getVerbose() 257 { 258 final String value = (String )getEnvValue( PropertyKeys.VERBOSE_KEY ); 259 260 return( value != null && Boolean.valueOf( value ).booleanValue() ); 261 } 262 263 protected void 264 printVerbose( final Object o ) 265 { 266 if ( getVerbose() ) 267 { 268 trace( o ); 269 } 270 } 271 272 273 protected void 274 warning( final String msg ) 275 { 276 trace( "\nWARNING: " + msg + "\n" ); 277 } 278 279 protected void 280 failure( final String msg ) 281 { 282 trace( "\nFAILURE: " + msg + "\n" ); 283 assert( false ) : msg; 284 throw new Error ( msg ); 285 } 286 287 protected void 288 checkAssertsOn() 289 { 290 try 291 { 292 assert( false ); 293 throw new Error ( "Assertions must be enabled for unit tests" ); 294 } 295 catch( AssertionError a ) 296 { 297 } 298 } 299 300 301 protected void 302 registerMBean( Object mbean, String name ) 303 throws MalformedObjectNameException , InstanceAlreadyExistsException , 304 NotCompliantMBeanException , MBeanRegistrationException 305 { 306 if ( sConn instanceof MBeanServer ) 307 { 308 ((MBeanServer )sConn).registerMBean( mbean, new ObjectName ( name ) ); 309 } 310 else 311 { 312 throw new IllegalArgumentException ( "test connection is not an MBeanServer" ); 313 } 314 } 315 316 public void 317 setUp() throws Exception 318 { 319 checkAssertsOn(); 320 assert( sConn != null ); 321 } 322 323 public void 324 tearDown() 325 throws Exception 326 { 327 } 329 330 }; 331 332 | Popular Tags |