From 3b6049f7c9314a56d0b9aa505963070dbe62c42e Mon Sep 17 00:00:00 2001 From: Heiko Klare Date: Sat, 3 Jan 2026 11:32:20 +0100 Subject: [PATCH] Replace Hamcrest matcher in resource tests with AssertJ assertion Hamcrest is available by default when using JUnit 4. With JUnit 5, Hamcrest has to be required as an additional dependency. At the same time, we already have a dependency to the more up-to-date AssertJ library. For that reason, this change adapts the last remaining Hamcrest usage in resource tests, a matcher for marker severities, with an equivalent assertion based on AssertJ. --- .../tests/resources/ProjectEncodingTest.java | 94 +++++++------------ 1 file changed, 36 insertions(+), 58 deletions(-) diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ProjectEncodingTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ProjectEncodingTest.java index b80ec2d3ccb..b6c08f72926 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ProjectEncodingTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ProjectEncodingTest.java @@ -18,8 +18,10 @@ import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor; import static org.eclipse.core.tests.resources.ResourceTestUtil.createUniqueString; import static org.eclipse.core.tests.resources.ResourceTestUtil.waitForBuild; -import static org.hamcrest.MatcherAssert.assertThat; +import org.assertj.core.api.AbstractAssert; +import org.assertj.core.api.Assertions; +import org.assertj.core.api.SoftAssertions; import org.eclipse.core.internal.resources.PreferenceInitializer; import org.eclipse.core.internal.resources.ValidateProjectEncoding; import org.eclipse.core.internal.utils.Messages; @@ -33,9 +35,6 @@ import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.core.tests.resources.util.WorkspaceResetExtension; import org.eclipse.osgi.util.NLS; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; -import org.hamcrest.DiagnosingMatcher; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -185,60 +184,39 @@ private void thenProjectHasNoEncodingMarker() throws Exception { } private void thenProjectHasEncodingMarkerOfSeverity(int expectedSeverity) throws Exception { - assertThat(project, hasEncodingMarkerOfSeverity(expectedSeverity)); - } - - private BaseMatcher hasEncodingMarkerOfSeverity(final int expectedSeverity) { - return new DiagnosingMatcher<>() { - - @Override - public boolean matches(Object item, Description mismatchDescription) { - IProject theProject = (IProject) item; - - try { - IMarker[] markers = theProject.findMarkers(ValidateProjectEncoding.MARKER_TYPE, false, - IResource.DEPTH_ONE); - if (markers.length == 1) { - IMarker marker = markers[0]; - String[] attributeNames = { IMarker.MESSAGE, IMarker.SEVERITY, IMarker.LOCATION }; - Object[] values = marker.getAttributes(attributeNames); - - boolean msgOk = getExpectedMarkerMessage().equals(values[0]); - boolean sevOk = ((Integer) expectedSeverity).equals(values[1]); - boolean locOk = project.getFullPath().toString().equals(values[2]); - - if (!msgOk) { - mismatchDescription.appendText("\n has marker message: " + values[0]); - } - if (!sevOk) { - mismatchDescription.appendText("\n has marker severity: " + values[1]); - } - if (!locOk) { - mismatchDescription.appendText("\n has marker location: " + values[2]); - } - - return msgOk && sevOk && locOk; - } - mismatchDescription.appendText("\n has " + markers.length + " encoding markers"); - - } catch (CoreException e) { - mismatchDescription.appendText("\n cannot access markers: " + e.getMessage()); - } - return false; - } - - @Override - public void describeTo(Description description) { - description.appendText("\n has marker of message: '" + getExpectedMarkerMessage() + "'"); - description.appendText("\n has marker severity: " + expectedSeverity); - description.appendText("\n has location: "); - } - - private String getExpectedMarkerMessage() { - return NLS.bind(Messages.resources_checkExplicitEncoding_problemText, project.getName()); - } - - }; + IProjectMatcher.assertThat(project).hasEncodingMarkerOfSeverity(expectedSeverity); + } + + private static class IProjectMatcher extends AbstractAssert { + + private IProjectMatcher(IProject actual) { + super(actual, IProjectMatcher.class); + } + + public static IProjectMatcher assertThat(IProject project) { + return new IProjectMatcher(project); + } + + public void hasEncodingMarkerOfSeverity(int expectedSeverity) throws CoreException { + isNotNull(); + + IMarker[] markers = actual.findMarkers(ValidateProjectEncoding.MARKER_TYPE, false, IResource.DEPTH_ONE); + Assertions.assertThat(markers).hasSize(1).allSatisfy(marker -> { + String[] attributeNames = { IMarker.MESSAGE, IMarker.SEVERITY, IMarker.LOCATION }; + Object[] values = marker.getAttributes(attributeNames); + + SoftAssertions softAssert = new SoftAssertions(); + softAssert.assertThat(values[0]).as("marker message").isEqualTo(getExpectedMarkerMessage()); + softAssert.assertThat(values[1]).as("marker severity").isEqualTo(expectedSeverity); + softAssert.assertThat(values[2]).as("marker location").isEqualTo(actual.getFullPath().toString()); + softAssert.assertAll(); + }); + } + + private String getExpectedMarkerMessage() { + return NLS.bind(Messages.resources_checkExplicitEncoding_problemText, actual.getName()); + } + } private void buildAndWaitForBuildFinish() {