Skip to content

Commit f8afe21

Browse files
committed
Clean-up and simplify PDE JUnit Runtime tests
1 parent f49b833 commit f8afe21

File tree

5 files changed

+74
-127
lines changed

5 files changed

+74
-127
lines changed

ui/org.eclipse.pde.junit.runtime.tests/META-INF/MANIFEST.MF

+1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="3.29.0",
1919
org.eclipse.pde.ui.tests;bundle-version="3.11.500"
2020
Import-Package: org.assertj.core.api;version="3.14.0",
2121
org.junit,
22+
org.junit.rules,
2223
org.junit.runner,
2324
org.junit.runners

ui/org.eclipse.pde.junit.runtime.tests/src/org/eclipse/pde/junit/runtime/tests/JUnit5SuiteExecutionTest.java

+24-26
Original file line numberDiff line numberDiff line change
@@ -14,76 +14,74 @@
1414
package org.eclipse.pde.junit.runtime.tests;
1515

1616
import static org.assertj.core.api.Assertions.assertThat;
17+
import static org.eclipse.pde.junit.runtime.tests.JUnitExecutionTest.findType;
18+
19+
import java.util.StringJoiner;
1720

1821
import org.eclipse.core.runtime.Platform;
22+
import org.eclipse.jdt.core.IJavaProject;
1923
import org.eclipse.jdt.internal.junit.model.TestElement;
2024
import org.eclipse.jdt.junit.model.ITestElement;
2125
import org.eclipse.jdt.junit.model.ITestElementContainer;
2226
import org.eclipse.jdt.junit.model.ITestRunSession;
23-
import org.eclipse.pde.junit.runtime.tests.JUnitExecutionTest.TestInput;
2427
import org.junit.Assert;
2528
import org.junit.BeforeClass;
2629
import org.junit.Test;
2730

