Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 73206d7a4037833e49d8fa457fcf50f27761269c 1 parent 12526fc
@vasiliygagin authored
Showing with 2,611 additions and 0 deletions.
  1. +3 −0  .gitignore
  2. +4 −0 junitrunner-spring/.gitignore
  3. +1 −0  junitrunner-spring/hsql.cmd
  4. +57 −0 junitrunner-spring/pom.xml
  5. +25 −0 junitrunner-spring/src/main/java/org/junitrunner/spring/SpringContextManagerStatement.java
  6. +150 −0 junitrunner-spring/src/main/java/org/junitrunner/spring/SpringPlugin.java
  7. +23 −0 junitrunner-spring/src/main/java/org/junitrunner/spring/SpringPrepareTestInstanceStatement.java
  8. +22 −0 junitrunner-spring/src/main/java/org/junitrunner/spring/SpringRunner.java
  9. +62 −0 junitrunner-spring/src/test/java/org/junitrunner/spring/MockRunNotifier.java
  10. +31 −0 junitrunner-spring/src/test/java/org/junitrunner/spring/SpringRunnerTest.java
  11. +55 −0 junitrunner-spring/src/test/java/org/junitrunner/spring/Spring_Plugin_Test.java
  12. +52 −0 junitrunner-spring/src/test/java/org/junitrunner/spring/Spring_Runner_Test.java
  13. +3 −0  junitrunner-spring/src/test/resources/db/.gitignore
  14. +4 −0 junitrunner-spring/src/test/resources/db/db.properties
  15. +46 −0 junitrunner-spring/src/test/resources/db/db.script
  16. +17 −0 junitrunner-spring/src/test/resources/spring-context.xml
  17. +4 −0 junitrunner-unitils/.gitignore
  18. +1 −0  junitrunner-unitils/hsql.cmd
  19. +56 −0 junitrunner-unitils/pom.xml
  20. +28 −0 junitrunner-unitils/src/main/java/org/junitrunner/unitils/UnitilsPlugin.java
  21. +23 −0 junitrunner-unitils/src/main/java/org/junitrunner/unitils/UnitilsRunner.java
  22. +30 −0 junitrunner-unitils/src/main/java/org/junitrunner/unitils/UnitilsRunnerListener.java
  23. +30 −0 junitrunner-unitils/src/main/java/org/junitrunner/unitils/UnitilsTestInterceptor.java
  24. +58 −0 junitrunner-unitils/src/test/java/org/junitrunner/unitils/Unitils_Plugin_Test.java
  25. +56 −0 junitrunner-unitils/src/test/java/org/junitrunner/unitils/Unitils_Runner_Test.java
  26. +6 −0 junitrunner-unitils/src/test/resources/UnitilsTest-verify.xml
  27. +6 −0 junitrunner-unitils/src/test/resources/UnitilsTest.xml
  28. +3 −0  junitrunner-unitils/src/test/resources/db/.gitignore
  29. +4 −0 junitrunner-unitils/src/test/resources/db/db.properties
  30. +46 −0 junitrunner-unitils/src/test/resources/db/db.script
  31. +16 −0 junitrunner-unitils/src/test/resources/unitils-context.xml
  32. +8 −0 junitrunner-unitils/src/test/resources/unitils.properties
  33. +4 −0 junitrunner/.gitignore
  34. +34 −0 junitrunner/pom.xml
  35. +40 −0 junitrunner/src/main/java/org/junitrunner/AnnotationHelper.java
  36. +45 −0 junitrunner/src/main/java/org/junitrunner/JUnitBasePlugin.java
  37. +25 −0 junitrunner/src/main/java/org/junitrunner/JUnitPlugin.java
  38. +251 −0 junitrunner/src/main/java/org/junitrunner/JUnitRunner.java
  39. +5 −0 junitrunner/src/main/java/org/junitrunner/JUnitStep.java
  40. +121 −0 junitrunner/src/main/java/org/junitrunner/JUnitSuite.java
  41. +48 −0 junitrunner/src/main/java/org/junitrunner/JUnitTask.java
  42. +34 −0 junitrunner/src/main/java/org/junitrunner/JUnitTest.java
  43. +43 −0 junitrunner/src/main/java/org/junitrunner/RunNotifierWrapper.java
  44. +64 −0 junitrunner/src/main/java/org/junitrunner/TestHelper.java
  45. +6 −0 junitrunner/src/main/java/org/junitrunner/TestInterceptor.java
  46. +42 −0 junitrunner/src/main/java/org/junitrunner/TestObjectFactory.java
  47. +17 −0 junitrunner/src/main/java/org/junitrunner/WithPlugins.java
  48. +21 −0 junitrunner/src/main/java/org/junitrunner/javamethod/AfterMethodStatement.java
  49. +43 −0 junitrunner/src/main/java/org/junitrunner/javamethod/AroundStatement.java
  50. +21 −0 junitrunner/src/main/java/org/junitrunner/javamethod/BeforeMethodStatement.java
  51. +45 −0 junitrunner/src/main/java/org/junitrunner/javamethod/JUnitJavaMethodTest.java
  52. +47 −0 junitrunner/src/main/java/org/junitrunner/listener/BaseRunnerListener.java
  53. +28 −0 junitrunner/src/main/java/org/junitrunner/listener/RunnerListener.java
  54. +100 −0 junitrunner/src/main/java/org/junitrunner/listener/RunnerListenerContainer.java
  55. +29 −0 junitrunner/src/main/java/org/junitrunner/listener/TestInvocationListenerStatement.java
  56. +83 −0 junitrunner/src/main/java/org/junitrunner/plugin/annotation/AnnotationClassRuleFactory.java
  57. +135 −0 junitrunner/src/main/java/org/junitrunner/plugin/annotation/AnnotationPlugin.java
  58. +30 −0 junitrunner/src/main/java/org/junitrunner/plugin/annotation/AnnotationTestFactory.java
  59. +107 −0 junitrunner/src/main/java/org/junitrunner/plugin/annotation/AnnotationTestRuleFactory.java
  60. +10 −0 junitrunner/src/main/java/org/junitrunner/plugin/annotation/ClassRuleFactory.java
  61. +10 −0 junitrunner/src/main/java/org/junitrunner/plugin/annotation/TestFactory.java
  62. +16 −0 junitrunner/src/main/java/org/junitrunner/plugin/annotation/TestRuleFactory.java
  63. +15 −0 junitrunner/src/main/java/org/junitrunner/plugin/annotation/WithClassRuleFactories.java
  64. +17 −0 junitrunner/src/main/java/org/junitrunner/plugin/annotation/WithListeners.java
  65. +15 −0 junitrunner/src/main/java/org/junitrunner/plugin/annotation/WithTestFactories.java
  66. +15 −0 junitrunner/src/main/java/org/junitrunner/plugin/annotation/WithTestRuleFactories.java
  67. +35 −0 junitrunner/src/test/java/org/junitrunner/JUnitRunnerTest.java
  68. +62 −0 junitrunner/src/test/java/org/junitrunner/MockRunNotifier.java
  69. +18 −0 pom.xml
