Skip to content

Commit

Permalink
Migrate all tests in o.e.c.tests.internal.builders 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
* Remove RepeatedTestSuite that was only provided for specific manual
debugging purposes and is incompatible with JUnit 4

Contributes to
#903
  • Loading branch information
HeikoKlare committed Dec 11, 2023
1 parent d97ecdb commit de0295e
Show file tree
Hide file tree
Showing 16 changed files with 348 additions and 230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({ BuilderCycleTest.class, BuilderEventTest.class, BuilderNatureTest.class, BuilderTest.class,
RebuildTest.class,
BuildDeltaVerificationTest.class, CustomBuildTriggerTest.class, EmptyDeltaTest.class,
MultiProjectBuildTest.class, RelaxedSchedRuleBuilderTest.class, BuildConfigurationsTest.class,
BuildContextTest.class, ParallelBuildChainTest.class, ComputeProjectOrderTest.class, AutoBuildJobTest.class })
@Suite.SuiteClasses({ //
AutoBuildJobTest.class, //
BuildConfigurationsTest.class, //
BuildContextTest.class, //
BuildDeltaVerificationTest.class, //
BuilderCycleTest.class, //
BuilderEventTest.class, //
BuilderNatureTest.class, //
BuilderTest.class, //
ComputeProjectOrderTest.class, //
CustomBuildTriggerTest.class, //
EmptyDeltaTest.class, //
MultiProjectBuildTest.class, //
ParallelBuildChainTest.class, //
RebuildTest.class, //
RelaxedSchedRuleBuilderTest.class, //
})
public class AllBuilderTests {

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor;
import static org.eclipse.core.tests.resources.ResourceTestUtil.setAutoBuilding;
import static org.eclipse.core.tests.resources.ResourceTestUtil.updateProjectDescription;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

import java.util.Map;
Expand All @@ -40,22 +41,28 @@
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.core.tests.internal.builders.TestBuilder.BuilderRuleCallback;
import org.eclipse.core.tests.resources.ResourceTest;
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;

/**
* Test for various AutoBuildJob scheduling use cases
*/
public class AutoBuildJobTest extends ResourceTest {
public class AutoBuildJobTest {

@Rule
public TestName testName = new TestName();

@Rule
public WorkspaceTestRule workspaceRule = new WorkspaceTestRule();

private IProject project;
private AtomicLong running;
private AtomicLong scheduled;

public AutoBuildJobTest(String name) {
super(name);
}

IJobChangeListener jobChangeListener = new JobChangeAdapter() {

@Override
Expand All @@ -73,29 +80,26 @@ public void running(IJobChangeEvent event) {
}
};

@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
scheduled = new AtomicLong(0);
running = new AtomicLong(0);
setupProjectWithOurBuilder();
setAutoBuilding(true);
Job.getJobManager().addJobChangeListener(jobChangeListener);
}

@Override
protected void tearDown() throws Exception {
@After
public void tearDown() throws Exception {
Job.getJobManager().removeJobChangeListener(jobChangeListener);
super.tearDown();
}

private void setupProjectWithOurBuilder() throws CoreException {
project = getWorkspace().getRoot().getProject(getName());
project = getWorkspace().getRoot().getProject(testName.getMethodName());
project.create(createTestMonitor());
project.open(createTestMonitor());
updateProjectDescription(project).addingCommand(EmptyDeltaBuilder.BUILDER_NAME)
.withTestBuilderId(getName()).apply();
;
.withTestBuilderId(testName.getMethodName()).apply();
}

private void requestAutoBuildJobExecution() {
Expand All @@ -108,10 +112,12 @@ private BuildManager getBuildManager() {
return ((Workspace) project.getWorkspace()).getBuildManager();
}

@Test
public void testNoBuildIfBuildRequestedFromSameThread() throws Exception {
triggerAutobuildAndCheckNoExtraBuild(false);
}

@Test
public void testNoBuildIfBuildRequestedFromSameThreadAfterCancel() throws Exception {
triggerAutobuildAndCheckNoExtraBuild(true);
}
Expand Down Expand Up @@ -141,6 +147,7 @@ public IProject[] build(int kind, Map<String, String> args, IProgressMonitor mon
}
}

@Test
public void testExtraBuildIfBuildRequestedFromOtherThreadDuringRun() throws Exception {
EmptyDeltaBuilder.getInstance().setRuleCallback(new BuilderRuleCallback() {
@Override
Expand All @@ -165,27 +172,27 @@ public IProject[] build(int kind, Map<String, String> args, IProgressMonitor mon
}

@Test
public void testWaitForAutoBuild_JobManagerIsSuspended_ExceptionIsThrown()
throws InterruptedException, CoreException, ExecutionException {
Job.getJobManager().suspend();

assertEquals("Scheduled calls", 0, scheduled.get());
assertEquals("Running calls", 0, running.get());
public void testWaitForAutoBuild_JobManagerIsSuspended_ExceptionIsThrown() throws Exception {
try {
Job.getJobManager().suspend();

triggerAutoBuildAndWait();
Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_BUILD, null);
assertEquals("Scheduled calls", 0, scheduled.get());
assertEquals("Running calls", 0, running.get());

assertEquals("Scheduled calls", 1, scheduled.get());
assertEquals("Running calls", 0, running.get());
triggerAutoBuildAndWait();
Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_BUILD, null);

assertThrows(JobManagerSuspendedException.class, () -> waitForAutoBuild(2_000));
assertEquals("Scheduled calls", 1, scheduled.get());
assertEquals("Running calls", 0, running.get());

Job.getJobManager().resume();
assertThrows(JobManagerSuspendedException.class, () -> waitForAutoBuild(2_000));
} finally {
Job.getJobManager().resume();
}
}

@Test
public void testWaitForAutoBuild_JobManagerIsRunning_NoExceptionIsThrown()
throws Throwable {
public void testWaitForAutoBuild_JobManagerIsRunning_NoExceptionIsThrown() throws Throwable {
assertEquals("Scheduled calls", 0, scheduled.get());
assertEquals("Running calls", 0, running.get());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor;
import static org.eclipse.core.tests.resources.ResourceTestUtil.setAutoBuilding;
import static org.eclipse.core.tests.resources.ResourceTestUtil.updateProjectDescription;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.Map;
import org.eclipse.core.resources.IBuildConfiguration;
Expand All @@ -34,13 +38,19 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.tests.internal.builders.TestBuilder.BuilderRuleCallback;
import org.eclipse.core.tests.resources.ResourceDeltaVerifier;
import org.eclipse.core.tests.resources.ResourceTest;
import org.eclipse.core.tests.resources.WorkspaceTestRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

/**
* These tests exercise the project buildConfigs functionality which allows a different
* builder to be run for different project buildConfigs.
*/
public class BuildConfigurationsTest extends ResourceTest {
public class BuildConfigurationsTest {

@Rule
public WorkspaceTestRule workspaceRule = new WorkspaceTestRule();

private IProject project0;
private IProject project1;
Expand All @@ -50,13 +60,8 @@ public class BuildConfigurationsTest extends ResourceTest {
private static final String variant1 = "Variant1";
private static final String variant2 = "Variant2";

public BuildConfigurationsTest(String name) {
super(name);
}

@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
// Create resources
IWorkspaceRoot root = getWorkspace().getRoot();
project0 = root.getProject("BuildVariantTest_p0");
Expand Down Expand Up @@ -89,6 +94,7 @@ private void setupProject(IProject project) throws CoreException {
* Tests that an incremental builder is run/not run correctly, depending on deltas,
* and is given the correct deltas depending on which project variant is being built
*/
@Test
public void testDeltas() throws CoreException {
ConfigurationBuilder.clearStats();
// Run some incremental builds while varying the active variant and whether the project was modified
Expand All @@ -107,6 +113,7 @@ public void testDeltas() throws CoreException {
/**
* Tests that deltas are preserved per variant when a project is closed then opened.
*/
@Test
public void testCloseAndOpenProject() throws CoreException {
ConfigurationBuilder.clearStats();
file0.setContents(createRandomContentsStream(), true, true, createTestMonitor());
Expand All @@ -126,6 +133,7 @@ public void testCloseAndOpenProject() throws CoreException {
/**
* Tests that deltas are restored in the correct order per variant when a project is closed then opened.
*/
@Test
public void testCloseAndOpenProject_Bug361675() throws CoreException {
IWorkspaceRoot root = getWorkspace().getRoot();
IProject tempProject = root.getProject("BuildVariantTest_pTemp");
Expand Down Expand Up @@ -190,6 +198,7 @@ public void testCloseAndOpenProject_Bug361675() throws CoreException {
* Build order should be:
* p0,v1 p1,v0 p1,v2 p0,v0
*/
@Test
public void testBuildReferences() throws CoreException {
ConfigurationBuilder.clearStats();
ConfigurationBuilder.clearBuildOrder();
Expand Down Expand Up @@ -240,6 +249,7 @@ public void testBuildReferences() throws CoreException {
* p1 is closed.
* p0v0 should still be built.
*/
@Test
public void testBuildReferencesOfClosedProject() throws CoreException {
ConfigurationBuilder.clearStats();
ConfigurationBuilder.clearBuildOrder();
Expand Down Expand Up @@ -279,6 +289,7 @@ public void testBuildReferencesOfClosedProject() throws CoreException {
/**
* Tests that cleaning a project variant does not affect other buildConfigs in the same project
*/
@Test
public void testClean() throws CoreException {
ConfigurationBuilder.clearStats();
incrementalBuild(1, project0, variant0, true, 1, IncrementalProjectBuilder.FULL_BUILD);
Expand Down Expand Up @@ -342,6 +353,7 @@ private void checkBuild(int testId, IProject project, String variant, boolean sh
* another (not yet imported) project. Xtext builder reports it is interested in
* the project that isn't there (but is referenced in the .project file).
*/
@Test
public void testBuildProjectWithNotExistingReference() throws Exception {
// need a build to create builder
IBuildConfiguration buildConfig = project0.getBuildConfig(variant0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,28 @@
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.tests.resources.ResourceTest;
import org.eclipse.core.tests.resources.WorkspaceTestRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

/**
* These tests exercise the build context functionality that tells a builder in what context
* it was called.
*/
public class BuildContextTest extends ResourceTest {
public class BuildContextTest {

@Rule
public WorkspaceTestRule workspaceRule = new WorkspaceTestRule();

private IProject project0;
private IProject project1;
private IProject project2;
private static final String variant0 = "Variant0";
private static final String variant1 = "Variant1";

public BuildContextTest(String name) {
super(name);
}

@Override
protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
// Create resources
IWorkspaceRoot root = getWorkspace().getRoot();
project0 = root.getProject("BuildContextTests_p0");
Expand Down Expand Up @@ -122,6 +123,7 @@ private void setReferences(IBuildConfiguration variant, IBuildConfiguration[] re
* Setup a reference graph, then test the build context for for each project involved
* in the 'build'.
*/
@Test
public void testBuildContext() {
// Create reference graph
IBuildConfiguration p0v0 = getWorkspace().newBuildConfig(project0.getName(), variant0);
Expand Down Expand Up @@ -152,6 +154,7 @@ public void testBuildContext() {
assertThat(context.getAllReferencingBuildConfigs(), emptyArray());
}

@Test
public void testSingleProjectBuild() throws CoreException {
setAutoBuilding(true);

Expand All @@ -177,6 +180,7 @@ public void testSingleProjectBuild() throws CoreException {
/**
* Tests building a single project with and without references
*/
@Test
public void testWorkspaceBuildProject() throws CoreException {
setupSimpleReferences();
ContextBuilder.clearStats();
Expand Down Expand Up @@ -212,6 +216,7 @@ public void testWorkspaceBuildProject() throws CoreException {
/**
* Builds a couple configurations, including references
*/
@Test
public void testWorkspaceBuildProjects() throws CoreException {
setupSimpleReferences();
ContextBuilder.clearStats();
Expand All @@ -237,6 +242,7 @@ public void testWorkspaceBuildProjects() throws CoreException {
/**
* Sets references to the 'active' project build configuration
*/
@Test
public void testReferenceActiveVariant() throws CoreException {
setReferences(project0.getActiveBuildConfig(), new IBuildConfiguration[] {getWorkspace().newBuildConfig(project1.getName(), null)});
setReferences(project1.getActiveBuildConfig(), new IBuildConfiguration[] {getWorkspace().newBuildConfig(project2.getName(), null)});
Expand Down Expand Up @@ -265,6 +271,7 @@ public void testReferenceActiveVariant() throws CoreException {
* Attempts to build a project that references the active variant of another project,
* and the same variant directly. This should only result in one referenced variant being built.
*/
@Test
public void testReferenceVariantTwice() throws CoreException {
IBuildConfiguration ref1 = new BuildConfiguration(project1, null);
IBuildConfiguration ref2 = new BuildConfiguration(project1, project1.getActiveBuildConfig().getName());
Expand Down
Loading

0 comments on commit de0295e

Please sign in to comment.