TestRunner.java revision ada1678a4262b208a7b87391f520a7767d25287c
193N/A/*
553N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
193N/A *
193N/A * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
193N/A *
193N/A * The contents of this file are subject to the terms of either the GNU
193N/A * General Public License Version 2 only ("GPL") or the Common Development
193N/A * and Distribution License("CDDL") (collectively, the "License"). You
193N/A * may not use this file except in compliance with the License. You can
193N/A * obtain a copy of the License at
193N/A * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
193N/A * or packager/legal/LICENSE.txt. See the License for the specific
193N/A * language governing permissions and limitations under the License.
193N/A *
193N/A * When distributing the software, include this License Header Notice in each
193N/A * file and include the License file at packager/legal/LICENSE.txt.
193N/A *
193N/A * GPL Classpath Exception:
553N/A * Oracle designates this particular file as subject to the "Classpath"
553N/A * exception as provided by Oracle in the GPL Version 2 section of the License
553N/A * file that accompanied this code.
193N/A *
193N/A * Modifications:
193N/A * If applicable, add the following below the License Header, with the fields
193N/A * enclosed by brackets [] replaced by your own identifying information:
193N/A * "Portions Copyright [year] [name of copyright owner]"
193N/A *
193N/A * Contributor(s):
193N/A * If you wish your version of this file to be governed by only the CDDL or
193N/A * only the GPL Version 2, indicate your decision by adding "[Contributor]
193N/A * elects to include this software in this distribution under the [CDDL or GPL
193N/A * Version 2] license." If you don't indicate a single choice of license, a
193N/A * recipient has the option to distribute your version of this file under
193N/A * either the CDDL, the GPL Version 2 or to extend the choice of license to
193N/A * its licensees as provided above. However, if you add GPL Version 2 code
193N/A * and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
/*
*/
package org.glassfish.admin.amxtest;
import com.sun.appserv.management.DomainRoot;
import com.sun.appserv.management.base.Util;
import com.sun.appserv.management.client.AppserverConnectionSource;
import com.sun.appserv.management.client.ConnectionSource;
import com.sun.appserv.management.client.HandshakeCompletedListenerImpl;
import com.sun.appserv.management.client.ProxyFactory;
import com.sun.appserv.management.client.TLSParams;
import com.sun.appserv.management.util.jmx.JMXUtil;
import com.sun.appserv.management.util.jmx.ObjectNameComparator;
import com.sun.appserv.management.util.stringifier.SmartStringifier;
import junit.extensions.ActiveTestSuite;
import junit.framework.TestSuite;
import org.glassfish.admin.amxtest.JMXTestBase;
import javax.management.JMException;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
Class that supports running all the unit tests for AMX
*/
public final class TestRunner {
final ConnectionSource mConn;
boolean mVerbose;
public TestRunner(final ConnectionSource conn) {
mConn = conn;
mVerbose = false;
}
public int
runSuite(
String name,
TestSuite suite) {
System.out.println("*** testing " + name + " ***");
junit.textui.TestRunner runner = new junit.textui.TestRunner();
junit.framework.TestResult result = runner.doRun(suite, false);
return (result.failureCount());
}
public int
testClass(final Class<junit.framework.TestCase> theClass) {
final TestSuite suite = new TestSuite(theClass);
return (runSuite(theClass.getName(), suite));
}
public int
testClassThreaded(final Class<junit.framework.TestCase> theClass) {
final TestSuite suite = new ActiveTestSuite(theClass);
return (runSuite(theClass.getName(), suite));
}
public void
runTests(
List<Class<junit.framework.TestCase>> testClasses,
boolean threaded)
throws Exception {
for (final Class<junit.framework.TestCase> theClass : testClasses) {
final int failureCount = threaded ?
testClassThreaded(theClass) :
testClass(theClass);
if (failureCount != 0) {
println("Test " + theClass.getName() + " had failures: " + failureCount);
}
}
}
public long
elapsed(final long start) {
return (System.currentTimeMillis() - start);
}
private long
testGetMBeanInfoSpeed(
final MBeanServerConnection conn,
final ObjectName[] objectNames)
throws IOException, JMException {
// sorting provides consistent, ordered output
Arrays.sort(objectNames, ObjectNameComparator.INSTANCE);
final long startAll = System.currentTimeMillis();
for (int i = 0; i < objectNames.length; ++i) {
final ObjectName objectName = objectNames[i];
final long start = System.currentTimeMillis();
final MBeanInfo mbeanInfo = conn.getMBeanInfo(objectName);
final long elapsed = elapsed(start);
String id = objectName.toString();
String value;
if ((value = objectName.getKeyProperty("type")) != null) {
id = value;
} else if ((value = objectName.getKeyProperty("j2eeType")) != null) {
id = value;
}
if ((value = objectName.getKeyProperty("name")) != null) {
id = Util.concatenateProps(id, Util.makeNameProp(value));
}
//printVerbose( "GetMBeanInfo time for " + id + " = " + elapsed );
}
final long elapsed = System.currentTimeMillis() - startAll;
return (elapsed);
}
protected void
printVerbose(final Object o) {
if (mVerbose) {
println(o);
}
}
private void
println(final Object o) {
System.out.println(toString(o));
}
private void
print(final Object o) {
System.out.print(toString(o));
}
private void
testGetMBeanInfoSpeed(
final MBeanServerConnection conn,
final String domain,
final String props)
throws IOException, JMException {
final ObjectName pattern = Util.newObjectNamePattern(domain, props);
final Set<ObjectName> objectNameSet = JMXUtil.queryNames(conn, pattern, null);
final ObjectName[] objectNames = new ObjectName[objectNameSet.size()];
objectNameSet.toArray(objectNames);
final long elapsed = testGetMBeanInfoSpeed(conn, objectNames);
println("Time to getMBeanInfo on " + domain + ":" + props + " (" + objectNames.length + " MBeans)" +
" = " + elapsed + "ms");
}
public MBeanServerConnection
getMBeanServerConnection()
throws IOException {
return (mConn == null ? null : mConn.getMBeanServerConnection(false));
}
public void
testSpeed()
throws IOException, JMException {
final DomainRoot domainRootProxy = ProxyFactory.getInstance(mConn).
createDomainRoot();
final MBeanServerConnection conn = getMBeanServerConnection();
testGetMBeanInfoSpeed(conn, Util.getObjectName(domainRootProxy).getDomain(), JMXUtil.WILD_ALL);
}
/**
Comment in call to this for hard-coded test.
*/
private void
testAppserverConnectionSource(
final String host,
final String user,
final String password)
throws IOException {
MBeanServerConnection conn = null;
final TestClientTrustStoreTrustManager tm = new TestClientTrustStoreTrustManager();
final HandshakeCompletedListenerImpl hcl = new HandshakeCompletedListenerImpl();
tm.setPrompt(true);
final TLSParams tlsParams =
new TLSParams(new X509TrustManager[]{tm}, hcl);
println("\ntestAppserverConnectionSource: testing: " + AppserverConnectionSource.PROTOCOL_RMI);
final ConnectionSource rmiSource =
new AppserverConnectionSource(AppserverConnectionSource.PROTOCOL_RMI,
host, 8686, user, password, null);
conn = rmiSource.getMBeanServerConnection(true);
conn.isRegistered(JMXUtil.getMBeanServerDelegateObjectName());
println(AppserverConnectionSource.PROTOCOL_RMI + " OK using " + rmiSource);
println("\ntestAppserverConnectionSource: testing: " + AppserverConnectionSource.PROTOCOL_HTTP);
final Map<String, String> env = Collections.emptyMap();
final ConnectionSource httpSource =
new AppserverConnectionSource(AppserverConnectionSource.PROTOCOL_HTTP,
host, 1234, user, password, tlsParams, env);
conn = httpSource.getMBeanServerConnection(true);
assert conn.isRegistered(JMXUtil.getMBeanServerDelegateObjectName());
println(AppserverConnectionSource.PROTOCOL_HTTP + " OK using " + httpSource);
}
public static String
toString(Object o) {
return (SmartStringifier.toString(o));
}
/**
@param threaded if true, run the tests from each TestCase in separate threads
@param env arbitrary environment values for JMXTestBase
*/
protected void
runAll(
final List<Class<junit.framework.TestCase>> testClasses,
final boolean threaded,
final Map<String, Object> env)
throws Exception {
mVerbose = Boolean.valueOf((String) env.get(PropertyKeys.VERBOSE_KEY)).booleanValue();
//testSpeed();
// use the current connection which must contain the AMX MBeans
final MBeanServerConnection conn = getMBeanServerConnection();
JMXTestBase.setGlobalConnection(conn);
JMXTestBase.setEnvValues(env);
println("\n--- " + testClasses.size() + " TEST CLASSES ---");
for (final Class<junit.framework.TestCase> theClass : testClasses) {
println(theClass.getName());
}
println("\n--- BEGIN TESTS ---");
runTests(testClasses, threaded);
}
}