Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactoring

  • Loading branch information...
commit a5e15412dc20070cefc3794bbc095ce3d7a335d2 1 parent 0cce691
jto authored
Showing with 103 additions and 87 deletions.
  1. +103 −87 helpers/src/main/java/com/linkedin/plugin/NGTests.java
View
190 helpers/src/main/java/com/linkedin/plugin/NGTests.java
@@ -27,114 +27,130 @@
// 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();
- }
-
- private Class testClass(ITestResult testResult){
- return testResult.getTestClass().getRealClass();
- }
-
- private <T extends Annotation> T getAnnotationFromMethodOrClass(Class<T> c, ITestResult testResult){
- Class clazz = testClass(testResult);
- Method m = testMethod(testResult);
+ private class AnnotationsReader {
- T classAnn = (T)clazz.getAnnotation(c);
- T a = m.getAnnotation(c);
+ private ITestResult itr;
+
+ public AnnotationsReader(ITestResult testResult){
+ itr = testResult;
+ }
+
+ private Method testMethod(){
+ return itr.getMethod().getConstructorOrMethod().getMethod();
+ }
- if(a != null)
- return a;
- else
- return classAnn;
- }
+ private Class testClass(){
+ return itr.getTestClass().getRealClass();
+ }
- private WithFakeApplication getFakeApp(ITestResult testResult){
- return getAnnotationFromMethodOrClass(WithFakeApplication.class, testResult);
- }
+ private <T extends Annotation> T getAnnotationFromMethodOrClass(Class<T> c){
+ Class clazz = testClass();
+ Method m = testMethod();
- private WithTestServer getTestServer(ITestResult testResult){
- return getAnnotationFromMethodOrClass(WithTestServer.class, testResult);
- }
-
- private Map<String, String> getConf(ITestResult testResult){
- Map<String, String> conf = new HashMap<String, String>();
-
- if(getFakeApp(testResult) == null)
- 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());
+ T classAnn = (T)clazz.getAnnotation(c);
+ T a = m.getAnnotation(c);
+
+ if(a != null)
+ return a;
+ else
+ return classAnn;
}
-
- if(classConf != null)
- conf.put(classConf.key(), classConf.value());
-
- if(methodConfs != null){
- for(Conf c : methodConfs.value())
- conf.put(c.key(), c.value());
+
+ private WithFakeApplication getFakeApp(){
+ return getAnnotationFromMethodOrClass(WithFakeApplication.class);
}
-
- 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);
- WithPlugins classPlugins = (WithPlugins)clazz.getAnnotation(WithPlugins.class);
- WithPlugins methodPlugins = m.getAnnotation(WithPlugins.class);
+ private WithTestServer getTestServer(){
+ return getAnnotationFromMethodOrClass(WithTestServer.class);
+ }
- List<String> plugins = new ArrayList<String>();
- if(classPlugins != null)
- plugins.addAll(Arrays.asList(classPlugins.value()));
- if(methodPlugins != null)
- plugins.addAll(Arrays.asList(methodPlugins.value()));
+ private Map<String, String> getConf(){
+ Map<String, String> conf = new HashMap<String, String>();
- return plugins;
- }
+ if(getFakeApp() == null)
+ return conf;
- private FakeApplication buildFakeApplication(WithFakeApplication fa, ITestResult testResult){
- if(fa != null){
- String path = fa.path();
- return new FakeApplication(new File(path), Helpers.class.getClassLoader(), getConf(testResult), getPlugins(testResult));
+ Class clazz = testClass();
+ Method m = testMethod();
+
+ 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(){
+ Class clazz = testClass();
+ Method m = testMethod();
+
+ WithPlugins classPlugins = (WithPlugins)clazz.getAnnotation(WithPlugins.class);
+ WithPlugins methodPlugins = m.getAnnotation(WithPlugins.class);
+
+ List<String> plugins = new ArrayList<String>();
+ if(classPlugins != null)
+ plugins.addAll(Arrays.asList(classPlugins.value()));
+ if(methodPlugins != null)
+ plugins.addAll(Arrays.asList(methodPlugins.value()));
+
+ return plugins;
+ }
+
+ private FakeApplication buildFakeApplication(WithFakeApplication fa){
+ if(fa != null){
+ String path = fa.path();
+ return new FakeApplication(new File(path), Helpers.class.getClassLoader(), getConf(), getPlugins());
+ }
+ return null;
+ }
+
+ private TestServer buildTestServer(WithTestServer ts) {
+ FakeApplication fake = buildFakeApplication(ts.fakeApplication());
+ return testServer(ts.port(), fake);
}
- return null;
+ }
+
+ // 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;
}
public void run(final IHookCallBack icb, final ITestResult testResult) {
- WithFakeApplication fa = getFakeApp(testResult);
- WithTestServer ts = getTestServer(testResult);
+
+ AnnotationsReader reader = new AnnotationsReader(testResult);
+
+ WithFakeApplication fa = reader.getFakeApp();
+ WithTestServer ts = reader.getTestServer();
if(fa != null){
- FakeApplication app = buildFakeApplication(fa, testResult);
+ FakeApplication app = reader.buildFakeApplication(fa);
start(app);
icb.runTestMethod(testResult);
stop(app);
}
else if(ts != null){
- FakeApplication fake = buildFakeApplication(ts.fakeApplication(), testResult);
- TestServer server =testServer(ts.port(), fake);
-
+ TestServer server = reader.buildTestServer(ts);
+ // TODO: parameterize WebDriver
running(server, HTMLUNIT, new Callback<TestBrowser>() {
public void invoke(final TestBrowser browser) {
_testBrowser = browser;
Please sign in to comment.
Something went wrong with that request. Please try again.