Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

@WithTestServer

  • Loading branch information...
commit 0cce69119b39aef28c89e695c183ce16ae0f517b 1 parent c12e6c5
jto authored
57 helpers/src/main/java/com/linkedin/plugin/NGTests.java
View
@@ -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();
@@ -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){
@@ -94,15 +113,35 @@ private WithFakeApplication getFakeApp(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);
}
29 helpers/src/main/java/com/linkedin/plugin/WithTestServer.java
View
@@ -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 sample/test/IntegrationTest.java
View
@@ -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  sample/test/resources/testng.yaml
View
@@ -6,3 +6,4 @@ tests:
classes:
- SimpleTest
- AllWithFakeApp
+ - IntegrationTest
Please sign in to comment.
Something went wrong with that request. Please try again.