1717import static org .junit .Assert .assertNotNull ;
1818
1919import java .net .URL ;
20- import java .util .Arrays ;
21- import java .util .Collection ;
2220import java .util .Collections ;
21+ import java .util .regex .Pattern ;
2322
24- import org .eclipse .core .resources .IProject ;
25- import org .eclipse .core .resources .IProjectDescription ;
2623import org .eclipse .core .resources .IWorkspaceRoot ;
2724import org .eclipse .core .resources .IncrementalProjectBuilder ;
2825import org .eclipse .core .resources .ResourcesPlugin ;
2926import org .eclipse .core .runtime .FileLocator ;
30- import org .eclipse .core .runtime .IPath ;
3127import org .eclipse .jdt .core .IJavaProject ;
3228import org .eclipse .jdt .core .IMethod ;
3329import org .eclipse .jdt .core .IPackageFragment ;
3935import org .eclipse .jdt .junit .model .ITestElement .Result ;
4036import org .eclipse .jdt .junit .model .ITestElementContainer ;
4137import org .eclipse .jdt .junit .model .ITestRunSession ;
38+ import org .eclipse .pde .ui .tests .util .ProjectUtils ;
39+ import org .eclipse .pde .ui .tests .util .TargetPlatformUtil ;
4240import org .junit .Assert ;
4341import org .junit .Assume ;
4442import org .junit .BeforeClass ;
43+ import org .junit .ClassRule ;
4544import org .junit .Test ;
45+ import org .junit .rules .TestRule ;
4646import org .junit .runner .RunWith ;
4747import org .junit .runners .Parameterized ;
4848import org .junit .runners .Parameterized .Parameter ;
5353@ RunWith (Parameterized .class )
5454public 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}
0 commit comments