Skip to content

Commit

Permalink
@WithTestServer
Browse files Browse the repository at this point in the history
  • Loading branch information
jto committed Aug 23, 2012
1 parent c12e6c5 commit 0cce691
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 9 deletions.
57 changes: 48 additions & 9 deletions helpers/src/main/java/com/linkedin/plugin/NGTests.java
Expand Up @@ -22,7 +22,18 @@
import play.test.*;
import static play.test.Helpers.*;

public class NGTests implements IHookable{
import play.libs.F.*;

// TODO: refactor
public class NGTests implements IHookable {

// XXX: Evil hack, may lead to race conditions...
private TestBrowser _testBrowser = null;
protected TestBrowser browser(){
if(_testBrowser == null)
throw new RuntimeException("No TestBrowser available, test class or method must be annotated with @WithTestServer");
return _testBrowser;
}

private Method testMethod(ITestResult testResult){
return testResult.getMethod().getConstructorOrMethod().getMethod();
Expand All @@ -31,18 +42,26 @@ private Method testMethod(ITestResult testResult){
private Class testClass(ITestResult testResult){
return testResult.getTestClass().getRealClass();
}
private WithFakeApplication getFakeApp(ITestResult testResult){

private <T extends Annotation> T getAnnotationFromMethodOrClass(Class<T> c, ITestResult testResult){
Class clazz = testClass(testResult);
Method m = testMethod(testResult);

WithFakeApplication classFakeApp = (WithFakeApplication)clazz.getAnnotation(WithFakeApplication.class);
WithFakeApplication a = m.getAnnotation(WithFakeApplication.class);
T classAnn = (T)clazz.getAnnotation(c);
T a = m.getAnnotation(c);

if(a != null)
return a;
else
return classFakeApp;
return classAnn;
}

private WithFakeApplication getFakeApp(ITestResult testResult){
return getAnnotationFromMethodOrClass(WithFakeApplication.class, testResult);
}

private WithTestServer getTestServer(ITestResult testResult){
return getAnnotationFromMethodOrClass(WithTestServer.class, testResult);
}

private Map<String, String> getConf(ITestResult testResult){
Expand Down Expand Up @@ -94,15 +113,35 @@ private List<String> getPlugins(ITestResult testResult){
return plugins;
}

public void run(final IHookCallBack icb, ITestResult testResult) {
WithFakeApplication fa = getFakeApp(testResult);
private FakeApplication buildFakeApplication(WithFakeApplication fa, ITestResult testResult){
if(fa != null){
String path = fa.path();
FakeApplication app = new FakeApplication(new File(path), Helpers.class.getClassLoader(), getConf(testResult), getPlugins(testResult));
return new FakeApplication(new File(path), Helpers.class.getClassLoader(), getConf(testResult), getPlugins(testResult));
}
return null;
}

public void run(final IHookCallBack icb, final ITestResult testResult) {
WithFakeApplication fa = getFakeApp(testResult);
WithTestServer ts = getTestServer(testResult);

if(fa != null){
FakeApplication app = buildFakeApplication(fa, testResult);
start(app);
icb.runTestMethod(testResult);
stop(app);
}
else if(ts != null){
FakeApplication fake = buildFakeApplication(ts.fakeApplication(), testResult);
TestServer server =testServer(ts.port(), fake);

running(server, HTMLUNIT, new Callback<TestBrowser>() {
public void invoke(final TestBrowser browser) {
_testBrowser = browser;
icb.runTestMethod(testResult);
}
});
}
else{
icb.runTestMethod(testResult);
}
Expand Down
29 changes: 29 additions & 0 deletions helpers/src/main/java/com/linkedin/plugin/WithTestServer.java
@@ -0,0 +1,29 @@
// Copyright 2012 LinkedIn
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.linkedin.plugin;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;
import java.lang.annotation.ElementType;

import play.test.FakeApplication;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface WithTestServer {
WithFakeApplication fakeApplication() default @WithFakeApplication;
int port() default 3333;
}
22 changes: 22 additions & 0 deletions sample/test/IntegrationTest.java
@@ -0,0 +1,22 @@
import org.testng.annotations.*;

import play.mvc.*;
import play.test.*;
import play.libs.F.*;

import static play.test.Helpers.*;
import static org.fest.assertions.Assertions.*;

import static org.fluentlenium.core.filter.FilterConstructor.*;

import com.linkedin.plugin.*;

@WithTestServer
public class IntegrationTest extends NGTests {
@Test
@WithTestServer
public void test() {
browser().goTo("http://localhost:3333");
assertThat(browser().pageSource()).contains("Helloworld");
}
}
1 change: 1 addition & 0 deletions sample/test/resources/testng.yaml
Expand Up @@ -6,3 +6,4 @@ tests:
classes:
- SimpleTest
- AllWithFakeApp
- IntegrationTest

0 comments on commit 0cce691

Please sign in to comment.