[P-unit-devel] SF.net SVN: p-unit: [230] trunk/punit
Status: Beta
Brought to you by:
zhanghuangzhu
|
From: <le...@us...> - 2007-07-20 13:04:21
|
Revision: 230
http://p-unit.svn.sourceforge.net/p-unit/?rev=230&view=rev
Author: leshik
Date: 2007-07-20 06:04:13 -0700 (Fri, 20 Jul 2007)
Log Message:
-----------
* Added a new EventListener which sets public test fields on start up. Added listener package and a test for a new functionality.
* Renamed Parameterizable -> Parameterized. The latter one has fewer letters and my spellchecker doesn't underline it with red.
* Renamed setup -> setUp to be consistent with JUnit.
* Enhanced before/after class events to take a test instance instead a class, fixed TODO comment to supply an exception to an after class listener.
* Fixed a number of misspells and added few lines of javadoc.
* Removed TypeUtil class, moved the checks to the Convention interface. Added org.punit.convention.AbstractConvention minimizing code duplication.
Modified Paths:
--------------
trunk/punit/src/org/punit/convention/Convention.java
trunk/punit/src/org/punit/convention/NameConvention.java
trunk/punit/src/org/punit/events/EventListener.java
trunk/punit/src/org/punit/method/builder/MethodBuilderImpl.java
trunk/punit/src/org/punit/method/builder/TestMethodBuilder.java
trunk/punit/src/org/punit/method/runner/AbstractMethodRunner.java
trunk/punit/src/org/punit/reporter/TestResult.java
trunk/punit/src/org/punit/reporter/stream/StreamLogger.java
trunk/punit/src/org/punit/runner/AbstractRunner.java
trunk/punit/src/org/punit/suite/builder/TestSuiteLabel.java
trunk/punit/src/org/punit/type/Loop.java
trunk/punit/src/org/punit/type/Parameter.java
trunk/punit/src/org/punit/type/Test.java
trunk/punit/src/org/punit/util/IntParameter.java
trunk/punit/src/org/punit/util/ReflectionUtil.java
trunk/punit.extension/src/org/punit/annotation/After.java
trunk/punit.extension/src/org/punit/annotation/Before.java
trunk/punit.extension/src/org/punit/annotation/Test.java
trunk/punit.extension/src/org/punit/convention/AnnotationConvention.java
trunk/punit.extension/src/org/punit/convention/JUnitAnnotationConvention.java
trunk/punit.extension/src/org/punit/reporter/chart/AbstractChartReporter.java
trunk/punit.extension/src/org/punit/reporter/chart/TestClassReporter.java
trunk/punit.extension/src/org/punit/reporter/chart/image/ImageRender.java
trunk/punit.extension/src/org/punit/util/AnnotationUtil.java
trunk/punit.samples/src/samples/LoopTestSample.java
trunk/punit.samples/src/samples/ParamTestClass.java
trunk/punit.test/src/tests/api/org/punit/convention/AnnotationFilterTest.java
trunk/punit.test/src/tests/api/org/punit/convention/JUnitAnnotationConventionTest.java
trunk/punit.test/src/tests/api/org/punit/convention/NameConventionFilterTest.java
trunk/punit.test/src/tests/api/org/punit/method/builder/TestMethodBuilderTest.java
trunk/punit.test/src/tests/api/org/punit/runner/ConcurrentRunnerTest.java
trunk/punit.test/src/tests/api/org/punit/runner/MockEventListener.java
trunk/punit.test/src/tests/api/org/punit/runner/SoloRunnerTest.java
trunk/punit.test/src/tests/api/org/punit/testclasses/LoopTestClass.java
trunk/punit.test/src/tests/api/org/punit/testclasses/SetResultTestClass.java
trunk/punit.test/src/tests/api/org/punit/testclasses/TestClass2.java
trunk/punit.test/src/tests/api/org/punit/testclasses/TestSuiteClass.java
Added Paths:
-----------
trunk/punit/src/org/punit/convention/AbstractConvention.java
trunk/punit/src/org/punit/type/Parameterized.java
trunk/punit.extension/src/org/punit/listener/
trunk/punit.extension/src/org/punit/listener/FieldSetter.java
trunk/punit.test/src/tests/api/org/punit/testclasses/ConcurrentParameterizedTestClass.java
trunk/punit.test/src/tests/api/org/punit/testclasses/ParameterizedTestClass.java
trunk/punit.test/src/tests/samples/ConcurrentParameterizedTestSample.java
trunk/punit.test/src/tests/samples/ParameterizedTestSample.java
Removed Paths:
-------------
trunk/punit/src/org/punit/type/Parameterizable.java
trunk/punit/src/org/punit/util/TypeUtil.java
trunk/punit.test/src/tests/api/org/punit/testclasses/ConcurrentParameterizableTestClass.java
trunk/punit.test/src/tests/api/org/punit/testclasses/ParameterizableTestClass.java
trunk/punit.test/src/tests/samples/ConcurrentParameterizableTestSample.java
trunk/punit.test/src/tests/samples/ParameterizableTestSample.java
Added: trunk/punit/src/org/punit/convention/AbstractConvention.java
===================================================================
--- trunk/punit/src/org/punit/convention/AbstractConvention.java (rev 0)
+++ trunk/punit/src/org/punit/convention/AbstractConvention.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -0,0 +1,52 @@
+/* (C) Copyright 2007, by Andrew Zhang */
+
+package org.punit.convention;
+
+import java.lang.reflect.Method;
+
+import org.punit.type.Concurrent;
+import org.punit.type.Loop;
+import org.punit.type.Parameter;
+import org.punit.type.Parameterized;
+import org.punit.type.Test;
+
+abstract public class AbstractConvention implements Convention {
+
+ public boolean isExcluded(Class method) {
+ return false;
+ }
+
+ public Class getExpectedException(Method method) {
+ return null;
+ }
+
+ public int getConcurrentCount(Object testInstance, Method method) {
+ if (testInstance instanceof Concurrent) {
+ return ((Concurrent) testInstance).concurrentCount();
+ }
+ return 0;
+ }
+
+ public boolean isPUnitTest(Class clazz) {
+ return Test.class.isAssignableFrom(clazz);
+ }
+
+ public boolean isParameter(Class clazz) {
+ return Parameter.class.isAssignableFrom(clazz);
+ }
+
+ public boolean isParameterizedTest(Class clazz) {
+ return Parameterized.class.isAssignableFrom(clazz);
+ }
+
+ public boolean isLoopTest(Class clazz) {
+ return Loop.class.isAssignableFrom(clazz);
+ }
+
+ public long toWork(Object testInstance) {
+ if (isLoopTest(testInstance.getClass())) {
+ return ((Loop) testInstance).toWork();
+ }
+ return 0;
+ }
+}
Property changes on: trunk/punit/src/org/punit/convention/AbstractConvention.java
___________________________________________________________________
Name: svn:executable
+ *
Modified: trunk/punit/src/org/punit/convention/Convention.java
===================================================================
--- trunk/punit/src/org/punit/convention/Convention.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit/src/org/punit/convention/Convention.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -34,17 +34,21 @@
public int getConcurrentCount(Object testInstance, Method method);
- public Method getSetupMethod(Class test);
+ public Method getSetUpMethod(Class test);
public Method getTearDownMethod(Class test);
public Method getBeforeClassMethod(Class test);
public Method getAfterClassMethod(Class test);
+
+ public boolean isPUnitTest(Class clazz);
- public boolean isLoopTest(Object testInstance);
+ public boolean isParameter(Class clazz);
+
+ public boolean isParameterizedTest(Class clazz);
+
+ public boolean isLoopTest(Class clazz);
public long toWork(Object testInstance);
-
- public long toStop(Object testInstance);
}
Modified: trunk/punit/src/org/punit/convention/NameConvention.java
===================================================================
--- trunk/punit/src/org/punit/convention/NameConvention.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit/src/org/punit/convention/NameConvention.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -2,19 +2,14 @@
package org.punit.convention;
-import java.lang.reflect.*;
+import java.lang.reflect.Method;
-import org.punit.type.*;
-import org.punit.util.*;
+import org.punit.util.ReflectionUtil;
-public class NameConvention implements Convention {
+public class NameConvention extends AbstractConvention {
private static final long serialVersionUID = -1252188754463864599L;
- public boolean isExcluded(Class method) {
- return false;
- }
-
public Method getCheckMethod(Method method) {
String checkMethodName = "check_" + method.getName(); //$NON-NLS-1$
return ReflectionUtil.getMethod(method.getDeclaringClass(),
@@ -33,7 +28,7 @@
}
/**
- * Judeges whether the modifier of is method conforms to the convention of a
+ * Judges whether the modifier of is method conforms to the convention of a
* test method.
*
* @param method
@@ -46,8 +41,8 @@
}
/**
- * Judeges whether the name of is method conforms to the convention of a
- * test method.
+ * Judges whether the name of is method conforms to the convention of a test
+ * method.
*
* @param method
* to be judged
@@ -61,78 +56,50 @@
}
/**
- * Judges whether the paramaters of this method conforms to the convention
- * of a test method.
+ * Judges whether the parameters of this method conform to the convention of
+ * a test method.
*
* @param method
- * @return returns true if the parameter length is zero and the test class
- * is not marked as <code>Parameterizable</code>, or the
- * parameter length is 1, and the parameter is assignable from
- * <code>Parameter</code>.
+ * @return <code>true</code> if the number of method parameters is zero
+ * and the test class is not marked as <code>Parameterized</code>,
+ * or the parameter length is 1, and the parameter is assignable
+ * from <code>Parameter</code>.
*/
protected boolean isParamValid(Method method) {
Class clazz = method.getDeclaringClass();
Class[] params = method.getParameterTypes();
- if (TypeUtil.isPUnitParameterizableTest((clazz))) {
- return params.length == 1
- && Parameter.class.isAssignableFrom(params[0]);
+ if (isParameterizedTest(clazz)) {
+ return params.length == 1 && isParameter(params[0]);
} else {
return params.length == 0;
}
}
- public Class getExpectedException(Method method) {
- return null;
- }
-
- public int getConcurrentCount(Object testInstance, Method method) {
- if(testInstance instanceof Concurrent) {
- return ((Concurrent)testInstance).concurrentCount();
- }
- return 0;
- }
-
public Method getAfterClassMethod(Class test) {
- Method method = ReflectionUtil.getMethodAndSetAccessible(test, "beforeClass", new Class[] {}); //$NON-NLS-1$
- if(method != null && ReflectionUtil.isStatic(method)) {
+ Method method = ReflectionUtil.getMethodAndSetAccessible(test,
+ "beforeClass", new Class[] {}); //$NON-NLS-1$
+ if (method != null && ReflectionUtil.isStatic(method)) {
return method;
}
return null;
}
public Method getBeforeClassMethod(Class test) {
- Method method = ReflectionUtil.getMethodAndSetAccessible(test, "afterClass", new Class[] {}); //$NON-NLS-1$
- if(method != null && ReflectionUtil.isStatic(method)) {
+ Method method = ReflectionUtil.getMethodAndSetAccessible(test,
+ "afterClass", new Class[] {}); //$NON-NLS-1$
+ if (method != null && ReflectionUtil.isStatic(method)) {
return method;
}
return null;
}
- public Method getSetupMethod(Class test) {
- return ReflectionUtil.getMethodAndSetAccessible(test, "setUp", new Class[] {}); //$NON-NLS-1$
+ public Method getSetUpMethod(Class test) {
+ return ReflectionUtil.getMethodAndSetAccessible(test,
+ "setUp", new Class[] {}); //$NON-NLS-1$
}
public Method getTearDownMethod(Class test) {
- return ReflectionUtil.getMethodAndSetAccessible(test, "tearDown", new Class[] {}); //$NON-NLS-1$
+ return ReflectionUtil.getMethodAndSetAccessible(test,
+ "tearDown", new Class[] {}); //$NON-NLS-1$
}
-
- public boolean isLoopTest(Object testInstance) {
- return testInstance instanceof Loop;
- }
-
- public long toStop(Object testInstance) {
- if(isLoopTest(testInstance)) {
- return ((Loop) testInstance).toStop();
- }
- return 0;
- }
-
- public long toWork(Object testInstance) {
- if(isLoopTest(testInstance)) {
- return ((Loop) testInstance).toWork();
- }
- return 0;
- }
-
-
}
Modified: trunk/punit/src/org/punit/events/EventListener.java
===================================================================
--- trunk/punit/src/org/punit/events/EventListener.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit/src/org/punit/events/EventListener.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -51,14 +51,23 @@
public void onSuiteEnd(TestSuite suite);
/**
- * Is triggered when the test class is to be executed.
+ * Is triggered when the test class is already instantiated and is ready to
+ * be executed.
+ *
+ * @param testInstance
+ * a test class instance
*/
- public void onClassStart(Class clazz);
+ public void onClassStart(Object testInstance);
/**
* Is triggered after executing the test class.
+ *
+ * @param testInstance
+ * the test class instance
+ * @param t
+ * an error was triggered while tearing the test down
*/
- public void onClassEnd(Class clazz);
+ public void onClassEnd(Object testInstance, Throwable t);
/**
* Is triggered when the method to be executed.
Modified: trunk/punit/src/org/punit/method/builder/MethodBuilderImpl.java
===================================================================
--- trunk/punit/src/org/punit/method/builder/MethodBuilderImpl.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit/src/org/punit/method/builder/MethodBuilderImpl.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -34,9 +34,9 @@
}
/**
- * @see TestMethodBuilder#buildTestMethods(Class)
+ * @see TestMethodBuilder#extractTestMethods(Class)
*/
- public Collection buildTestMethods(Class testClass) {
+ public Collection extractTestMethods(Class testClass) {
Collection testMethods = null;
if (isAlphabetical(testClass)) {
testMethods = new TreeSet(new AlphabeticalMethodNameComparator());
Modified: trunk/punit/src/org/punit/method/builder/TestMethodBuilder.java
===================================================================
--- trunk/punit/src/org/punit/method/builder/TestMethodBuilder.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit/src/org/punit/method/builder/TestMethodBuilder.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -13,16 +13,15 @@
*/
public interface TestMethodBuilder extends Serializable {
/**
- * Builds the test methods from the test class. Returns a list of methods.
- * The real return type should be Method[]. It returns Object[] for java 1.4
- * compatibility.
+ * Builds a collection of test methods from the test class. Returns a list
+ * of methods.
*
- * @param clazz -
- * the test class to be built
- * @return a <code>Collection</code> of <code>java.lang.reflect.Method</code>.
+ * @param clazz
+ * the test class to be extract methods from
+ * @return a <code>Collection</code> of
+ * <code>java.lang.reflect.Method</code>.
*/
- public Collection buildTestMethods(Class testClass);
+ public Collection extractTestMethods(Class testClass); // Collection<Method>
-
public void setConvention(Convention convention);
}
Modified: trunk/punit/src/org/punit/method/runner/AbstractMethodRunner.java
===================================================================
--- trunk/punit/src/org/punit/method/runner/AbstractMethodRunner.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit/src/org/punit/method/runner/AbstractMethodRunner.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -13,12 +13,11 @@
import org.punit.exception.ReflectionException;
import org.punit.runner.RunnerProperties;
import org.punit.type.Parameter;
-import org.punit.type.Parameterizable;
+import org.punit.type.Parameterized;
import org.punit.type.Test;
import org.punit.util.ReflectionUtil;
import org.punit.util.Traverser;
import org.punit.util.TraverserUtil;
-import org.punit.util.TypeUtil;
import org.punit.watcher.TimeWatcher;
import org.punit.watcher.Watcher;
@@ -165,7 +164,7 @@
_class = testInstance.getClass();
_params = params;
_method = method;
- _setUpMethod = _convention.getSetupMethod(_class);
+ _setUpMethod = _convention.getSetUpMethod(_class);
_tearDownMethod = _convention.getTearDownMethod(_class);
_checkMethod = _convention.getCheckMethod(method);
_expectedException = _convention.getExpectedException(method);
@@ -197,19 +196,19 @@
*
*/
protected void setUpBeforeWatchers(Object[] params) throws Throwable {
- if (TypeUtil.isPUnitTest(_class)) {
+ if (_convention.isPUnitTest(_class)) {
((Test) _testInstance).setUpBeforeWatchers();
- } else if (TypeUtil.isPUnitParameterizableTest(_class)) {
- ((Parameterizable) _testInstance)
+ } else if (_convention.isParameterizedTest(_class)) {
+ ((Parameterized) _testInstance)
.setUpBeforeWatchers((Parameter) params[0]);
}
}
private void setUpAfterWatchers(Object[] params) throws Throwable {
- if (TypeUtil.isPUnitTest(_class)) {
+ if (_convention.isPUnitTest(_class)) {
((Test) _testInstance).setUpAfterWatchers();
- } else if (TypeUtil.isPUnitParameterizableTest(_class)) {
- ((Parameterizable) _testInstance)
+ } else if (_convention.isParameterizedTest(_class)) {
+ ((Parameterized) _testInstance)
.setUpAfterWatchers((Parameter) params[0]);
} else {
setUp();
@@ -217,10 +216,10 @@
}
private void tearDownBeforeWatchers(Object[] params) throws Throwable {
- if (TypeUtil.isPUnitTest(_class)) {
+ if (_convention.isPUnitTest(_class)) {
((Test) _testInstance).tearDownBeforeWatchers();
- } else if (TypeUtil.isPUnitParameterizableTest(_class)) {
- ((Parameterizable) _testInstance)
+ } else if (_convention.isParameterizedTest(_class)) {
+ ((Parameterized) _testInstance)
.tearDownBeforeWatchers((Parameter) params[0]);
} else {
tearDown();
@@ -228,10 +227,10 @@
}
private void tearDownAfterWatchers(Object[] params) throws Throwable {
- if (TypeUtil.isPUnitTest(_class)) {
+ if (_convention.isPUnitTest(_class)) {
((Test) _testInstance).tearDownAfterWatchers();
- } else if (TypeUtil.isPUnitParameterizableTest(_class)) {
- ((Parameterizable) _testInstance)
+ } else if (_convention.isParameterizedTest(_class)) {
+ ((Parameterized) _testInstance)
.tearDownAfterWatchers((Parameter) params[0]);
}
}
Modified: trunk/punit/src/org/punit/reporter/TestResult.java
===================================================================
--- trunk/punit/src/org/punit/reporter/TestResult.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit/src/org/punit/reporter/TestResult.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -18,13 +18,14 @@
private List _failures = new ArrayList();
- public void onMethodEnd(Method method, Object testInstance, Object[] params, Throwable t, List Watchers) {
+ public void onMethodEnd(Method method, Object testInstance,
+ Object[] params, Throwable t, List Watchers) {
countMethod();
if (t != null) {
addThrowable(t);
}
}
-
+
public synchronized void countMethod() {
++_methodCount;
}
@@ -41,53 +42,61 @@
return _failures;
}
- public void onClassEnd(Class clazz) {
-
+ /**
+ * A method is required by <code>EventListener</code> interface. It does
+ * nothing when a new test class is instantiated.
+ */
+ public void onClassStart(Object testInstance) {
+
}
- public void onClassStart(Class clazz) {
-
+ /**
+ * A method is required by <code>EventListener</code> interface. It does
+ * nothing when a test class execution is completed.
+ */
+ public void onClassEnd(Object testInstance, Throwable t) {
+
}
+ public void onMethodStart(Method method, Object testInstance,
+ Object[] params) {
- public void onMethodStart(Method method, Object testInstance, Object[] params) {
-
}
public void onRunnerEnd(Class clazz, Runner runner) {
-
+
}
public void onRunnerStart(Class clazz, Runner runner) {
-
+
}
public void onSuiteEnd(TestSuite suite) {
-
+
}
public void onSuiteStart(TestSuite suite) {
-
+
}
public void onWatcherEnd(Watcher watcher) {
-
+
}
public void onWatcherStart(Watcher watcher) {
-
+
}
public void onWatchersEnd(List watchers) {
-
+
}
public void onWatchersStart(List watchers) {
-
+
}
public boolean supportParentRunner() {
return false;
}
-
+
}
Modified: trunk/punit/src/org/punit/reporter/stream/StreamLogger.java
===================================================================
--- trunk/punit/src/org/punit/reporter/stream/StreamLogger.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit/src/org/punit/reporter/stream/StreamLogger.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -105,9 +105,12 @@
log(sb.toString(), Level.INFO);
}
+ public void onClassStart(Object testInstance) {
+ logln(testInstance.getClass().getName(), Level.INFO);
+ }
- public void onClassStart(Class clazz) {
- logln(clazz.getName(), Level.INFO);
+ public void onClassEnd(Object testInstance, Throwable t) {
+ // nothing needs to be done
}
public void onMethodEnd(Method method, Object instance, Object[] params,
@@ -190,30 +193,26 @@
}
public void onWatchersStart(List watchers) {
- // nothing needs to do
+ // nothing needs to be done
}
public void onWatcherStart(Watcher watcher) {
- // nothing needs to do
+ // nothing needs to be done
}
public void onWatchersEnd(List watchers) {
- // nothing needs to do
+ // nothing needs to be done
}
public void onWatcherEnd(Watcher watcher) {
- // nothing needs to do
+ // nothing needs to be done
}
public void onSuiteEnd(TestSuite suite) {
- // nothing needs to do
+ // nothing needs to be done
}
- public void onClassEnd(Class clazz) {
- // nothing needs to do
- }
-
public void onMethodStart(Method method, Object instance, Object[] params) {
- // nothing needs to do
+ // nothing needs to be done
}
}
\ No newline at end of file
Modified: trunk/punit/src/org/punit/runner/AbstractRunner.java
===================================================================
--- trunk/punit/src/org/punit/runner/AbstractRunner.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit/src/org/punit/runner/AbstractRunner.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -20,14 +20,13 @@
import org.punit.suite.builder.TestSuiteBuilder;
import org.punit.suite.builder.TestSuiteLabel;
import org.punit.type.Parameter;
-import org.punit.type.Parameterizable;
+import org.punit.type.Parameterized;
import org.punit.type.TestSuite;
import org.punit.type.VM;
import org.punit.util.IOUtil;
import org.punit.util.ReflectionUtil;
import org.punit.util.Traverser;
import org.punit.util.TraverserUtil;
-import org.punit.util.TypeUtil;
public abstract class AbstractRunner implements Runner {
@@ -37,8 +36,7 @@
private MethodRunner _methodRunner;
- // List <EventListener>
- private final List _eventListeners = new ArrayList();
+ private final List _eventListeners = new ArrayList(); // List<EventListener>
private TestResult _testResult = new TestResult();
@@ -253,24 +251,29 @@
}
private void runTestClassImpl(final Class clazz) {
- onClassStart(clazz);
+ Object testInstance = ReflectionUtil.newInstance(clazz);
+ Throwable storedException = null;
+ Collection testMethods = extractTestMethods(clazz);
+
+ onClassStart(testInstance);
try {
beforeClass(clazz);
- Collection testMethods = buildTestMethod(clazz);
TraverserUtil.traverse(testMethods.iterator(), new Traverser() {
public void traverse(Object obj) {
Method method = (Method) obj;
runTestMethod(clazz, method);
}
});
+ } catch (Throwable t) {
+ storedException = t;
} finally {
try {
afterClass(clazz);
} catch (Throwable t) {
- // TODO: will be passed to onClassEnd
+ storedException = t;
}
}
- onClassEnd(clazz);
+ onClassEnd(testInstance, storedException);
}
private void beforeClass(Class clazz) {
@@ -303,44 +306,40 @@
});
}
- private void onClassStart(final Class clazz) {
+ private void onClassStart(final Object testInstance) {
TraverserUtil.traverse(_eventListeners.iterator(), new Traverser() {
public void traverse(Object obj) {
- ((EventListener) obj).onClassStart(clazz);
+ ((EventListener) obj).onClassStart(testInstance);
}
});
}
- private void onClassEnd(final Class clazz) {
+ private void onClassEnd(final Object testInstance, final Throwable t) {
TraverserUtil.traverse(_eventListeners.iterator(), new Traverser() {
public void traverse(Object obj) {
- ((EventListener) obj).onClassEnd(clazz);
+ ((EventListener) obj).onClassEnd(testInstance, t);
}
});
}
private void runTestMethod(Class clazz, Method method) {
Object testInstance = ReflectionUtil.newInstance(clazz);
- if (TypeUtil.isPUnitParameterizableTest(clazz)) {
- Parameterizable pInstance = (Parameterizable) testInstance;
+ if (_convention.isParameterizedTest(clazz)) {
+ Parameterized pInstance = (Parameterized) testInstance;
Parameter[] params = pInstance.parameters();
for (int i = 0; i < params.length; ++i) {
- runTestMethod(testInstance, method, new Object[] { params[i] });
+ _methodRunner.run(testInstance, method,
+ new Object[] { params[i] });
}
} else {
- runTestMethod(testInstance, method, new Object[] {});
+ _methodRunner.run(testInstance, method, new Object[] {});
}
}
- private void runTestMethod(Object testInstance, Method method,
- Object[] params) {
- _methodRunner.run(testInstance, method, params);
+ private Collection extractTestMethods(Class testClass) {
+ return _testMethodBuilder.extractTestMethods(testClass);
}
- private Collection buildTestMethod(Class testClass) {
- return _testMethodBuilder.buildTestMethods(testClass);
- }
-
public TestMethodBuilder methodBuilder() {
return _testMethodBuilder;
}
Modified: trunk/punit/src/org/punit/suite/builder/TestSuiteLabel.java
===================================================================
--- trunk/punit/src/org/punit/suite/builder/TestSuiteLabel.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit/src/org/punit/suite/builder/TestSuiteLabel.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -2,22 +2,23 @@
package org.punit.suite.builder;
-import org.punit.type.*;
+import org.punit.type.TestSuite;
/**
* Represents a test suite label. TestSuiteBuilder inserts this label in the
* result to indicate the start and the end of a test suite.
+ *
* @see TestSuiteBuilder#buildTestClasses(Class)
*/
public interface TestSuiteLabel {
-
+
/**
* @return the test suite it represents
*/
public TestSuite suite();
/**
- * @return returns true if it represents the start of a test suite.
+ * @return <code>true</code> if it represents the start of a test suite.
*/
public boolean isStart();
}
Modified: trunk/punit/src/org/punit/type/Loop.java
===================================================================
--- trunk/punit/src/org/punit/type/Loop.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit/src/org/punit/type/Loop.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -3,17 +3,12 @@
package org.punit.type;
/**
- * If a test implements this interface, punit concurrent runner will executes
- * its method repeatedly until timeout.
+ * If a test implements this interface, PUnit concurrent runner executes
+ * the method repeatedly until timeout.
*/
public interface Loop {
/**
* @return returns the time to loop
*/
public long toWork();
-
- /**
- * @return returns the time to stop the test gracefully
- */
- public long toStop();
}
Modified: trunk/punit/src/org/punit/type/Parameter.java
===================================================================
--- trunk/punit/src/org/punit/type/Parameter.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit/src/org/punit/type/Parameter.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -2,13 +2,13 @@
package org.punit.type;
-import org.punit.type.Parameterizable;
+import org.punit.type.Parameterized;
/**
- * If a test implements <code>Parameterizable</code> interface, the test
+ * If a test implements <code>Parameterized</code> interface, the test
* method with this interface will be executed.
*
- * @see Parameterizable
+ * @see Parameterized
*
*/
public interface Parameter {
Deleted: trunk/punit/src/org/punit/type/Parameterizable.java
===================================================================
--- trunk/punit/src/org/punit/type/Parameterizable.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit/src/org/punit/type/Parameterizable.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -1,52 +0,0 @@
-/* (C) Copyright 2007, by Andrew Zhang */
-
-package org.punit.type;
-
-import org.punit.type.Parameter;
-
-/**
- * If a test implements <code>Parameterizable</code> interface, the test
- * method with only a <code>Parameter</code> argument (i.e.
- * <code>testFoo(Parameter p))</code> will be executed.
- */
-public interface Parameterizable {
-
- /**
- * This method will be invoked before all watchers start.
- *
- * @param param
- * @throws Exception
- */
- public void setUpBeforeWatchers(Parameter param) throws Exception;
-
- /**
- * This method will be invoked after all watchers start.
- *
- * @param param
- * @throws Exception
- */
- public void setUpAfterWatchers(Parameter param) throws Exception;
-
- /**
- * This method will be invoked before all watchers stop.
- *
- * @param param
- * @throws Exception
- */
- public void tearDownBeforeWatchers(Parameter param) throws Exception;
-
- /**
- * This method will be invoked after all watchers stop.
- *
- * @param param
- * @throws Exception
- */
- public void tearDownAfterWatchers(Parameter param) throws Exception;
-
- /**
- * Returns a parameter array for the test.
- *
- * @return
- */
- Parameter[] parameters();
-}
Added: trunk/punit/src/org/punit/type/Parameterized.java
===================================================================
--- trunk/punit/src/org/punit/type/Parameterized.java (rev 0)
+++ trunk/punit/src/org/punit/type/Parameterized.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -0,0 +1,48 @@
+/* (C) Copyright 2007, by Andrew Zhang */
+
+package org.punit.type;
+
+/**
+ * If a test implements <code>Parameterized</code> interface, the test method
+ * with only a <code>Parameter</code> argument (i.e.
+ * <code>testFoo(Parameter p))</code> will be executed.
+ */
+public interface Parameterized {
+
+ /**
+ * Is invoked before all watchers start.
+ *
+ * @param param
+ * @throws Exception
+ */
+ public void setUpBeforeWatchers(Parameter param) throws Exception;
+
+ /**
+ * Is invoked after all watchers start.
+ *
+ * @param param
+ * @throws Exception
+ */
+ public void setUpAfterWatchers(Parameter param) throws Exception;
+
+ /**
+ * Is invoked before all watchers stop.
+ *
+ * @param param
+ * @throws Exception
+ */
+ public void tearDownBeforeWatchers(Parameter param) throws Exception;
+
+ /**
+ * Is invoked after all watchers stop.
+ *
+ * @param param
+ * @throws Exception
+ */
+ public void tearDownAfterWatchers(Parameter param) throws Exception;
+
+ /**
+ * @return a parameter array for the test
+ */
+ Parameter[] parameters();
+}
Modified: trunk/punit/src/org/punit/type/Test.java
===================================================================
--- trunk/punit/src/org/punit/type/Test.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit/src/org/punit/type/Test.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -3,7 +3,7 @@
package org.punit.type;
/**
- * Marks this test as a punit test. It has more setUp/tearDown control than
+ * Marks this test as a PUnit test. It has more setUp/tearDown control than
* normal test class.
*
*/
Modified: trunk/punit/src/org/punit/util/IntParameter.java
===================================================================
--- trunk/punit/src/org/punit/util/IntParameter.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit/src/org/punit/util/IntParameter.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -2,22 +2,22 @@
package org.punit.util;
-import org.punit.type.*;
+import org.punit.type.Parameter;
public class IntParameter implements Parameter {
-
- private int _intValue;
- public IntParameter(int value) {
- _intValue = value;
- }
-
- public int intValue() {
- return _intValue;
- }
-
- public String toString() {
- return String.valueOf(_intValue);
- }
+ private int _intValue;
+ public IntParameter(int value) {
+ _intValue = value;
+ }
+
+ public int intValue() {
+ return _intValue;
+ }
+
+ public String toString() {
+ return String.valueOf(_intValue);
+ }
+
}
Modified: trunk/punit/src/org/punit/util/ReflectionUtil.java
===================================================================
--- trunk/punit/src/org/punit/util/ReflectionUtil.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit/src/org/punit/util/ReflectionUtil.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -2,80 +2,121 @@
package org.punit.util;
-import java.lang.reflect.*;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
-import org.punit.exception.*;
+import org.punit.exception.ReflectionException;
public class ReflectionUtil {
-
- public static Object newInstance(Class clazz) {
- try {
- Constructor constructor = clazz.getDeclaredConstructor(new Class[] {});
- constructor.setAccessible(true);
- return constructor.newInstance(new Object [] {});
- } catch (Exception e) {
- throw new ReflectionException(e);
- }
- }
-
- public static Class newClass(String className) {
- try {
- return Class.forName(className);
- } catch (Exception e) {
- throw new ReflectionException(e);
- }
- }
-
- public static Method getMethod(Class clazz, String methodName, Class[] params) {
- try {
- Method method = clazz.getDeclaredMethod(methodName, params);
- return method;
- } catch (Exception e) {
- return null;
- }
- }
-
- /**
- * Gets the method and sets the accessible to true
- * @param clazz
- * @param methodName
- * @param params
- * @return
- */
- public static Method getMethodAndSetAccessible(Class clazz, String methodName, Class[] params) {
- try {
- Method method = clazz.getDeclaredMethod(methodName, params);
- method.setAccessible(true);
- return method;
- } catch (Exception e) {
- return null;
- }
- }
-
- public static Object invokeMethod(Method method, Object instance, Object[] params) throws ReflectionException {
- try {
- return method.invoke(instance, params);
- } catch (IllegalArgumentException e) {
- throw new ReflectionException(e);
- } catch (IllegalAccessException e) {
- throw new ReflectionException(e);
- } catch (InvocationTargetException e) {
- throw new ReflectionException(e.getTargetException());
- }
- }
-
- public static boolean isPublic(Method method) {
- int modifier = method.getModifiers();
- return isBitSet(modifier, Modifier.PUBLIC);
- }
-
- public static boolean isStatic(Method method) {
- int modifier = method.getModifiers();
- return isBitSet(modifier, Modifier.STATIC);
- }
-
- private static boolean isBitSet(int value, int expectedBit) {
- return (value & expectedBit) != 0;
-
- }
+
+ public static Object newInstance(Class clazz) {
+ try {
+ Constructor constructor = clazz
+ .getDeclaredConstructor(new Class[] {});
+ constructor.setAccessible(true);
+ return constructor.newInstance(new Object[] {});
+ } catch (Exception e) {
+ throw new ReflectionException(e);
+ }
+ }
+
+ public static Class newClass(String className) {
+ try {
+ return Class.forName(className);
+ } catch (Exception e) {
+ throw new ReflectionException(e);
+ }
+ }
+
+ public static Method getMethod(Class clazz, String methodName,
+ Class[] params) {
+ try {
+ Method method = clazz.getDeclaredMethod(methodName, params);
+ return method;
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+ /**
+ * Sets a field with a given value after converting the value to the field
+ * type.
+ *
+ * @param field
+ * the field to assign
+ * @param value
+ * the string value to assign
+ */
+ public static void setField(Object testInstance, Field field, String value) {
+ // System.out.println("Field = " + field + ", value = " + value);
+ // System.out.println("value = " + Integer.getInteger(value));
+
+ try {
+ field.setAccessible(true);
+ Class fieldClass = field.getType();
+ if (fieldClass.equals(int.class)) {
+ int intVal = Integer.parseInt(value);
+ field.setInt(testInstance, intVal);
+ } else if (fieldClass.equals(long.class)) {
+ long longVal = Long.parseLong(value);
+ field.setLong(testInstance, longVal);
+ } else {
+ field.set(testInstance, value);
+ }
+ } catch (NumberFormatException nfe) {
+ throw new IllegalArgumentException(nfe);
+ } catch (Exception e) {
+ throw new ReflectionException(e);
+ }
+ }
+
+ /**
+ * Gets the method and sets the accessible to true
+ *
+ * @param clazz
+ * @param methodName
+ * @param params
+ * @return
+ */
+ public static Method getMethodAndSetAccessible(Class clazz,
+ String methodName, Class[] params) {
+ try {
+ Method method = clazz.getDeclaredMethod(methodName, params);
+ method.setAccessible(true);
+ return method;
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+ public static Object invokeMethod(Method method, Object instance,
+ Object[] params) throws ReflectionException {
+ try {
+ return method.invoke(instance, params);
+ } catch (IllegalArgumentException e) {
+ throw new ReflectionException(e);
+ } catch (IllegalAccessException e) {
+ throw new ReflectionException(e);
+ } catch (InvocationTargetException e) {
+ throw new ReflectionException(e.getTargetException());
+ }
+ }
+
+ public static boolean isPublic(Method method) {
+ int modifier = method.getModifiers();
+ return isBitSet(modifier, Modifier.PUBLIC);
+ }
+
+ public static boolean isStatic(Method method) {
+ int modifier = method.getModifiers();
+ return isBitSet(modifier, Modifier.STATIC);
+ }
+
+ private static boolean isBitSet(int value, int expectedBit) {
+ return (value & expectedBit) != 0;
+
+ }
}
Deleted: trunk/punit/src/org/punit/util/TypeUtil.java
===================================================================
--- trunk/punit/src/org/punit/util/TypeUtil.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit/src/org/punit/util/TypeUtil.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -1,16 +0,0 @@
-/* (C) Copyright 2007, by Andrew Zhang */
-
-package org.punit.util;
-
-import org.punit.type.*;
-
-public class TypeUtil {
-
- public static boolean isPUnitTest(Class clazz) {
- return Test.class.isAssignableFrom(clazz);
- }
-
- public static boolean isPUnitParameterizableTest(Class clazz) {
- return Parameterizable.class.isAssignableFrom(clazz);
- }
-}
Modified: trunk/punit.extension/src/org/punit/annotation/After.java
===================================================================
--- trunk/punit.extension/src/org/punit/annotation/After.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit.extension/src/org/punit/annotation/After.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -1,7 +1,10 @@
/* (C) Copyright 2007, by Andrew Zhang */
package org.punit.annotation;
-import java.lang.annotation.*;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
Modified: trunk/punit.extension/src/org/punit/annotation/Before.java
===================================================================
--- trunk/punit.extension/src/org/punit/annotation/Before.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit.extension/src/org/punit/annotation/Before.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -1,11 +1,13 @@
/* (C) Copyright 2007, by Andrew Zhang */
package org.punit.annotation;
-import java.lang.annotation.*;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
+public @interface Before {
-public @interface Before {
-
}
Modified: trunk/punit.extension/src/org/punit/annotation/Test.java
===================================================================
--- trunk/punit.extension/src/org/punit/annotation/Test.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit.extension/src/org/punit/annotation/Test.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -2,18 +2,19 @@
package org.punit.annotation;
-import java.lang.annotation.*;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Test {
- Class<? extends Throwable> expected() default NoException.class;
+ Class<? extends Throwable> expected() default NoException.class;
- String checkMethod() default "";
-
- int concurrentCount() default 0;
+ String checkMethod() default "";
- public class NoException extends Throwable {
- private static final long serialVersionUID = 3987745685001380514L;
- }
+ int concurrentCount() default 0;
+
+ public class NoException extends Throwable {
+ private static final long serialVersionUID = 3987745685001380514L;
+ }
}
Modified: trunk/punit.extension/src/org/punit/convention/AnnotationConvention.java
===================================================================
--- trunk/punit.extension/src/org/punit/convention/AnnotationConvention.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit.extension/src/org/punit/convention/AnnotationConvention.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -2,26 +2,52 @@
package org.punit.convention;
+import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
-
-import org.punit.annotation.After;
-import org.punit.annotation.AfterClass;
-import org.punit.annotation.Before;
-import org.punit.annotation.BeforeClass;
-import org.punit.annotation.Ignore;
import org.punit.annotation.Test;
-import org.punit.annotation.Test.NoException;
import org.punit.util.AnnotationUtil;
import org.punit.util.ReflectionUtil;
-public class AnnotationConvention implements Convention {
+/**
+ * The class merges annotation and name conventions with annotations taking
+ * preference.
+ */
+public class AnnotationConvention extends AbstractConvention {
- private static final long serialVersionUID = -2043378593194849589L;
-
- private NameConvention _delegate = new NameConvention();
+ private static final long serialVersionUID = -6800242731470821241L;
+ protected NameConvention _delegate = new NameConvention();
+
+ private Class<? extends Annotation> _after;
+ private Class<? extends Annotation> _afterClass;
+ private Class<? extends Annotation> _before;
+ private Class<? extends Annotation> _beforeClass;
+ private Class<? extends Annotation> _ignore;
+ protected Class<? extends Annotation> _test;
+
+ public AnnotationConvention() {
+ this(org.punit.annotation.After.class,
+ org.punit.annotation.AfterClass.class,
+ org.punit.annotation.Before.class,
+ org.punit.annotation.BeforeClass.class,
+ org.punit.annotation.Ignore.class, Test.class);
+ }
+
+ public AnnotationConvention(Class<? extends Annotation> after,
+ Class<? extends Annotation> afterClass,
+ Class<? extends Annotation> before,
+ Class<? extends Annotation> beforeClass,
+ Class<? extends Annotation> ignore, Class<? extends Annotation> test) {
+ _after = after;
+ _afterClass = afterClass;
+ _before = before;
+ _beforeClass = beforeClass;
+ _ignore = ignore;
+ _test = test;
+ }
+
public Method getCheckMethod(Method method) {
- Test testAnnotation = getTestAnnotation(method);
+ Test testAnnotation = (Test) method.getAnnotation(_test);
String checkMethodName = null;
if (testAnnotation != null) {
checkMethodName = testAnnotation.checkMethod();
@@ -32,60 +58,46 @@
}
return _delegate.getCheckMethod(method);
}
-
+
@SuppressWarnings("unchecked")
public boolean isExcluded(Class clazz) {
- Ignore ignore = (Ignore) clazz.getAnnotation(Ignore.class);
- if(ignore == null) {
- return false;
- }
- return true;
+ return clazz.isAnnotationPresent(_ignore);
}
-
+
public boolean isTestMethod(Method method) {
- Ignore ignore = (Ignore) method.getAnnotation(Ignore.class);
- if(ignore != null) {
+ if (method.isAnnotationPresent(_ignore)) {
return false;
}
- Test testAnnotation = getTestAnnotation(method);
- return (testAnnotation != null) || _delegate.isTestMethod(method);
+ return method.isAnnotationPresent(_test) || _delegate.isTestMethod(method);
}
public Class<? extends Throwable> getExpectedException(Method method) {
- Test testAnnotation = getTestAnnotation(method);
- if(testAnnotation == null) {
+ Test testAnnotation = (Test) method.getAnnotation(_test);
+ if (testAnnotation == null) {
return null;
}
Class<? extends Throwable> expected = testAnnotation.expected();
- if(expected == NoException.class) {
+ if (expected == Test.NoException.class) {
return null;
}
return expected;
}
public int getConcurrentCount(Object testInstance, Method method) {
- Test testAnnotation = getTestAnnotation(method);
- if(testAnnotation == null) {
- testAnnotation = getTestAnnotation(testInstance.getClass());
+ Test testAnnotation = (Test) method.getAnnotation(_test);
+ if (testAnnotation == null) {
+ testAnnotation = (Test) testInstance.getClass().getAnnotation(_test);
}
- if(testAnnotation == null) {
+ if (testAnnotation == null) {
return _delegate.getConcurrentCount(testInstance, method);
}
return testAnnotation.concurrentCount();
}
-
- private Test getTestAnnotation(Method method) {
- return method.getAnnotation(Test.class);
- }
-
- private Test getTestAnnotation(Class<?> clazz) {
- return (Test) clazz.getAnnotation(Test.class);
- }
@SuppressWarnings("unchecked")
public Method getAfterClassMethod(Class test) {
- Method method = AnnotationUtil.getMethodByAnnotation(test, AfterClass.class);
- if(method == null) {
+ Method method = AnnotationUtil.getMethodByAnnotation(test, _afterClass);
+ if (method == null) {
method = _delegate.getAfterClassMethod(test);
}
return method;
@@ -93,40 +105,30 @@
@SuppressWarnings("unchecked")
public Method getBeforeClassMethod(Class test) {
- Method method = AnnotationUtil.getMethodByAnnotation(test, BeforeClass.class);
- if(method == null) {
+ Method method = AnnotationUtil
+ .getMethodByAnnotation(test, _beforeClass);
+ if (method == null) {
method = _delegate.getBeforeClassMethod(test);
}
return method;
}
@SuppressWarnings("unchecked")
- public Method getSetupMethod(Class test) {
- Method method = AnnotationUtil.getMethodByAnnotation(test, Before.class);
- if(method == null) {
- method = _delegate.getSetupMethod(test);
+ public Method getSetUpMethod(Class test) {
+ Method method = AnnotationUtil.getMethodByAnnotation(test, _before);
+ if (method == null) {
+ method = _delegate.getSetUpMethod(test);
}
return method;
}
@SuppressWarnings("unchecked")
public Method getTearDownMethod(Class test) {
- Method method = AnnotationUtil.getMethodByAnnotation(test, After.class);
- if(method == null) {
+ Method method = AnnotationUtil.getMethodByAnnotation(test, _after);
+ if (method == null) {
method = _delegate.getTearDownMethod(test);
}
return method;
}
- public long toStop(Object testInstance) {
- return 0;
- }
-
- public long toWork(Object testInstance) {
- return 0;
- }
-
- public boolean isLoopTest(Object testInstance) {
- return false;
- }
}
Modified: trunk/punit.extension/src/org/punit/convention/JUnitAnnotationConvention.java
===================================================================
--- trunk/punit.extension/src/org/punit/convention/JUnitAnnotationConvention.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit.extension/src/org/punit/convention/JUnitAnnotationConvention.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -2,101 +2,38 @@
package org.punit.convention;
-import java.lang.reflect.*;
+import java.lang.reflect.Method;
-import org.junit.*;
-import org.junit.Test.*;
-import org.punit.util.*;
+import org.junit.Test;
-public class JUnitAnnotationConvention implements Convention {
-
- private static final long serialVersionUID = 2826000346348614825L;
-
- private NameConvention _delegate = new NameConvention();
+public class JUnitAnnotationConvention extends AnnotationConvention {
- public Method getCheckMethod(Method method) {
- return _delegate.getCheckMethod(method);
+ private static final long serialVersionUID = 5775534409608907458L;
+
+ public JUnitAnnotationConvention() {
+ super(org.junit.After.class, org.junit.AfterClass.class,
+ org.junit.Before.class, org.junit.BeforeClass.class,
+ org.junit.Ignore.class, Test.class);
}
-
- @SuppressWarnings("unchecked")
- public boolean isExcluded(Class clazz) {
- return false;
- }
-
- public boolean isTestMethod(Method method) {
- Ignore ignore = (Ignore) method.getAnnotation(Ignore.class);
- if(ignore != null) {
- return false;
- }
- Test testAnnotation = getTestAnnotation(method);
- return (testAnnotation != null) || _delegate.isTestMethod(method);
- }
public Class<? extends Throwable> getExpectedException(Method method) {
- Test testAnnotation = getTestAnnotation(method);
- if(testAnnotation == null) {
+ Test testAnnotation = (Test) method.getAnnotation(_test);
+ if (testAnnotation == null) {
return null;
}
Class<? extends Throwable> expected = testAnnotation.expected();
- if(expected == None.class) {
+ if (expected == Test.None.class) {
return null;
}
return expected;
}
+ public Method getCheckMethod(Method method) {
+ return _delegate.getCheckMethod(method);
+ }
+
public int getConcurrentCount(Object testInstance, Method method) {
return _delegate.getConcurrentCount(testInstance, method);
}
-
- private Test getTestAnnotation(Method method) {
- return method.getAnnotation(Test.class);
- }
- @SuppressWarnings("unchecked")
- public Method getAfterClassMethod(Class test) {
- Method method = AnnotationUtil.getMethodByAnnotation(test, AfterClass.class);
- if(method == null) {
- method = _delegate.getAfterClassMethod(test);
- }
- return method;
- }
-
- @SuppressWarnings("unchecked")
- public Method getBeforeClassMethod(Class test) {
- Method method = AnnotationUtil.getMethodByAnnotation(test, BeforeClass.class);
- if(method == null) {
- method = _delegate.getBeforeClassMethod(test);
- }
- return method;
- }
-
- @SuppressWarnings("unchecked")
- public Method getSetupMethod(Class test) {
- Method method = AnnotationUtil.getMethodByAnnotation(test, Before.class);
- if(method == null) {
- method = _delegate.getSetupMethod(test);
- }
- return method;
- }
-
- @SuppressWarnings("unchecked")
- public Method getTearDownMethod(Class test) {
- Method method = AnnotationUtil.getMethodByAnnotation(test, After.class);
- if(method == null) {
- method = _delegate.getTearDownMethod(test);
- }
- return method;
- }
-
- public long toStop(Object testInstance) {
- return 0;
- }
-
- public long toWork(Object testInstance) {
- return 0;
- }
-
- public boolean isLoopTest(Object testInstance) {
- return false;
- }
}
Added: trunk/punit.extension/src/org/punit/listener/FieldSetter.java
===================================================================
--- trunk/punit.extension/src/org/punit/listener/FieldSetter.java (rev 0)
+++ trunk/punit.extension/src/org/punit/listener/FieldSetter.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -0,0 +1,104 @@
+/* (C) Copyright 2007, by Andrew Zhang */
+package org.punit.listener;
+
+import java.lang.reflect.Method;
+import java.util.List;
+import java.util.Properties;
+
+import org.punit.events.EventListener;
+import org.punit.runner.Runner;
+import org.punit.type.TestSuite;
+import org.punit.watcher.Watcher;
+import java.lang.reflect.*;
+import org.punit.util.*;
+
+/**
+ * The listener sets class public fields using properties.
+ */
+public class FieldSetter implements EventListener {
+
+ private static final long serialVersionUID = 5194809928409791777L;
+
+ private final Properties _properties;
+
+ public FieldSetter(Properties properties) {
+ _properties = properties;
+ }
+
+ @SuppressWarnings("unchecked")
+ public void onMethodEnd(Method method, Object testInstance,
+ Object[] params, Throwable t, List Watchers) {
+ }
+
+ public void onClassEnd(Object testInstance, Throwable t) {
+ }
+
+ public void onClassStart(Object testInstance) {
+ setClassFields(testInstance, _properties);
+ }
+
+ public void onMethodStart(Method method, Object testInstance,
+ Object[] params) {
+
+ }
+
+ @SuppressWarnings("unchecked")
+ public void onRunnerEnd(Class clazz, Runner runner) {
+ }
+
+ @SuppressWarnings("unchecked")
+ public void onRunnerStart(Class clazz, Runner runner) {
+ }
+
+ public void onSuiteEnd(TestSuite suite) {
+
+ }
+
+ public void onSuiteStart(TestSuite suite) {
+
+ }
+
+ public void onWatcherEnd(Watcher watcher) {
+
+ }
+
+ public void onWatcherStart(Watcher watcher) {
+
+ }
+
+ @SuppressWarnings("unchecked")
+ public void onWatchersEnd(List watchers) {
+
+ }
+
+ @SuppressWarnings("unchecked")
+ public void onWatchersStart(List watchers) {
+
+ }
+
+ public boolean supportParentRunner() {
+ return false;
+ }
+
+ private final String PACKAGE_SEPARATOR = ".";
+
+ /**
+ * Set public class fields from java properties.
+ *
+ * @param clazz
+ * a class to set fields
+ * @param properites
+ * a set of properties
+ */
+ private void setClassFields(Object testInstance, Properties properties) {
+ for (Field field : testInstance.getClass().getFields()) {
+ String fieldName = PACKAGE_SEPARATOR + field.getName();
+ for (Object key : properties.keySet()) {
+ final String keyName = key.toString();
+ if (fieldName.indexOf(PACKAGE_SEPARATOR + keyName) >= 0) {
+ ReflectionUtil.setField(testInstance, field, properties.getProperty(keyName));
+ }
+ }
+ }
+ }
+}
Property changes on: trunk/punit.extension/src/org/punit/listener/FieldSetter.java
___________________________________________________________________
Name: svn:executable
+ *
Modified: trunk/punit.extension/src/org/punit/reporter/chart/AbstractChartReporter.java
===================================================================
--- trunk/punit.extension/src/org/punit/reporter/chart/AbstractChartReporter.java 2007-07-12 07:08:43 UTC (rev 229)
+++ trunk/punit.extension/src/org/punit/reporter/chart/AbstractChartReporter.java 2007-07-20 13:04:13 UTC (rev 230)
@@ -36,7 +36,7 @@
protected transient RunnerProperties _runnerProperties;
- protected transient Class<?> _currentClass;
+ protected transient Object _testInstance;
public AbstractChartReporter(ChartRender render) {
_render = render;
@@ -180,14 +180,12 @@
return true;
}
- @SuppressWarnings("unchecked")
- public void onClassStart(Class clazz) {
- _currentClass = clazz;
+ public void onClassStart(Object testInstance) {
+ _testInstance = testInstance;
}
- @SuppressWarnings("unchecked")
- public void onClassEnd(Class clazz) {
- _currentClass = null;
+ public void onClassEnd(Object testInstance, Throwable t) {
+ _testInstance = null;
}
public void onSuiteStart(TestSuite suite) {
@@ -223,26 +221,26 @@
}
public void onWatcherStart(Watcher watcher) {
- // nothing needs to do
+ // nothing needs to be done
}
public void onWatcherEnd(Watcher watcher) {
- // nothing needs to do
+ // nothing needs to be done
}
@SuppressWarnings("unchecked")
public void onWatchersStart(List wa...
[truncated message content] |