Skip to content

Commit

Permalink
Replace calls to CoreTest.assertEquals() with matcher statements #903
Browse files Browse the repository at this point in the history
Replaces all calls to CoreTest.assertEquals() for comparing array
contents with matcher statements. This makes the assertions independent
from JUnit 3 and improves the provided error messages in case of a test
failure. The change also improves the other assertions in the touched
test classes.

Contributes to
#903
  • Loading branch information
HeikoKlare committed Dec 4, 2023
1 parent c75e9a1 commit bf5a19d
Show file tree
Hide file tree
Showing 10 changed files with 377 additions and 331 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.builders;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.emptyArray;

import org.eclipse.core.internal.events.BuildContext;
import org.eclipse.core.internal.resources.BuildConfiguration;
import org.eclipse.core.resources.IBuildConfiguration;
Expand Down Expand Up @@ -91,8 +95,8 @@ private IBuildConfiguration changeActiveBuildConfig(IProject project) throws Cor
return config;
}
}
assertTrue(false);
return null;
throw new IllegalStateException(
"No build config other than the active one could be found for project: " + project);
}

/**
Expand Down Expand Up @@ -129,22 +133,22 @@ public void testBuildContext() {
IBuildContext context;

context = new BuildContext(p0v0, new IBuildConfiguration[] {p0v0, p1v0}, buildOrder);
assertEquals("1.0", new IBuildConfiguration[] {}, context.getAllReferencedBuildConfigs());
assertEquals("1.1", new IBuildConfiguration[] {p0v1, p1v0}, context.getAllReferencingBuildConfigs());
assertEquals("1.2", new IBuildConfiguration[] {p0v0, p1v0}, context.getRequestedConfigs());
assertThat(context.getAllReferencedBuildConfigs(), emptyArray());
assertThat(context.getAllReferencingBuildConfigs(), arrayContaining(p0v1, p1v0));
assertThat(context.getRequestedConfigs(), arrayContaining(p0v0, p1v0));

context = new BuildContext(p0v1, buildOrder, buildOrder);
assertEquals("2.0", new IBuildConfiguration[] {p0v0}, context.getAllReferencedBuildConfigs());
assertEquals("2.1", new IBuildConfiguration[] {p1v0}, context.getAllReferencingBuildConfigs());
assertThat(context.getAllReferencedBuildConfigs(), arrayContaining(p0v0));
assertThat(context.getAllReferencingBuildConfigs(), arrayContaining(p1v0));

context = new BuildContext(p1v0, buildOrder, buildOrder);
assertEquals("3.0", new IBuildConfiguration[] {p0v0, p0v1}, context.getAllReferencedBuildConfigs());
assertEquals("3.1", new IBuildConfiguration[] {}, context.getAllReferencingBuildConfigs());
assertThat(context.getAllReferencedBuildConfigs(), arrayContaining(p0v0, p0v1));
assertThat(context.getAllReferencingBuildConfigs(), emptyArray());

// And it works with no build context too
context = new BuildContext(p1v0);
assertEquals("4.0", new IBuildConfiguration[] {}, context.getAllReferencedBuildConfigs());
assertEquals("4.1", new IBuildConfiguration[] {}, context.getAllReferencingBuildConfigs());
assertThat(context.getAllReferencedBuildConfigs(), emptyArray());
assertThat(context.getAllReferencingBuildConfigs(), emptyArray());
}

public void testSingleProjectBuild() throws CoreException {
Expand All @@ -153,20 +157,20 @@ public void testSingleProjectBuild() throws CoreException {
setupSimpleReferences();
ContextBuilder.clearStats();
project0.build(IncrementalProjectBuilder.FULL_BUILD, getMonitor());
assertTrue("1.0", ContextBuilder.checkValid());
ContextBuilder.assertValid();

IBuildContext context = ContextBuilder.getContext(project0.getActiveBuildConfig());
assertEquals("2.0", 0, context.getAllReferencedBuildConfigs().length);
assertEquals("2.1", 0, context.getAllReferencingBuildConfigs().length);
assertThat(context.getAllReferencedBuildConfigs(), emptyArray());
assertThat(context.getAllReferencingBuildConfigs(), emptyArray());

// Change the active build configuration will cause the project to be rebuilt
ContextBuilder.clearStats();
IBuildConfiguration newActive = changeActiveBuildConfig(project0);
waitForBuild();
assertTrue("3.0", ContextBuilder.checkValid());
ContextBuilder.assertValid();

context = ContextBuilder.getContext(newActive);
assertEquals("3.1", 0, context.getAllReferencedBuildConfigs().length);
assertThat(context.getAllReferencedBuildConfigs(), emptyArray());
}

/**
Expand All @@ -178,28 +182,30 @@ public void testWorkspaceBuildProject() throws CoreException {

// Build project and resolve references
getWorkspace().build(new IBuildConfiguration[] {project0.getActiveBuildConfig()}, IncrementalProjectBuilder.FULL_BUILD, true, getMonitor());
assertTrue("1.0", ContextBuilder.checkValid());
ContextBuilder.assertValid();

IBuildContext context = ContextBuilder.getContext(project0.getActiveBuildConfig());
assertEquals("2.0", new IBuildConfiguration[] {project2.getActiveBuildConfig(), project1.getActiveBuildConfig()}, context.getAllReferencedBuildConfigs());
assertEquals("2.1", 0, context.getAllReferencingBuildConfigs().length);
assertThat(context.getAllReferencedBuildConfigs(),
arrayContaining(project2.getActiveBuildConfig(), project1.getActiveBuildConfig()));
assertThat(context.getAllReferencingBuildConfigs(), emptyArray());

context = ContextBuilder.getBuilder(project1.getActiveBuildConfig()).contextForLastBuild;
assertEquals("3.0", new IBuildConfiguration[] {project2.getActiveBuildConfig()}, context.getAllReferencedBuildConfigs());
assertEquals("3.1", new IBuildConfiguration[] {project0.getActiveBuildConfig()}, context.getAllReferencingBuildConfigs());
assertThat(context.getAllReferencedBuildConfigs(), arrayContaining(project2.getActiveBuildConfig()));
assertThat(context.getAllReferencingBuildConfigs(), arrayContaining(project0.getActiveBuildConfig()));

context = ContextBuilder.getBuilder(project2.getActiveBuildConfig()).contextForLastBuild;
assertEquals("4.0", 0, context.getAllReferencedBuildConfigs().length);
assertEquals("4.1", new IBuildConfiguration[] {project1.getActiveBuildConfig(), project0.getActiveBuildConfig()}, context.getAllReferencingBuildConfigs());
assertThat(context.getAllReferencedBuildConfigs(), emptyArray());
assertThat(context.getAllReferencingBuildConfigs(),
arrayContaining(project1.getActiveBuildConfig(), project0.getActiveBuildConfig()));

// Build just project0
ContextBuilder.clearStats();
getWorkspace().build(new IBuildConfiguration[] {project0.getActiveBuildConfig()}, IncrementalProjectBuilder.FULL_BUILD, false, getMonitor());
assertTrue("5.0", ContextBuilder.checkValid());
ContextBuilder.assertValid();

context = ContextBuilder.getContext(project0.getActiveBuildConfig());
assertTrue("5.1", context.getAllReferencedBuildConfigs().length == 0);
assertTrue("5.2", context.getAllReferencingBuildConfigs().length == 0);
assertThat(context.getAllReferencedBuildConfigs(), emptyArray());
assertThat(context.getAllReferencingBuildConfigs(), emptyArray());
}

/**
Expand All @@ -210,19 +216,21 @@ public void testWorkspaceBuildProjects() throws CoreException {
ContextBuilder.clearStats();
// build project0 & project2 ; project1 will end up being built too.
getWorkspace().build(new IBuildConfiguration[] {project0.getActiveBuildConfig(), project2.getActiveBuildConfig()}, IncrementalProjectBuilder.FULL_BUILD, true, getMonitor());
assertTrue("1.0", ContextBuilder.checkValid());
ContextBuilder.assertValid();

IBuildContext context = ContextBuilder.getContext(project0.getActiveBuildConfig());
assertEquals("2.0", new IBuildConfiguration[] {project2.getActiveBuildConfig(), project1.getActiveBuildConfig()}, context.getAllReferencedBuildConfigs());
assertEquals("2.1", 0, context.getAllReferencingBuildConfigs().length);
assertThat(context.getAllReferencedBuildConfigs(),
arrayContaining(project2.getActiveBuildConfig(), project1.getActiveBuildConfig()));
assertThat(context.getAllReferencingBuildConfigs(), emptyArray());

context = ContextBuilder.getBuilder(project1.getActiveBuildConfig()).contextForLastBuild;
assertEquals("3.0", new IBuildConfiguration[] {project2.getActiveBuildConfig()}, context.getAllReferencedBuildConfigs());
assertEquals("3.1", new IBuildConfiguration[] {project0.getActiveBuildConfig()}, context.getAllReferencingBuildConfigs());
assertThat(context.getAllReferencedBuildConfigs(), arrayContaining(project2.getActiveBuildConfig()));
assertThat(context.getAllReferencingBuildConfigs(), arrayContaining(project0.getActiveBuildConfig()));

context = ContextBuilder.getBuilder(project2.getActiveBuildConfig()).contextForLastBuild;
assertEquals("4.0", 0, context.getAllReferencedBuildConfigs().length);
assertEquals("4.1", new IBuildConfiguration[] {project1.getActiveBuildConfig(), project0.getActiveBuildConfig()}, context.getAllReferencingBuildConfigs());
assertThat(context.getAllReferencedBuildConfigs(), emptyArray());
assertThat(context.getAllReferencingBuildConfigs(),
arrayContaining(project1.getActiveBuildConfig(), project0.getActiveBuildConfig()));
}

/**
Expand All @@ -235,19 +243,21 @@ public void testReferenceActiveVariant() throws CoreException {

ContextBuilder.clearStats();
getWorkspace().build(new IBuildConfiguration[] {project0.getActiveBuildConfig()}, IncrementalProjectBuilder.FULL_BUILD, true, getMonitor());
assertTrue("1.0", ContextBuilder.checkValid());
ContextBuilder.assertValid();

IBuildContext context = ContextBuilder.getContext(project0.getActiveBuildConfig());
assertEquals("2.0", new IBuildConfiguration[] {project2.getActiveBuildConfig(), project1.getActiveBuildConfig()}, context.getAllReferencedBuildConfigs());
assertEquals("2.1", 0, context.getAllReferencingBuildConfigs().length);
assertThat(context.getAllReferencedBuildConfigs(),
arrayContaining(project2.getActiveBuildConfig(), project1.getActiveBuildConfig()));
assertThat(context.getAllReferencingBuildConfigs(), emptyArray());

context = ContextBuilder.getBuilder(project1.getActiveBuildConfig()).contextForLastBuild;
assertEquals("3.0", new IBuildConfiguration[] {project2.getActiveBuildConfig()}, context.getAllReferencedBuildConfigs());
assertEquals("3.1", new IBuildConfiguration[] {project0.getActiveBuildConfig()}, context.getAllReferencingBuildConfigs());
assertThat(context.getAllReferencedBuildConfigs(), arrayContaining(project2.getActiveBuildConfig()));
assertThat(context.getAllReferencingBuildConfigs(), arrayContaining(project0.getActiveBuildConfig()));

context = ContextBuilder.getBuilder(project2.getActiveBuildConfig()).contextForLastBuild;
assertEquals("4.0", 0, context.getAllReferencedBuildConfigs().length);
assertEquals("4.1", new IBuildConfiguration[] {project1.getActiveBuildConfig(), project0.getActiveBuildConfig()}, context.getAllReferencingBuildConfigs());
assertThat(context.getAllReferencedBuildConfigs(), emptyArray());
assertThat(context.getAllReferencingBuildConfigs(),
arrayContaining(project1.getActiveBuildConfig(), project0.getActiveBuildConfig()));
}

/**
Expand All @@ -262,27 +272,27 @@ public void testReferenceVariantTwice() throws CoreException {

ContextBuilder.clearStats();
getWorkspace().build(new IBuildConfiguration[] {project0.getActiveBuildConfig()}, IncrementalProjectBuilder.FULL_BUILD, true, getMonitor());
assertTrue("1.0", ContextBuilder.checkValid());
ContextBuilder.assertValid();

IBuildContext context = ContextBuilder.getContext(project0.getActiveBuildConfig());
assertEquals("2.0", new IBuildConfiguration[] {project1.getActiveBuildConfig()}, context.getAllReferencedBuildConfigs());
assertEquals("2.1", 0, context.getAllReferencingBuildConfigs().length);
assertEquals("2.2", new IBuildConfiguration[] {project0.getActiveBuildConfig()}, context.getRequestedConfigs());
assertThat(context.getAllReferencedBuildConfigs(), arrayContaining(project1.getActiveBuildConfig()));
assertThat(context.getAllReferencingBuildConfigs(), emptyArray());
assertThat(context.getRequestedConfigs(), arrayContaining(project0.getActiveBuildConfig()));

context = ContextBuilder.getBuilder(project1.getActiveBuildConfig()).contextForLastBuild;
assertEquals("3.0", 0, context.getAllReferencedBuildConfigs().length);
assertEquals("3.1", new IBuildConfiguration[] {project0.getActiveBuildConfig()}, context.getAllReferencingBuildConfigs());
assertThat(context.getAllReferencedBuildConfigs(), emptyArray());
assertThat(context.getAllReferencingBuildConfigs(), arrayContaining(project0.getActiveBuildConfig()));

// Change the active configuration of project1, and test that two configurations are built
ContextBuilder.clearStats();
IBuildConfiguration project1PreviousActive = project1.getActiveBuildConfig();
IBuildConfiguration project1NewActive = changeActiveBuildConfig(project1);
getWorkspace().build(new IBuildConfiguration[] {project0.getActiveBuildConfig()}, IncrementalProjectBuilder.FULL_BUILD, true, getMonitor());
assertTrue("4.0", ContextBuilder.checkValid());
ContextBuilder.assertValid();

context = ContextBuilder.getContext(project0.getActiveBuildConfig());
assertEquals("4.1", new IBuildConfiguration[] {project1PreviousActive, project1NewActive}, context.getAllReferencedBuildConfigs());
assertEquals("4.2", 0, context.getAllReferencingBuildConfigs().length);
assertEquals("4.3", new IBuildConfiguration[] {project0.getActiveBuildConfig()}, context.getRequestedConfigs());
assertThat(context.getAllReferencedBuildConfigs(), arrayContaining(project1PreviousActive, project1NewActive));
assertThat(context.getAllReferencingBuildConfigs(), emptyArray());
assertThat(context.getRequestedConfigs(), arrayContaining(project0.getActiveBuildConfig()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.builders;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.resources.*;
import org.eclipse.core.resources.IBuildConfiguration;
import org.eclipse.core.resources.IBuildContext;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
Expand Down Expand Up @@ -53,16 +59,13 @@ public static IBuildContext getContext(IBuildConfiguration variant) {
return getBuilder(variant).contextForLastBuild;
}

public static boolean checkValid() {
public static void assertValid() {
for (ContextBuilder builder : builders.values()) {
if (builder.getRuleCalledForLastBuild && !builder.contextForLastBuild.equals(builder.contextForLastBuildInGetRule)) {
return false;
}
if (builder.getRuleCalledForLastBuild && !builder.buildConfigurationForLastBuild.equals(builder.buildConfigurationForLastBuildInGetRule)) {
return false;
if (builder.getRuleCalledForLastBuild) {
assertThat(builder.contextForLastBuild, is(builder.contextForLastBuildInGetRule));
assertThat(builder.buildConfigurationForLastBuild, is(builder.buildConfigurationForLastBuildInGetRule));
}
}
return true;
}

public static void clearStats() {
Expand Down
Loading

0 comments on commit bf5a19d

Please sign in to comment.