2831
public class JUnit5SuiteExecutionTest {
2932

30-
private static TestInput input;
33+
private static IJavaProject project;
3134

3235
@BeforeClass
3336
public static void setupProjects() throws Exception {
3437
Assert.assertNotNull("junit-platform-suite-engine bundle missing", Platform.getBundle("junit-platform-suite-engine"));
3538
Assert.assertNotNull("org.eclipse.jdt.junit5.runtime bundle missing", Platform.getBundle("org.eclipse.jdt.junit5.runtime"));
36-
39+
3740
JUnitExecutionTest.setupProjects();
38-
input = new TestInput("JUnit5 Suite", "verification.tests.junit5.suite");
41+
project = JUnitExecutionTest.getJProject("verification.tests.junit5.suite");
3942
}
4043

4144
@Test
4245
public void executeSuite() throws Exception {
43-
ITestRunSession session = TestExecutionUtil.runTest(input.findType("TestSuite"));
44-
46+
ITestRunSession session = TestExecutionUtil.runTest(findType(project, "TestSuite"));
4547
JUnitExecutionTest.assertSuccessful(session);
46-
Assert.assertEquals(
47-
"verification.tests.junit5.suite.TestSuite\n"
48-
+ " JUnit Jupiter\n"
49-
+ " verification.tests.junit5.Test1\n"
50-
+ " test1(verification.tests.junit5.Test1)\n"
51-
+ " test2(verification.tests.junit5.Test1)\n"
52-
+ " verification.tests.junit5.Test2\n"
53-
+ " test(verification.tests.junit5.Test2)\n",
54-
toString(session));
48+
Assert.assertEquals("""
49+
verification.tests.junit5.suite.TestSuite
50+
JUnit Jupiter
51+
verification.tests.junit5.Test1
52+
test1(verification.tests.junit5.Test1)
53+
test2(verification.tests.junit5.Test1)
54+
verification.tests.junit5.Test2
55+
test(verification.tests.junit5.Test2)
56+
""".strip(), toString(session).strip());
5557
}
5658

5759
@Test
5860
public void executePackage() throws Exception {
59-
ITestRunSession session = TestExecutionUtil.runTest(input.findType("TestSuite").getPackageFragment());
61+
ITestRunSession session = TestExecutionUtil.runTest(findType(project, "TestSuite").getPackageFragment());
6062
JUnitExecutionTest.assertSuccessful(session);
6163
assertThat(session.getChildren()).isNotEmpty();
6264
}
6365

6466
@Test
6567
public void executeProject() throws Exception {
66-
ITestRunSession session = TestExecutionUtil.runTest(input.project);
68+
ITestRunSession session = TestExecutionUtil.runTest(project);
6769
JUnitExecutionTest.assertSuccessful(session);
6870
assertThat(session.getChildren()).isNotEmpty();
6971
}
7072

7173
private static String toString(ITestRunSession session) {
72-
StringBuilder sb = new StringBuilder();
74+
StringJoiner sb = new StringJoiner("\n");
7375
for (ITestElement element : session.getChildren()) {
7476
append(sb, element, 0);
7577
}
76-
7778
return sb.toString();
7879
}
7980

80-
private static void append(StringBuilder sb, ITestElement element, int indent) {
81-
sb.append(" ".repeat(indent));
82-
sb.append(((TestElement) element).getTestName());
83-
sb.append('\n');
84-
85-
if (element instanceof ITestElementContainer) {
86-
for (ITestElement child : ((ITestElementContainer) element).getChildren()) {
81+
private static void append(StringJoiner sb, ITestElement element, int indent) {
82+
sb.add(" ".repeat(indent) + ((TestElement) element).getTestName());
83+
if (element instanceof ITestElementContainer container) {
84+
for (ITestElement child : container.getChildren()) {
8785
append(sb, child, indent + 1);
8886
}
8987
}

ui/org.eclipse.pde.junit.runtime.tests/src/org/eclipse/pde/junit/runtime/tests/JUnitExecutionTest.java

+45-61
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,13 @@
1717
import static org.junit.Assert.assertNotNull;
1818

1919
import java.net.URL;
20-
import java.util.Arrays;
21-
import java.util.Collection;
2220
import java.util.Collections;
21+
import java.util.regex.Pattern;
2322

24-
import org.eclipse.core.resources.IProject;
25-
import org.eclipse.core.resources.IProjectDescription;
2623
import org.eclipse.core.resources.IWorkspaceRoot;
2724
import org.eclipse.core.resources.IncrementalProjectBuilder;
2825
import org.eclipse.core.resources.ResourcesPlugin;
2926
import org.eclipse.core.runtime.FileLocator;
30-
import org.eclipse.core.runtime.IPath;
3127
import org.eclipse.jdt.core.IJavaProject;
3228
import org.eclipse.jdt.core.IMethod;
3329
import org.eclipse.jdt.core.IPackageFragment;
@@ -39,10 +35,14 @@
3935
import org.eclipse.jdt.junit.model.ITestElement.Result;
4036
import org.eclipse.jdt.junit.model.ITestElementContainer;
4137
import org.eclipse.jdt.junit.model.ITestRunSession;
38+
import org.eclipse.pde.ui.tests.util.ProjectUtils;
39+
import org.eclipse.pde.ui.tests.util.TargetPlatformUtil;
4240
import org.junit.Assert;
4341
import org.junit.Assume;
4442
import org.junit.BeforeClass;
43+
import org.junit.ClassRule;
4544
import org.junit.Test;
45+
import org.junit.rules.TestRule;
4646
import org.junit.runner.RunWith;
4747
import org.junit.runners.Parameterized;
4848
import org.junit.runners.Parameterized.Parameter;
@@ -53,51 +53,52 @@
5353
@RunWith(Parameterized.class)
5454
public class JUnitExecutionTest {
5555

56-
@Parameter
57-
public TestInput input;
56+
@ClassRule
57+
public static final TestRule CLEAR_WORKSPACE = ProjectUtils.DELETE_ALL_WORKSPACE_PROJECTS_BEFORE_AND_AFTER;
5858

5959
@BeforeClass
6060
public static void setupProjects() throws Exception {
61-
TargetPlatformUtil.setRunningPlatformAsTarget();
61+
Pattern junitRuntimeIDs = Pattern
62+
.compile("\\.junit\\d*\\.runtime|junit\\..+\\.engine$|org\\.junit\\.platform\\.launcher");
63+
TargetPlatformUtil.setRunningPlatformSubSetAsTarget(TargetPlatformUtil.class + "_target", b -> {
64+
// filter out junit.runtime and test engine bundles from the target platform
65+
// this tests the scenario where PDE supplies them from the host installation
66+
return !junitRuntimeIDs.matcher(b.getSymbolicName()).find();
67+
});
6268

6369
Bundle bundle = FrameworkUtil.getBundle(JUnitExecutionTest.class);
6470

6571
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
66-
for (URL resource : Collections.list(bundle.findEntries("test-bundles", "*", false))) {
67-
IPath resourcePath = IPath.fromOSString(FileLocator.toFileURL(resource).getPath());
68-
IPath descriptionPath = resourcePath.append(IProjectDescription.DESCRIPTION_FILE_NAME);
69-
if (!descriptionPath.toFile().exists())
70-
continue;
71-
72-
IProjectDescription projectDescription = workspaceRoot.getWorkspace()
73-
.loadProjectDescription(descriptionPath);
74-
IProject project = workspaceRoot.getProject(resourcePath.lastSegment());
75-
if (!project.exists()) {
76-
project.create(projectDescription, null);
77-
project.open(null);
78-
}
72+
for (URL resource : Collections.list(bundle.findEntries("test-bundles", "verification.tests.*", false))) {
73+
ProjectUtils.importTestProject(FileLocator.toFileURL(resource));
7974
}
80-
8175
workspaceRoot.getWorkspace().build(IncrementalProjectBuilder.FULL_BUILD, null);
8276
}
8377

8478
@Parameters(name = "{0}")
85-
public static Collection<TestInput> parameters() {
86-
return Arrays.asList(
87-
new TestInput("JUnit5", "verification.tests.junit5"),
88-
new TestInput("JUnit5 Fragment", "verification.tests.junit5.fragment"),
89-
new TestInput("JUnit4", "verification.tests.junit4"),
90-
new TestInput("JUnit4 Fragment", "verification.tests.junit4.fragment"),
91-
new TestInput("JUnit4 (JUnitPlatform)", "verification.tests.junit4.platform"),
92-
new TestInput("JUnit4 (JUnitPlatform) Fragment", "verification.tests.junit4.platform.fragment"),
93-
new TestInput("Java 11 bundle with module limit", "verification.tests.limitmodules"),
94-
new TestInput("Using a 'test' source folder", "verification.tests.testfolder")
95-
);
79+
public static Object[][] parameters() {
80+
return new Object[][] { { "JUnit5", getJProject("verification.tests.junit5") },
81+
{ "JUnit5 Fragment", getJProject("verification.tests.junit5.fragment") },
82+
{ "JUnit4", getJProject("verification.tests.junit4") },
83+
{ "JUnit4 Fragment", getJProject("verification.tests.junit4.fragment") },
84+
{ "JUnit4 (JUnitPlatform)", getJProject("verification.tests.junit4.platform") },
85+
{ "JUnit4 (JUnitPlatform) Fragment", getJProject("verification.tests.junit4.platform.fragment") },
86+
{ "Java 11 bundle with module limit", getJProject("verification.tests.limitmodules") },
87+
{ "Using a 'test' source folder", getJProject("verification.tests.testfolder") } };
88+
}
89+
90+
static IJavaProject getJProject(String projectName) {
91+
return JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().getProject(projectName));
9692
}
9793

94+
@Parameter(0)
95+
public String testCaseName; // Just for display
96+
@Parameter(1)
97+
public IJavaProject project;
98+
9899
@Test
99100
public void executeType() throws Exception {
100-
IType testClass = input.findType("Test1");
101+
IType testClass = findType(project, "Test1");
101102
ITestRunSession session = TestExecutionUtil.runTest(testClass);
102103

103104
assertThat(session.getChildren()).hasSize(1);
@@ -106,7 +107,7 @@ public void executeType() throws Exception {
106107

107108
@Test
108109
public void executePackage() throws Exception {
109-
IPackageFragment testPackage = input.findType("Test1").getPackageFragment();
110+
IPackageFragment testPackage = findType(project, "Test1").getPackageFragment();
110111
ITestRunSession session = TestExecutionUtil.runTest(testPackage);
111112

112113
assertThat(session.getChildren()).hasSize(2);
@@ -115,15 +116,15 @@ public void executePackage() throws Exception {
115116

116117
@Test
117118
public void executeProject() throws Exception {
118-
ITestRunSession session = TestExecutionUtil.runTest(input.project);
119+
ITestRunSession session = TestExecutionUtil.runTest(project);
119120

120121
assertThat(session.getChildren()).hasSize(2);
121122
assertSuccessful(session);
122123
}
123124

124125
@Test
125126
public void executeMethod() throws Exception {
126-
IMethod testMethod = input.findType("Test1").getMethod("test1", new String[0]);
127+
IMethod testMethod = findType(project, "Test1").getMethod("test1", new String[0]);
127128
Assume.assumeTrue(testMethod.exists());
128129
ITestRunSession session = TestExecutionUtil.runTest(testMethod);
129130

@@ -145,7 +146,8 @@ static void assertSuccessful(ITestRunSession session) {
145146
private static void addFailureTraces(ITestElement element, AssertionError assertionFailedError) {
146147
FailureTrace trace = element.getFailureTrace();
147148
if (trace != null) {
148-
assertionFailedError.addSuppressed(new AssertionError("FailureTrace of " + element + ":\n\n" + trace.getTrace()));
149+
assertionFailedError
150+
.addSuppressed(new AssertionError("FailureTrace of " + element + ":\n\n" + trace.getTrace()));
149151
}
150152

151153
if (element instanceof ITestElementContainer) {
@@ -155,28 +157,10 @@ private static void addFailureTraces(ITestElement element, AssertionError assert
155157
}
156158
}
157159

158-
static class TestInput {
159-
private final String displayName;
160-
161-
final IJavaProject project;
162-
163-
public TestInput(String displayName, String projectName) {
164-
this.displayName = displayName;
165-
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
166-
this.project = JavaCore.create(project);
167-
}
168-
169-
@Override
170-
public String toString() {
171-
return displayName;
172-
}
173-
174-
public IType findType(String name) throws JavaModelException {
175-
IType type = project.findType(project.getElementName(), name);
176-
assertNotNull(type);
177-
Assert.assertTrue(type.exists());
178-
return type;
179-
}
160+
static IType findType(IJavaProject project, String name) throws JavaModelException {
161+
IType type = project.findType(project.getElementName(), name);
162+
assertNotNull(type);
163+
Assert.assertTrue(type.exists());
164+
return type;
180165
}
181-
182166
}

ui/org.eclipse.pde.junit.runtime.tests/src/org/eclipse/pde/junit/runtime/tests/TargetPlatformUtil.java

-40
This file was deleted.

ui/org.eclipse.pde.ui.tests/src/org/eclipse/pde/ui/tests/util/ProjectUtils.java

+4
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ public static IProject importTestProject(String path) throws IOException, CoreEx
169169
if (entry == null) {
170170
throw new IllegalArgumentException(path + " does not exist");
171171
}
172+
return importTestProject(entry);
173+
}
174+
175+
public static IProject importTestProject(URL entry) throws CoreException {
172176
IPath projectFile = IPath.fromPortableString(entry.getPath()).append(IProjectDescription.DESCRIPTION_FILE_NAME);
173177
IWorkspace workspace = ResourcesPlugin.getWorkspace();
174178
IProjectDescription projectDescription = workspace.loadProjectDescription(projectFile);

0 commit comments

Comments
 (0)