Skip to content

Commit

Permalink
Migrate all tests in o.e.c.tests.resources.refresh to JUnit 4 #903
Browse files Browse the repository at this point in the history
* Replace the ResourceTest class hierarchy with WorkspaceTestRule
* Add @test annotations

Contributes to
#903
  • Loading branch information
HeikoKlare committed Dec 9, 2023
1 parent 74cd359 commit a5789fb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import static org.eclipse.core.internal.refresh.RefreshJob.UPDATE_DELAY;
import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.lang.reflect.Field;
import java.nio.file.Files;
Expand All @@ -47,13 +50,25 @@
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.ResourceTest;
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;

/**
* Tests for RefreshJob
*/
public class RefreshJobTest extends ResourceTest {
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;
Expand All @@ -68,9 +83,8 @@ public class RefreshJobTest extends ResourceTest {

private RefreshJob originalJob;

@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() {
IEclipsePreferences prefs = getPrefs();
defaultRefresh = prefs.getBoolean(ResourcesPlugin.PREF_AUTO_REFRESH, false);
if (defaultRefresh) {
Expand All @@ -82,13 +96,12 @@ protected void setUp() throws Exception {
updateDelay = 0;
}

@Override
protected void tearDown() throws Exception {
@After
public void tearDown() throws Exception {
restoreRefreshJob();
if (shouldResetDefault) {
getPrefs().putBoolean(ResourcesPlugin.PREF_AUTO_REFRESH, defaultRefresh);
}
super.tearDown();
}

private IEclipsePreferences getPrefs() {
Expand All @@ -98,6 +111,7 @@ private IEclipsePreferences getPrefs() {
/**
* Test to ensure that there is no endless loop on refresh
*/
@Test
public void testBug578487_refreshLoop() throws Exception {
String name = "testBug578487_refreshLoop";
int minDepth = 0;
Expand All @@ -119,6 +133,7 @@ public void testBug578487_refreshLoop() throws Exception {
* Just a trivial test that few directories can be refreshed with default
* settings and default max depth of 2
*/
@Test
public void testBasicRefresh() throws Exception {
String name = "testBasicRefresh";
int minDepth = 0;
Expand All @@ -135,6 +150,7 @@ public void testBasicRefresh() throws Exception {
* Test that few directories can be refreshed with max depth of 16 (simulating a
* very fast file system)
*/
@Test
public void testFastRefresh() throws Exception {
String name = "testFastRefresh";
int minDepth = 0;
Expand All @@ -154,10 +170,10 @@ public void testFastRefresh() throws Exception {

/**
* Test that lot of directories can be refreshed with max depth of 8
*
* XXX: test is disabled because it needs 400 seconds on fast SSD on Linux
*/
public void XtestSmallRecursionRefresh() throws Exception {
@Test
@Ignore("test is disabled because it needs 400 seconds on fast SSD on Linux")
public void testSmallRecursionRefresh() throws Exception {
String name = "testSmallRecursionRefresh";
maxRecursionDeep = 8;
int minDepth = 0;
Expand All @@ -177,10 +193,10 @@ public void XtestSmallRecursionRefresh() throws Exception {

/**
* Test that lot of directories can be refreshed with max possible depth
*
* XXX: test is disabled because it needs 250 seconds on fast SSD on Linux
*/
public void XtestBigRecursionDeepRefresh() throws Exception {
@Test
@Ignore("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
int minDepth = 0;
Expand All @@ -202,6 +218,7 @@ public void XtestBigRecursionDeepRefresh() throws Exception {
* Test that few directories can be refreshed with max depth of 1 (simulating a
* very slow file system)
*/
@Test
public void testSlowRefresh() throws Exception {
String name = "testSlowRefresh";
int minDepth = 0;
Expand All @@ -223,9 +240,10 @@ public void testSlowRefresh() throws Exception {
* RefreshJob should use right rule to refresh resources, so it should wait with
* refresh if
*/
@Test
public void testProjectRule() throws Exception {
TestRefreshJob refreshJob = createAndReplaceDefaultJob();
IProject project = createProject(getName());
IProject project = createProject(testName.getMethodName());
try {
IFolder parent = project.getFolder("parent");
parent.create(true, true, null);
Expand All @@ -246,7 +264,7 @@ public void testProjectRule() throws Exception {
assertEquals("Should not visit anything yet", Collections.EMPTY_SET, refreshJob.visitedResources);
Job.getJobManager().endRule(project);
releaseRule = false;
TestUtil.waitForJobs(getName(), 100, 1000);
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);
Expand All @@ -256,14 +274,16 @@ public void testProjectRule() throws Exception {
}
}
} finally {
deleteProject(getName());
deleteProject(testName.getMethodName());
}
}

// Disabled for now, is unstable
public void XtestUnrelatedRule() throws Exception {
@Test
@Ignore("disabled for now, is unstable")
public void testUnrelatedRule() throws Exception {
TestRefreshJob refreshJob = createAndReplaceDefaultJob();
IProject project = createProject(getName());
IProject project = createProject(testName.getMethodName());
try {
IFolder parent = project.getFolder("parent");
parent.create(true, true, null);
Expand All @@ -277,7 +297,7 @@ public void XtestUnrelatedRule() throws Exception {
try {
Job.getJobManager().beginRule(rule, null);
refreshJob.refresh(child);
TestUtil.waitForJobs(getName(), 10, 60_000, ResourcesPlugin.FAMILY_AUTO_REFRESH);
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);
Job.getJobManager().endRule(rule);
Expand All @@ -289,7 +309,7 @@ public void XtestUnrelatedRule() throws Exception {
}
}
} finally {
deleteProject(getName());
deleteProject(testName.getMethodName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
package org.eclipse.core.tests.resources.refresh;

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.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.emptyArray;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -34,29 +38,38 @@
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.ResourceTest;
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;

/**
* Tests the IRefreshMonitor interface
*/
public class RefreshProviderTest extends ResourceTest {
public class RefreshProviderTest {

@Rule
public TestName testName = new TestName();

@Rule
public WorkspaceTestRule workspaceRule = new WorkspaceTestRule();

private boolean originalRefreshSetting;

@Override
protected void setUp() throws Exception {
super.setUp();
@Before
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);
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
@After
public void tearDown() {
//turn off autorefresh
TestRefreshProvider.reset();
IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(ResourcesPlugin.PI_RESOURCES);
Expand All @@ -67,9 +80,10 @@ protected void tearDown() throws Exception {
* 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();
deleteOnTearDown(location);
workspaceRule.deleteOnTearDown(location);
String name = "testUnmonitorLinkedResource";
IProject project = getWorkspace().getRoot().getProject(name);
createInWorkspace(project);
Expand All @@ -96,6 +110,7 @@ public void testLinkedFile() throws Exception {
* Test to ensure that a refresh provider is given the correct events when a project
* is closed or opened.
*/
@Test
public void testProjectCloseOpen() throws Exception {
String name = "testProjectCloseOpen";
IProject project = getWorkspace().getRoot().getProject(name);
Expand All @@ -122,14 +137,15 @@ public void testProjectCloseOpen() throws Exception {
* Test to ensure that a refresh provider is given the correct events when a project
* is closed or opened.
*/
@Test
public void testProjectCreateDelete() 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(getName(), 5, 100);
TestUtil.waitForJobs(testName.getMethodName(), 5, 100);
}
try {
assertTrue(createProject(name).isAccessible());
Expand Down

0 comments on commit a5789fb

Please sign in to comment.