From 6eefa0f435cba1d9241cb53e2245bdca218a0748 Mon Sep 17 00:00:00 2001 From: Gintas Grigelionis Date: Mon, 16 Apr 2018 07:10:44 +0200 Subject: [PATCH] More isEmpty(), less assertThat() --- .../tools/ant/filters/HeadTailTest.java | 29 ++++++++++++------- .../apache/tools/ant/taskdefs/AntTest.java | 6 ++-- .../apache/tools/ant/taskdefs/BZip2Test.java | 11 ++----- .../tools/ant/taskdefs/EchoXMLTest.java | 26 +++++++---------- .../types/optional/ScriptSelectorTest.java | 18 +++++------- 5 files changed, 41 insertions(+), 49 deletions(-) diff --git a/src/tests/junit/org/apache/tools/ant/filters/HeadTailTest.java b/src/tests/junit/org/apache/tools/ant/filters/HeadTailTest.java index 21d25a1aa..31a84a3d7 100644 --- a/src/tests/junit/org/apache/tools/ant/filters/HeadTailTest.java +++ b/src/tests/junit/org/apache/tools/ant/filters/HeadTailTest.java @@ -49,7 +49,8 @@ public class HeadTailTest { buildRule.executeTarget("testHead"); File expected = buildRule.getProject().resolveFile("expected/head-tail.head.test"); File result = new File(buildRule.getProject().getProperty("output") + "/head-tail.head.test"); - assertEquals("testHead: Result not like expected", FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); + assertEquals("testHead: Result not like expected", + FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); } @Test @@ -57,7 +58,8 @@ public class HeadTailTest { buildRule.executeTarget("testHeadLines"); File expected = buildRule.getProject().resolveFile("expected/head-tail.headLines.test"); File result = new File(buildRule.getProject().getProperty("output") + "/head-tail.headLines.test"); - assertEquals("testHeadLines: Result not like expected", FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); + assertEquals("testHeadLines: Result not like expected", + FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); } @Test @@ -65,7 +67,8 @@ public class HeadTailTest { buildRule.executeTarget("testHeadSkip"); File expected = buildRule.getProject().resolveFile("expected/head-tail.headSkip.test"); File result = new File(buildRule.getProject().getProperty("output") + "/head-tail.headSkip.test"); - assertEquals("testHeadSkip: Result not like expected", FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); + assertEquals("testHeadSkip: Result not like expected", + FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); } @Test @@ -73,7 +76,8 @@ public class HeadTailTest { buildRule.executeTarget("testHeadLinesSkip"); File expected = buildRule.getProject().resolveFile("expected/head-tail.headLinesSkip.test"); File result = new File(buildRule.getProject().getProperty("output") + "/head-tail.headLinesSkip.test"); - assertEquals("testHeadLinesSkip: Result not like expected", FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); + assertEquals("testHeadLinesSkip: Result not like expected", + FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); } @Test @@ -88,9 +92,10 @@ public class HeadTailTest { @Test public void testTail() throws IOException { buildRule.executeTarget("testTail"); - File expected =buildRule.getProject().resolveFile("expected/head-tail.tail.test"); + File expected = buildRule.getProject().resolveFile("expected/head-tail.tail.test"); File result = new File(buildRule.getProject().getProperty("output") + "/head-tail.tail.test"); - assertEquals("testTail: Result not like expected", FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); + assertEquals("testTail: Result not like expected", + FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); } @Test @@ -98,7 +103,8 @@ public class HeadTailTest { buildRule.executeTarget("testTailLines"); File expected = buildRule.getProject().resolveFile("expected/head-tail.tailLines.test"); File result = new File(buildRule.getProject().getProperty("output") + "/head-tail.tailLines.test"); - assertEquals("testTailLines: Result not like expected", FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); + assertEquals("testTailLines: Result not like expected", + FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); } @Test @@ -106,7 +112,8 @@ public class HeadTailTest { buildRule.executeTarget("testTailSkip"); File expected = buildRule.getProject().resolveFile("expected/head-tail.tailSkip.test"); File result = new File(buildRule.getProject().getProperty("output") + "/head-tail.tailSkip.test"); - assertEquals("testTailSkip: Result not like expected", FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); + assertEquals("testTailSkip: Result not like expected", + FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); } @Test @@ -114,7 +121,8 @@ public class HeadTailTest { buildRule.executeTarget("testTailLinesSkip"); File expected = buildRule.getProject().resolveFile("expected/head-tail.tailLinesSkip.test"); File result = new File(buildRule.getProject().getProperty("output") + "/head-tail.tailLinesSkip.test"); - assertEquals("testTailLinesSkip: Result not like expected", FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); + assertEquals("testTailLinesSkip: Result not like expected", + FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); } @Test @@ -131,7 +139,8 @@ public class HeadTailTest { buildRule.executeTarget("testHeadTail"); File expected = buildRule.getProject().resolveFile("expected/head-tail.headtail.test"); File result = new File(buildRule.getProject().getProperty("output") + "/head-tail.headtail.test"); - assertEquals("testHeadTail: Result not like expected", FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); + assertEquals("testHeadTail: Result not like expected", + FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/AntTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/AntTest.java index 778fba7b1..0cb267487 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/AntTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/AntTest.java @@ -425,7 +425,7 @@ public class AntTest { } public void targetStarted(BuildEvent event) { - if (event.getTarget().getName().equals("")) { + if (event.getTarget().getName().isEmpty()) { return; } if (error == null) { @@ -476,7 +476,7 @@ public class AntTest { } public void targetStarted(BuildEvent event) { - if (event.getTarget().getName().equals("")) { + if (event.getTarget().getName().isEmpty()) { return; } if (error == null) { @@ -602,7 +602,7 @@ public class AntTest { } public void targetStarted(BuildEvent event) { - if (event.getTarget().getName().equals("")) { + if (event.getTarget().getName().isEmpty()) { return; } if (calls >= expectedValues.length) { diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/BZip2Test.java b/src/tests/junit/org/apache/tools/ant/taskdefs/BZip2Test.java index d080fae63..9b5ce0ba8 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/BZip2Test.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/BZip2Test.java @@ -32,7 +32,6 @@ import java.io.InputStream; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; /** */ @@ -83,14 +82,8 @@ public class BZip2Test { while (true) { int expected = originalIn.read(); int actual = actualIn.read(); - if (expected >= 0) { - if (expected != actual) { - fail("File content mismatch"); - } - } else { - if (actual >= 0) { - fail("File content mismatch"); - } + assertEquals("File content mismatch", expected, actual); + if (expected < 0) { break; } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/EchoXMLTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/EchoXMLTest.java index 5d83a8f77..48689b179 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/EchoXMLTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/EchoXMLTest.java @@ -24,16 +24,16 @@ import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; - -import static org.hamcrest.Matchers.containsString; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; +import org.junit.rules.ExpectedException; public class EchoXMLTest { @Rule public final BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/taskdefs/echoxml.xml"); @@ -51,22 +51,16 @@ public class EchoXMLTest { @Test public void testFail() { - try { - buildRule.executeTarget("testFail"); - fail("BuildException expected: must fail"); - } catch (BuildException ex) { - assertThat(ex.getMessage(), containsString("${foo}=bar")); - } + thrown.expect(BuildException.class); + thrown.expectMessage("${foo}=bar"); + buildRule.executeTarget("testFail"); } @Test public void testEmpty() { - try { - buildRule.executeTarget("testEmpty"); - fail("BuildException expected: must fail"); - } catch (BuildException ex) { - assertThat(ex.getMessage(), containsString("No nested XML specified")); - } + thrown.expect(BuildException.class); + thrown.expectMessage("No nested XML specified"); + buildRule.executeTarget("testEmpty"); } } diff --git a/src/tests/junit/org/apache/tools/ant/types/optional/ScriptSelectorTest.java b/src/tests/junit/org/apache/tools/ant/types/optional/ScriptSelectorTest.java index d1a8fe165..2b8e5bd96 100644 --- a/src/tests/junit/org/apache/tools/ant/types/optional/ScriptSelectorTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/optional/ScriptSelectorTest.java @@ -22,10 +22,7 @@ import org.apache.tools.ant.BuildFileRule; import org.junit.Before; import org.junit.Rule; import org.junit.Test; - -import static org.hamcrest.Matchers.containsString; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; +import org.junit.rules.ExpectedException; /** * Test that scripting selection works. Needs scripting support to work @@ -35,6 +32,9 @@ public class ScriptSelectorTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/types/selectors/scriptselector.xml"); @@ -42,13 +42,9 @@ public class ScriptSelectorTest { @Test public void testNolanguage() { - try { - buildRule.executeTarget("testNolanguage"); - fail("Absence of language attribute not detected"); - } catch (BuildException ex) { - assertThat(ex.getMessage(), containsString("script language must be specified")); - - } + thrown.expect(BuildException.class); + thrown.expectMessage("script language must be specified"); + buildRule.executeTarget("testNolanguage"); } @Test