Skip to content

Commit 93e7d58

Browse files
committed
Move trace information from UITestCase to CloseTestWindowsRule
The tracing information for UI tests (test name printed at start/end of each test to stdout) is the only remaining functionality of UITestCase. When the CloseTestWindowsRule has been extracted out of this test case, the test classes that have been adapted to only use that rule miss that tracing information since this. This change moves the tracing to the CloseTestWindowsRule as the central test rule for UI-related tests. This implicitly readds the tracing information to tests that use the rule. With this change, the inheritance to UITestCase can simply be replaced with the use of the CloseWindowsTestRule in all plain JUnit 4 tests.
1 parent e75fad0 commit 93e7d58

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/CloseTestWindowsRule.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,21 @@
2727
import org.eclipse.ui.IWorkbenchWindow;
2828
import org.eclipse.ui.PlatformUI;
2929
import org.junit.rules.ExternalResource;
30+
import org.junit.runner.Description;
31+
import org.junit.runners.model.Statement;
3032

3133
/**
32-
* Rule that close windows opened during the test case and checks for shells
33-
* unintentionally leaked from the test case.
34+
* Rule for UI tests to clean up windows/shells:
35+
* <ul>
36+
* <li>prints the test name to the log before and after each test case
37+
* <li>closes windows opened during the test case
38+
* <li>checks for shells unintentionally leaked from the test case
39+
* </ul>
3440
*/
3541
public class CloseTestWindowsRule extends ExternalResource {
3642

43+
private String testName;
44+
3745
private final List<IWorkbenchWindow> testWindows;
3846

3947
private TestWindowListener windowListener;
@@ -46,21 +54,39 @@ public CloseTestWindowsRule() {
4654
testWindows = new ArrayList<>(3);
4755
}
4856

57+
@Override
58+
public Statement apply(Statement base, Description description) {
59+
this.testName = description.getDisplayName();
60+
return super.apply(base, description);
61+
}
62+
4963
@Override
5064
protected void before() throws Exception {
5165
addWindowListener();
5266
storeInitialShells();
67+
trace(TestRunLogUtil.formatTestStartMessage(testName));
5368
}
5469

5570
@Override
5671
protected void after() {
72+
trace(TestRunLogUtil.formatTestFinishedMessage(testName));
5773
removeWindowListener();
5874
processEvents();
5975
closeAllTestWindows();
6076
processEvents();
6177
checkForLeakedShells();
6278
}
6379

80+
/**
81+
* Outputs a trace message to the trace output device, if enabled. By default,
82+
* trace messages are sent to <code>System.out</code>.
83+
*
84+
* @param msg the trace message
85+
*/
86+
private static void trace(String msg) {
87+
System.out.println(msg);
88+
}
89+
6490
/**
6591
* Adds a window listener to the workbench to keep track of opened test windows.
6692
*/
@@ -134,4 +160,12 @@ public void disableLeakChecks() {
134160
this.leakChecksDisabled = true;
135161
}
136162

163+
/**
164+
* Ability to set the test name if the rule is not used as an ordinary JUnit 4
165+
* rules. Temporarily necessary until JUnit 3 compatibility is dropped.
166+
*/
167+
void setTestName(String testName) {
168+
this.testName = testName;
169+
}
170+
137171
}

tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/UITestCase.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,6 @@ public UITestCase(String testName) {
6565
super(testName);
6666
}
6767

68-
/**
69-
* Outputs a trace message to the trace output device, if enabled.
70-
* By default, trace messages are sent to <code>System.out</code>.
71-
*
72-
* @param msg the trace message
73-
*/
74-
protected void trace(String msg) {
75-
System.out.println(msg);
76-
}
77-
7868
/**
7969
* Simple implementation of setUp. Subclasses are prevented from overriding this
8070
* method to maintain logging consistency. doSetUp() should be overridden
@@ -88,9 +78,9 @@ protected void trace(String msg) {
8878
@Override
8979
public final void setUp() throws Exception {
9080
super.setUp();
91-
closeTestWindows.before();
9281
String name = runningTest != null ? runningTest : this.getName();
93-
trace(TestRunLogUtil.formatTestStartMessage(name));
82+
closeTestWindows.setTestName(name);
83+
closeTestWindows.before();
9484
doSetUp();
9585
}
9686

@@ -116,8 +106,6 @@ protected void doSetUp() throws Exception {
116106
@After
117107
@Override
118108
public final void tearDown() throws Exception {
119-
String name = runningTest != null ? runningTest : this.getName();
120-
trace(TestRunLogUtil.formatTestFinishedMessage(name));
121109
doTearDown();
122110
closeTestWindows.after();
123111
}

0 commit comments

Comments
 (0)