1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
package net.sourceforge.jnlp.browsertesting;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import net.sourceforge.jnlp.annotations.TestInBrowsers;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import net.sourceforge.jnlp.ServerAccess;
import org.junit.Ignore;
import org.junit.internal.AssumptionViolatedException;
import org.junit.internal.runners.model.EachTestNotifier;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
public class BrowserTestRunner extends BlockJUnit4ClassRunner {
public BrowserTestRunner(java.lang.Class<?> testClass) throws InitializationError {
super(testClass);
}
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
Method mm = method.getMethod();
TestInBrowsers tib = mm.getAnnotation(TestInBrowsers.class);
injectBrowserCatched(method, Browsers.none);
boolean browserIgnoration = false;
if (tib != null) {
try {
List<Browsers> testableBrowsers = BrowserFactory.getFactory().getBrowsers(tib);
String mbr = System.getProperty("modified.browsers.run");
if (mbr != null) {
if (mbr.equalsIgnoreCase("all")) {
if (!isBrowsersNoneSet(tib)) {
testableBrowsers = BrowserFactory.getFactory().getBrowsers(new Browsers[]{Browsers.all});
}
} else if (mbr.equalsIgnoreCase("one")) {
//this complication here is for case like
// namely enumerated concrete browsers, so we want to pick up
// random one from those already enumerated
if (isBrowsersNoneSet(tib)) {
testableBrowsers = Arrays.asList(new Browsers[]{testableBrowsers.get(new Random().nextInt(testableBrowsers.size()))});
}
} else if (mbr.equalsIgnoreCase("ignore")) {
testableBrowsers = BrowserFactory.getFactory().getBrowsers(new Browsers[]{Browsers.none});
browserIgnoration = true;
} else {
ServerAccess.logErrorReprint("unrecognized value of modified.browsers.run - " + mbr);
}
}
for (Browsers browser : testableBrowsers) {
try {
injcetBrowser(method, browser);
runChildX(method, notifier, browser, browserIgnoration);
} catch (Exception ex) {
//throw new RuntimeException("unabled to inject browser", ex);
ServerAccess.logException(ex, true);
}
}
} finally {
injectBrowserCatched(method, Browsers.none);
}
} else {
runChildX(method, notifier, null, false);
}
}
private boolean isBrowsersNoneSet(TestInBrowsers tib) {
if (tib.testIn().length == 1 && tib.testIn()[0] == Browsers.none) {
return true;
}
return false;
}
private void injectBrowserCatched(FrameworkMethod method, Browsers browser) {
try {
injcetBrowser(method, browser);
} catch (Exception ex) {
//throw new RuntimeException("unabled to inject browser", ex);
ServerAccess.logException(ex, true);
}
}
private void injcetBrowser(FrameworkMethod method, Browsers browser) throws IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
Method ff = method.getMethod().getDeclaringClass().getMethod("setBrowser", Browsers.class);
ff.invoke(null, browser);
}
protected void runChildX(final FrameworkMethod method, RunNotifier notifier, Browsers browser, boolean browserIgnoration) {
Description description = describeChild(method, browser);
if (method.getAnnotation(Ignore.class) != null) {
notifier.fireTestIgnored(description);
} else {
try {
runLeaf(methodBlock(method), description, notifier, browserIgnoration);
// ServerAccess.logOutputReprint("trying leaf");
// Method m = this.getClass().getMethod("runLeaf", Statement.class, Description.class, RunNotifier.class);
// m.setAccessible(true);
// m.invoke(this, methodBlock(method), description, notifier);
// ServerAccess.logOutputReprint("leaf invoked");
} catch (Exception ex) {
//throw new RuntimeException("unabled to lunch test on leaf", ex);
ServerAccess.logException(ex, true);
}
}
}
/**
* Runs a {@link Statement} that represents a leaf (aka atomic) test.
*/
protected final void runLeaf(Statement statement, Description description,
RunNotifier notifier, boolean ignore) {
EachTestNotifier eachNotifier = new EachTestNotifier(notifier, description);
eachNotifier.fireTestStarted();
if (ignore) {
eachNotifier.fireTestIgnored();
return;
}
try {
statement.evaluate();
} catch (AssumptionViolatedException e) {
eachNotifier.addFailedAssumption(e);
} catch (Throwable e) {
eachNotifier.addFailure(e);
} finally {
eachNotifier.fireTestFinished();
}
}
protected Description describeChild(FrameworkMethod method, Browsers browser) {
if (browser == null) {
return super.describeChild(method);
} else {
try {
return Description.createTestDescription(getTestClass().getJavaClass(),
testName(method) + " - " + browser.toString(), method.getAnnotations());
} catch (Exception ex) {
ServerAccess.logException(ex, true);
return super.describeChild(method);
}
}
}
}
|