Skip to content

Commit

Permalink
Fixes broken Unit4 & 5 support after hamcrest update from 1.x to 2.x
Browse files Browse the repository at this point in the history
- additionally avoids returning "null" classpath entries from JUnit 5
container and properly logs if a bundle was missing.

See eclipse-platform/eclipse.platform.releng.aggregator#1245
  • Loading branch information
iloveeclipse committed Aug 11, 2023
1 parent 7787b3f commit 7e18055
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public class JUnitPreferencesConstants {
*/
public static final String HAMCREST_CORE_JAVADOC= JUnitCorePlugin.PLUGIN_ID + ".junit4.hamcrest.core.javadoclocation"; //$NON-NLS-1$

/**
* Javadoc location for org.hamcrest (JUnit 4)
*/
public static final String HAMCREST_JAVADOC= JUnitCorePlugin.PLUGIN_ID + ".junit4.hamcrest.javadoclocation"; //$NON-NLS-1$

/**
* Javadoc location for org.junit.jupiter.api (JUnit 5)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ public IClasspathEntry getLibraryEntry() {

return JavaCore.newLibraryEntry(bundleRootLocation, srcLocation, null, getAccessRules(), attributes, false);
}
String message = "Unable to compute bundle location for '" + bundleId + "' with range " + versionRange; //$NON-NLS-1$//$NON-NLS-2$
JUnitCorePlugin.log(Status.error(message, new IllegalStateException(message)));
return null;
}

Expand Down Expand Up @@ -303,7 +305,10 @@ public boolean accept(File dir, String name) {
"org.junit", new VersionRange("[4.13.0,5.0.0)"), null, "org.junit_4.*.jar", "org.junit.source", "source-bundle/", JUnitPreferencesConstants.JUNIT4_JAVADOC); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$

private static final JUnitPluginDescription HAMCREST_CORE_PLUGIN= new JUnitPluginDescription(
"org.hamcrest.core", new VersionRange("[1.1.0,2.0.0)"), null, "org.hamcrest.core_1.*.jar", "org.hamcrest.core.source", "source-bundle/", JUnitPreferencesConstants.HAMCREST_CORE_JAVADOC); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
"org.hamcrest.core", new VersionRange("[2.2.0,2.3.0)"), null, "org.hamcrest.core_2.*.jar", "org.hamcrest.core.source", "source-bundle/", JUnitPreferencesConstants.HAMCREST_CORE_JAVADOC); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$

private static final JUnitPluginDescription HAMCREST_PLUGIN= new JUnitPluginDescription(
"org.hamcrest", new VersionRange("[2.2.0,2.3.0)"), null, "org.hamcrest_2.*.jar", "org.hamcrest.source", "source-bundle/", JUnitPreferencesConstants.HAMCREST_JAVADOC); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$

public static final JUnitPluginDescription JUNIT_JUPITER_API_PLUGIN= new JUnitPluginDescription(
"junit-jupiter-api", new VersionRange("[5.0.0,6.0.0)"), null, "junit-jupiter-api_5.*.jar", "junit-jupiter-api.source", "", //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$//$NON-NLS-5$
Expand Down Expand Up @@ -423,6 +428,13 @@ public static IClasspathEntry getHamcrestCoreLibraryEntry() {
return HAMCREST_CORE_PLUGIN.getLibraryEntry();
}

/**
* @return the org.hamcrest library, or <code>null</code> if not available
*/
public static IClasspathEntry getHamcrestLibraryEntry() {
return HAMCREST_PLUGIN.getLibraryEntry();
}

/**
* @return the org.junit.jupiter.api library, or <code>null</code> if not available
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,16 @@ public void initialize(IPath containerPath, IJavaProject project) throws CoreExc

private static JUnitContainer getNewContainer(IPath containerPath) {
List<IClasspathEntry> entriesList= new ArrayList<>();
IClasspathEntry entry= null;
IClasspathEntry entry2= null;
String version= containerPath.segment(1);
if (null != version) switch (version) {
case JUNIT3_8_1:
case JUNIT3:
entry= BuildPathSupport.getJUnit3LibraryEntry();
if (entry == null) { // JUnit 4 includes most of JUnit 3, so let's cheat
entry= BuildPathSupport.getJUnit4as3LibraryEntry();
}
entriesList.add(BuildPathSupport.getJUnit4as3LibraryEntry());
break;
case JUNIT4:
entry= BuildPathSupport.getJUnit4LibraryEntry();
entry2= BuildPathSupport.getHamcrestCoreLibraryEntry();
entriesList.add(BuildPathSupport.getJUnit4LibraryEntry());
entriesList.add(BuildPathSupport.getHamcrestLibraryEntry());
entriesList.add(BuildPathSupport.getHamcrestCoreLibraryEntry());
break;
case JUNIT5:
entriesList.add(BuildPathSupport.getJUnitJupiterApiLibraryEntry());
Expand All @@ -137,21 +133,15 @@ private static JUnitContainer getNewContainer(IPath containerPath) {
entriesList.add(BuildPathSupport.getJUnitOpentest4jLibraryEntry());
entriesList.add(BuildPathSupport.getJUnitApiGuardianLibraryEntry());
entriesList.add(BuildPathSupport.getJUnit4LibraryEntry());
entriesList.add(BuildPathSupport.getHamcrestCoreLibraryEntry());
entriesList.add(BuildPathSupport.getHamcrestLibraryEntry());
entriesList.add(BuildPathSupport.getHamcrestCoreLibraryEntry());
// errors will be reported above
entriesList.removeIf(e -> e == null);
break;
default:
break;
}
IClasspathEntry[] entries;
if (!entriesList.isEmpty() ) {
entries= entriesList.toArray(new IClasspathEntry[entriesList.size()]);
} else if (entry == null) {
entries= new IClasspathEntry[] { };
} else if (entry2 == null) {
entries= new IClasspathEntry[] { entry };
} else {
entries= new IClasspathEntry[] { entry, entry2 };
}
IClasspathEntry[] entries= entriesList.toArray(new IClasspathEntry[entriesList.size()]);
return new JUnitContainer(containerPath, entries);
}

Expand Down

0 comments on commit 7e18055

Please sign in to comment.