Skip to content

Commit

Permalink
Improve array length assertions in ant and debug tests
Browse files Browse the repository at this point in the history
In order to reduce/avoid usage of ambiguous `assertEquals` (hard to
distinguish expected/actual) and prepare for a migration to JUnit 5 with
swapped expected/actual parameters in the `assertEquals` signature, this
change replaces certain `assertEquals` calls:
* Replace `assertEquals(MESSAGE, EXPECTED, ACTUAL.length)` with
`assertThat(MESSAGE, ACTUAL, arrayWithSize(EXPECTED))` and remove
obsolete message where possible
* Replace `assertEquals(EXPECTED, ACTUAL.length)` with
`assertThat(ACTUAL.length, is(EXPECTED))` if `ACTUAL` is a primitive
type array
* Replace `arrayWithSize(0)` with `emptyArray()` to improve failure
messages
* Combine array size comparison with array contents comparison where
possible
  • Loading branch information
HeikoKlare committed Jan 11, 2024
1 parent 187c536 commit 142f58f
Show file tree
Hide file tree
Showing 27 changed files with 262 additions and 336 deletions.
1 change: 1 addition & 0 deletions ant/org.eclipse.ant.tests.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Require-Bundle: org.eclipse.ui.ide;resolution:=optional,
org.eclipse.ant.core,
org.eclipse.core.runtime,
org.eclipse.pde.core
Import-Package: org.assertj.core.api
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-17
Eclipse-BundleShape: dir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* IBM Corporation - initial API and implementation
* Ericsson AB, Julian Enoch - Bug 465594
*******************************************************************************/
package org.eclipse.ant.tests.core.tests;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -265,7 +266,7 @@ public void testGetTargets() throws CoreException {
runner.setBuildFileLocation(buildFile.getLocation().toFile().toString());
}
TargetInfo[] infos = runner.getAvailableTargets();
assertTrue("incorrect number of targets retrieved", infos != null && infos.length == 3); //$NON-NLS-1$
assertThat(infos).as("number of retrieved targets").hasSize(3); //$NON-NLS-1$
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.ant.tests.core.tests;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
Expand All @@ -35,7 +35,7 @@ public class TargetTests extends AbstractAntTest {
@Test
public void testDefaultTarget() throws CoreException {
TargetInfo[] targets = getTargets("TestForEcho.xml"); //$NON-NLS-1$
assertEquals("Should be two targets in TestForEcho.xml", 2, targets.length); //$NON-NLS-1$
assertThat(targets).as("number of targets in TestForEcho.xml").hasSize(2); //$NON-NLS-1$
assertTrue("Test for Echo should be the default target", targets[1].isDefault()); //$NON-NLS-1$
}

Expand All @@ -45,7 +45,7 @@ public void testDefaultTarget() throws CoreException {
@Test
public void testGetTargetsWithDataTypes() throws CoreException {
TargetInfo[] targets = getTargets("Bug32551.xml"); //$NON-NLS-1$
assertEquals("Should be one targets in Bug32551.xml", 1, targets.length); //$NON-NLS-1$
assertThat(targets).as("number of targets in Bug32551.xml").hasSize(1); //$NON-NLS-1$
}

/**
Expand All @@ -70,7 +70,7 @@ public void testGetTargetsWithAntHome() {
@Test
public void testTargetNames() throws CoreException {
String[] targetNames = getTargetNames("TestForEcho.xml"); //$NON-NLS-1$
assertEquals("Should be two targets in TestForEcho.xml", 2, targetNames.length); //$NON-NLS-1$
assertThat(targetNames).as("number of targets in TestForEcho.xml").hasSize(2); //$NON-NLS-1$
assertEquals("First name should be init", "init", targetNames[0]); //$NON-NLS-1$ //$NON-NLS-2$
assertEquals("Second name should be Test for Echo", "Test for Echo", targetNames[1]); //$NON-NLS-1$ //$NON-NLS-2$
}
Expand All @@ -81,7 +81,7 @@ public void testTargetNames() throws CoreException {
@Test
public void testTargetDescription() throws CoreException {
String[] targetDescriptions = getTargetDescriptions("TestForEcho.xml"); //$NON-NLS-1$
assertEquals("Should be two targets in TestForEcho.xml", 2, targetDescriptions.length); //$NON-NLS-1$
assertThat(targetDescriptions).as("number of targets in TestForEcho.xml").hasSize(2); //$NON-NLS-1$
assertNull("First description should be null", targetDescriptions[0]); //$NON-NLS-1$
assertEquals("Second description should be Calls other targets", "Calls other echos", targetDescriptions[1]); //$NON-NLS-1$ //$NON-NLS-2$
}
Expand All @@ -101,8 +101,7 @@ public void testTargetProject() throws CoreException {
@Test
public void testTargetDependencies() throws CoreException {
String[] dependencies = getDependencies("TestForEcho.xml", "Test for Echo"); //$NON-NLS-1$ //$NON-NLS-2$
assertNotNull("Dependencies should not be null", dependencies); //$NON-NLS-1$
assertEquals("Should be one dependency in Test for Echo", 1, dependencies.length); //$NON-NLS-1$
assertThat(dependencies).as("number of dependencies in Test for Echo").hasSize(1); //$NON-NLS-1$
assertEquals("First dependency should be init", "init", dependencies[0]); //$NON-NLS-1$ //$NON-NLS-2$
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*******************************************************************************/
package org.eclipse.ant.tests.ui.debug;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -67,7 +68,7 @@ private void systemProperties(boolean sepVM) throws Exception, CoreException {

AntStackFrame frame = (AntStackFrame) thread.getTopStackFrame();
IVariable[] vars = frame.getVariables();
assertTrue("Should be a bunch of properties", 0 < vars.length); //$NON-NLS-1$
assertThat(vars).hasSizeGreaterThan(0);
AntProperty property = frame.findProperty("ant.library.dir"); //$NON-NLS-1$
assertNotNull(property);

Expand Down Expand Up @@ -103,7 +104,7 @@ private void userProperties(boolean sepVM) throws Exception {

AntStackFrame frame = (AntStackFrame) thread.getTopStackFrame();
IVariable[] vars = frame.getVariables();
assertTrue("Should be a bunch of properties", 0 < vars.length); //$NON-NLS-1$
assertThat(vars).hasSizeGreaterThan(0);
AntProperty property = frame.findProperty("ant.home"); //$NON-NLS-1$
assertNotNull(property);

Expand Down Expand Up @@ -135,7 +136,7 @@ private void runtimeProperties(boolean sepVM) throws Exception, CoreException {

AntStackFrame frame = (AntStackFrame) thread.getTopStackFrame();
IVariable[] vars = frame.getVariables();
assertTrue("Should be a bunch of properties", 0 < vars.length); //$NON-NLS-1$
assertThat(vars).hasSizeGreaterThan(0);
AntProperty property = frame.findProperty("ant.home"); //$NON-NLS-1$
assertNotNull(property);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*******************************************************************************/
package org.eclipse.ant.tests.ui.debug;

import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;

import org.eclipse.ant.internal.launching.debug.model.AntThread;
import org.eclipse.core.resources.IFile;
Expand Down Expand Up @@ -47,7 +47,7 @@ private void antCallStack(boolean sepVM) throws CoreException {

IStackFrame[] frames = thread.getStackFrames();

assertTrue(frames.length == 3);
assertThat(frames).hasSize(3);
IStackFrame frame = frames[0];
frame.getName().equals(""); //$NON-NLS-1$
}
Expand Down
Loading

0 comments on commit 142f58f

Please sign in to comment.