Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

@WithPlugins

  • Loading branch information...
commit 70ba5c761bf1c1c17c803cd277ecef01ec12546a 1 parent 7a7ecf4
jtournay authored
View
22 sample/app/plugins/DummyPlugin.java
@@ -0,0 +1,22 @@
+package plugins;
+
+import play.*;
+import play.db.*;
+
+public class DummyPlugin extends Plugin {
+
+ private final Application application;
+
+ public DummyPlugin(Application application) {
+ this.application = application;
+ }
+
+ public void onStart() {
+ System.out.println("Starting plugin " + this.getClass() + " in application " + application);
+ }
+
+ public void onStop() {
+ System.out.println("Stoping plugin " + this.getClass() + " in application " + application);
+ }
+
+}
View
1  sample/test/AllWithFakeApp.java
@@ -13,6 +13,7 @@
@Conf(key="test.fakeconf", value="fake")
})
@Conf(key="test.anotherConf", value="fake")
+@WithPlugins({"plugins.DummyPlugin"})
public class AllWithFakeApp extends LITests {
@Test
View
1  sample/test/SimpleTest.java
@@ -21,6 +21,7 @@ public void aFastTest() {
@Conf(key="test.fakeconf", value="fake"),
@Conf(key="test.loutre", value="oink")
})
+ @WithPlugins({"plugins.DummyPlugin"})
public void aFailingTest() {
String f = play.Play.application().configuration().getString("test.fakeconf");
if(!f.equals("fake"))
View
91 src/main/java/com/linkedin/plugin/LITests.java
@@ -1,48 +1,85 @@
package com.linkedin.plugin;
+import java.lang.annotation.*;
+import java.lang.reflect.*;
import java.util.*;
-import java.lang.reflect.*;
-import java.lang.annotation.*;
import org.testng.*;
-
import play.test.*;
import static play.test.Helpers.*;
public class LITests implements IHookable{
- public void run(final IHookCallBack icb, ITestResult testResult) {
-
- Class clazz = testResult.getTestClass().getRealClass();
- Method m = testResult.getMethod().getConstructorOrMethod().getMethod();
-
+
+ private Method testMethod(ITestResult testResult){
+ return testResult.getMethod().getConstructorOrMethod().getMethod();
+ }
+
+ private Class testClass(ITestResult testResult){
+ return testResult.getTestClass().getRealClass();
+ }
+
+ private Boolean hasFakeApp(ITestResult testResult){
+ Class clazz = testClass(testResult);
+ Method m = testMethod(testResult);
+
WithFakeApplication classFakeApp = (WithFakeApplication)clazz.getAnnotation(WithFakeApplication.class);
WithFakeApplication a = m.getAnnotation(WithFakeApplication.class);
+ return classFakeApp != null || a != null;
+ }
+
+ private Map<String, String> getConf(ITestResult testResult){
+ Map<String, String> conf = new HashMap<String, String>();
+
+ if(!hasFakeApp(testResult))
+ return conf;
+
+ Class clazz = testClass(testResult);
+ Method m = testMethod(testResult);
+
Confs classConfs = (Confs)clazz.getAnnotation(Confs.class);
Conf classConf = (Conf)clazz.getAnnotation(Conf.class);
Confs methodConfs = m.getAnnotation(Confs.class);
Conf methodConf = m.getAnnotation(Conf.class);
+
+ if(classConfs != null){
+ for(Conf c : classConfs.value())
+ conf.put(c.key(), c.value());
+ }
+
+ if(classConf != null)
+ conf.put(classConf.key(), classConf.value());
+
+ if(methodConfs != null){
+ for(Conf c : methodConfs.value())
+ conf.put(c.key(), c.value());
+ }
+
+ if(methodConf != null)
+ conf.put(methodConf.key(), methodConf.value());
+
+ return conf;
+ }
+
+ private List<String> getPlugins(ITestResult testResult){
+ Class clazz = testClass(testResult);
+ Method m = testMethod(testResult);
- if(classFakeApp != null || a != null){
- Map<String, String> conf = new HashMap<String, String>();
+ WithPlugins classPlugins = (WithPlugins)clazz.getAnnotation(WithPlugins.class);
+ WithPlugins methodPlugins = m.getAnnotation(WithPlugins.class);
- if(classConfs != null){
- for(Conf c : classConfs.value())
- conf.put(c.key(), c.value());
- }
-
- if(classConf != null)
- conf.put(classConf.key(), classConf.value());
-
- if(methodConfs != null){
- for(Conf c : methodConfs.value())
- conf.put(c.key(), c.value());
- }
-
- if(methodConf != null)
- conf.put(methodConf.key(), methodConf.value());
+ List<String> plugins = new ArrayList<String>();
+ if(classPlugins != null)
+ plugins.addAll(Arrays.asList(classPlugins.value()));
+ if(methodPlugins != null)
+ plugins.addAll(Arrays.asList(methodPlugins.value()));
- FakeApplication app = fakeApplication(conf);
+ return plugins;
+ }
+
+ public void run(final IHookCallBack icb, ITestResult testResult) {
+ if(hasFakeApp(testResult)){
+ FakeApplication app = fakeApplication(getConf(testResult), getPlugins(testResult));
start(app);
icb.runTestMethod(testResult);
stop(app);
@@ -51,4 +88,4 @@ public void run(final IHookCallBack icb, ITestResult testResult) {
icb.runTestMethod(testResult);
}
}
-}
+}
View
13 src/main/java/com/linkedin/plugin/WithPlugins.java
@@ -0,0 +1,13 @@
+package com.linkedin.plugin;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+import java.lang.annotation.Target;
+import java.lang.annotation.ElementType;
+
+@Target({ElementType.METHOD, ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface WithPlugins {
+ String[] value() default {};
+}
Please sign in to comment.
Something went wrong with that request. Please try again.