From 761f8e60bc203d830c25afb2136cd1d435817024 Mon Sep 17 00:00:00 2001 From: Gintas Grigelionis Date: Mon, 16 Apr 2018 09:00:15 +0200 Subject: [PATCH] More string matcher assertions --- .../tools/ant/filters/ConcatFilterTest.java | 13 ++-- .../tools/ant/filters/TokenFilterTest.java | 25 +++---- .../apache/tools/ant/taskdefs/BZip2Test.java | 8 +- .../apache/tools/ant/taskdefs/GzipTest.java | 73 +++++++++---------- .../tools/ant/taskdefs/ManifestTest.java | 3 +- .../taskdefs/optional/TraXLiaisonTest.java | 9 ++- .../optional/junit/JUnitTaskTest.java | 37 ++++------ .../ant/types/resources/JavaResourceTest.java | 7 +- .../types/selectors/ModifiedSelectorTest.java | 5 +- .../apache/tools/ant/util/FileUtilsTest.java | 23 +++--- .../apache/tools/ant/util/JAXPUtilsTest.java | 9 ++- .../tools/ant/util/JavaEnvUtilsTest.java | 33 +++++---- .../util/LayoutPreservingPropertiesTest.java | 6 +- .../tools/ant/util/StringUtilsTest.java | 14 ++-- 14 files changed, 134 insertions(+), 131 deletions(-) diff --git a/src/tests/junit/org/apache/tools/ant/filters/ConcatFilterTest.java b/src/tests/junit/org/apache/tools/ant/filters/ConcatFilterTest.java index 06cd2f8d9..a7857b2de 100644 --- a/src/tests/junit/org/apache/tools/ant/filters/ConcatFilterTest.java +++ b/src/tests/junit/org/apache/tools/ant/filters/ConcatFilterTest.java @@ -28,8 +28,10 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertThat; /** * JUnit Testcases for ConcatReader @@ -79,8 +81,8 @@ public class ConcatFilterTest { buildRule.executeTarget("testFilterReaderNoArgs"); File expected = new File(buildRule.getProject().getProperty("output"), "concatfilter.test"); File result = new File(buildRule.getProject().getProperty("output"), "concat.FilterReaderNoArgs.test"); - assertEquals("testFilterReaderNoArgs: Result not like expected", FileUtilities.getFileContents(expected), - FileUtilities.getFileContents(result)); + assertEquals("testFilterReaderNoArgs: Result not like expected", + FileUtilities.getFileContents(expected), FileUtilities.getFileContents(result)); } @Test @@ -131,9 +133,8 @@ public class ConcatFilterTest { buildRule.executeTarget(target); String resultContent = FileUtilities.getFileContents( new File(buildRule.getProject().getProperty("output") + "/concat." + target.substring(4) + ".test")); - assertTrue("First 5 lines differs.", resultContent.startsWith(expectedStart)); - assertTrue("Last 5 lines differs.", resultContent.endsWith(expectedEnd)); + assertThat("First 5 lines differs.", resultContent, startsWith(expectedStart)); + assertThat("Last 5 lines differs.", resultContent, endsWith(expectedEnd)); } - } diff --git a/src/tests/junit/org/apache/tools/ant/filters/TokenFilterTest.java b/src/tests/junit/org/apache/tools/ant/filters/TokenFilterTest.java index fb60f2c8d..30f5bc0be 100644 --- a/src/tests/junit/org/apache/tools/ant/filters/TokenFilterTest.java +++ b/src/tests/junit/org/apache/tools/ant/filters/TokenFilterTest.java @@ -18,11 +18,12 @@ package org.apache.tools.ant.filters; +import static org.hamcrest.Matchers.both; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.not; -import static org.junit.Assert.assertFalse; +import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; import java.io.FileReader; @@ -77,7 +78,6 @@ public class TokenFilterTest { @Test public void testDosLineOutput() throws IOException { - buildRule.executeTarget("doslineoutput"); assertThat(getFileString(buildRule.getProject().getProperty("output") + "/doslineoutput"), containsString("\r\nThis\r\nis\r\na\r\nnumber\r\nof\r\nwords\r\n")); @@ -86,9 +86,8 @@ public class TokenFilterTest { @Test public void testFileTokenizer() throws IOException { buildRule.executeTarget("filetokenizer"); - String contents = getFileString(buildRule.getProject().getProperty("output") + "/filetokenizer"); - assertThat(contents, containsString(" of words")); - assertThat(contents, not(containsString(" This is"))); + assertThat(getFileString(buildRule.getProject().getProperty("output") + "/filetokenizer"), + both(containsString(" of words")).and(not(containsString(" This is")))); } @Test @@ -107,9 +106,9 @@ public class TokenFilterTest { @Test public void testContainsString() throws IOException { buildRule.executeTarget("containsstring"); - String contents = getFileString(buildRule.getProject().getProperty("output") + "/containsstring"); - assertThat(contents, containsString("this is a line contains foo")); - assertThat(contents, not(containsString("this line does not"))); + assertThat(getFileString(buildRule.getProject().getProperty("output") + "/containsstring"), + both(containsString("this is a line contains foo")) + .and(not(containsString("this line does not")))); } @Test @@ -154,8 +153,8 @@ public class TokenFilterTest { public void testTrimFile() throws IOException { buildRule.executeTarget("trimfile"); String contents = getFileString(buildRule.getProject().getProperty("output") + "/trimfile"); - assertTrue("no ws at start", contents.startsWith("This is th")); - assertTrue("no ws at end", contents.endsWith("second line.")); + assertThat("no ws at start", contents, startsWith("This is th")); + assertThat("no ws at end", contents, endsWith("second line.")); assertThat(contents, containsString(" This is the second")); } @@ -163,8 +162,8 @@ public class TokenFilterTest { public void testTrimFileByLine() throws IOException { buildRule.executeTarget("trimfilebyline"); String contents = getFileString(buildRule.getProject().getProperty("output") + "/trimfilebyline"); - assertFalse("no ws at start", contents.startsWith("This is th")); - assertFalse("no ws at end", contents.endsWith("second line.")); + assertThat("no ws at start", contents, not(startsWith("This is th"))); + assertThat("no ws at end", contents, not(endsWith("second line."))); assertThat(contents, not(containsString(" This is the second"))); assertThat(contents, containsString("file.\nThis is the second")); } 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 9b5ce0ba8..5620afd3f 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/BZip2Test.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/BZip2Test.java @@ -30,8 +30,9 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import static org.hamcrest.Matchers.endsWith; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertThat; /** */ @@ -101,9 +102,8 @@ public class BZip2Test { public void testDateCheck() { buildRule.executeTarget("testDateCheck"); String log = buildRule.getLog(); - assertTrue( - "Expecting message ending with 'asf-logo.gif.bz2 is up to date.' but got '" + log + "'", - log.endsWith("asf-logo.gif.bz2 is up to date.")); + assertThat("Expecting message ending with 'asf-logo.gif.bz2 is up to date.' but got '" + + log + "'", log, endsWith("asf-logo.gif.bz2 is up to date.")); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/GzipTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/GzipTest.java index 76342b823..f2862e342 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/GzipTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/GzipTest.java @@ -24,9 +24,11 @@ import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.Matchers.startsWith; +import static org.junit.Assert.assertThat; /** */ @@ -35,60 +37,58 @@ public class GzipTest { @Rule public final BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); @Before public void setUp() { buildRule.configureProject("src/etc/testcases/taskdefs/gzip.xml"); } - @Test + /** + * Fail due to missing required argument + */ + @Test(expected = BuildException.class) public void test1() { - try { - buildRule.executeTarget("test1"); - fail("BuildException expected: required argument missing"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("test1"); + // TODO assert value } - @Test + /** + * Fail due to missing required argument + */ + @Test(expected = BuildException.class) public void test2() { - try { - buildRule.executeTarget("test2"); - fail("BuildException expected: required argument missing"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("test2"); + // TODO assert value } - @Test + /** + * Fail due to missing required argument + */ + @Test(expected = BuildException.class) public void test3() { - try { - buildRule.executeTarget("test3"); - fail("BuildException expected: required argument missing"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("test3"); + // TODO assert value } - @Test + /** + * Fail due to zipfile pointing to a directory + */ + @Test(expected = BuildException.class) public void test4() { - try { - buildRule.executeTarget("test4"); - fail("BuildException expected: zipfile must not point to a directory"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("test4"); + // TODO assert value } @Test public void testGZip() { buildRule.executeTarget("realTest"); String log = buildRule.getLog(); - assertTrue("Expecting message starting with 'Building:' but got '" - + log + "'", log.startsWith("Building:")); - assertTrue("Expecting message ending with 'asf-logo.gif.gz' but got '" - + log + "'", log.endsWith("asf-logo.gif.gz")); + assertThat("Expecting message starting with 'Building:' but got '" + + log + "'", log, startsWith("Building:")); + assertThat("Expecting message ending with 'asf-logo.gif.gz' but got '" + + log + "'", log, endsWith("asf-logo.gif.gz")); } @Test @@ -100,9 +100,8 @@ public class GzipTest { public void testDateCheck() { buildRule.executeTarget("testDateCheck"); String log = buildRule.getLog(); - assertTrue( - "Expecting message ending with 'asf-logo.gif.gz is up to date.' but got '" + log + "'", - log.endsWith("asf-logo.gif.gz is up to date.")); + assertThat("Expecting message ending with 'asf-logo.gif.gz is up to date.' but got '" + + log + "'", log, endsWith("asf-logo.gif.gz is up to date.")); } @After diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java index 283181e79..7a72fd355 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java @@ -37,6 +37,7 @@ import org.junit.rules.ExpectedException; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; @@ -370,7 +371,7 @@ public class ManifestTest { assertNotEquals(Manifest.getDefaultManifest(), mf); String mfAsString = mf.toString(); assertNotNull(mfAsString); - assertTrue(mfAsString.startsWith("Manifest-Version: 2.0")); + assertThat(mfAsString, startsWith("Manifest-Version: 2.0")); assertThat(mfAsString, containsString("Foo: Bar")); mf = getManifest(new File(outDir, "mftest2.mf")); diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/TraXLiaisonTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/TraXLiaisonTest.java index 196b38cbd..704a4c0c3 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/TraXLiaisonTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/TraXLiaisonTest.java @@ -18,8 +18,9 @@ package org.apache.tools.ant.taskdefs.optional; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.startsWith; +import static org.junit.Assert.assertThat; import static org.junit.Assume.assumeFalse; import static org.junit.Assume.assumeNoException; import static org.junit.Assume.assumeTrue; @@ -162,8 +163,8 @@ public class TraXLiaisonTest extends AbstractXSLTLiaisonTest implements XSLTLogg file = new File("/user/local/bin"); } String systemid = JAXPUtils.getSystemId(file); - assertTrue("SystemIDs should start by file:/", systemid.startsWith("file:/")); - assertFalse("SystemIDs should not start with file:////", systemid.startsWith("file:////")); + assertThat("SystemIDs should start by file:/", systemid, startsWith("file:/")); + assertThat("SystemIDs should not start with file:////", systemid, not(startsWith("file:////"))); } public void log(String message) { diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTaskTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTaskTest.java index 5df984b8b..0eeb833f4 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTaskTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTaskTest.java @@ -19,6 +19,7 @@ package org.apache.tools.ant.taskdefs.optional.junit; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -27,7 +28,6 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; - import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -254,9 +254,11 @@ public class JUnitTaskTest { assertTrue("The collector file '" + collectorFile.getAbsolutePath() + "' should exist after the 4th run.", collectorFile.exists()); - //TODO: these two statements fail - //buildRule.executeTarget("A.test02");assertThat(buildRule.getOutput(), not(containsString("4th run: should not run A.test02"))); - //buildRule.executeTarget("A.test03");assertThat(buildRule.getOutput(), not(containsString("4th run: should not run A.test03"))); + //TODO: these statements fail + //buildRule.executeTarget("A.test02"); + //assertThat(buildRule.getOutput(), not(containsString("4th run: should not run A.test02"))); + //buildRule.executeTarget("A.test03"); + //assertThat(buildRule.getOutput(), not(containsString("4th run: should not run A.test03"))); buildRule.executeTarget("B.test04"); assertThat(buildRule.getOutput(), containsString("4th run: should run B.test04")); buildRule.executeTarget("D.test10"); @@ -307,41 +309,32 @@ public class JUnitTaskTest { } private void assertOutput() throws IOException { - FileReader inner = new FileReader(new File(buildRule.getOutputDir(), - "testlog.txt")); - BufferedReader reader = new BufferedReader(inner); - try { + FileReader inner = new FileReader(new File(buildRule.getOutputDir(), "testlog.txt")); + try (BufferedReader reader = new BufferedReader(inner)) { String line = reader.readLine(); - assertEquals("Testsuite: org.apache.tools.ant.taskdefs.optional.junit.Printer", - line); + assertEquals("Testsuite: org.apache.tools.ant.taskdefs.optional.junit.Printer", line); line = reader.readLine(); assertNotNull(line); - assertTrue(line.startsWith("Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed:")); + assertThat(line, startsWith("Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed:")); line = reader.readLine(); - assertEquals("------------- Standard Output ---------------", - line); + assertEquals("------------- Standard Output ---------------", line); assertPrint(reader.readLine(), "static", "out"); assertPrint(reader.readLine(), "constructor", "out"); assertPrint(reader.readLine(), "method", "out"); line = reader.readLine(); - assertEquals("------------- ---------------- ---------------", - line); + assertEquals("------------- ---------------- ---------------", line); line = reader.readLine(); - assertEquals("------------- Standard Error -----------------", - line); + assertEquals("------------- Standard Error -----------------", line); assertPrint(reader.readLine(), "static", "err"); assertPrint(reader.readLine(), "constructor", "err"); assertPrint(reader.readLine(), "method", "err"); line = reader.readLine(); - assertEquals("------------- ---------------- ---------------", - line); + assertEquals("------------- ---------------- ---------------", line); line = reader.readLine(); assertEquals("", line); line = reader.readLine(); assertNotNull(line); - assertTrue(line.startsWith("Testcase: testNoCrash took ")); - } finally { - inner.close(); + assertThat(line, startsWith("Testcase: testNoCrash took ")); } } diff --git a/src/tests/junit/org/apache/tools/ant/types/resources/JavaResourceTest.java b/src/tests/junit/org/apache/tools/ant/types/resources/JavaResourceTest.java index a67db39bc..8325ed070 100644 --- a/src/tests/junit/org/apache/tools/ant/types/resources/JavaResourceTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/resources/JavaResourceTest.java @@ -22,9 +22,10 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertThat; public class JavaResourceTest { @@ -43,8 +44,8 @@ public class JavaResourceTest { // this actually relies on the first manifest being found on // the classpath (probably rt.jar's) being valid - assertTrue(buildRule.getProject().getProperty("manifest") - .startsWith("Manifest-Version:")); + assertThat(buildRule.getProject().getProperty("manifest"), + startsWith("Manifest-Version:")); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/types/selectors/ModifiedSelectorTest.java b/src/tests/junit/org/apache/tools/ant/types/selectors/ModifiedSelectorTest.java index 7d7b00ee0..cd7143427 100644 --- a/src/tests/junit/org/apache/tools/ant/types/selectors/ModifiedSelectorTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/selectors/ModifiedSelectorTest.java @@ -47,6 +47,7 @@ import org.junit.Rule; import org.junit.Test; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; @@ -161,13 +162,13 @@ public class ModifiedSelectorTest { @Test public void testCustomAlgorithm() { String algo = getAlgoName("org.apache.tools.ant.types.selectors.modifiedselector.HashvalueAlgorithm"); - assertTrue("Wrong algorithm used: " + algo, algo.startsWith("HashvalueAlgorithm")); + assertThat("Wrong algorithm used: " + algo, algo, startsWith("HashvalueAlgorithm")); } @Test public void testCustomAlgorithm2() { String algo = getAlgoName("org.apache.tools.ant.types.selectors.MockAlgorithm"); - assertTrue("Wrong algorithm used: " + algo, algo.startsWith("MockAlgorithm")); + assertThat("Wrong algorithm used: " + algo, algo, startsWith("MockAlgorithm")); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java b/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java index c44c8f955..43520adc0 100644 --- a/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java @@ -28,9 +28,12 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; +import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.junit.Assume.assumeTrue; @@ -344,8 +347,8 @@ public class FileUtilsTest { File tmp1 = FILE_UTILS.createTempFile("pre", ".suf", null, false, true); String tmploc = System.getProperty("java.io.tmpdir"); String name = tmp1.getName(); - assertTrue("starts with pre", name.startsWith("pre")); - assertTrue("ends with .suf", name.endsWith(".suf")); + assertThat("starts with pre", name, startsWith("pre")); + assertThat("ends with .suf", name, endsWith(".suf")); assertTrue("File was created", tmp1.exists()); assertEquals((new File(tmploc, tmp1.getName())).getAbsolutePath(), tmp1.getAbsolutePath()); @@ -357,8 +360,8 @@ public class FileUtilsTest { File tmp2 = FILE_UTILS.createTempFile("pre", ".suf", dir2, true, true); String name2 = tmp2.getName(); - assertTrue("starts with pre", name2.startsWith("pre")); - assertTrue("ends with .suf", name2.endsWith(".suf")); + assertThat("starts with pre", name2, startsWith("pre")); + assertThat("ends with .suf", name2, endsWith(".suf")); assertTrue("File was created", tmp2.exists()); assertEquals((new File(dir2, tmp2.getName())).getAbsolutePath(), tmp2.getAbsolutePath()); @@ -370,8 +373,8 @@ public class FileUtilsTest { assertFalse("new file", tmp1.exists()); name = tmp1.getName(); - assertTrue("starts with pre", name.startsWith("pre")); - assertTrue("ends with .suf", name.endsWith(".suf")); + assertThat("starts with pre", name, startsWith("pre")); + assertThat("ends with .suf", name, endsWith(".suf")); assertEquals("is inside parent dir", parent.getAbsolutePath(), tmp1 .getParent()); @@ -485,14 +488,14 @@ public class FileUtilsTest { } if (File.pathSeparatorChar == '/') { assertEquals("file:/foo", removeExtraneousAuthority(FILE_UTILS.toURI("/foo"))); - assertTrue("file: URIs must name absolute paths", FILE_UTILS.toURI("./foo").startsWith("file:/")); - assertTrue(FILE_UTILS.toURI("./foo").endsWith("/foo")); + assertThat("file: URIs must name absolute paths", FILE_UTILS.toURI("./foo"), startsWith("file:/")); + assertThat(FILE_UTILS.toURI("./foo"), endsWith("/foo")); assertEquals("file:/" + dosRoot + "foo%20bar", removeExtraneousAuthority(FILE_UTILS.toURI("/foo bar"))); assertEquals("file:/" + dosRoot + "foo%23bar", removeExtraneousAuthority(FILE_UTILS.toURI("/foo#bar"))); } else if (File.pathSeparatorChar == '\\') { assertEquals("file:/" + dosRoot + "foo", removeExtraneousAuthority(FILE_UTILS.toURI("\\foo"))); - assertTrue("file: URIs must name absolute paths", FILE_UTILS.toURI(".\\foo").startsWith("file:/")); - assertTrue(FILE_UTILS.toURI(".\\foo").endsWith("/foo")); + assertThat("file: URIs must name absolute paths", FILE_UTILS.toURI(".\\foo"), startsWith("file:/")); + assertThat(FILE_UTILS.toURI(".\\foo"), endsWith("/foo")); assertEquals("file:/" + dosRoot + "foo%20bar", removeExtraneousAuthority(FILE_UTILS.toURI("\\foo bar"))); assertEquals("file:/" + dosRoot + "foo%23bar", removeExtraneousAuthority(FILE_UTILS.toURI("\\foo#bar"))); } diff --git a/src/tests/junit/org/apache/tools/ant/util/JAXPUtilsTest.java b/src/tests/junit/org/apache/tools/ant/util/JAXPUtilsTest.java index 924de6e3c..c6ef5de30 100644 --- a/src/tests/junit/org/apache/tools/ant/util/JAXPUtilsTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/JAXPUtilsTest.java @@ -21,8 +21,9 @@ import org.junit.Test; import java.io.File; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.startsWith; +import static org.junit.Assert.assertThat; /** * JAXPUtils test case @@ -38,7 +39,7 @@ public class JAXPUtilsTest { file = new File("/user/local/bin"); } String systemid = JAXPUtils.getSystemId(file); - assertTrue("SystemIDs should start by file:/", systemid.startsWith("file:/")); - assertFalse("SystemIDs should not start with file:////", systemid.startsWith("file:////")); + assertThat("SystemIDs should start by file:/", systemid, startsWith("file:/")); + assertThat("SystemIDs should not start with file:////", systemid, not(startsWith("file:////"))); } } diff --git a/src/tests/junit/org/apache/tools/ant/util/JavaEnvUtilsTest.java b/src/tests/junit/org/apache/tools/ant/util/JavaEnvUtilsTest.java index fcffdce3e..271774662 100644 --- a/src/tests/junit/org/apache/tools/ant/util/JavaEnvUtilsTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/JavaEnvUtilsTest.java @@ -22,8 +22,11 @@ import java.io.File; import org.apache.tools.ant.taskdefs.condition.Os; import org.junit.Test; +import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeFalse; import static org.junit.Assume.assumeTrue; @@ -52,26 +55,26 @@ public class JavaEnvUtilsTest { .getAbsolutePath(); String j = JavaEnvUtils.getJreExecutable("java"); - assertTrue(j.endsWith(".exe")); + assertThat(j, endsWith(".exe")); assertTrue(j + " is absolute", (new File(j)).isAbsolute()); try { - assertTrue(j + " is normalized and in the JRE dir", j.startsWith(javaHome)); + assertThat(j + " is normalized and in the JRE dir", j, startsWith(javaHome)); } catch (AssertionError e) { // java.home is bogus assertEquals("java.exe", j); } j = JavaEnvUtils.getJdkExecutable("javac"); - assertTrue(j.endsWith(".exe")); + assertThat(j, endsWith(".exe")); try { assertTrue(j + " is absolute", (new File(j)).isAbsolute()); String javaHomeParent = FILE_UTILS.normalize(javaHome + "/..").getAbsolutePath(); - assertTrue(j + " is normalized and in the JDK dir", j.startsWith(javaHomeParent)); + assertThat(j + " is normalized and in the JDK dir", j, startsWith(javaHomeParent)); if (JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.JAVA_9)) { - assertTrue(j + " is normalized and not in the JRE dir", j.startsWith(javaHome)); + assertThat(j + " is normalized and not in the JRE dir", j, startsWith(javaHome)); } else { - assertFalse(j + " is normalized and not in the JRE dir", j.startsWith(javaHome)); + assertThat(j + " is normalized and not in the JRE dir", j, not(startsWith(javaHome))); } } catch (AssertionError e) { // java.home is bogus @@ -92,25 +95,25 @@ public class JavaEnvUtilsTest { String extension = Os.isFamily("dos") ? ".exe" : ""; String j = JavaEnvUtils.getJreExecutable("java"); - if (!extension.equals("")) { - assertTrue(j.endsWith(extension)); + if (extension.isEmpty()) { + assertThat(j, endsWith(extension)); } assertTrue(j + " is absolute", (new File(j)).isAbsolute()); - assertTrue(j + " is normalized and in the JRE dir", j.startsWith(javaHome)); + assertThat(j + " is normalized and in the JRE dir", j, startsWith(javaHome)); j = JavaEnvUtils.getJdkExecutable("javac"); - if (!extension.equals("")) { - assertTrue(j.endsWith(extension)); + if (extension.isEmpty()) { + assertThat(j, endsWith(extension)); } assertTrue(j + " is absolute", (new File(j)).isAbsolute()); String javaHomeParent = FILE_UTILS.normalize(javaHome + "/..").getAbsolutePath(); - assertTrue(j + " is normalized and in the JDK dir", j.startsWith(javaHomeParent)); + assertThat(j + " is normalized and in the JDK dir", j, startsWith(javaHomeParent)); if (JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.JAVA_9)) { - assertTrue(j + " is normalized and in the JRE dir", j.startsWith(javaHome)); + assertThat(j + " is normalized and in the JRE dir", j, startsWith(javaHome)); } else { - assertFalse(j + " is normalized and not in the JRE dir", j.startsWith(javaHome)); + assertThat(j + " is normalized and not in the JRE dir", j, not(startsWith(javaHome))); } assertEquals("foo" + extension, JavaEnvUtils.getJreExecutable("foo")); diff --git a/src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java b/src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java index 3aa855920..30c8791c4 100644 --- a/src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java @@ -28,9 +28,9 @@ import org.junit.Test; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; public class LayoutPreservingPropertiesTest { @@ -150,9 +150,7 @@ public class LayoutPreservingPropertiesTest { fos.close(); // and check that the resulting file looks okay - String s = readFile(tmp); - - assertTrue("should have had header ", s.startsWith("#file-header")); + assertThat("should have had header ", readFile(tmp), startsWith("#file-header")); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/util/StringUtilsTest.java b/src/tests/junit/org/apache/tools/ant/util/StringUtilsTest.java index d3f64d8c1..7b49d6b48 100644 --- a/src/tests/junit/org/apache/tools/ant/util/StringUtilsTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/StringUtilsTest.java @@ -17,17 +17,19 @@ */ package org.apache.tools.ant.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - import java.util.Arrays; import java.util.Collection; import java.util.Vector; import org.junit.Test; +import static org.hamcrest.Matchers.endsWith; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + /** * Test for StringUtils */ @@ -103,7 +105,7 @@ public class StringUtilsTest { public void testEndsWithJDKPerf() { StringBuffer buf = getFilledBuffer(1024 * 300, 'a'); for (int i = 0; i < 1000; i++) { - assertTrue(buf.toString().endsWith("aa")); + assertThat(buf.toString(), endsWith("aa")); } }