Skip to content

Commit

Permalink
Migrate resource refresh tests to JUnit 5 eclipse-platform#903
Browse files Browse the repository at this point in the history
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
eclipse-platform#903
  • Loading branch information
HeikoKlare committed Aug 15, 2024
1 parent 6b0a2e0 commit c62d47a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -95,7 +90,7 @@ public void setUp() {
updateDelay = 0;
}

@After
@AfterEach
public void tearDown() throws Exception {
restoreRefreshJob();
if (shouldResetDefault) {
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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());
}
}

Expand All @@ -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);
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,52 +35,50 @@
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();
}

/**
* Test to ensure that a refresh provider is given the correct events when a linked
* 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);
Expand Down Expand Up @@ -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<Integer, Throwable> 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());
Expand Down

0 comments on commit c62d47a

Please sign in to comment.