From c62d47a5e2a3d4ae3b986f7f1cfd57ea38f50c1b Mon Sep 17 00:00:00 2001 From: Heiko Klare Date: Thu, 15 Aug 2024 13:33:12 +0200 Subject: [PATCH] Migrate resource refresh tests to JUnit 5 #903 Migrates the tests in org.eclipse.core.tests.resources.refresh to JUnit 5. Replaces test annotations and migrates assertions. The change also improves test isolation for the RefreshProviderTest. The tests could not be executed individually, because modified refresh settings are not followed by a wait for a potentially pending refresh request and resetting the TestRefreshProvider is performed at before instead of after changing the refresh settings. Contributes to https://github.com/eclipse-platform/eclipse.platform/issues/903 --- .../resources/refresh/RefreshJobTest.java | 77 +++++++++---------- .../refresh/RefreshProviderTest.java | 46 ++++++----- 2 files changed, 58 insertions(+), 65 deletions(-) diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/refresh/RefreshJobTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/refresh/RefreshJobTest.java index 4e09afae69e..8984aed1286 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/refresh/RefreshJobTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/refresh/RefreshJobTest.java @@ -21,9 +21,9 @@ import static org.eclipse.core.internal.refresh.RefreshJob.SLOW_REFRESH_THRESHOLD; import static org.eclipse.core.internal.refresh.RefreshJob.UPDATE_DELAY; import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.Field; import java.nio.file.Files; @@ -50,25 +50,20 @@ import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.core.tests.resources.TestUtil; -import org.eclipse.core.tests.resources.WorkspaceTestRule; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestName; +import org.eclipse.core.tests.resources.util.WorkspaceResetExtension; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; +import org.junit.jupiter.api.extension.ExtendWith; /** * Tests for RefreshJob */ +@ExtendWith(WorkspaceResetExtension.class) public class RefreshJobTest { - @Rule - public TestName testName = new TestName(); - - @Rule - public WorkspaceTestRule workspaceRule = new WorkspaceTestRule(); - private static final String REFRESH_JOB_FIELD_NAME = "refreshJob"; private boolean defaultRefresh; private boolean shouldResetDefault; @@ -82,7 +77,7 @@ public class RefreshJobTest { private RefreshJob originalJob; - @Before + @BeforeEach public void setUp() { IEclipsePreferences prefs = getPrefs(); defaultRefresh = prefs.getBoolean(ResourcesPlugin.PREF_AUTO_REFRESH, false); @@ -95,7 +90,7 @@ public void setUp() { updateDelay = 0; } - @After + @AfterEach public void tearDown() throws Exception { restoreRefreshJob(); if (shouldResetDefault) { @@ -171,7 +166,7 @@ public void testFastRefresh() throws Exception { * Test that lot of directories can be refreshed with max depth of 8 */ @Test - @Ignore("test is disabled because it needs 400 seconds on fast SSD on Linux") + @Disabled("test is disabled because it needs 400 seconds on fast SSD on Linux") public void testSmallRecursionRefresh() throws Exception { String name = "testSmallRecursionRefresh"; maxRecursionDeep = 8; @@ -194,7 +189,7 @@ public void testSmallRecursionRefresh() throws Exception { * Test that lot of directories can be refreshed with max possible depth */ @Test - @Ignore("test is disabled because it needs 250 seconds on fast SSD on Linux") + @Disabled("test is disabled because it needs 250 seconds on fast SSD on Linux") public void testBigRecursionDeepRefresh() throws Exception { String name = "testBigRecursionDeepRefresh"; maxRecursionDeep = MAX_RECURSION;// 2 << 29; // 1073741824 @@ -240,9 +235,9 @@ public void testSlowRefresh() throws Exception { * refresh if */ @Test - public void testProjectRule() throws Exception { + public void testProjectRule(TestInfo testInfo) throws Exception { TestRefreshJob refreshJob = createAndReplaceDefaultJob(); - IProject project = createProject(testName.getMethodName()); + IProject project = createProject(testInfo.getDisplayName()); try { IFolder parent = project.getFolder("parent"); parent.create(true, true, null); @@ -258,31 +253,31 @@ public void testProjectRule() throws Exception { Job.getJobManager().beginRule(project, null); refreshJob.refresh(project); Thread.sleep(1000); - assertTrue("Refresh was not started", refreshJob.refreshStarted); - assertFalse("Refresh should wait on rule", refreshJob.refreshDone); - assertEquals("Should not visit anything yet", Collections.EMPTY_SET, refreshJob.visitedResources); + assertTrue(refreshJob.refreshStarted, "Refresh was not started"); + assertFalse(refreshJob.refreshDone, "Refresh should wait on rule"); + assertEquals(Collections.EMPTY_SET, refreshJob.visitedResources, "Should not visit anything yet"); Job.getJobManager().endRule(project); releaseRule = false; - TestUtil.waitForJobs(testName.getMethodName(), 100, 1000); - assertTrue("Refresh was not started", refreshJob.refreshStarted); - assertTrue("Refresh was not finished", refreshJob.refreshDone); - assertEquals("Missing refresh", expected, refreshJob.visitedResources); + TestUtil.waitForJobs(testInfo.getDisplayName(), 100, 1000); + assertTrue(refreshJob.refreshStarted, "Refresh was not started"); + assertTrue(refreshJob.refreshDone, "Refresh was not finished"); + assertEquals(expected, refreshJob.visitedResources, "Missing refresh"); } finally { if (releaseRule) { Job.getJobManager().endRule(project); } } } finally { - deleteProject(testName.getMethodName()); + deleteProject(testInfo.getDisplayName()); } } // Disabled for now, is unstable @Test - @Ignore("disabled for now, is unstable") - public void testUnrelatedRule() throws Exception { + @Disabled("disabled for now, is unstable") + public void testUnrelatedRule(TestInfo testInfo) throws Exception { TestRefreshJob refreshJob = createAndReplaceDefaultJob(); - IProject project = createProject(testName.getMethodName()); + IProject project = createProject(testInfo.getDisplayName()); try { IFolder parent = project.getFolder("parent"); parent.create(true, true, null); @@ -296,19 +291,19 @@ public void testUnrelatedRule() throws Exception { try { Job.getJobManager().beginRule(rule, null); refreshJob.refresh(child); - TestUtil.waitForJobs(testName.getMethodName(), 10, 60_000, ResourcesPlugin.FAMILY_AUTO_REFRESH); - assertTrue("Refresh was not started", refreshJob.refreshStarted); - assertTrue("Refresh was not finished", refreshJob.refreshDone); + TestUtil.waitForJobs(testInfo.getDisplayName(), 10, 60_000, ResourcesPlugin.FAMILY_AUTO_REFRESH); + assertTrue(refreshJob.refreshStarted, "Refresh was not started"); + assertTrue(refreshJob.refreshDone, "Refresh was not finished"); Job.getJobManager().endRule(rule); releaseRule = false; - assertEquals("Missing refresh", expected, refreshJob.visitedResources); + assertEquals(expected, refreshJob.visitedResources, "Missing refresh"); } finally { if (releaseRule) { Job.getJobManager().endRule(rule); } } } finally { - deleteProject(testName.getMethodName()); + deleteProject(testInfo.getDisplayName()); } } @@ -322,7 +317,7 @@ private void runtest(String name, int minDepth, int maxDepth, int directoriesCou AtomicInteger result = new AtomicInteger(0); createDirectoriesViaFileIo(projectRoot.toFile().toPath(), directoriesCount, filesCount, createDepth, result); - assertTrue("Expect to generate some files", result.get() > 0); + assertTrue(result.get() > 0, "Expect to generate some files"); System.out.println("\nTest " + name + " generated " + result + " files"); project.open(null); @@ -337,8 +332,8 @@ private void runtest(String name, int minDepth, int maxDepth, int directoriesCou } private void assertDepth(TestRefreshJob refreshJob, int minDepth, int maxDepth) { - assertEquals("Unexpected min depth", minDepth, refreshJob.minDepth); - assertEquals("Unexpected max depth", maxDepth, refreshJob.maxDepth); + assertEquals(minDepth, refreshJob.minDepth, "Unexpected min depth"); + assertEquals(maxDepth, refreshJob.maxDepth, "Unexpected max depth"); } private void assertAllResourcesRefreshed(IProject project, TestRefreshJob refreshJob) throws Exception { diff --git a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/refresh/RefreshProviderTest.java b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/refresh/RefreshProviderTest.java index 9ea5c23b8e6..591837b65d3 100644 --- a/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/refresh/RefreshProviderTest.java +++ b/resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/refresh/RefreshProviderTest.java @@ -15,13 +15,13 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace; -import static org.eclipse.core.tests.harness.FileSystemHelper.getRandomLocation; import static org.eclipse.core.tests.resources.ResourceTestUtil.createInWorkspace; import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor; import static org.eclipse.core.tests.resources.ResourceTestUtil.removeFromWorkspace; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import java.nio.file.Path; import java.util.HashMap; import java.util.Map; import junit.framework.AssertionFailedError; @@ -35,42 +35,41 @@ import org.eclipse.core.runtime.jobs.Job; import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.InstanceScope; +import org.eclipse.core.tests.resources.ResourceTestUtil; import org.eclipse.core.tests.resources.TestUtil; -import org.eclipse.core.tests.resources.WorkspaceTestRule; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestName; +import org.eclipse.core.tests.resources.util.WorkspaceResetExtension; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; /** * Tests the IRefreshMonitor interface */ +@ExtendWith(WorkspaceResetExtension.class) public class RefreshProviderTest { - @Rule - public TestName testName = new TestName(); - - @Rule - public WorkspaceTestRule workspaceRule = new WorkspaceTestRule(); - private boolean originalRefreshSetting; - @Before + @BeforeEach public void setUp() { - TestRefreshProvider.reset(); //turn on autorefresh IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(ResourcesPlugin.PI_RESOURCES); originalRefreshSetting = prefs.getBoolean(ResourcesPlugin.PREF_AUTO_REFRESH, false); prefs.putBoolean(ResourcesPlugin.PREF_AUTO_REFRESH, true); + ResourceTestUtil.waitForRefresh(); + TestRefreshProvider.reset(); } - @After + @AfterEach public void tearDown() { //turn off autorefresh - TestRefreshProvider.reset(); IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(ResourcesPlugin.PI_RESOURCES); prefs.putBoolean(ResourcesPlugin.PREF_AUTO_REFRESH, originalRefreshSetting); + ResourceTestUtil.waitForRefresh(); + TestRefreshProvider.reset(); } /** @@ -78,9 +77,8 @@ public void tearDown() { * file is created and deleted. */ @Test - public void testLinkedFile() throws Exception { - IPath location = getRandomLocation(); - workspaceRule.deleteOnTearDown(location); + public void testLinkedFile(@TempDir Path tempDirectory) throws Exception { + IPath location = IPath.fromPath(tempDirectory).append("test"); String name = "testUnmonitorLinkedResource"; IProject project = getWorkspace().getRoot().getProject(name); createInWorkspace(project); @@ -135,14 +133,14 @@ public void testProjectCloseOpen() throws Exception { * is closed or opened. */ @Test - public void testProjectCreateDelete() throws Exception { + public void testProjectCreateDelete(TestInfo testInfo) throws Exception { String name = "testProjectCreateDelete"; final int maxRuns = 1000; int i = 0; Map fails = new HashMap<>(); for (; i < maxRuns; i++) { if (i % 50 == 0) { - TestUtil.waitForJobs(testName.getMethodName(), 5, 100); + TestUtil.waitForJobs(testInfo.getDisplayName(), 5, 100); } try { assertTrue(createProject(name).isAccessible());