View
3  .gitignore
@@ -0,0 +1,3 @@
+.settings
+.project
+target
View
4 junitrunner-spring/.gitignore
@@ -0,0 +1,4 @@
+.settings
+.classpath
+.project
+target
View
1  junitrunner-spring/hsql.cmd
@@ -0,0 +1 @@
+java -cp C:\dev\artifact-repository\org\hsqldb\hsqldb\2.3.1\hsqldb-2.3.1.jar org.hsqldb.util.DatabaseManagerSwing
View
57 junitrunner-spring/pom.xml
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.junitrunner</groupId>
+ <artifactId>junitrunner-spring</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <name>JUnit Runner</name>
+
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.3.2</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>org.junitrunner</groupId>
+ <artifactId>junitrunner</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-jdbc</artifactId>
+ <version>3.2.3.RELEASE</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-context</artifactId>
+ <version>3.2.3.RELEASE</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-test</artifactId>
+ <version>3.2.3.RELEASE</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.hsqldb</groupId>
+ <artifactId>hsqldb</artifactId>
+ <version>2.3.1</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
View
25 junitrunner-spring/src/main/java/org/junitrunner/spring/SpringContextManagerStatement.java
@@ -0,0 +1,25 @@
+package org.junitrunner.spring;
+
+import org.junit.runners.model.Statement;
+import org.junitrunner.javamethod.AroundStatement;
+import org.springframework.test.context.TestContextManager;
+
+public class SpringContextManagerStatement extends AroundStatement {
+
+ private final TestContextManager testContextManager;
+
+ protected SpringContextManagerStatement(TestContextManager testContextManager, Statement next) {
+ super(next);
+ this.testContextManager = testContextManager;
+ }
+
+ @Override
+ protected void before() throws Throwable {
+ testContextManager.beforeTestClass();
+ }
+
+ @Override
+ protected void after(Throwable failure) throws Throwable {
+ testContextManager.afterTestClass();
+ }
+}
View
150 junitrunner-spring/src/main/java/org/junitrunner/spring/SpringPlugin.java
@@ -0,0 +1,150 @@
+package org.junitrunner.spring;
+
+import java.lang.reflect.Method;
+import java.util.List;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.junit.Test;
+import org.junit.internal.runners.statements.ExpectException;
+import org.junit.internal.runners.statements.FailOnTimeout;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+import org.junitrunner.JUnitBasePlugin;
+import org.junitrunner.JUnitRunner;
+import org.junitrunner.JUnitSuite;
+import org.junitrunner.JUnitTest;
+import org.junitrunner.javamethod.JUnitJavaMethodTest;
+import org.springframework.test.annotation.ProfileValueUtils;
+import org.springframework.test.annotation.Repeat;
+import org.springframework.test.annotation.Timed;
+import org.springframework.test.context.TestContextManager;
+import org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks;
+import org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks;
+import org.springframework.test.context.junit4.statements.SpringFailOnTimeout;
+import org.springframework.test.context.junit4.statements.SpringRepeat;
+
+public class SpringPlugin extends JUnitBasePlugin {
+
+ private static Log logger = LogFactory.getLog(SpringPlugin.class);
+
+ private final Class<?> testClass;
+
+ private TestContextManager testContextManager;
+
+ public SpringPlugin(Class<?> testClass) {
+ this.testClass = testClass;
+ }
+
+ @Override
+ public void plug(JUnitRunner jUnitRunner) {
+
+ testContextManager = new TestContextManager(testClass, null);
+ }
+
+ @Override
+ public Statement enhanceClassStatement(Statement statement, Description description, List<Throwable> errors) {
+
+ return new SpringContextManagerStatement(testContextManager, statement);
+ }
+
+ @Override
+ public Statement enhanceTestCore(JUnitTest test, Object testObject, Statement statement) {
+
+ Test testAnnotation = test.getAnnotation(Test.class);
+ Class<? extends Throwable> junitExpectedException = (testAnnotation != null && testAnnotation.expected() != Test.None.class ? testAnnotation
+ .expected() : null);
+
+ @SuppressWarnings("deprecation")
+ org.springframework.test.annotation.ExpectedException expectedExAnn = test
+ .getAnnotation(org.springframework.test.annotation.ExpectedException.class);
+ @SuppressWarnings("deprecation")
+ Class<? extends Throwable> springExpectedException = (expectedExAnn != null ? expectedExAnn.value() : null);
+
+ if (springExpectedException != null && junitExpectedException != null) {
+ JUnitJavaMethodTest methodTest = (JUnitJavaMethodTest) test;
+ String msg = "Test method [" + methodTest.getMethod() + "] has been configured with Spring's @ExpectedException("
+ + springExpectedException.getName() + ".class) and JUnit's @Test(expected=" + junitExpectedException.getName()
+ + ".class) annotations. " + "Only one declaration of an 'expected exception' is permitted per test method.";
+ logger.error(msg);
+ throw new IllegalStateException(msg);
+ }
+
+ return springExpectedException != null ? new ExpectException(statement, springExpectedException) : statement;
+ }
+
+ @Override
+ public Statement enhanceTest(JUnitTest test, Object testObject, Statement statement) {
+
+ if (test instanceof JUnitJavaMethodTest) {
+ JUnitJavaMethodTest methodTest = (JUnitJavaMethodTest) test;
+
+ statement = new RunBeforeTestMethodCallbacks(statement, testObject, methodTest.getMethod(), testContextManager);
+ statement = new RunAfterTestMethodCallbacks(statement, testObject, methodTest.getMethod(), testContextManager);
+ statement = withPotentialRepeat(methodTest, statement);
+ statement = withPotentialTimeout(methodTest, statement);
+ }
+
+ statement = new SpringPrepareTestInstanceStatement(testContextManager, testObject, statement);
+ return statement;
+ }
+
+ @Override
+ public void configureSuite(JUnitSuite suite, List<Throwable> errors) {
+
+ if (!ProfileValueUtils.isTestEnabledInThisEnvironment(testClass)) {
+ suite.ignore();
+ }
+ }
+
+ @Override
+ public void configureTest(JUnitTest test, List<Throwable> errors) {
+
+ if (test instanceof JUnitJavaMethodTest) {
+ JUnitJavaMethodTest methodTest = (JUnitJavaMethodTest) test;
+ Method method = methodTest.getMethod();
+ if (!ProfileValueUtils.isTestEnabledInThisEnvironment(method, testClass)) {
+ test.ignore();
+ }
+ }
+ }
+
+ private Statement withPotentialRepeat(JUnitJavaMethodTest methodTest, Statement next) {
+
+ Repeat repeatAnnotation = methodTest.getAnnotation(Repeat.class);
+ int repeat = (repeatAnnotation != null ? repeatAnnotation.value() : 1);
+ return new SpringRepeat(next, methodTest.getMethod(), repeat);
+ }
+
+ private Statement withPotentialTimeout(JUnitJavaMethodTest methodTest, Statement next) {
+
+ Statement statement = null;
+ long springTimeout = getSpringTimeout(methodTest);
+ long junitTimeout = getJUnitTimeout(methodTest);
+ if (springTimeout > 0 && junitTimeout > 0) {
+ String msg = "Test method [" + methodTest.getMethod() + "] has been configured with Spring's @Timed(millis=" + springTimeout
+ + ") and JUnit's @Test(timeout=" + junitTimeout
+ + ") annotations. Only one declaration of a 'timeout' is permitted per test method.";
+ logger.error(msg);
+ throw new IllegalStateException(msg);
+ } else if (springTimeout > 0) {
+ statement = new SpringFailOnTimeout(next, springTimeout);
+ } else if (junitTimeout > 0) {
+ statement = new FailOnTimeout(next, junitTimeout);
+ } else {
+ statement = next;
+ }
+
+ return statement;
+ }
+
+ private long getJUnitTimeout(JUnitJavaMethodTest methodTest) {
+ Test testAnnotation = methodTest.getAnnotation(Test.class);
+ return (testAnnotation != null && testAnnotation.timeout() > 0 ? testAnnotation.timeout() : 0);
+ }
+
+ private long getSpringTimeout(JUnitJavaMethodTest methodTest) {
+ Timed timedAnnotation = methodTest.getAnnotation(Timed.class);
+ return (timedAnnotation != null && timedAnnotation.millis() > 0 ? timedAnnotation.millis() : 0);
+ }
+}
View
23 ...unner-spring/src/main/java/org/junitrunner/spring/SpringPrepareTestInstanceStatement.java
@@ -0,0 +1,23 @@
+package org.junitrunner.spring;
+
+import org.junit.runners.model.Statement;
+import org.springframework.test.context.TestContextManager;
+
+public class SpringPrepareTestInstanceStatement extends Statement {
+
+ private final TestContextManager testContextManager;
+ private final Object testObject;
+ private final Statement next;
+
+ public SpringPrepareTestInstanceStatement(TestContextManager testContextManager, Object testObject, Statement next) {
+ this.testContextManager = testContextManager;
+ this.testObject = testObject;
+ this.next = next;
+ }
+
+ @Override
+ public void evaluate() throws Throwable {
+ testContextManager.prepareTestInstance(testObject);
+ next.evaluate();
+ }
+}
View
22 junitrunner-spring/src/main/java/org/junitrunner/spring/SpringRunner.java
@@ -0,0 +1,22 @@
+package org.junitrunner.spring;
+
+import java.util.List;
+
+import org.junit.runners.model.InitializationError;
+import org.junitrunner.JUnitPlugin;
+import org.junitrunner.JUnitRunner;
+
+public class SpringRunner extends JUnitRunner {
+
+ public SpringRunner(Class<?> testClass) throws InitializationError {
+ super(testClass);
+ }
+
+ @Override
+ protected List<JUnitPlugin> discoverPlugins() throws InitializationError {
+
+ List<JUnitPlugin> plugins = super.discoverPlugins();
+ plugins.add(new SpringPlugin(testClass));
+ return plugins;
+ }
+}
View
62 junitrunner-spring/src/test/java/org/junitrunner/spring/MockRunNotifier.java
@@ -0,0 +1,62 @@
+package org.junitrunner.spring;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.runner.Description;
+import org.junit.runner.Result;
+import org.junit.runner.notification.Failure;
+import org.junit.runner.notification.RunNotifier;
+import org.junit.runner.notification.StoppedByUserException;
+
+final class MockRunNotifier extends RunNotifier {
+
+ private final List<String> calls = new ArrayList<String>();
+
+ MockRunNotifier() {
+ }
+
+ public List<String> getCalls() {
+ return calls;
+ }
+
+ @Override
+ public void fireTestRunStarted(Description description) {
+ calls.add("fireTestRunStarted");
+ }
+
+ @Override
+ public void fireTestRunFinished(Result result) {
+ calls.add("fireTestRunFinished");
+ }
+
+ @Override
+ public void fireTestStarted(Description description) throws StoppedByUserException {
+ calls.add("fireTestStarted");
+ }
+
+ @Override
+ public void fireTestFailure(Failure failure) {
+ calls.add("fireTestFailure");
+ }
+
+ @Override
+ public void fireTestAssumptionFailed(Failure failure) {
+ calls.add("fireTestFailure");
+ }
+
+ @Override
+ public void fireTestIgnored(Description description) {
+ calls.add("fireTestIgnored");
+ }
+
+ @Override
+ public void fireTestFinished(Description description) {
+ calls.add("fireTestFinished");
+ }
+
+ @Override
+ public void pleaseStop() {
+ calls.add("pleaseStop");
+ }
+}
View
31 junitrunner-spring/src/test/java/org/junitrunner/spring/SpringRunnerTest.java
@@ -0,0 +1,31 @@
+package org.junitrunner.spring;
+
+import static org.hamcrest.CoreMatchers.*;
+
+import java.util.Arrays;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.springframework.test.annotation.IfProfileValue;
+
+public class SpringRunnerTest {
+
+ @Test
+ public void shouldProcessIfProfileValue() throws Exception {
+
+ System.setProperty("b", "y");
+ SpringRunner unit = new SpringRunner(A.class);
+ MockRunNotifier notifier = new MockRunNotifier();
+ unit.run(notifier);
+
+ Assert.assertThat(notifier.getCalls(), is(Arrays.asList("fireTestIgnored")));
+ }
+
+ @IfProfileValue(name = "b", value="x")
+ public static class A {
+
+ @Test
+ public void x() {
+ }
+ }
+}
View
55 junitrunner-spring/src/test/java/org/junitrunner/spring/Spring_Plugin_Test.java
@@ -0,0 +1,55 @@
+package org.junitrunner.spring;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import javax.annotation.Resource;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junitrunner.JUnitRunner;
+import org.junitrunner.WithPlugins;
+import org.springframework.dao.DataAccessException;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.core.ResultSetExtractor;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.transaction.annotation.Transactional;
+
+@RunWith(JUnitRunner.class)
+@WithPlugins(SpringPlugin.class)
+@ContextConfiguration("/spring-context.xml")
+@Transactional
+@SuppressWarnings("boxing")
+public class Spring_Plugin_Test {
+
+ @Resource
+ private JdbcTemplate jdbcTemplate;
+
+ @Before
+ public void prepareDB() {
+
+ jdbcTemplate.update("insert into PERSON (NAME, AGE) values ('FIRST LAST', 31)");
+ }
+
+ @Test
+ public void canPrepare() {
+
+ jdbcTemplate.query("select * from PERSON", new ResultSetExtractor<Object>() {
+
+ @Override
+ public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
+
+ assertThat(rs.next(), is(true));
+ assertThat(rs.getString("NAME"), is("FIRST LAST"));
+ assertThat(rs.getInt("AGE"), is(31));
+ assertThat(rs.next(), is(false));
+ return null;
+ }
+
+ });
+ }
+}
View
52 junitrunner-spring/src/test/java/org/junitrunner/spring/Spring_Runner_Test.java
@@ -0,0 +1,52 @@
+package org.junitrunner.spring;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import javax.annotation.Resource;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.dao.DataAccessException;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.core.ResultSetExtractor;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.transaction.annotation.Transactional;
+
+@RunWith(SpringRunner.class)
+@ContextConfiguration("/spring-context.xml")
+@Transactional
+@SuppressWarnings("boxing")
+public class Spring_Runner_Test {
+
+ @Resource
+ private JdbcTemplate jdbcTemplate;
+
+ @Before
+ public void prepareDB() {
+
+ jdbcTemplate.update("insert into PERSON (NAME, AGE) values ('FIRST LAST', 31)");
+ }
+
+ @Test
+ public void canPrepare() {
+
+ jdbcTemplate.query("select * from PERSON", new ResultSetExtractor<Object>() {
+
+ @Override
+ public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
+
+ assertThat(rs.next(), is(true));
+ assertThat(rs.getString("NAME"), is("FIRST LAST"));
+ assertThat(rs.getInt("AGE"), is(31));
+ assertThat(rs.next(), is(false));
+ return null;
+ }
+
+ });
+ }
+}
View
3  junitrunner-spring/src/test/resources/db/.gitignore
@@ -0,0 +1,3 @@
+db.tmp
+db.lck
+db.log
View
4 junitrunner-spring/src/test/resources/db/db.properties
@@ -0,0 +1,4 @@
+#HSQL Database Engine 2.3.0
+#Thu Feb 06 09:52:33 EST 2014
+version=2.3.0
+modified=yes
View
46 junitrunner-spring/src/test/resources/db/db.script
@@ -0,0 +1,46 @@
+SET DATABASE UNIQUE NAME HSQLDB43F3699038
+SET DATABASE GC 0
+SET DATABASE DEFAULT RESULT MEMORY ROWS 0
+SET DATABASE EVENT LOG LEVEL 0
+SET DATABASE SQL NAMES FALSE
+SET DATABASE SQL REFERENCES FALSE
+SET DATABASE SQL SIZE TRUE
+SET DATABASE SQL TYPES FALSE
+SET DATABASE SQL TDC DELETE TRUE
+SET DATABASE SQL TDC UPDATE TRUE
+SET DATABASE SQL TRANSLATE TTI TYPES TRUE
+SET DATABASE SQL CONCAT NULLS TRUE
+SET DATABASE SQL UNIQUE NULLS TRUE
+SET DATABASE SQL CONVERT TRUNCATE TRUE
+SET DATABASE SQL AVG SCALE 0
+SET DATABASE SQL DOUBLE NAN TRUE
+SET DATABASE TRANSACTION CONTROL LOCKS
+SET DATABASE DEFAULT ISOLATION LEVEL READ COMMITTED
+SET DATABASE TRANSACTION ROLLBACK ON CONFLICT TRUE
+SET DATABASE TEXT TABLE DEFAULTS ''
+SET FILES WRITE DELAY 500 MILLIS
+SET FILES BACKUP INCREMENT TRUE
+SET FILES CACHE SIZE 10000
+SET FILES CACHE ROWS 50000
+SET FILES SCALE 32
+SET FILES LOB SCALE 32
+SET FILES DEFRAG 0
+SET FILES NIO TRUE
+SET FILES NIO SIZE 256
+SET FILES LOG TRUE
+SET FILES LOG SIZE 50
+CREATE USER SA PASSWORD DIGEST 'd41d8cd98f00b204e9800998ecf8427e'
+ALTER USER SA SET LOCAL TRUE
+CREATE SCHEMA PUBLIC AUTHORIZATION DBA
+SET SCHEMA PUBLIC
+CREATE MEMORY TABLE PUBLIC.PERSON(NAME VARCHAR(100) PRIMARY KEY,AGE INTEGER)
+ALTER SEQUENCE SYSTEM_LOBS.LOB_ID RESTART WITH 1
+SET DATABASE DEFAULT INITIAL SCHEMA PUBLIC
+GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.SQL_IDENTIFIER TO PUBLIC
+GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.YES_OR_NO TO PUBLIC
+GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.TIME_STAMP TO PUBLIC
+GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.CARDINAL_NUMBER TO PUBLIC
+GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.CHARACTER_DATA TO PUBLIC
+GRANT DBA TO SA
+SET SCHEMA SYSTEM_LOBS
+INSERT INTO BLOCKS VALUES(0,2147483647,0)
View
17 junitrunner-spring/src/test/resources/spring-context.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
+ xsi:schemaLocation="
+ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+ "
+ default-lazy-init="true">
+
+ <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="org.hsqldb.jdbc.JDBCDriver"
+ p:url="jdbc:hsqldb:file:src/test/resources/db/db" p:username="SA" p:password="" />
+
+ <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource" />
+
+ <bean id="transactionManager" name="org.springframework.jdbc.datasource.DataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
+ <property name="dataSource" ref="dataSource" />
+ </bean>
+
+</beans>
View
4 junitrunner-unitils/.gitignore
@@ -0,0 +1,4 @@
+.settings
+.classpath
+.project
+target
View
1  junitrunner-unitils/hsql.cmd
@@ -0,0 +1 @@
+java -cp C:\dev\artifact-repository\org\hsqldb\hsqldb\2.3.1\hsqldb-2.3.1.jar org.hsqldb.util.DatabaseManagerSwing
View
56 junitrunner-unitils/pom.xml
@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.junitrunner</groupId>
+ <artifactId>junitrunner-unitils</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <name>JUnit Runner</name>
+
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.3.2</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>org.junitrunner</groupId>
+ <artifactId>junitrunner</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-jdbc</artifactId>
+ <version>3.2.3.RELEASE</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.unitils</groupId>
+ <artifactId>unitils-dbunit</artifactId>
+ <version>3.3</version>
+ </dependency>
+ <dependency>
+ <groupId>org.unitils</groupId>
+ <artifactId>unitils-spring</artifactId>
+ <version>3.3</version>
+ </dependency>
+ <dependency>
+ <groupId>org.hsqldb</groupId>
+ <artifactId>hsqldb</artifactId>
+ <version>2.3.1</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
View
28 junitrunner-unitils/src/main/java/org/junitrunner/unitils/UnitilsPlugin.java
@@ -0,0 +1,28 @@
+package org.junitrunner.unitils;
+
+import org.junit.runners.model.Statement;
+import org.junitrunner.JUnitBasePlugin;
+import org.junitrunner.JUnitRunner;
+import org.junitrunner.JUnitTest;
+import org.junitrunner.javamethod.JUnitJavaMethodTest;
+
+public class UnitilsPlugin extends JUnitBasePlugin {
+
+ public UnitilsPlugin(@SuppressWarnings("unused") Class<?> testClass) {
+ }
+
+ @Override
+ public void plug(JUnitRunner jUnitRunner) {
+
+ jUnitRunner.registerListener(new UnitilsRunnerListener());
+ }
+
+ @Override
+ public Statement enhanceTest(JUnitTest test, Object testObject, Statement statement) {
+ if (test instanceof JUnitJavaMethodTest) {
+ return new UnitilsTestInterceptor((JUnitJavaMethodTest) test, testObject, statement);
+ } else {
+ return statement;
+ }
+ }
+}
View
23 junitrunner-unitils/src/main/java/org/junitrunner/unitils/UnitilsRunner.java
@@ -0,0 +1,23 @@
+package org.junitrunner.unitils;
+
+import java.util.List;
+
+import org.junit.runners.model.InitializationError;
+import org.junitrunner.JUnitPlugin;
+import org.junitrunner.JUnitRunner;
+
+public class UnitilsRunner extends JUnitRunner {
+
+ public UnitilsRunner(Class<?> testClass) throws Exception {
+ super(testClass);
+
+ registerListener(new UnitilsRunnerListener());
+ }
+
+ @Override
+ protected List<JUnitPlugin> discoverPlugins() throws InitializationError {
+ List<JUnitPlugin> plugins = super.discoverPlugins();
+ plugins.add(new UnitilsPlugin(testClass));
+ return plugins;
+ }
+}
View
30 junitrunner-unitils/src/main/java/org/junitrunner/unitils/UnitilsRunnerListener.java
@@ -0,0 +1,30 @@
+package org.junitrunner.unitils;
+
+import java.lang.reflect.Method;
+
+import org.junitrunner.JUnitTask;
+import org.junitrunner.javamethod.JUnitJavaMethodTest;
+import org.junitrunner.listener.BaseRunnerListener;
+import org.unitils.core.TestListener;
+import org.unitils.core.Unitils;
+
+public class UnitilsRunnerListener extends BaseRunnerListener {
+
+ private TestListener testListener = Unitils.getInstance().getTestListener();
+
+ @Override
+ public void testCoreStarted(JUnitTask test, Object testObject) {
+ if (test instanceof JUnitJavaMethodTest) {
+ Method method = ((JUnitJavaMethodTest) test).getMethod();
+ testListener.beforeTestMethod(testObject, method);
+ }
+ }
+
+ @Override
+ public void testCoreFinished(JUnitTask test, Object testObject, Throwable error) {
+ if (test instanceof JUnitJavaMethodTest) {
+ Method method = ((JUnitJavaMethodTest) test).getMethod();
+ testListener.afterTestMethod(testObject, method, error);
+ }
+ }
+}
View
30 junitrunner-unitils/src/main/java/org/junitrunner/unitils/UnitilsTestInterceptor.java
@@ -0,0 +1,30 @@
+package org.junitrunner.unitils;
+
+import org.junit.runners.model.Statement;
+import org.junitrunner.javamethod.AroundStatement;
+import org.junitrunner.javamethod.JUnitJavaMethodTest;
+import org.unitils.core.TestListener;
+import org.unitils.core.Unitils;
+
+public class UnitilsTestInterceptor extends AroundStatement {
+
+ private final JUnitJavaMethodTest test;
+ private final Object testObject;
+ private TestListener testListener = Unitils.getInstance().getTestListener();
+
+ public UnitilsTestInterceptor(JUnitJavaMethodTest test, Object testObject, Statement statement) {
+ super(statement);
+ this.test = test;
+ this.testObject = testObject;
+ }
+
+ @Override
+ protected void before() throws Throwable {
+ testListener.beforeTestSetUp(testObject, test.getMethod());
+ }
+
+ @Override
+ protected void after(Throwable failure) throws Throwable {
+ testListener.afterTestTearDown(testObject, test.getMethod());
+ }
+}
View
58 junitrunner-unitils/src/test/java/org/junitrunner/unitils/Unitils_Plugin_Test.java
@@ -0,0 +1,58 @@
+package org.junitrunner.unitils;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junitrunner.JUnitRunner;
+import org.junitrunner.WithPlugins;
+import org.springframework.dao.DataAccessException;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.core.ResultSetExtractor;
+import org.unitils.database.annotations.Transactional;
+import org.unitils.database.util.TransactionMode;
+import org.unitils.dbunit.annotation.DataSet;
+import org.unitils.dbunit.annotation.ExpectedDataSet;
+import org.unitils.spring.annotation.SpringApplicationContext;
+import org.unitils.spring.annotation.SpringBeanByName;
+
+@RunWith(JUnitRunner.class)
+@WithPlugins({ UnitilsPlugin.class })
+@SpringApplicationContext({ "/unitils-context.xml" })
+@DataSet("/UnitilsTest.xml")
+@Transactional(TransactionMode.ROLLBACK)
+@SuppressWarnings("boxing")
+public class Unitils_Plugin_Test {
+
+ @SpringBeanByName
+ private JdbcTemplate jdbcTemplate;
+
+ @Test
+ public void canPrepare() {
+
+ jdbcTemplate.query("select * from PERSON", new ResultSetExtractor<Object>() {
+
+ @Override
+ public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
+
+ assertThat(rs.next(), is(true));
+ assertThat(rs.getString("NAME"), is("FIRST LAST"));
+ assertThat(rs.getInt("AGE"), is(31));
+ assertThat(rs.next(), is(false));
+ return null;
+ }
+
+ });
+ }
+
+ @Test
+ @ExpectedDataSet("/UnitilsTest-verify.xml")
+ public void canVerify() throws Exception {
+
+ jdbcTemplate.update("insert into PERSON (NAME, AGE) values (?,?)", new Object[] { "X", 1 });
+ }
+}
View
56 junitrunner-unitils/src/test/java/org/junitrunner/unitils/Unitils_Runner_Test.java
@@ -0,0 +1,56 @@
+package org.junitrunner.unitils;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.*;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junitrunner.unitils.UnitilsRunner;
+import org.springframework.dao.DataAccessException;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.core.ResultSetExtractor;
+import org.unitils.database.annotations.Transactional;
+import org.unitils.database.util.TransactionMode;
+import org.unitils.dbunit.annotation.DataSet;
+import org.unitils.dbunit.annotation.ExpectedDataSet;
+import org.unitils.spring.annotation.SpringApplicationContext;
+import org.unitils.spring.annotation.SpringBeanByName;
+
+@RunWith(UnitilsRunner.class)
+@SpringApplicationContext({ "/unitils-context.xml" })
+@DataSet("/UnitilsTest.xml")
+@Transactional(TransactionMode.ROLLBACK)
+@SuppressWarnings("boxing")
+public class Unitils_Runner_Test {
+
+ @SpringBeanByName
+ private JdbcTemplate jdbcTemplate;
+
+ @Test
+ public void canPrepare() {
+
+ jdbcTemplate.query("select * from PERSON", new ResultSetExtractor<Object>() {
+
+ @Override
+ public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
+
+ assertThat(rs.next(), is(true));
+ assertThat(rs.getString("NAME"), is("FIRST LAST"));
+ assertThat(rs.getInt("AGE"), is(31));
+ assertThat(rs.next(), is(false));
+ return null;
+ }
+
+ });
+ }
+
+ @Test
+ @ExpectedDataSet("/UnitilsTest-verify.xml")
+ public void canVerify() throws Exception {
+
+ jdbcTemplate.update("insert into PERSON (NAME, AGE) values (?,?)", new Object[] {"X", 1});
+ }
+}
View
6 junitrunner-unitils/src/test/resources/UnitilsTest-verify.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<dataset>
+
+ <PERSON NAME="X" AGE="1" />
+
+</dataset>
View
6 junitrunner-unitils/src/test/resources/UnitilsTest.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<dataset>
+
+ <PERSON NAME="FIRST LAST" AGE="31" />
+
+</dataset>
View
3  junitrunner-unitils/src/test/resources/db/.gitignore
@@ -0,0 +1,3 @@
+db.tmp
+db.lck
+db.log
View
4 junitrunner-unitils/src/test/resources/db/db.properties
@@ -0,0 +1,4 @@
+#HSQL Database Engine 2.3.0
+#Thu Feb 06 09:52:35 EST 2014
+version=2.3.0
+modified=yes
View
46 junitrunner-unitils/src/test/resources/db/db.script
@@ -0,0 +1,46 @@
+SET DATABASE UNIQUE NAME HSQLDB43F3699038
+SET DATABASE GC 0
+SET DATABASE DEFAULT RESULT MEMORY ROWS 0
+SET DATABASE EVENT LOG LEVEL 0
+SET DATABASE SQL NAMES FALSE
+SET DATABASE SQL REFERENCES FALSE
+SET DATABASE SQL SIZE TRUE
+SET DATABASE SQL TYPES FALSE
+SET DATABASE SQL TDC DELETE TRUE
+SET DATABASE SQL TDC UPDATE TRUE
+SET DATABASE SQL TRANSLATE TTI TYPES TRUE
+SET DATABASE SQL CONCAT NULLS TRUE
+SET DATABASE SQL UNIQUE NULLS TRUE
+SET DATABASE SQL CONVERT TRUNCATE TRUE
+SET DATABASE SQL AVG SCALE 0
+SET DATABASE SQL DOUBLE NAN TRUE
+SET DATABASE TRANSACTION CONTROL LOCKS
+SET DATABASE DEFAULT ISOLATION LEVEL READ COMMITTED
+SET DATABASE TRANSACTION ROLLBACK ON CONFLICT TRUE
+SET DATABASE TEXT TABLE DEFAULTS ''
+SET FILES WRITE DELAY 500 MILLIS
+SET FILES BACKUP INCREMENT TRUE
+SET FILES CACHE SIZE 10000
+SET FILES CACHE ROWS 50000
+SET FILES SCALE 32
+SET FILES LOB SCALE 32
+SET FILES DEFRAG 0
+SET FILES NIO TRUE
+SET FILES NIO SIZE 256
+SET FILES LOG TRUE
+SET FILES LOG SIZE 50
+CREATE USER SA PASSWORD DIGEST 'd41d8cd98f00b204e9800998ecf8427e'
+ALTER USER SA SET LOCAL TRUE
+CREATE SCHEMA PUBLIC AUTHORIZATION DBA
+SET SCHEMA PUBLIC
+CREATE MEMORY TABLE PUBLIC.PERSON(NAME VARCHAR(100) PRIMARY KEY,AGE INTEGER)
+ALTER SEQUENCE SYSTEM_LOBS.LOB_ID RESTART WITH 1
+SET DATABASE DEFAULT INITIAL SCHEMA PUBLIC
+GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.SQL_IDENTIFIER TO PUBLIC
+GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.YES_OR_NO TO PUBLIC
+GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.TIME_STAMP TO PUBLIC
+GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.CARDINAL_NUMBER TO PUBLIC
+GRANT USAGE ON DOMAIN INFORMATION_SCHEMA.CHARACTER_DATA TO PUBLIC
+GRANT DBA TO SA
+SET SCHEMA SYSTEM_LOBS
+INSERT INTO BLOCKS VALUES(0,2147483647,0)
View
16 junitrunner-unitils/src/test/resources/unitils-context.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
+ xsi:schemaLocation="
+ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+ "
+ default-lazy-init="true">
+
+ <bean id="dataSource" class="org.unitils.database.UnitilsDataSourceFactoryBean" />
+
+ <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource" />
+
+ <bean id="transactionManager" name="org.springframework.jdbc.datasource.DataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
+ <property name="dataSource" ref="dataSource" />
+ </bean>
+
+</beans>
View
8 junitrunner-unitils/src/test/resources/unitils.properties
@@ -0,0 +1,8 @@
+database.driverClassName=org.hsqldb.jdbc.JDBCDriver
+database.url=jdbc:hsqldb:file:src/test/resources/db/db
+database.userName=SA
+database.password=
+
+database.dialect=hsqldb
+database.schemaNames=PUBLIC
+DbUnitModule.DataSet.loadStrategy.default=org.unitils.dbunit.datasetloadstrategy.impl.RefreshLoadStrategy
View
4 junitrunner/.gitignore
@@ -0,0 +1,4 @@
+.settings
+.classpath
+.project
+target
View
34 junitrunner/pom.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.junitrunner</groupId>
+ <artifactId>junitrunner</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <name>JUnit Runner</name>
+
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.3.2</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.11</version>
+ </dependency>
+
+ </dependencies>
+
+</project>
View
40 junitrunner/src/main/java/org/junitrunner/AnnotationHelper.java
@@ -0,0 +1,40 @@
+package org.junitrunner;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class AnnotationHelper {
+
+ public static <T> T createObject(Class<? extends T> cls, Class<?> arg) {
+
+ try {
+ return cls.getConstructor(Class.class).newInstance(arg);
+ } catch (Exception exc) {
+ throw new RuntimeException(exc);
+ }
+ }
+
+ static <T> T createObject(Class<? extends T> cls) {
+
+ try {
+ return cls.getConstructor().newInstance();
+ } catch (Exception exc) {
+ throw new RuntimeException(exc);
+ }
+ }
+
+ public static <T> List<T> constructInstances(Class<? extends T>[] classes, Class<? extends T> def) {
+
+ List<T> result = new ArrayList<T>();
+ if (classes == null || classes.length == 0) {
+ if (def != null) {
+ result.add(createObject(def));
+ }
+ } else {
+ for (Class<? extends T> cls : classes) {
+ result.add(createObject(cls));
+ }
+ }
+ return result;
+ }
+}
View
45 junitrunner/src/main/java/org/junitrunner/JUnitBasePlugin.java
@@ -0,0 +1,45 @@
+package org.junitrunner;
+
+import java.util.List;
+
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+public abstract class JUnitBasePlugin implements JUnitPlugin {
+
+ @Override
+ public void plug(JUnitRunner jUnitRunner) {
+ }
+
+ @Override
+ public Statement enhanceClassStatement(Statement statement, Description description, List<Throwable> errors) {
+ return statement;
+ }
+
+ @Override
+ public void validateTestRules(List<Throwable> errors) {
+ }
+
+ @Override
+ public List<JUnitTask> createTasks(List<Throwable> errors) {
+ return null;
+ }
+
+ @Override
+ public Statement enhanceTestCore(JUnitTest test, Object testObject, Statement statement) {
+ return statement;
+ }
+
+ @Override
+ public Statement enhanceTest(JUnitTest test, Object testObject, Statement statement) {
+ return statement;
+ }
+
+ @Override
+ public void configureSuite(JUnitSuite suite, List<Throwable> errors) {
+ }
+
+ @Override
+ public void configureTest(JUnitTest test, List<Throwable> errors) {
+ }
+}
View
25 junitrunner/src/main/java/org/junitrunner/JUnitPlugin.java
@@ -0,0 +1,25 @@
+package org.junitrunner;
+
+import java.util.List;
+
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+public interface JUnitPlugin {
+
+ void plug(JUnitRunner jUnitRunner);
+
+ Statement enhanceClassStatement(Statement statement, Description description, List<Throwable> errors);
+
+ void validateTestRules(List<Throwable> errors);
+
+ List<JUnitTask> createTasks(List<Throwable> errors);
+
+ Statement enhanceTestCore(JUnitTest test, Object testObject, Statement statement);
+
+ Statement enhanceTest(JUnitTest test, Object testObject, Statement statement);
+
+ void configureSuite(JUnitSuite suite, List<Throwable> errors);
+
+ void configureTest(JUnitTest test, List<Throwable> errors);
+}
View
251 junitrunner/src/main/java/org/junitrunner/JUnitRunner.java
@@ -0,0 +1,251 @@
+package org.junitrunner;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+
+import org.junit.internal.runners.statements.Fail;
+import org.junit.runner.Description;
+import org.junit.runner.Runner;
+import org.junit.runner.manipulation.Filter;
+import org.junit.runner.manipulation.Filterable;
+import org.junit.runner.manipulation.NoTestsRemainException;
+import org.junit.runner.manipulation.Sortable;
+import org.junit.runner.manipulation.Sorter;
+import org.junit.runner.notification.Failure;
+import org.junit.runner.notification.RunNotifier;
+import org.junit.runner.notification.StoppedByUserException;
+import org.junit.runners.model.InitializationError;
+import org.junit.runners.model.Statement;
+import org.junitrunner.listener.RunnerListener;
+import org.junitrunner.listener.RunnerListenerContainer;
+import org.junitrunner.listener.TestInvocationListenerStatement;
+import org.junitrunner.plugin.annotation.AnnotationPlugin;
+
+public class JUnitRunner extends Runner implements Filterable, Sortable {
+
+ protected final Class<?> testClass;
+
+ private List<JUnitPlugin> plugins;
+ private JUnitSuite suite;
+ private TestObjectFactory testObjectFactory;
+ final RunnerListenerContainer listenerContainer = new RunnerListenerContainer();
+ private Statement classStatement;
+
+ public JUnitRunner(Class<?> testClass) throws InitializationError {
+
+ this.testClass = testClass;
+
+ suite = new JUnitSuite(testClass);
+
+ List<Throwable> errors = new ArrayList<Throwable>();
+
+ processPlugins();
+
+ testObjectFactory = new TestObjectFactory(testClass);
+
+ computeClassStatement(errors);
+ validateTestRules(errors);
+ discoverTests(errors);
+ configureTestSuite(errors);
+
+ if (suite.isEmpty()) {
+ errors.add(new Exception("No runnable methods"));
+ }
+
+ if (!errors.isEmpty()) {
+ throw new InitializationError(errors);
+ }
+ }
+
+ public JUnitSuite getSuite() {
+ return suite;
+ }
+
+ private void processPlugins() throws InitializationError {
+
+ plugins = discoverPlugins();
+ for (JUnitPlugin plugin : plugins) {
+ plugin.plug(this);
+ }
+ }
+
+ protected List<JUnitPlugin> discoverPlugins() throws InitializationError {
+
+ WithPlugins pluginsAnnotation = testClass.getAnnotation(WithPlugins.class);
+
+ List<JUnitPlugin> result = new ArrayList<JUnitPlugin>();
+
+ if (pluginsAnnotation != null) {
+ List<Class<? extends JUnitPlugin>> pluginClasses = getPluginClasses(pluginsAnnotation);
+ for (Class<? extends JUnitPlugin> cls : pluginClasses) {
+ result.add(AnnotationHelper.createObject(cls, testClass));
+ }
+ } else {
+ result.add(AnnotationHelper.createObject(AnnotationPlugin.class, testClass));
+ }
+
+ return result;
+ }
+
+ List<Class<? extends JUnitPlugin>> getPluginClasses(WithPlugins pluginsAnnotation) throws InitializationError {
+
+ Class<? extends JUnitPlugin>[] pluginClasses = pluginsAnnotation.value();
+ if (pluginClasses == null || pluginClasses.length == 0) {
+ throw new InitializationError(WithPlugins.class.getSimpleName() + " annotation specifies no plugins");
+ }
+
+ List<Class<? extends JUnitPlugin>> result = new ArrayList<Class<? extends JUnitPlugin>>();
+
+ if (!pluginsAnnotation.skipDefault()) {
+ result.add(AnnotationPlugin.class);
+ }
+
+ for (Class<? extends JUnitPlugin> cls : pluginClasses) {
+ if (result.contains(cls)) {
+ throw new InitializationError("Plugin type " + cls + " registered twice");
+ }
+ result.add(cls);
+ }
+ return result;
+ }
+
+ private void computeClassStatement(List<Throwable> errors) {
+
+ Description description = getDescription();
+
+ Statement statement = suite.constructInvokeStatement(this);
+
+ for (JUnitPlugin plugin : plugins) {
+ statement = plugin.enhanceClassStatement(statement, description, errors);
+ }
+
+ classStatement = statement;
+ }
+
+ private void configureTestSuite(List<Throwable> errors) {
+
+ for (JUnitPlugin plugin : plugins) {
+ suite.configureTask(testClass, plugin, errors);
+ }
+ }
+
+ private void discoverTests(List<Throwable> errors) {
+
+ for (JUnitPlugin plugin : plugins) {
+ List<JUnitTask> tasks = plugin.createTasks(errors);
+ if (tasks != null) {
+ for (JUnitTask task : tasks) {
+ suite.addTask(task);
+ }
+ }
+ }
+ }
+
+ private void validateTestRules(List<Throwable> errors) {
+
+ for (JUnitPlugin plugin : plugins) {
+ plugin.validateTestRules(errors);
+ }
+ }
+
+ public void registerListener(RunnerListener listener) {
+ listenerContainer.addListener(listener);
+ }
+
+ public void registerListeners(List<RunnerListener> listeners) {
+ for (RunnerListener listener : listeners) {
+ listenerContainer.addListener(listener);
+ }
+ }
+
+ @Override
+ public void run(final RunNotifier notifier) {
+
+ registerListener(new RunNotifierWrapper(notifier));
+
+ if (suite.isIgnored()) {
+ listenerContainer.taskIgnored(suite);
+ return;
+ }
+
+ Description description = getDescription();
+ listenerContainer.testClassStarted(description);
+
+ try {
+
+ classStatement.evaluate();
+ } catch (StoppedByUserException e) {
+ throw e;
+ } catch (Throwable e) {
+ notifier.fireTestFailure(new Failure(description, e));
+ } finally {
+ listenerContainer.testClassFinished(description);
+ }
+ }
+
+ protected Statement buildTestParts(final JUnitTest test, Object testObject, Statement statement) {
+
+ for (JUnitPlugin plugin : plugins) {
+ statement = plugin.enhanceTestCore(test, testObject, statement);
+ }
+ return statement;
+ }
+
+ public Statement buildTestInteceptors(JUnitTest test, final Object testObject, Statement statement) {
+ for (JUnitPlugin plugin : plugins) {
+ statement = plugin.enhanceTest(test, testObject, statement);
+ }
+ return statement;
+ }
+
+ @Override
+ public Description getDescription() {
+ return suite.describe();
+ }
+
+ public void filter(Filter filter) throws NoTestsRemainException {
+
+ suite.filterTask(filter);
+ if (suite.isFilteredOut()) {
+ throw new NoTestsRemainException();
+ }
+ }
+
+ public void sort(final Sorter sorter) {
+
+ Comparator<JUnitTask> descriptionComparator = new Comparator<JUnitTask>() {
+ public int compare(JUnitTask o1, JUnitTask o2) {
+ return sorter.compare(o1.describe(), o2.describe());
+ }
+ };
+
+ suite.sortSuite(descriptionComparator);
+ }
+
+ protected final Statement constructTestStatement(JUnitTest test) {
+
+ Statement statement;
+ try {
+ Object testObject = testObjectFactory.constructTestObject();
+ statement = test.constructExecuteStatement(testObject);
+ statement = buildTestParts(test, testObject, statement);
+ statement = new TestInvocationListenerStatement(test, statement, testObject, listenerContainer);
+ statement = buildTestInteceptors(test, testObject, statement);
+ } catch (Throwable e) {
+ statement = new Fail(e);
+ }
+ return statement;
+ }
+
+ protected Statement constructSuiteStatement(final JUnitSuite suite) {
+
+ return new Statement() {
+
+ @Override
+ public void evaluate() throws Throwable {
+ suite.invoke(JUnitRunner.this);
+ }
+ };
+ }
+}
View
5 junitrunner/src/main/java/org/junitrunner/JUnitStep.java
@@ -0,0 +1,5 @@
+package org.junitrunner;
+
+public class JUnitStep {
+
+}
View
121 junitrunner/src/main/java/org/junitrunner/JUnitSuite.java
@@ -0,0 +1,121 @@
+package org.junitrunner;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import org.junit.runner.Description;
+import org.junit.runner.manipulation.Filter;
+import org.junit.runners.model.Statement;
+
+public class JUnitSuite extends JUnitTask {
+
+ private final Class<?> testClass;
+ private List<JUnitTask> children = new ArrayList<JUnitTask>();
+
+ public JUnitSuite(Class<?> testClass) {
+ this.testClass = testClass;
+ }
+
+ void addTask(JUnitTask task) {
+ children.add(task);
+ }
+
+ public boolean isEmpty() {
+ return children.isEmpty();
+ }
+
+ @Override
+ public Description describe() {
+
+ Description description = Description.createSuiteDescription(testClass.getName(), testClass.getAnnotations());
+ for (JUnitTask test : children) {
+ if (test.isFilteredOut()) {
+ continue;
+ }
+ description.addChild(test.describe());
+ }
+ return description;
+ }
+
+ protected void invoke(JUnitRunner jUnitRunner) throws Throwable {
+
+ boolean hasFailures = false;
+
+ for (JUnitTask task : children) {
+ if (task.isFilteredOut()) {
+ continue;
+ }
+ if (task.isIgnored()) {
+ jUnitRunner.listenerContainer.taskIgnored(task);
+ } else {
+ Statement statement = task.constructInvokeStatement(jUnitRunner);
+ jUnitRunner.listenerContainer.taskStarted(task);
+ try {
+ statement.evaluate();
+ jUnitRunner.listenerContainer.taskFinished(task);
+ } catch (Throwable exc) {
+ jUnitRunner.listenerContainer.taskFailed(task, exc);
+ hasFailures = true;
+ }
+ }
+ }
+
+ if (hasFailures) {
+ throw new Exception("Some tests failed");
+ }
+ }
+
+ protected final Statement constructInvokeStatement(JUnitRunner jUnitRunner) {
+ return jUnitRunner.constructSuiteStatement(this);
+ }
+
+ private boolean allTestsFilteredOut() {
+
+ boolean allTestsFilteredOut = true;
+ for (JUnitTask task : children) {
+ if (!task.isFilteredOut()) {
+ allTestsFilteredOut = false;
+ break;
+ }
+ }
+ return allTestsFilteredOut;
+ }
+
+ @Override
+ protected void filterTask(Filter filter) {
+
+ super.filterTask(filter);
+
+ for (JUnitTask task : children) {
+ if (!task.isFilteredOut()) {
+ task.filterTask(filter);
+ }
+ }
+
+ if (allTestsFilteredOut()) {
+ filterOut();
+ }
+ }
+
+ void sortSuite(Comparator<JUnitTask> comparator) {
+
+ for (JUnitTask task : children) {
+ if (task instanceof JUnitSuite) {
+ JUnitSuite suite = (JUnitSuite) task;
+ suite.sortSuite(comparator);
+ }
+ }
+
+ Collections.sort(children, comparator);
+ }
+
+ @Override
+ protected void configureTask(Class<?> testClass, JUnitPlugin plugin, List<Throwable> errors) {
+ plugin.configureSuite(this, errors);
+ for (JUnitTask child : children) {
+ child.configureTask(testClass, plugin, errors);
+ }
+ }
+}
View
48 junitrunner/src/main/java/org/junitrunner/JUnitTask.java
@@ -0,0 +1,48 @@
+package org.junitrunner;
+
+import java.util.List;
+
+import org.junit.runner.Description;
+import org.junit.runner.manipulation.Filter;
+import org.junit.runners.model.Statement;
+
+public abstract class JUnitTask {
+
+ private boolean ignored;
+ private boolean filteredOut;
+
+ protected JUnitTask() {
+ }
+
+ public boolean isIgnored() {
+ return ignored;
+ }
+
+ public void ignore() {
+ ignored = true;
+ }
+
+ public boolean isFilteredOut() {
+ return filteredOut;
+ }
+
+ protected void filterOut() {
+ filteredOut = true;
+ }
+
+ public abstract Description describe();
+
+ protected abstract Statement constructInvokeStatement(JUnitRunner jUnitRunner);
+
+ protected void filterTask(Filter filter) {
+
+ Description description = describe();
+ if (!filter.shouldRun(description)) {
+ filteredOut = true;
+ }
+ }
+
+ @SuppressWarnings("unused")
+ protected void configureTask(Class<?> testClass, JUnitPlugin plugin, List<Throwable> errors) {
+ }
+}
View
34 junitrunner/src/main/java/org/junitrunner/JUnitTest.java
@@ -0,0 +1,34 @@
+package org.junitrunner;
+
+import java.lang.annotation.Annotation;
+import java.util.List;
+
+import org.junit.runners.model.Statement;
+
+public abstract class JUnitTest extends JUnitTask {
+
+ public <T extends Annotation> T getAnnotation(@SuppressWarnings("unused") Class<T> cls) {
+ return null;
+ }
+
+ protected abstract void invoke(final Object testObject) throws Throwable;
+
+ protected final Statement constructInvokeStatement(JUnitRunner jUnitRunner) {
+ return jUnitRunner.constructTestStatement(this);
+ }
+
+ public Statement constructExecuteStatement(final Object testObject) {
+ return new Statement() {
+
+ @Override
+ public void evaluate() throws Throwable {
+ invoke(testObject);
+ }
+ };
+ }
+
+ @Override
+ protected void configureTask(Class<?> testClass, JUnitPlugin plugin, List<Throwable> errors) {
+ plugin.configureTest(this, errors);
+ }
+}
View
43 junitrunner/src/main/java/org/junitrunner/RunNotifierWrapper.java
@@ -0,0 +1,43 @@
+package org.junitrunner;
+
+import org.junit.internal.AssumptionViolatedException;
+import org.junit.runner.Description;
+import org.junit.runner.notification.Failure;
+import org.junit.runner.notification.RunNotifier;
+import org.junitrunner.listener.BaseRunnerListener;
+
+public class RunNotifierWrapper extends BaseRunnerListener {
+
+ public final RunNotifier runNotifier;
+
+ public RunNotifierWrapper(RunNotifier runNotifier) {
+ this.runNotifier = runNotifier;
+ }
+
+ @Override
+ public void testIgnored(JUnitTask test) {
+ runNotifier.fireTestIgnored(test.describe());
+ }
+
+ @Override
+ public void testStarted(JUnitTask test) {
+ runNotifier.fireTestStarted(test.describe());
+ }
+
+ @Override
+ public void testFinished(JUnitTask test) {
+ runNotifier.fireTestFinished(test.describe());
+ }
+
+ @Override
+ public void testFailed(JUnitTask test, Throwable e) {
+ Description description = test.describe();
+ Failure failure = new Failure(description, e);
+ if (e instanceof AssumptionViolatedException) {
+ runNotifier.fireTestAssumptionFailed(failure);
+ } else {
+ runNotifier.fireTestFailure(failure);
+ }
+ runNotifier.fireTestFinished(description);
+ }
+}
View
64 junitrunner/src/main/java/org/junitrunner/TestHelper.java
@@ -0,0 +1,64 @@
+package org.junitrunner;
+
+import java.lang.annotation.Annotation;
+import java.util.List;
+
+import org.junit.runners.model.FrameworkMethod;
+import org.junit.runners.model.TestClass;
+
+public class TestHelper {
+
+ private final Class<?> testClass;
+ private final TestClass tc;
+
+ public TestHelper(Class<?> testClass) {
+ this.testClass = testClass;
+ tc = new TestClass(testClass);
+ }
+
+ public Class<?> getTestClass() {
+ return testClass;
+ }
+
+ @Deprecated
+ public TestClass getTc() {
+ return tc;
+ }
+
+ public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
+
+ return testClass.getAnnotation(annotationType);
+ }
+
+ public List<FrameworkMethod> getAnnotatedMethods(Class<? extends Annotation> annotation) {
+
+ return tc.getAnnotatedMethods(annotation);
+ }
+
+ public Annotation[] getAnnotations() {
+
+ return testClass.getAnnotations();
+ }
+
+ public String getName() {
+
+ return testClass.getName();
+ }
+
+ public <T> List<T> getAnnotatedMethodValues(Object test, Class<? extends Annotation> annotationClass, Class<T> valueClass) {
+ return tc.getAnnotatedMethodValues(test, annotationClass, valueClass);
+ }
+
+ public <T> List<T> getAnnotatedFieldValues(Object test, Class<? extends Annotation> annotationClass, Class<T> valueClass) {
+ return tc.getAnnotatedFieldValues(test, annotationClass, valueClass);
+ }
+
+ public void validatePublicVoidNoArgMethods(Class<? extends Annotation> annotation, boolean isStatic, List<Throwable> errors) {
+
+ List<FrameworkMethod> methods = getAnnotatedMethods(annotation);
+
+ for (FrameworkMethod eachTestMethod : methods) {
+ eachTestMethod.validatePublicVoidNoArg(isStatic, errors);
+ }
+ }
+}
View
6 junitrunner/src/main/java/org/junitrunner/TestInterceptor.java
@@ -0,0 +1,6 @@
+package org.junitrunner;
+
+public interface TestInterceptor {
+
+ void invoke(Object testObject);
+}
View
42 junitrunner/src/main/java/org/junitrunner/TestObjectFactory.java
@@ -0,0 +1,42 @@
+package org.junitrunner;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Modifier;
+
+import org.junit.runners.model.InitializationError;
+
+public class TestObjectFactory {
+
+ private Constructor<?> constructor;
+
+ public TestObjectFactory(Class<?> testClass) throws InitializationError {
+
+ if (isANonStaticInnerClass(testClass)) {
+ throw new InitializationError("The inner class " + testClass.getName() + " is not static.");
+ }
+
+ Constructor<?>[] constructors = testClass.getDeclaredConstructors();
+ if (constructors.length > 1) {
+ throw new InitializationError("Test class should have exactly one public constructor");
+ }
+
+ if (constructors[0].getParameterTypes().length > 0) {
+ throw new InitializationError("Test class should have exactly one public zero-argument constructor");
+ }
+
+ constructor = constructors[0];
+ }
+
+ boolean isANonStaticInnerClass(Class<?> testClass) {
+ return testClass.isMemberClass() && !Modifier.isStatic(testClass.getModifiers());
+ }
+
+ public Object constructTestObject() throws Throwable {
+ try {
+ return constructor.newInstance();
+ } catch (InvocationTargetException exc) {
+ throw exc.getTargetException();
+ }
+ }
+}
View
17 junitrunner/src/main/java/org/junitrunner/WithPlugins.java
@@ -0,0 +1,17 @@
+package org.junitrunner;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+@Inherited
+public @interface WithPlugins {
+
+ Class<? extends JUnitPlugin>[] value();
+
+ boolean skipDefault() default false;
+}
View
21 junitrunner/src/main/java/org/junitrunner/javamethod/AfterMethodStatement.java
@@ -0,0 +1,21 @@
+package org.junitrunner.javamethod;
+
+import org.junit.runners.model.FrameworkMethod;
+import org.junit.runners.model.Statement;
+
+public final class AfterMethodStatement extends AroundStatement {
+
+ private Object testObject;
+ private FrameworkMethod frameworkMethod;
+
+ public AfterMethodStatement(Object testObject, FrameworkMethod frameworkMethod, Statement next) {
+ super(next);
+ this.testObject = testObject;
+ this.frameworkMethod = frameworkMethod;
+ }
+
+ @Override
+ protected void after(Throwable failure) throws Throwable {
+ frameworkMethod.invokeExplosively(testObject);
+ }
+}
View
43 junitrunner/src/main/java/org/junitrunner/javamethod/AroundStatement.java
@@ -0,0 +1,43 @@
+package org.junitrunner.javamethod;
+
+import org.junit.runners.model.Statement;
+
+public class AroundStatement extends Statement {
+
+ private final Statement next;
+
+ protected AroundStatement(Statement next) {
+ this.next = next;
+ }
+
+ protected void before() throws Throwable {
+ }
+
+ protected void after(@SuppressWarnings("unused") Throwable failure) throws Throwable {
+ }
+
+ @Override
+ public void evaluate() throws Throwable {
+
+ before();
+
+ Throwable failure = null;
+ try {
+ next.evaluate();
+ } catch (Throwable exc) {
+ failure = exc;
+ }
+
+ try {
+ after(failure);
+ } catch (Throwable exc) {
+ if (failure == null) {
+ throw exc;
+ }
+ }
+
+ if (failure != null) {
+ throw failure;
+ }
+ }
+}
View
21 junitrunner/src/main/java/org/junitrunner/javamethod/BeforeMethodStatement.java
@@ -0,0 +1,21 @@
+package org.junitrunner.javamethod;
+
+import org.junit.runners.model.FrameworkMethod;
+import org.junit.runners.model.Statement;
+
+public class BeforeMethodStatement extends AroundStatement {
+
+ private final FrameworkMethod frameworkMethod;
+ private final Object testObject;
+
+ public BeforeMethodStatement(Statement next, FrameworkMethod frameworkMethod, Object testObject) {
+ super(next);
+ this.frameworkMethod = frameworkMethod;
+ this.testObject = testObject;
+ }
+
+ @Override
+ protected void before() throws Throwable {
+ frameworkMethod.invokeExplosively(testObject);
+ }
+}
View
45 junitrunner/src/main/java/org/junitrunner/javamethod/JUnitJavaMethodTest.java
@@ -0,0 +1,45 @@
+package org.junitrunner.javamethod;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+
+import org.junit.internal.runners.model.ReflectiveCallable;
+import org.junit.runner.Description;
+import org.junitrunner.JUnitTest;
+
+public class JUnitJavaMethodTest extends JUnitTest {
+
+ private final Method method;
+ private Description description;
+
+ public JUnitJavaMethodTest(Class<?> testClass, Method method) {
+ this.method = method;
+
+ description = Description.createTestDescription(testClass, method.getName(), method.getAnnotations());
+ }
+
+ @Override
+ public Description describe() {
+ return description;
+ }
+
+ public Method getMethod() {
+ return method;
+ }
+
+ @Override
+ public void invoke(final Object testObject) throws Throwable {
+
+ new ReflectiveCallable() {
+ @Override
+ protected Object runReflectiveCall() throws Throwable {
+ return method.invoke(testObject);
+ }
+ }.run();
+ }
+
+ @Override
+ public <T extends Annotation> T getAnnotation(Class<T> cls) {
+ return method.getAnnotation(cls);
+ }
+}
View
47 junitrunner/src/main/java/org/junitrunner/listener/BaseRunnerListener.java
@@ -0,0 +1,47 @@
+package org.junitrunner.listener;
+
+import org.junit.runner.Description;
+import org.junitrunner.JUnitTask;
+
+public class BaseRunnerListener implements RunnerListener {
+
+ @Override
+ public void testClassStarted(Description description) {
+ }
+
+ @Override
+ public void testClassFinished(Description description) {
+ }
+
+ @Override
+ public void testSuiteStarted(JUnitTask test) {
+ }
+
+ @Override
+ public void testSuiteFinished(JUnitTask test) {
+ }
+
+ @Override
+ public void testIgnored(JUnitTask test) {
+ }
+
+ @Override
+ public void testStarted(JUnitTask test) {
+ }
+
+ @Override
+ public void testFailed(JUnitTask test, Throwable e) {
+ }
+
+ @Override
+ public void testFinished(JUnitTask test) {
+ }
+
+ @Override
+ public void testCoreStarted(JUnitTask test, Object testObject) {
+ }
+
+ @Override
+ public void testCoreFinished(JUnitTask test, Object testObject, Throwable error) {
+ }
+}
View
28 junitrunner/src/main/java/org/junitrunner/listener/RunnerListener.java
@@ -0,0 +1,28 @@
+package org.junitrunner.listener;
+
+import org.junit.runner.Description;
+import org.junitrunner.JUnitTask;
+
+public interface RunnerListener {
+
+ void testClassStarted(Description description);
+
+ void testClassFinished(Description description);
+
+ void testSuiteStarted(JUnitTask test);
+
+ void testSuiteFinished(JUnitTask test);
+
+ void testCoreStarted(JUnitTask test, Object testObject);
+
+ void testCoreFinished(JUnitTask test, Object testObject, Throwable error);
+
+ void testStarted(JUnitTask test);
+
+ void testFailed(JUnitTask test, Throwable e);
+
+ void testFinished(JUnitTask test);
+
+ void testIgnored(JUnitTask test);
+
+}
View
100 junitrunner/src/main/java/org/junitrunner/listener/RunnerListenerContainer.java
@@ -0,0 +1,100 @@
+package org.junitrunner.listener;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.runner.Description;
+import org.junitrunner.JUnitSuite;
+import org.junitrunner.JUnitTask;
+import org.junitrunner.JUnitTest;
+
+public class RunnerListenerContainer {// implements RunnerListener {
+
+ private final List<RunnerListener> listeners = new ArrayList<RunnerListener>();
+
+ public void addListener(RunnerListener listener) {
+ listeners.add(listener);
+ }
+
+ public void testClassStarted(Description description) {
+ for (RunnerListener listener : listeners) {
+ listener.testClassStarted(description);
+ }
+ }
+
+ public void testClassFinished(Description description) {
+ for (RunnerListener listener : listeners) {
+ listener.testClassFinished(description);
+ }
+ }
+
+ public void taskIgnored(JUnitTask test) {
+ for (RunnerListener listener : listeners) {
+ listener.testIgnored(test);
+ }
+ }
+
+ public void taskStarted(JUnitTask task) {
+ if (task instanceof JUnitSuite) {
+ testSuiteStarted(task);
+ } else {
+ testStarted(task);
+ }
+ }
+
+ public void taskFailed(JUnitTask task, Throwable exception) {
+ if (task instanceof JUnitTest) {
+ testFailed(task, exception);
+ }
+ }
+
+ public void taskFinished(JUnitTask task) {
+ if (task instanceof JUnitSuite) {
+ testSuiteFinished(task);
+ } else {
+ testFinished(task);
+ }
+ }
+
+ private void testSuiteStarted(JUnitTask test) {
+ for (RunnerListener listener : listeners) {
+ listener.testSuiteStarted(test);
+ }
+ }
+
+ private void testSuiteFinished(JUnitTask test) {
+ for (RunnerListener listener : listeners) {
+ listener.testSuiteFinished(test);
+ }
+ }
+
+ private void testStarted(JUnitTask test) {
+ for (RunnerListener listener : listeners) {
+ listener.testStarted(test);
+ }
+ }
+
+ private void testFailed(JUnitTask test, Throwable e) {
+ for (RunnerListener listener : listeners) {
+ listener.testFailed(test, e);
+ }
+ }
+
+ private void testFinished(JUnitTask test) {
+ for (RunnerListener listener : listeners) {
+ listener.testFinished(test);
+ }
+ }
+
+ public void testCoreStarted(JUnitTask test, Object testObject) {
+ for (RunnerListener listener : listeners) {
+ listener.testCoreStarted(test, testObject);
+ }
+ }
+
+ public void testCoreFinished(JUnitTask test, Object testObject, Throwable error) {
+ for (RunnerListener listener : listeners) {
+ listener.testCoreFinished(test, testObject, error);
+ }
+ }
+}
View
29 junitrunner/src/main/java/org/junitrunner/listener/TestInvocationListenerStatement.java
@@ -0,0 +1,29 @@
+package org.junitrunner.listener;
+
+import org.junit.runners.model.Statement;
+import org.junitrunner.JUnitTask;
+import org.junitrunner.javamethod.AroundStatement;
+
+public class TestInvocationListenerStatement extends AroundStatement {
+
+ private final JUnitTask test;
+ private final Object testObject;
+ private final RunnerListenerContainer listenerContainer;
+
+ public TestInvocationListenerStatement(JUnitTask test, Statement next, Object testObject, RunnerListenerContainer listenerContainer) {
+ super(next);
+ this.test = test;
+ this.testObject = testObject;
+ this.listenerContainer = listenerContainer;
+ }
+
+ @Override
+ protected void before() throws Throwable {
+ listenerContainer.testCoreStarted(test, testObject);
+ }
+
+ @Override
+ protected void after(Throwable failure) throws Throwable {
+ listenerContainer.testCoreFinished(test, testObject, failure);
+ }
+}
View
83 junitrunner/src/main/java/org/junitrunner/plugin/annotation/AnnotationClassRuleFactory.java
@@ -0,0 +1,83 @@
+package org.junitrunner.plugin.annotation;
+
+import static org.junit.internal.runners.rules.RuleFieldValidator.*;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.FrameworkMethod;
+import org.junit.runners.model.Statement;
+import org.junitrunner.TestHelper;
+import org.junitrunner.javamethod.AfterMethodStatement;
+import org.junitrunner.javamethod.BeforeMethodStatement;
+
+public class AnnotationClassRuleFactory implements ClassRuleFactory {
+
+ @Override
+ public List<TestRule> createRules(Class<?> testClass, List<Throwable> errors) {
+
+ TestHelper testHelper = new TestHelper(testClass);
+ testHelper.validatePublicVoidNoArgMethods(BeforeClass.class, true, errors);
+ testHelper.validatePublicVoidNoArgMethods(AfterClass.class, true, errors);
+ CLASS_RULE_VALIDATOR.validate(testHelper.getTc(), errors);
+ CLASS_RULE_METHOD_VALIDATOR.validate(testHelper.getTc(), errors);
+
+ List<TestRule> classRules = new ArrayList<TestRule>();
+
+ for (TestRule testRule : withClassBefores(testHelper)) {
+ classRules.add(testRule);
+ }
+ for (TestRule testRule : withClassAfters(testHelper)) {
+ classRules.add(testRule);
+ }
+ for (TestRule testRule : classRules(testHelper)) {
+ classRules.add(testRule);
+ }
+
+ return classRules;
+ }
+
+ List<TestRule> withClassBefores(TestHelper testHelper) {
+
+ List<TestRule> classRules = new ArrayList<TestRule>();
+ List<FrameworkMethod> befores = testHelper.getAnnotatedMethods(BeforeClass.class);
+ for (final FrameworkMethod each : befores) {
+ classRules.add(0, new TestRule() {
+
+ @Override
+ public Statement apply(Statement base, Description description) {