diff --git a/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/HttpJUnitRunner.java b/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/HttpJUnitRunner.java index 84466e5..e251d6a 100644 --- a/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/HttpJUnitRunner.java +++ b/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/HttpJUnitRunner.java @@ -1,12 +1,9 @@ /******************************************************************************* - * Copyright (c) 2011 EclipseSource and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 + * Copyright (c) 2011 EclipseSource and others. All rights reserved. This program and the + * accompanying materials are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Holger Staudacher - initial API and implementation + * http://www.eclipse.org/legal/epl-v10.html Contributors: Holger Staudacher - initial API and + * implementation ******************************************************************************/ package com.eclipsesource.restfuse; @@ -15,30 +12,43 @@ import java.util.List; import org.junit.Test; +import org.junit.rules.RunRules; +import org.junit.rules.TestRule; import org.junit.runners.BlockJUnit4ClassRunner; import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.InitializationError; +import org.junit.runners.model.Statement; +import com.eclipsesource.restfuse.annotation.HttpConfig; import com.eclipsesource.restfuse.annotation.HttpTest; - /** - *

The HttpJUnitRunner can be used in your TestCase to avoid the double annotation - * of test methods with the Test and {@link HttpTest} annotation. The - * runner detects all {@link HttpTest} annotated methods and executes them as normal - * JUnit test methods.

+ *

+ * The HttpJUnitRunner can be used in your TestCase to avoid the double annotation of + * test methods with the Test and {@link HttpTest} annotation. The runner + * detects all {@link HttpTest} annotated methods and executes them as normal JUnit + * test methods. + *

*/ public class HttpJUnitRunner extends BlockJUnit4ClassRunner { public HttpJUnitRunner( Class klass ) throws InitializationError { super( klass ); } - + + RuleStrategy getRuleStrategy( Object test ) { + HttpConfig annotation = test.getClass().getAnnotation( HttpConfig.class ); + if( annotation == null ) { + return RuleStrategy.DEFAULT; + } + return annotation.ruleStrategy(); + } + @Override protected List computeTestMethods() { ArrayList result = new ArrayList(); - result.addAll( getTestClass().getAnnotatedMethods(HttpTest.class) ); - List testAnnotatedMethods = getTestClass().getAnnotatedMethods(Test.class); + result.addAll( getTestClass().getAnnotatedMethods( HttpTest.class ) ); + List testAnnotatedMethods = getTestClass().getAnnotatedMethods( Test.class ); for( FrameworkMethod method : testAnnotatedMethods ) { if( !result.contains( method ) ) { result.add( method ); @@ -47,5 +57,44 @@ protected List computeTestMethods() { Collections.sort( result, new HttpOrderComparator() ); return result; } - + + @Override + protected Statement methodInvoker( FrameworkMethod method, Object test ) { + if( getRuleStrategy( test ) == RuleStrategy.HTTP_CALL_AFTER_BEFORE ) { + Statement methodInvoker = super.methodInvoker( method, test ); + return withDestinationRules( method, test, methodInvoker ); + } + return super.methodInvoker( method, test ); + } + + @Override + protected List getTestRules( Object target ) { + if( getRuleStrategy( target ) == RuleStrategy.HTTP_CALL_AFTER_BEFORE ) { + List testRules = super.getTestRules( target ); + List nonDestinationRules = new ArrayList( testRules ); + for( TestRule t : testRules ) { + if( t instanceof Destination ) { + nonDestinationRules.remove( t ); + } + } + return nonDestinationRules; + } + return super.getTestRules( target ); + } + + protected List getDestinationRules( Object target ) { + List testRules = super.getTestRules( target ); + List destinationRules = new ArrayList(); + for( TestRule t : testRules ) { + if( t instanceof Destination ) { + destinationRules.add( t ); + } + } + return destinationRules; + } + + private Statement withDestinationRules( FrameworkMethod method, Object target, Statement statement ) + { + return new RunRules( statement, getDestinationRules( target ), describeChild( method ) ); + } } diff --git a/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/RuleStrategy.java b/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/RuleStrategy.java new file mode 100644 index 0000000..6fb5e92 --- /dev/null +++ b/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/RuleStrategy.java @@ -0,0 +1,16 @@ +package com.eclipsesource.restfuse; + + +public enum RuleStrategy { + /** + * default behaviour: + *

destination rule is executed as junit does

+ */ + DEFAULT, + /** + * default behaviour: + *

destination rule is wrapped around the actual method invocation. + * This means that @Before methods are executed before the rest service is called

+ */ + HTTP_CALL_AFTER_BEFORE +} diff --git a/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/annotation/HttpConfig.java b/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/annotation/HttpConfig.java new file mode 100644 index 0000000..824272f --- /dev/null +++ b/com.eclipsesource.restfuse/src/com/eclipsesource/restfuse/annotation/HttpConfig.java @@ -0,0 +1,14 @@ +package com.eclipsesource.restfuse.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import com.eclipsesource.restfuse.RuleStrategy; + +@Retention( RetentionPolicy.RUNTIME ) +@Target( { ElementType.TYPE } ) +public @interface HttpConfig { + RuleStrategy ruleStrategy() default RuleStrategy.DEFAULT; +}