Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

execute @Before annotated methods before REST requests #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;


/**
* <p>The <code>HttpJUnitRunner</code> can be used in your TestCase to avoid the double annotation
* of test methods with the <code>Test</code> and </code>{@link HttpTest}</code> annotation. The
* runner detects all <code>{@link HttpTest}</code> annotated methods and executes them as normal
* JUnit test methods.</p>
* <p>
* The <code>HttpJUnitRunner</code> can be used in your TestCase to avoid the double annotation of
* test methods with the <code>Test</code> and </code>{@link HttpTest}</code> annotation. The runner
* detects all <code>{@link HttpTest}</code> annotated methods and executes them as normal JUnit
* test methods.
* </p>
*/
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<FrameworkMethod> computeTestMethods() {
ArrayList<FrameworkMethod> result = new ArrayList<FrameworkMethod>();
result.addAll( getTestClass().getAnnotatedMethods(HttpTest.class) );
List<FrameworkMethod> testAnnotatedMethods = getTestClass().getAnnotatedMethods(Test.class);
result.addAll( getTestClass().getAnnotatedMethods( HttpTest.class ) );
List<FrameworkMethod> testAnnotatedMethods = getTestClass().getAnnotatedMethods( Test.class );
for( FrameworkMethod method : testAnnotatedMethods ) {
if( !result.contains( method ) ) {
result.add( method );
Expand All @@ -47,5 +57,44 @@ protected List<FrameworkMethod> 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<TestRule> getTestRules( Object target ) {
if( getRuleStrategy( target ) == RuleStrategy.HTTP_CALL_AFTER_BEFORE ) {
List<TestRule> testRules = super.getTestRules( target );
List<TestRule> nonDestinationRules = new ArrayList<TestRule>( testRules );
for( TestRule t : testRules ) {
if( t instanceof Destination ) {
nonDestinationRules.remove( t );
}
}
return nonDestinationRules;
}
return super.getTestRules( target );
}

protected List<TestRule> getDestinationRules( Object target ) {
List<TestRule> testRules = super.getTestRules( target );
List<TestRule> destinationRules = new ArrayList<TestRule>();
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 ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.eclipsesource.restfuse;


public enum RuleStrategy {
/**
* default behaviour:
* <p>destination rule is executed as junit does</p>
*/
DEFAULT,
/**
* default behaviour:
* <p>destination rule is wrapped around the actual method invocation.
* This means that <code>@Before</code> methods are executed before the rest service is called</p>
*/
HTTP_CALL_AFTER_BEFORE
}
Original file line number Diff line number Diff line change
@@ -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;
}