17
17
import static org .junit .Assert .assertNotNull ;
18
18
19
19
import java .net .URL ;
20
- import java .util .Arrays ;
21
- import java .util .Collection ;
22
20
import java .util .Collections ;
21
+ import java .util .regex .Pattern ;
23
22
24
- import org .eclipse .core .resources .IProject ;
25
- import org .eclipse .core .resources .IProjectDescription ;
26
23
import org .eclipse .core .resources .IWorkspaceRoot ;
27
24
import org .eclipse .core .resources .IncrementalProjectBuilder ;
28
25
import org .eclipse .core .resources .ResourcesPlugin ;
29
26
import org .eclipse .core .runtime .FileLocator ;
30
- import org .eclipse .core .runtime .IPath ;
31
27
import org .eclipse .jdt .core .IJavaProject ;
32
28
import org .eclipse .jdt .core .IMethod ;
33
29
import org .eclipse .jdt .core .IPackageFragment ;
39
35
import org .eclipse .jdt .junit .model .ITestElement .Result ;
40
36
import org .eclipse .jdt .junit .model .ITestElementContainer ;
41
37
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 ;
42
40
import org .junit .Assert ;
43
41
import org .junit .Assume ;
44
42
import org .junit .BeforeClass ;
43
+ import org .junit .ClassRule ;
45
44
import org .junit .Test ;
45
+ import org .junit .rules .TestRule ;
46
46
import org .junit .runner .RunWith ;
47
47
import org .junit .runners .Parameterized ;
48
48
import org .junit .runners .Parameterized .Parameter ;
53
53
@ RunWith (Parameterized .class )
54
54
public class JUnitExecutionTest {
55
55
56
- @ Parameter
57
- public TestInput input ;
56
+ @ ClassRule
57
+ public static final TestRule CLEAR_WORKSPACE = ProjectUtils . DELETE_ALL_WORKSPACE_PROJECTS_BEFORE_AND_AFTER ;
58
58
59
59
@ BeforeClass
60
60
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
+ });
62
68
63
69
Bundle bundle = FrameworkUtil .getBundle (JUnitExecutionTest .class );
64
70
65
71
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 ));
79
74
}
80
-
81
75
workspaceRoot .getWorkspace ().build (IncrementalProjectBuilder .FULL_BUILD , null );
82
76
}
83
77
84
78
@ 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 ));
96
92
}
97
93
94
+ @ Parameter (0 )
95
+ public String testCaseName ; // Just for display
96
+ @ Parameter (1 )
97
+ public IJavaProject project ;
98
+
98
99
@ Test
99
100
public void executeType () throws Exception {
100
- IType testClass = input . findType ("Test1" );
101
+ IType testClass = findType (project , "Test1" );
101
102
ITestRunSession session = TestExecutionUtil .runTest (testClass );
102
103
103
104
assertThat (session .getChildren ()).hasSize (1 );
@@ -106,7 +107,7 @@ public void executeType() throws Exception {
106
107
107
108
@ Test
108
109
public void executePackage () throws Exception {
109
- IPackageFragment testPackage = input . findType ("Test1" ).getPackageFragment ();
110
+ IPackageFragment testPackage = findType (project , "Test1" ).getPackageFragment ();
110
111
ITestRunSession session = TestExecutionUtil .runTest (testPackage );
111
112
112
113
assertThat (session .getChildren ()).hasSize (2 );
@@ -115,15 +116,15 @@ public void executePackage() throws Exception {
115
116
116
117
@ Test
117
118
public void executeProject () throws Exception {
118
- ITestRunSession session = TestExecutionUtil .runTest (input . project );
119
+ ITestRunSession session = TestExecutionUtil .runTest (project );
119
120
120
121
assertThat (session .getChildren ()).hasSize (2 );
121
122
assertSuccessful (session );
122
123
}
123
124
124
125
@ Test
125
126
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 ]);
127
128
Assume .assumeTrue (testMethod .exists ());
128
129
ITestRunSession session = TestExecutionUtil .runTest (testMethod );
129
130
@@ -145,7 +146,8 @@ static void assertSuccessful(ITestRunSession session) {
145
146
private static void addFailureTraces (ITestElement element , AssertionError assertionFailedError ) {
146
147
FailureTrace trace = element .getFailureTrace ();
147
148
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 ()));
149
151
}
150
152
151
153
if (element instanceof ITestElementContainer ) {
@@ -155,28 +157,10 @@ private static void addFailureTraces(ITestElement element, AssertionError assert
155
157
}
156
158
}
157
159
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 ;
180
165
}
181
-
182
166
}
0 commit comments