1 23 24 26 27 package com.sun.enterprise.management; 28 29 import java.io.IOException ; 30 import java.util.Set ; 31 import java.util.Map ; 32 import java.util.List ; 33 import java.util.ArrayList ; 34 import java.util.HashMap ; 35 import java.util.Arrays ; 36 import java.util.Collections ; 37 38 import javax.net.ssl.X509TrustManager; 39 import java.security.cert.Certificate ; 40 import java.security.cert.CertificateException ; 41 import java.security.KeyStoreException ; 42 import java.security.NoSuchAlgorithmException ; 43 44 import javax.management.MBeanServer ; 45 import javax.management.MBeanServerConnection ; 46 import javax.management.ObjectName ; 47 import javax.management.MBeanInfo ; 48 import javax.management.JMException ; 49 50 import junit.extensions.ActiveTestSuite; 51 import junit.framework.TestSuite; 52 53 import com.sun.appserv.management.DomainRoot; 54 import com.sun.appserv.management.base.Util; 55 import com.sun.appserv.management.client.TrustStoreTrustManager; 56 import com.sun.appserv.management.client.ConnectionSource; 57 import com.sun.appserv.management.client.AppserverConnectionSource; 58 import com.sun.appserv.management.client.TLSParams; 59 import com.sun.appserv.management.client.HandshakeCompletedListenerImpl; 60 import com.sun.appserv.management.client.ProxyFactory; 61 import com.sun.appserv.management.util.jmx.ObjectNameComparator; 62 import com.sun.appserv.management.util.jmx.JMXUtil; 63 64 import com.sun.enterprise.management.util.jmx.JMXTestBase; 65 import com.sun.appserv.management.util.stringifier.SmartStringifier; 66 67 68 71 public final class TestRunner 72 { 73 final ConnectionSource mConn; 74 boolean mVerbose; 75 76 public 77 TestRunner( final ConnectionSource conn ) 78 { 79 mConn = conn; 80 mVerbose = false; 81 } 82 83 84 public int 85 runSuite( String name, TestSuite suite ) 86 { 87 System.out.println( "*** testing " + name + " ***"); 88 89 junit.textui.TestRunner runner = new junit.textui.TestRunner(); 90 junit.framework.TestResult result = runner.doRun( suite, false ); 91 92 return( result.failureCount() ); 93 } 94 95 public int 96 testClass( final Class <junit.framework.TestCase> theClass ) 97 { 98 final TestSuite suite = new TestSuite( theClass ); 99 return( runSuite( theClass.getName(), suite ) ); 100 } 101 102 103 public int 104 testClassThreaded( final Class <junit.framework.TestCase> theClass ) 105 { 106 final TestSuite suite = new ActiveTestSuite( theClass ); 107 108 return( runSuite( theClass.getName(), suite ) ); 109 } 110 111 112 public void 113 runTests( List <Class <junit.framework.TestCase>> testClasses, boolean threaded ) 114 throws Exception 115 { 116 for( final Class <junit.framework.TestCase> theClass : testClasses ) 117 { 118 final int failureCount = threaded ? 119 testClassThreaded( theClass ) : 120 testClass( theClass ); 121 122 if ( failureCount != 0 ) 123 { 124 println( "Test " + theClass.getName() + " had failures: " + failureCount ); 125 } 126 } 127 } 128 129 public long 130 elapsed( final long start ) 131 { 132 return( System.currentTimeMillis() - start ); 133 } 134 135 private long 136 testGetMBeanInfoSpeed( 137 final MBeanServerConnection conn, 138 final ObjectName [] objectNames ) 139 throws IOException , JMException 140 { 141 Arrays.sort( objectNames, ObjectNameComparator.INSTANCE ); 143 144 final long startAll = System.currentTimeMillis(); 145 for( int i = 0; i < objectNames.length; ++i ) 146 { 147 final ObjectName objectName = objectNames[ i ]; 148 149 final long start = System.currentTimeMillis(); 150 151 final MBeanInfo mbeanInfo = conn.getMBeanInfo( objectName ); 152 153 final long elapsed = elapsed( start ); 154 155 String id = objectName.toString(); 156 String value; 157 158 if ( (value = objectName.getKeyProperty( "type" )) != null ) 159 { 160 id = value; 161 } 162 else if ( (value = objectName.getKeyProperty( "j2eeType" )) != null ) 163 { 164 id = value; 165 } 166 167 if ( (value = objectName.getKeyProperty( "name" )) != null ) 168 { 169 id = Util.concatenateProps( id, Util.makeNameProp( value ) ); 170 } 171 172 173 } 175 176 final long elapsed = System.currentTimeMillis() - startAll; 177 return( elapsed ); 178 } 179 180 181 protected void 182 printVerbose( final Object o ) 183 { 184 if ( mVerbose ) 185 { 186 println( o ); 187 } 188 } 189 190 private void 191 println( final Object o) 192 { 193 System.out.println( toString( o ) ); 194 } 195 196 private void 197 print( final Object o) 198 { 199 System.out.print( toString( o ) ); 200 } 201 202 private void 203 testGetMBeanInfoSpeed( 204 final MBeanServerConnection conn, 205 final String domain, 206 final String props ) 207 throws IOException , JMException 208 { 209 final ObjectName pattern = Util.newObjectNamePattern( domain, props ); 210 final Set <ObjectName > objectNameSet = JMXUtil.queryNames( conn, pattern, null); 211 212 final ObjectName [] objectNames = new ObjectName [ objectNameSet.size() ]; 213 objectNameSet.toArray( objectNames ); 214 215 final long elapsed = testGetMBeanInfoSpeed( conn, objectNames ); 216 217 println( "Time to getMBeanInfo on " + domain + ":" + props + " (" + objectNames.length + " MBeans)" + 218 " = " + elapsed + "ms" ); 219 } 220 221 public MBeanServerConnection 222 getMBeanServerConnection() 223 throws IOException 224 { 225 return( mConn == null ? null : mConn.getMBeanServerConnection( false ) ); 226 } 227 228 public void 229 testSpeed( ) 230 throws IOException , JMException 231 { 232 final DomainRoot domainRootProxy = ProxyFactory.getInstance( mConn ). 233 createDomainRoot(); 234 235 final MBeanServerConnection conn = getMBeanServerConnection(); 236 237 testGetMBeanInfoSpeed( conn, Util.getObjectName( domainRootProxy ).getDomain(), JMXUtil.WILD_ALL ); 238 } 239 240 241 242 245 private void 246 testAppserverConnectionSource( 247 final String host, 248 final String user, 249 final String password ) 250 throws IOException 251 { 252 MBeanServerConnection conn = null; 253 254 final TestClientTrustStoreTrustManager tm = new TestClientTrustStoreTrustManager(); 255 final HandshakeCompletedListenerImpl hcl = new HandshakeCompletedListenerImpl(); 256 tm.setPrompt( true ); 257 final TLSParams tlsParams = 258 new TLSParams( new X509TrustManager[] { tm }, hcl ); 259 260 println( "\ntestAppserverConnectionSource: testing: " + AppserverConnectionSource.PROTOCOL_RMI); 261 262 final ConnectionSource rmiSource = 263 new AppserverConnectionSource( AppserverConnectionSource.PROTOCOL_RMI, 264 host, 8686, user, password, null); 265 conn = rmiSource.getMBeanServerConnection( true ); 266 conn.isRegistered( JMXUtil.getMBeanServerDelegateObjectName() ); 267 268 println( AppserverConnectionSource.PROTOCOL_RMI + " OK using " + rmiSource ); 269 270 271 println( "\ntestAppserverConnectionSource: testing: " + AppserverConnectionSource.PROTOCOL_HTTP ); 272 final Map <String ,String > env = Collections.emptyMap(); 273 274 final ConnectionSource httpSource = 275 new AppserverConnectionSource( AppserverConnectionSource.PROTOCOL_HTTP, 276 host, 1234, user, password, tlsParams, env); 277 conn = httpSource.getMBeanServerConnection( true ); 278 assert conn.isRegistered( JMXUtil.getMBeanServerDelegateObjectName() ); 279 280 println( AppserverConnectionSource.PROTOCOL_HTTP + " OK using " + httpSource ); 281 282 } 283 284 285 public static String 286 toString( Object o ) 287 { 288 return( SmartStringifier.toString( o ) ); 289 } 290 291 295 protected void 296 runAll( 297 final List <Class <junit.framework.TestCase>> testClasses, 298 final boolean threaded, 299 final Map <String ,Object > env ) 300 throws Exception 301 { 302 mVerbose = Boolean.valueOf( (String )env.get( PropertyKeys.VERBOSE_KEY ) ).booleanValue(); 303 304 306 final MBeanServerConnection conn = getMBeanServerConnection(); 308 309 JMXTestBase.setGlobalConnection( conn ); 310 JMXTestBase.setEnvValues( env ); 311 312 println( "\n--- " + testClasses.size() + " TEST CLASSES ---" ); 313 314 for( final Class <junit.framework.TestCase> theClass : testClasses ) 315 { 316 println( theClass.getName() ); 317 } 318 319 println( "\n--- BEGIN TESTS ---" ); 320 runTests( testClasses, threaded ); 321 } 322 } 323 324 325 326 327 328 329 | Popular Tags |