001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.test.runners;
018:
019: import java.lang.reflect.Method;
020: import java.util.List;
021:
022: import org.apache.commons.beanutils.MethodUtils;
023: import org.junit.Test;
024: import org.junit.internal.runners.TestClassMethodsRunner;
025: import org.junit.internal.runners.TestIntrospector;
026: import org.junit.internal.runners.TestMethodRunner;
027: import org.junit.runner.Description;
028: import org.junit.runner.notification.Failure;
029: import org.junit.runner.notification.RunNotifier;
030:
031: import edu.iu.uis.eden.EdenConstants;
032:
033: public class RemotableTestClassMethodsRunner extends
034: TestClassMethodsRunner {
035:
036: public RemotableTestClassMethodsRunner(Class<?> klass) {
037: super (klass);
038: }
039:
040: @Override
041: public Description getDescription() {
042: List<Method> testMethods = new TestIntrospector(getTestClass())
043: .getTestMethods(Test.class);
044: Description spec = Description
045: .createSuiteDescription(getName());
046: for (Method method : testMethods) {
047: spec.addChild(methodDescription(method));
048: spec.addChild(webserviceMethodDescription(method));
049: }
050: return spec;
051: }
052:
053: @Override
054: protected void invokeTestMethod(Method method, RunNotifier notifier) {
055: Object test;
056: try {
057: test = createTest();
058: } catch (Exception e) {
059: testAborted(notifier, methodDescription(method));
060: return;
061: }
062: setTestName(test, method.getName());
063: TestMethodRunner runner1 = createMethodRunner(test, method,
064: notifier);
065:
066: // add a runner to run the test in webservice mode
067: try {
068: test = createTest();
069: } catch (Exception e) {
070: testAborted(notifier, webserviceMethodDescription(method));
071: return;
072: }
073: setTestName(test, method.getName() + "OverWebservices");
074: TestMethodRunner runner2 = createWebserviceMethodRunner(test,
075: method, notifier);
076:
077: // run the tests
078: runner1.run();
079: runner2.run();
080: }
081:
082: protected RemotableTestMethodRunner createWebserviceMethodRunner(
083: Object test, Method method, RunNotifier notifier) {
084: return new RemotableTestMethodRunner(test, method, notifier,
085: webserviceMethodDescription(method),
086: EdenConstants.WEBSERVICE_CLIENT_PROTOCOL);
087: }
088:
089: protected Description webserviceMethodDescription(Method method) {
090: return Description.createTestDescription(getTestClass(), method
091: .getName()
092: + "OverWebservices");
093: }
094:
095: private void setTestName(Object test, String name) {
096: try {
097: Method setNameMethod = MethodUtils.getAccessibleMethod(test
098: .getClass(), "setName",
099: new Class[] { String.class });
100: setNameMethod.invoke(test, new Object[] { name });
101: } catch (Exception e) {
102: // no setName method, or we failed to invoke it, so we can't set the name
103: }
104: }
105:
106: private void testAborted(RunNotifier notifier,
107: Description description) {
108: notifier.fireTestStarted(description);
109: notifier.fireTestFailure(new Failure(description,
110: new Exception("No runnable methods")));
111: notifier.fireTestFinished(description);
112: }
113:
114: }
|