From 81c3e6e3ddf5b1ef4e66018f1047e1b2ae8b3173 Mon Sep 17 00:00:00 2001 From: Gintas Grigelionis Date: Sat, 14 Apr 2018 14:36:57 +0200 Subject: [PATCH] Deprecate AntAssert --- WHATSNEW | 6 + .../junit/org/apache/tools/ant/AntAssert.java | 4 + .../org/apache/tools/ant/BuildFileRule.java | 14 +- .../junit/org/apache/tools/ant/CaseTest.java | 18 +- .../apache/tools/ant/ExtendedTaskdefTest.java | 32 ++-- .../org/apache/tools/ant/IncludeTest.java | 68 +++----- .../org/apache/tools/ant/LoaderRefTest.java | 16 +- .../org/apache/tools/ant/LocationTest.java | 11 +- .../ant/ProjectHelperRepositoryTest.java | 31 +--- .../org/apache/tools/ant/ProjectTest.java | 5 +- .../apache/tools/ant/PropertyFileCLITest.java | 5 +- .../apache/tools/ant/TaskContainerTest.java | 31 ++-- .../tools/ant/filters/DynamicFilterTest.java | 5 +- .../tools/ant/filters/TokenFilterTest.java | 104 ++++++----- .../apache/tools/ant/launch/LocatorTest.java | 25 ++- .../ant/taskdefs/AbstractCvsTaskTest.java | 7 +- .../tools/ant/taskdefs/AntStructureTest.java | 5 +- .../apache/tools/ant/taskdefs/AntTest.java | 17 +- .../tools/ant/taskdefs/CallTargetTest.java | 7 +- .../apache/tools/ant/taskdefs/ConcatTest.java | 28 +-- .../apache/tools/ant/taskdefs/CopyTest.java | 65 +++---- .../tools/ant/taskdefs/EchoXMLTest.java | 7 +- .../tools/ant/taskdefs/FixCrLfTest.java | 23 ++- .../apache/tools/ant/taskdefs/GetTest.java | 99 +++++------ .../apache/tools/ant/taskdefs/ImportTest.java | 86 ++++----- .../apache/tools/ant/taskdefs/JarTest.java | 108 +++++------- .../apache/tools/ant/taskdefs/JavaTest.java | 114 +++++------- .../apache/tools/ant/taskdefs/JavacTest.java | 9 +- .../tools/ant/taskdefs/LoadFileTest.java | 56 +++--- .../tools/ant/taskdefs/MacroDefTest.java | 99 +++++------ .../tools/ant/taskdefs/MakeUrlTest.java | 50 +++--- .../ant/taskdefs/ManifestClassPathTest.java | 82 +++++---- .../tools/ant/taskdefs/ManifestTest.java | 165 +++++++----------- .../apache/tools/ant/taskdefs/NiceTest.java | 25 ++- .../tools/ant/taskdefs/PreSetDefTest.java | 24 ++- .../tools/ant/taskdefs/PropertyTest.java | 58 +++--- .../tools/ant/taskdefs/RmicAdvancedTest.java | 63 +++---- .../tools/ant/taskdefs/SQLExecTest.java | 44 ++--- .../apache/tools/ant/taskdefs/StyleTest.java | 9 +- .../apache/tools/ant/taskdefs/SubAntTest.java | 41 ++--- .../apache/tools/ant/taskdefs/SyncTest.java | 33 ++-- .../tools/ant/taskdefs/TaskdefTest.java | 15 +- .../apache/tools/ant/taskdefs/TouchTest.java | 17 +- .../tools/ant/taskdefs/TypeAdapterTest.java | 25 ++- .../tools/ant/taskdefs/TypedefTest.java | 70 +++----- .../apache/tools/ant/taskdefs/ZipTest.java | 67 ++++--- .../compilers/DefaultCompilerAdapterTest.java | 14 +- .../condition/IsFileSelectedTest.java | 17 +- .../taskdefs/condition/IsReachableTest.java | 56 +++--- .../ant/taskdefs/condition/TypeFoundTest.java | 15 +- .../ant/taskdefs/optional/ANTLRTest.java | 17 +- .../taskdefs/optional/EchoPropertiesTest.java | 65 +++---- .../tools/ant/taskdefs/optional/RpmTest.java | 18 +- .../taskdefs/optional/SchemaValidateTest.java | 56 ++---- .../taskdefs/optional/XmlValidateTest.java | 69 +++----- .../tools/ant/taskdefs/optional/XsltTest.java | 19 +- .../taskdefs/optional/depend/DependTest.java | 37 ++-- .../taskdefs/optional/image/ImageTest.java | 32 ++-- .../optional/jdepend/JDependTest.java | 16 +- .../optional/junit/JUnitReportTest.java | 15 +- .../optional/junit/JUnitTaskTest.java | 73 ++++---- .../optional/junit/JUnitTestListenerTest.java | 37 ++-- .../optional/junit/JUnitTestRunnerTest.java | 5 +- .../optional/junit/TearDownOnVmCrashTest.java | 9 +- .../XMLFormatterWithCDATAOnSystemOut.java | 7 +- .../ant/taskdefs/optional/net/FTPTest.java | 5 +- .../optional/script/ScriptDefTest.java | 84 ++++----- .../apache/tools/ant/types/AddTypeTest.java | 46 +++-- .../tools/ant/types/AssertionsTest.java | 42 ++--- .../tools/ant/types/CommandlineJavaTest.java | 12 +- .../org/apache/tools/ant/types/PathTest.java | 4 +- .../org/apache/tools/ant/types/PolyTest.java | 11 +- .../ant/types/RedirectorElementTest.java | 9 +- .../types/optional/ScriptSelectorTest.java | 5 +- .../types/selectors/ModifiedSelectorTest.java | 8 +- .../util/LayoutPreservingPropertiesTest.java | 88 +++++----- 76 files changed, 1247 insertions(+), 1537 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index a017dfda4..611ff1490 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -13,6 +13,12 @@ Fixed bugs: * The junit task when used with includeantruntime="no" was incorrectly printing a warning about multiple versions of ant detected in path +Other changes: +-------------- + + * AntAssert is deprecated, assertThat from JUnit 4.4+, Hamcrest matchers and/or + ExpectedException rule provide equivalent functionality + Changes from Ant 1.10.2 TO Ant 1.10.3 ===================================== diff --git a/src/tests/junit/org/apache/tools/ant/AntAssert.java b/src/tests/junit/org/apache/tools/ant/AntAssert.java index d23792e6a..93a1562ee 100644 --- a/src/tests/junit/org/apache/tools/ant/AntAssert.java +++ b/src/tests/junit/org/apache/tools/ant/AntAssert.java @@ -23,7 +23,11 @@ import static org.junit.Assert.assertTrue; /** * Provides common assert functions for use across multiple tests, similar to the Asserts * within JUnit. + * + * @deprecated use assertThat() in JUnit 4.4+ in combination with containsString() matcher; + * for exception messages, use ExpectedException rule. */ +@Deprecated public class AntAssert { /** diff --git a/src/tests/junit/org/apache/tools/ant/BuildFileRule.java b/src/tests/junit/org/apache/tools/ant/BuildFileRule.java index c5a71c75a..dcbbcea4b 100644 --- a/src/tests/junit/org/apache/tools/ant/BuildFileRule.java +++ b/src/tests/junit/org/apache/tools/ant/BuildFileRule.java @@ -35,6 +35,9 @@ import org.junit.rules.ExternalResource; * \@Rule * public BuildFileRule rule = new BuildFileRule(); * + * \@Rule + * public ExpectedException thrown = ExpectedException.none(); + * * \@Before * public void setUp() { * rule.configureProject("my/and/file.xml"); @@ -48,12 +51,9 @@ import org.junit.rules.ExternalResource; * * \@Test * public void testException() { - * try { - * rule.executeTarget("failingTarget"); - * fail("Target should have thrown a BuildException"); - * } catch (BuildException ex) { - * assertContains("Exception did not contain correct text", "Could not find compiler on classpath", ex.getMessage()); - * } + * thrown.expect(BuildException.class); + * thrown.expectMessage("Could not find compiler on classpath"); + * rule.executeTarget("failingTarget"); * } * * } @@ -68,8 +68,6 @@ public class BuildFileRule extends ExternalResource { private StringBuffer outputBuffer; private StringBuffer errorBuffer; - - /** * Tidies up following a test execution. If the currently configured * project has a tearDown target then this will automatically diff --git a/src/tests/junit/org/apache/tools/ant/CaseTest.java b/src/tests/junit/org/apache/tools/ant/CaseTest.java index a466ea404..d2abc2752 100644 --- a/src/tests/junit/org/apache/tools/ant/CaseTest.java +++ b/src/tests/junit/org/apache/tools/ant/CaseTest.java @@ -21,9 +21,7 @@ package org.apache.tools.ant; import org.junit.Before; import org.junit.Rule; import org.junit.Test; - -import static org.apache.tools.ant.AntAssert.assertContains; -import static org.junit.Assert.fail; +import org.junit.rules.ExpectedException; /** * Simple tests of build file processing @@ -33,6 +31,9 @@ public class CaseTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/core/case.xml"); @@ -49,15 +50,12 @@ public class CaseTest { /** * Test whether the build file uses case when determining - * task names. + * task names. Task name should be case sensitive. */ @Test public void testTaskCase() { - try { - buildRule.executeTarget("taskcase"); - fail("Build exception should have been thrown due to case sensitivity of name"); - } catch (BuildException ex) { - assertContains("Task names should be case sensitive", "Problem: failed to create task or type ecHO", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Problem: failed to create task or type ecHO"); + buildRule.executeTarget("taskcase"); } } diff --git a/src/tests/junit/org/apache/tools/ant/ExtendedTaskdefTest.java b/src/tests/junit/org/apache/tools/ant/ExtendedTaskdefTest.java index e5413b6af..74143facc 100644 --- a/src/tests/junit/org/apache/tools/ant/ExtendedTaskdefTest.java +++ b/src/tests/junit/org/apache/tools/ant/ExtendedTaskdefTest.java @@ -17,14 +17,11 @@ */ package org.apache.tools.ant; -import static org.junit.Assert.fail; - -import static org.apache.tools.ant.AntAssert.assertContains; - import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; /** * created 16-Mar-2006 12:25:12 @@ -35,6 +32,9 @@ public class ExtendedTaskdefTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/core/extended-taskdef.xml"); @@ -45,24 +45,24 @@ public class ExtendedTaskdefTest { buildRule.executeTarget("teardown"); } + /** + * Exception should be thrown by a subclass + */ @Test public void testRun() { - try { - buildRule.executeTarget("testRun"); - fail("BuildException should have been thrown"); - } catch (BuildException ex) { - assertContains("exception thrown by the subclass", "executing the Foo task", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("executing the Foo task"); + buildRule.executeTarget("testRun"); } + /** + * Exception should be thrown by a subclass + */ @Test public void testRun2() { - try { - buildRule.executeTarget("testRun2"); - fail("BuildException should have been thrown"); - } catch (BuildException ex) { - assertContains("exception thrown by the subclass", "executing the Foo task", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("executing the Foo task"); + buildRule.executeTarget("testRun2"); } } diff --git a/src/tests/junit/org/apache/tools/ant/IncludeTest.java b/src/tests/junit/org/apache/tools/ant/IncludeTest.java index 80e53fe7c..444008dd9 100644 --- a/src/tests/junit/org/apache/tools/ant/IncludeTest.java +++ b/src/tests/junit/org/apache/tools/ant/IncludeTest.java @@ -18,13 +18,16 @@ package org.apache.tools.ant; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.both; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasProperty; +import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; /** * Test the build file inclusion using XML entities. @@ -35,6 +38,9 @@ public class IncludeTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Test public void test1() { buildRule.configureProject("src/etc/testcases/core/include/basic/include.xml"); @@ -72,59 +78,39 @@ public class IncludeTest { @Test public void testParseErrorInIncluding() { - try { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("location", hasProperty("fileName", + containsString("build.xml")))); buildRule.configureProject("src/etc/testcases/core/include/including_file_parse_error/build.xml"); - fail("should have caused a parser exception"); - } catch (BuildException e) { - assertContains(e.getLocation().toString() - + " should refer to build.xml", - "build.xml:", e.getLocation().toString()); - } } @Test public void testTaskErrorInIncluding() { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("location", + both(hasProperty("fileName", containsString("build.xml"))) + .and(hasProperty("lineNumber", equalTo(14))))); + thrown.expectMessage(startsWith("Warning: Could not find file ")); buildRule.configureProject("src/etc/testcases/core/include/including_file_task_error/build.xml"); - try { - buildRule.executeTarget("test"); - fail("should have cause a build failure"); - } catch (BuildException e) { - assertTrue(e.getMessage() - + " should start with \'Warning: Could not find", - e.getMessage().startsWith("Warning: Could not find file ")); - assertTrue(e.getLocation().toString() - + " should end with build.xml:14: ", - e.getLocation().toString().endsWith("build.xml:14: ")); - } } @Test public void testParseErrorInIncluded() { - try { - buildRule.configureProject("src/etc/testcases/core/include/included_file_parse_error/build.xml"); - fail("should have caused a parser exception"); - } catch (BuildException e) { - assertContains(e.getLocation().toString() - + " should refer to included_file.xml", - "included_file.xml:", - e.getLocation().toString()); - } + thrown.expect(BuildException.class); + thrown.expect(hasProperty("location", + hasProperty("fileName", containsString("included_file.xml")))); + buildRule.configureProject("src/etc/testcases/core/include/included_file_parse_error/build.xml"); } @Test public void testTaskErrorInIncluded() { + thrown.expect(BuildException.class); + thrown.expect(hasProperty("location", + both(hasProperty("fileName", containsString("included_file.xml"))) + .and(hasProperty("lineNumber", equalTo(2))))); + thrown.expectMessage(startsWith("Warning: Could not find file ")); buildRule.configureProject("src/etc/testcases/core/include/included_file_task_error/build.xml"); - try { - buildRule.executeTarget("test"); - fail("should have cause a build failure"); - } catch (BuildException e) { - assertTrue(e.getMessage() - + " should start with \'Warning: Could not find", - e.getMessage().startsWith("Warning: Could not find file ")); - assertTrue(e.getLocation().toString() - + " should end with included_file.xml:2: ", - e.getLocation().toString().endsWith("included_file.xml:2: ")); - } + buildRule.executeTarget("test"); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/LoaderRefTest.java b/src/tests/junit/org/apache/tools/ant/LoaderRefTest.java index 668170bdf..14c5a15a3 100644 --- a/src/tests/junit/org/apache/tools/ant/LoaderRefTest.java +++ b/src/tests/junit/org/apache/tools/ant/LoaderRefTest.java @@ -18,12 +18,10 @@ package org.apache.tools.ant; -import static org.apache.tools.ant.AntAssert.assertContains; -import static org.junit.Assert.fail; - import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; /** */ @@ -32,6 +30,9 @@ public class LoaderRefTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/core/loaderref/loaderref.xml"); @@ -41,11 +42,8 @@ public class LoaderRefTest { // override allowed on @Test public void testBadRef() { - try { - buildRule.executeTarget("testbadref"); - fail("BuildRule should have thrown an exception due to a bad classloader being specified"); - } catch (BuildException ex) { - assertContains("Should fail due to ref not being a class loader", "does not reference a class loader", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("does not reference a class loader"); + buildRule.executeTarget("testbadref"); } } diff --git a/src/tests/junit/org/apache/tools/ant/LocationTest.java b/src/tests/junit/org/apache/tools/ant/LocationTest.java index e6e3d535c..035167f1a 100644 --- a/src/tests/junit/org/apache/tools/ant/LocationTest.java +++ b/src/tests/junit/org/apache/tools/ant/LocationTest.java @@ -25,10 +25,11 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertThat; public class LocationTest { @@ -71,16 +72,16 @@ public class LocationTest { public void testMacrodefWrappedTask() { buildRule.executeTarget("testMacrodefWrappedTask"); Echo e = buildRule.getProject().getReference("echo3"); - assertTrue(buildRule.getLog().contains("Line: " - + (e.getLocation().getLineNumber() + 1))); + assertThat(buildRule.getLog(), + containsString("Line: " + (e.getLocation().getLineNumber() + 1))); } @Test public void testPresetdefWrappedTask() { buildRule.executeTarget("testPresetdefWrappedTask"); Echo e = buildRule.getProject().getReference("echo4"); - assertTrue(buildRule.getLog().contains("Line: " - + (e.getLocation().getLineNumber() + 1))); + assertThat(buildRule.getLog(), + containsString("Line: " + (e.getLocation().getLineNumber() + 1))); } public static class EchoLocation extends Task { diff --git a/src/tests/junit/org/apache/tools/ant/ProjectHelperRepositoryTest.java b/src/tests/junit/org/apache/tools/ant/ProjectHelperRepositoryTest.java index 6b766f3c0..3d72a75fa 100644 --- a/src/tests/junit/org/apache/tools/ant/ProjectHelperRepositoryTest.java +++ b/src/tests/junit/org/apache/tools/ant/ProjectHelperRepositoryTest.java @@ -26,7 +26,6 @@ import org.apache.tools.ant.types.resources.StringResource; import org.junit.Test; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; /** * Testing around the management of the project helpers @@ -74,32 +73,20 @@ public class ProjectHelperRepositoryTest { assertTrue(helper instanceof ProjectHelper2); } - @Test - public void testNoDefaultContructor() { + @Test(expected = BuildException.class) + public void testNoDefaultConstructor() { - class IncrrectHelper extends ProjectHelper { + class IncorrectHelper extends ProjectHelper { // the default constructor is not visible to ant here } - ProjectHelperRepository repo = ProjectHelperRepository.getInstance(); - try { - repo.registerProjectHelper(IncrrectHelper.class); - fail("Registring an helper with no default constructor should fail"); - } catch (BuildException e) { - // ok - //TODO we should be asserting a value in here - } + ProjectHelperRepository.getInstance().registerProjectHelper(IncorrectHelper.class); + // TODO we should be asserting a value in here } - @Test - public void testUnkwnowHelper() { - ProjectHelperRepository repo = ProjectHelperRepository.getInstance(); - try { - repo.registerProjectHelper("xxx.yyy.zzz.UnknownHelper"); - fail("Registring an unknwon helper should fail"); - } catch (BuildException e) { - // ok - //TODO we should be asserting a value in here - } + @Test(expected = BuildException.class) + public void testUnknownHelper() { + ProjectHelperRepository.getInstance().registerProjectHelper("xxx.yyy.zzz.UnknownHelper"); + // TODO we should be asserting a value in here } } diff --git a/src/tests/junit/org/apache/tools/ant/ProjectTest.java b/src/tests/junit/org/apache/tools/ant/ProjectTest.java index 15340b6ca..b98cf732f 100644 --- a/src/tests/junit/org/apache/tools/ant/ProjectTest.java +++ b/src/tests/junit/org/apache/tools/ant/ProjectTest.java @@ -32,12 +32,13 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -268,7 +269,7 @@ public class ProjectTest { // overriding target from imported buildfile is allowed buildRule.configureProject("src/etc/testcases/core/duplicate-target2.xml"); buildRule.executeTarget("once"); - assertContains("once from buildfile", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("once from buildfile")); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/PropertyFileCLITest.java b/src/tests/junit/org/apache/tools/ant/PropertyFileCLITest.java index 371888298..4f5e78e18 100644 --- a/src/tests/junit/org/apache/tools/ant/PropertyFileCLITest.java +++ b/src/tests/junit/org/apache/tools/ant/PropertyFileCLITest.java @@ -24,7 +24,8 @@ import java.io.FileWriter; import org.apache.tools.ant.util.FileUtils; import org.junit.Test; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; public class PropertyFileCLITest { @@ -54,7 +55,7 @@ public class PropertyFileCLITest { "-l", log.getAbsolutePath() }, null, null); String l = FileUtils.safeReadFully(fr = new FileReader(log)); - assertContains("Hello, world", l); + assertThat(l, containsString("Hello, world")); } finally { FileUtils.close(fw); FileUtils.close(fr); diff --git a/src/tests/junit/org/apache/tools/ant/TaskContainerTest.java b/src/tests/junit/org/apache/tools/ant/TaskContainerTest.java index 491d3618f..888bbeb6f 100644 --- a/src/tests/junit/org/apache/tools/ant/TaskContainerTest.java +++ b/src/tests/junit/org/apache/tools/ant/TaskContainerTest.java @@ -22,7 +22,8 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import static org.junit.Assert.assertTrue; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; public class TaskContainerTest { @@ -37,30 +38,30 @@ public class TaskContainerTest { @Test public void testPropertyExpansion() { buildRule.executeTarget("testPropertyExpansion"); - assertTrue("attribute worked", - buildRule.getLog().contains("As attribute: it worked")); - assertTrue("nested text worked", - buildRule.getLog().contains("As nested text: it worked")); + assertThat("attribute worked", buildRule.getLog(), + containsString(("As attribute: it worked"))); + assertThat("nested text worked", buildRule.getLog(), + containsString(("As nested text: it worked"))); } @Test public void testTaskdef() { buildRule.executeTarget("testTaskdef"); - assertTrue("attribute worked", - buildRule.getLog().contains("As attribute: it worked")); - assertTrue("nested text worked", - buildRule.getLog().contains("As nested text: it worked")); - assertTrue("nested text worked", - buildRule.getLog().contains("As nested task: it worked")); + assertThat("attribute worked", buildRule.getLog(), + containsString(("As attribute: it worked"))); + assertThat("nested text worked", buildRule.getLog(), + containsString(("As nested text: it worked"))); + assertThat("nested text worked", buildRule.getLog(), + containsString(("As nested task: it worked"))); } @Test public void testCaseInsensitive() { buildRule.executeTarget("testCaseInsensitive"); - assertTrue("works outside of container", - buildRule.getLog().contains("hello ")); - assertTrue("works inside of container", - buildRule.getLog().contains("world")); + assertThat("works outside of container", buildRule.getLog(), + containsString(("hello "))); + assertThat("works inside of container", buildRule.getLog(), + containsString(("world"))); } } diff --git a/src/tests/junit/org/apache/tools/ant/filters/DynamicFilterTest.java b/src/tests/junit/org/apache/tools/ant/filters/DynamicFilterTest.java index 85df0cec8..b85b41e00 100644 --- a/src/tests/junit/org/apache/tools/ant/filters/DynamicFilterTest.java +++ b/src/tests/junit/org/apache/tools/ant/filters/DynamicFilterTest.java @@ -28,7 +28,8 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; public class DynamicFilterTest { @@ -46,7 +47,7 @@ public class DynamicFilterTest { buildRule.executeTarget("dynamicfilter"); String content = FileUtilities.getFileContents( new File(buildRule.getProject().getProperty("output") + "/dynamicfilter")); - assertContains("hellO wOrld", content); + assertThat(content, containsString("hellO wOrld")); } public static class CustomFilter implements ChainableReader { 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 7d8267097..fb60f2c8d 100644 --- a/src/tests/junit/org/apache/tools/ant/filters/TokenFilterTest.java +++ b/src/tests/junit/org/apache/tools/ant/filters/TokenFilterTest.java @@ -18,9 +18,10 @@ package org.apache.tools.ant.filters; -import static org.apache.tools.ant.AntAssert.assertContains; -import static org.apache.tools.ant.AntAssert.assertNotContains; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; @@ -58,57 +59,57 @@ public class TokenFilterTest { @Test public void testTrimignore() { buildRule.executeTarget("trimignore"); - assertContains("Hello-World", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Hello-World")); } @Test public void testStringTokenizer() { buildRule.executeTarget("stringtokenizer"); - assertContains("#This#is#a#number#of#words#", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("#This#is#a#number#of#words#")); } @Test public void testUnixLineOutput() throws IOException { buildRule.executeTarget("unixlineoutput"); - assertContains("\nThis\nis\na\nnumber\nof\nwords\n", - getFileString(buildRule.getProject().getProperty("output") + "/unixlineoutput")); + assertThat(getFileString(buildRule.getProject().getProperty("output") + "/unixlineoutput"), + containsString("\nThis\nis\na\nnumber\nof\nwords\n")); } @Test public void testDosLineOutput() throws IOException { buildRule.executeTarget("doslineoutput"); - assertContains("\r\nThis\r\nis\r\na\r\nnumber\r\nof\r\nwords\r\n", - getFileString(buildRule.getProject().getProperty("output") + "/doslineoutput")); + assertThat(getFileString(buildRule.getProject().getProperty("output") + "/doslineoutput"), + containsString("\r\nThis\r\nis\r\na\r\nnumber\r\nof\r\nwords\r\n")); } @Test public void testFileTokenizer() throws IOException { buildRule.executeTarget("filetokenizer"); String contents = getFileString(buildRule.getProject().getProperty("output") + "/filetokenizer"); - assertContains(" of words", contents); - assertNotContains(" This is", contents); + assertThat(contents, containsString(" of words")); + assertThat(contents, not(containsString(" This is"))); } @Test public void testReplaceString() throws IOException { buildRule.executeTarget("replacestring"); - assertContains("this is the moon", - getFileString(buildRule.getProject().getProperty("output") + "/replacestring")); + assertThat(getFileString(buildRule.getProject().getProperty("output") + "/replacestring"), + containsString("this is the moon")); } @Test public void testReplaceStrings() { buildRule.executeTarget("replacestrings"); - assertContains("bar bar bar", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("bar bar bar")); } @Test public void testContainsString() throws IOException { buildRule.executeTarget("containsstring"); String contents = getFileString(buildRule.getProject().getProperty("output") + "/containsstring"); - assertContains("this is a line contains foo", contents); - assertNotContains("this line does not", contents); + assertThat(contents, containsString("this is a line contains foo")); + assertThat(contents, not(containsString("this line does not"))); } @Test @@ -120,12 +121,12 @@ public class TokenFilterTest { buildRule.executeTarget("replaceregex"); String contents = getFileString(buildRule.getProject().getProperty("output") + "/replaceregex"); - assertContains("world world world world", contents); - assertContains("dog Cat dog", contents); - assertContains("moon Sun Sun", contents); - assertContains("found WhiteSpace", contents); - assertContains("Found digits [1234]", contents); - assertNotContains("This is a line with digits", contents); + assertThat(contents, containsString("world world world world")); + assertThat(contents, containsString("dog Cat dog")); + assertThat(contents, containsString("moon Sun Sun")); + assertThat(contents, containsString("found WhiteSpace")); + assertThat(contents, containsString("Found digits [1234]")); + assertThat(contents, not(containsString("This is a line with digits"))); } @Test @@ -135,15 +136,16 @@ public class TokenFilterTest { getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp").contains("bye world")); buildRule.executeTarget("filterreplaceregex"); - String contents = getFileString(buildRule.getProject().getProperty("output") + "/filterreplaceregex"); - assertContains("world world world world", contents); + assertThat(getFileString(buildRule.getProject().getProperty("output") + "/filterreplaceregex"), + containsString("world world world world")); } @Test public void testHandleDollerMatch() throws IOException { buildRule.executeTarget("hasregex"); - assumeTrue("Regex not present", getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp").contains("bye world")); + assumeTrue("Regex not present", + getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp").contains("bye world")); buildRule.executeTarget("dollermatch"); } @@ -154,7 +156,7 @@ public class TokenFilterTest { 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.")); - assertContains(" This is the second", contents); + assertThat(contents, containsString(" This is the second")); } @Test @@ -163,21 +165,21 @@ public class TokenFilterTest { 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.")); - assertNotContains(" This is the second", contents); - assertContains("file.\nThis is the second", contents); + assertThat(contents, not(containsString(" This is the second"))); + assertThat(contents, containsString("file.\nThis is the second")); } @Test public void testFilterReplaceString() throws IOException { buildRule.executeTarget("filterreplacestring"); - String contents = getFileString(buildRule.getProject().getProperty("output") + "/filterreplacestring"); - assertContains("This is the moon", contents); + assertThat(getFileString(buildRule.getProject().getProperty("output") + "/filterreplacestring"), + containsString("This is the moon")); } @Test public void testFilterReplaceStrings() { buildRule.executeTarget("filterreplacestrings"); - assertContains("bar bar bar", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("bar bar bar")); } @Test @@ -189,40 +191,42 @@ public class TokenFilterTest { buildRule.executeTarget("containsregex"); String contents = getFileString(buildRule.getProject().getProperty("output") + "/containsregex"); - assertContains("hello world", contents); - assertNotContains("this is the moon", contents); - assertContains("World here", contents); + assertThat(contents, containsString("hello world")); + assertThat(contents, not(containsString("this is the moon"))); + assertThat(contents, containsString("World here")); } @Test public void testFilterContainsRegex() throws IOException { buildRule.executeTarget("hasregex"); - assumeTrue("Regex not present", getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp").contains("bye world")); + assumeTrue("Regex not present", + getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp").contains("bye world")); buildRule.executeTarget("filtercontainsregex"); String contents = getFileString(buildRule.getProject().getProperty("output") + "/filtercontainsregex"); - assertContains("hello world", contents); - assertNotContains("this is the moon", contents); - assertContains("World here", contents); + assertThat(contents, containsString("hello world")); + assertThat(contents, not(containsString("this is the moon"))); + assertThat(contents, containsString("World here")); } @Test public void testContainsRegex2() throws IOException { buildRule.executeTarget("hasregex"); - assumeTrue("Regex not present", getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp").contains("bye world")); + assumeTrue("Regex not present", + getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp").contains("bye world")); buildRule.executeTarget("containsregex2"); - String contents = getFileString(buildRule.getProject().getProperty("output") + "/containsregex2"); - assertContains("void register_bits();", contents); + assertThat(getFileString(buildRule.getProject().getProperty("output") + "/containsregex2"), + containsString("void register_bits();")); } @Test public void testDeleteCharacters() throws IOException { buildRule.executeTarget("deletecharacters"); String contents = getFileString(buildRule.getProject().getProperty("output") + "/deletechars"); - assertNotContains("#", contents); - assertNotContains("*", contents); - assertContains("This is some ", contents); + assertThat(contents, not(containsString("#"))); + assertThat(contents, not(containsString("*"))); + assertThat(contents, containsString("This is some ")); } @Test @@ -230,21 +234,25 @@ public class TokenFilterTest { assumeTrue("Project does not have 'testScriptFilter' target", buildRule.getProject().getTargets().contains("testScriptFilter")); buildRule.executeTarget("scriptfilter"); - assertContains("HELLO WORLD", getFileString(buildRule.getProject().getProperty("output") + "/scriptfilter")); + assertThat(getFileString(buildRule.getProject().getProperty("output") + "/scriptfilter"), + containsString("HELLO WORLD")); } @Test public void testScriptFilter2() throws IOException { - assumeTrue("Project does not have 'testScriptFilter' target", buildRule.getProject().getTargets().contains("testScriptFilter")); + assumeTrue("Project does not have 'testScriptFilter' target", + buildRule.getProject().getTargets().contains("testScriptFilter")); buildRule.executeTarget("scriptfilter2"); - assertContains("HELLO MOON", getFileString(buildRule.getProject().getProperty("output") + "/scriptfilter2")); + assertThat(getFileString(buildRule.getProject().getProperty("output") + "/scriptfilter2"), + containsString("HELLO MOON")); } @Test public void testCustomTokenFilter() throws IOException { buildRule.executeTarget("customtokenfilter"); - assertContains("Hello World", getFileString(buildRule.getProject().getProperty("output") + "/custom")); + assertThat(getFileString(buildRule.getProject().getProperty("output") + "/custom"), + containsString("Hello World")); } // ------------------------------------------------------ @@ -255,7 +263,7 @@ public class TokenFilterTest { Reader r = null; try { r = new FileReader(FILE_UTILS.resolveFile(buildRule.getProject().getBaseDir(),filename)); - return FileUtils.readFully(r); + return FileUtils.readFully(r); } finally { FileUtils.close(r); diff --git a/src/tests/junit/org/apache/tools/ant/launch/LocatorTest.java b/src/tests/junit/org/apache/tools/ant/launch/LocatorTest.java index b4e1e9b7c..fc43899b1 100644 --- a/src/tests/junit/org/apache/tools/ant/launch/LocatorTest.java +++ b/src/tests/junit/org/apache/tools/ant/launch/LocatorTest.java @@ -22,11 +22,12 @@ import java.io.File; import org.apache.tools.ant.taskdefs.condition.Os; import org.junit.Before; import org.junit.Ignore; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static junit.framework.Assert.assertEquals; -import static org.apache.tools.ant.AntAssert.assertContains; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertEquals; +import static org.junit.Assume.assumeTrue; /** Test the locator in the ant-launch JAR */ public class LocatorTest { @@ -36,6 +37,8 @@ public class LocatorTest { private static final String SHARED_JAR_URI = "jar:file:" + LAUNCHER_JAR + "!/org/apache/tools/ant/launch/Launcher.class"; + @Rule + public ExpectedException thrown = ExpectedException.none(); @Before public void setUp() { @@ -136,25 +139,19 @@ public class LocatorTest { */ @Test public void testFileFromRemoteShare() { + assumeTrue("not Windows", windows); String resolved = Locator.fromJarURI(SHARED_JAR_URI); File f = new File(resolved); String path = f.getAbsolutePath(); - if (windows) { - assertEquals(0, path.indexOf("\\\\")); - } + assertEquals(0, path.indexOf("\\\\")); } @Test public void testHttpURI() { String url = "http://ant.apache.org"; - try { - Locator.fromURI(url); - fail("Exception should have been thrown"); - } catch (IllegalArgumentException e) { - String message = e.getMessage(); - assertContains(Locator.ERROR_NOT_FILE_URI, message); - assertContains(url, message); - } + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage(Locator.ERROR_NOT_FILE_URI + url); + Locator.fromURI(url); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/AbstractCvsTaskTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/AbstractCvsTaskTest.java index 1f5ceeaf4..7b7e6c3f6 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/AbstractCvsTaskTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/AbstractCvsTaskTest.java @@ -25,8 +25,9 @@ import org.junit.Test; import java.io.File; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; /** @@ -57,7 +58,7 @@ public class AbstractCvsTaskTest { File f = new File(buildRule.getProject().getProperty("output") + "/src/Makefile"); assertFalse("starting empty", f.exists()); buildRule.executeTarget("package-attribute"); - assertContains("U src/Makefile", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("U src/Makefile")); assertTrue("now it is there", f.exists()); } @@ -66,7 +67,7 @@ public class AbstractCvsTaskTest { File f = new File(buildRule.getProject().getProperty("output") + "/src/Makefile"); assertFalse("starting empty", f.exists()); buildRule.executeTarget("tag-attribute"); - assertContains("OPENBSD_5_3", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("OPENBSD_5_3")); assertTrue("now it is there", f.exists()); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/AntStructureTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/AntStructureTest.java index 1eb24bc8a..8c05288a8 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/AntStructureTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/AntStructureTest.java @@ -29,9 +29,10 @@ import org.junit.Test; import java.io.PrintWriter; import java.util.Hashtable; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; 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.Assert.fail; @@ -70,7 +71,7 @@ public class AntStructureTest { // the test has likely been loaded via a different classloader // than this class. Therefore we make the printer assert its // state and only check for the tail invocation. - assertContains(MyPrinter.TAIL_CALLED, buildRule.getLog()); + assertThat(buildRule.getLog(), containsString(MyPrinter.TAIL_CALLED)); } public static class MyPrinter implements AntStructure.StructurePrinter { 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 9b93c77b6..778fba7b1 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/AntTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/AntTest.java @@ -34,12 +34,13 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -290,7 +291,7 @@ public class AntTest { buildRule.getProject().setUserProperty("test", "7"); buildRule.executeTarget("test-property-override-inheritall-start"); - assertContains("The value of test is 7", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("The value of test is 7")); } @Test @@ -298,28 +299,28 @@ public class AntTest { buildRule.getProject().setUserProperty("test", "7"); buildRule.executeTarget("test-property-override-no-inheritall-start"); - assertContains("The value of test is 7", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("The value of test is 7")); } @Test public void testOverrideWinsInheritAll() { buildRule.executeTarget("test-property-override-inheritall-start"); - assertContains("The value of test is 4", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("The value of test is 4")); } @Test public void testOverrideWinsNoInheritAll() { buildRule.executeTarget("test-property-override-no-inheritall-start"); - assertContains("The value of test is 4", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("The value of test is 4")); } @Test public void testPropertySet() { buildRule.executeTarget("test-propertyset"); - assertTrue(buildRule.getLog().contains("test1 is ${test1}")); - assertTrue(buildRule.getLog().contains("test2 is ${test2}")); - assertTrue(buildRule.getLog().contains("test1.x is 1")); + assertThat(buildRule.getLog(), containsString("test1 is ${test1}")); + assertThat(buildRule.getLog(), containsString("test2 is ${test2}")); + assertThat(buildRule.getLog(), containsString("test1.x is 1")); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/CallTargetTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/CallTargetTest.java index 6100d066f..81f7cbfaf 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/CallTargetTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/CallTargetTest.java @@ -26,8 +26,9 @@ import org.junit.Test; import java.util.Vector; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; /** @@ -47,7 +48,7 @@ public class CallTargetTest { @Test public void testInheritRefFileSet() { buildRule.executeTarget("testinheritreffileset"); - assertContains("calltarget.xml", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("calltarget.xml")); } // see bugrep 21724 (references not passing through with antcall) @@ -64,7 +65,7 @@ public class CallTargetTest { v.add("call-multi"); v.add("call-multi"); buildRule.getProject().executeTargets(v); - assertContains("multi is SETmulti is SET", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("multi is SETmulti is SET")); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ConcatTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ConcatTest.java index 2491937a2..e4e8b3fb4 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/ConcatTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ConcatTest.java @@ -29,9 +29,10 @@ import org.junit.Test; import java.io.File; import java.io.IOException; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; 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.Assert.fail; @@ -150,7 +151,7 @@ public class ConcatTest { String filename = "src/etc/testcases/taskdefs/thisfiledoesnotexist" .replace('/', File.separatorChar); buildRule.executeTarget("test6"); - assertContains(filename + " does not exist", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString(filename + " does not exist")); } @Test @@ -200,7 +201,7 @@ public class ConcatTest { @Test public void testFilter() { buildRule.executeTarget("testfilter"); - assertTrue(buildRule.getLog().contains("REPLACED")); + assertThat(buildRule.getLog(), containsString("REPLACED")); } @Test @@ -252,7 +253,7 @@ public class ConcatTest { @Test public void testfilterinline() { buildRule.executeTarget("testfilterinline"); - assertTrue(buildRule.getLog().contains("REPLACED")); + assertThat(buildRule.getLog(), containsString("REPLACED")); } /** @@ -261,8 +262,8 @@ public class ConcatTest { @Test public void testmultireader() { buildRule.executeTarget("testmultireader"); - assertTrue(buildRule.getLog().contains("Bye")); - assertFalse(buildRule.getLog().contains("Hello")); + assertThat(buildRule.getLog(), containsString("Bye")); + assertThat(buildRule.getLog(), not(containsString("Hello"))); } /** * Check if fixlastline works @@ -270,8 +271,8 @@ public class ConcatTest { @Test public void testfixlastline() throws IOException { buildRule.executeTarget("testfixlastline"); - assertContains("end of line" + System.lineSeparator() + "This has", - FileUtilities.getFileContents(buildRule.getProject(), "concat.line4")); + assertThat(FileUtilities.getFileContents(buildRule.getProject(), "concat.line4"), + containsString("end of line" + System.lineSeparator() + "This has")); } /** @@ -280,7 +281,8 @@ public class ConcatTest { @Test public void testfixlastlineeol() throws IOException { buildRule.executeTarget("testfixlastlineeol"); - assertContains("end of line\rThis has", FileUtilities.getFileContents(buildRule.getProject(), "concat.linecr")); + assertThat(FileUtilities.getFileContents(buildRule.getProject(), "concat.linecr"), + containsString("end of line\rThis has")); } @Test @@ -300,10 +302,8 @@ public class ConcatTest { throws IOException { buildRule.executeTarget(target); String content = FileUtilities.getFileContents(buildRule.getProject(), filename); - assertTrue( - "expecting file " + filename + " to contain " + - contains + - " but got " + content, content.contains(contains)); + assertThat("expecting file " + filename + " to contain " + contains + " but got " + content, + content, containsString(contains)); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/CopyTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/CopyTest.java index e7905afa6..64bb09c92 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/CopyTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/CopyTest.java @@ -27,18 +27,21 @@ import org.junit.Before; import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; +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.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; /** @@ -50,6 +53,8 @@ public class CopyTest { @Rule public final BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); @Before public void setUp() { @@ -61,18 +66,14 @@ public class CopyTest { public void test1() { buildRule.executeTarget("test1"); File f = new File(buildRule.getProject().getProperty("output"), "copytest1.tmp"); - if (!f.exists()) { - fail("Copy failed"); - } + assertTrue("Copy failed", f.exists()); } @Test public void test2() { buildRule.executeTarget("test2"); File f = new File(buildRule.getProject().getProperty("output"), "copytest1dir/copy.xml"); - if (!f.exists()) { - fail("Copy failed"); - } + assertTrue("Copy failed", f.exists()); } @Test @@ -100,19 +101,18 @@ public class CopyTest { // file time checks for java1.2+ assertEquals(file3a.lastModified(), file3.lastModified()); assertTrue(file3c.lastModified() < file3a.lastModified()); - } @Test public void testFilterTest() { buildRule.executeTarget("filtertest"); - assertFalse(buildRule.getLog().contains("loop in tokens")); + assertThat(buildRule.getLog(), not(containsString("loop in tokens"))); } @Test public void testInfiniteFilter() { buildRule.executeTarget("infinitetest"); - assertContains("loop in tokens", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("loop in tokens")); } @Test @@ -128,7 +128,7 @@ public class CopyTest { public void testFilterChain() throws IOException { buildRule.executeTarget("testFilterChain"); File tmp = new File(buildRule.getProject().getProperty("output"), "copy.filterchain.tmp"); - File check = new File(buildRule.getProject().getBaseDir(), "expected/copy.filterset.filtered"); + File check = new File(buildRule.getProject().getBaseDir(), "expected/copy.filterset.filtered"); assertTrue(tmp.exists()); assertEquals(FileUtilities.getFileContents(tmp), FileUtilities.getFileContents(check)); } @@ -136,16 +136,16 @@ public class CopyTest { @Test public void testSingleFileFileset() { buildRule.executeTarget("test_single_file_fileset"); - File file = new File(buildRule.getProject().getProperty("output"), - "copytest_single_file_fileset.tmp"); + File file = new File(buildRule.getProject().getProperty("output"), + "copytest_single_file_fileset.tmp"); assertTrue(file.exists()); } @Test public void testSingleFilePath() { buildRule.executeTarget("test_single_file_path"); - File file = new File(buildRule.getProject().getProperty("output"), - "copytest_single_file_path.tmp"); + File file = new File(buildRule.getProject().getProperty("output"), + "copytest_single_file_path.tmp"); assertTrue(file.exists()); } @@ -160,34 +160,27 @@ public class CopyTest { @Test public void testMissingFileIgnore() { buildRule.executeTarget("testMissingFileIgnore"); - assertContains("Warning: Could not find file", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Warning: Could not find file")); } @Test public void testMissingFileBail() { - try { - buildRule.executeTarget("testMissingFileBail"); - fail("not-there doesn't exist"); - } catch (BuildException ex) { - assertTrue(ex.getMessage() - .startsWith("Warning: Could not find file ")); - } + thrown.expect(BuildException.class); + thrown.expectMessage(startsWith("Warning: Could not find file ")); + buildRule.executeTarget("testMissingFileBail"); } @Test public void testMissingDirIgnore() { buildRule.executeTarget("testMissingDirIgnore"); - assertContains("Warning: ", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Warning: ")); } @Test public void testMissingDirBail() { - try { - buildRule.executeTarget("testMissingDirBail"); - fail("not-there doesn't exist"); - } catch (BuildException ex) { - assertTrue(ex.getMessage().endsWith(" does not exist.")); - } + thrown.expect(BuildException.class); + thrown.expectMessage(endsWith(" does not exist.")); + buildRule.executeTarget("testMissingDirBail"); } @Test @@ -218,11 +211,9 @@ public class CopyTest { buildRule.executeTarget("testFileResourceWithFilter"); File file1 = new File(buildRule.getProject().getProperty("to.dir") + "/fileNR.txt"); assertTrue(file1.exists()); - try { - try (FileReader f = new FileReader(file1)) { - String file1Content = FileUtils.readFully(f); - assertEquals("This is file 42", file1Content); - } + try (FileReader f = new FileReader(file1)) { + String file1Content = FileUtils.readFully(f); + assertEquals("This is file 42", file1Content); } catch (IOException e) { // no-op: not a real business error } 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 cc9ec24ec..5d83a8f77 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/EchoXMLTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/EchoXMLTest.java @@ -25,7 +25,8 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; public class EchoXMLTest { @@ -54,7 +55,7 @@ public class EchoXMLTest { buildRule.executeTarget("testFail"); fail("BuildException expected: must fail"); } catch (BuildException ex) { - assertContains("${foo}=bar", ex.getMessage()); + assertThat(ex.getMessage(), containsString("${foo}=bar")); } } @@ -64,7 +65,7 @@ public class EchoXMLTest { buildRule.executeTarget("testEmpty"); fail("BuildException expected: must fail"); } catch (BuildException ex) { - assertContains("No nested XML specified", ex.getMessage()); + assertThat(ex.getMessage(), containsString("No nested XML specified")); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/FixCrLfTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/FixCrLfTest.java index 48f1fd482..a34b9f4b2 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/FixCrLfTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/FixCrLfTest.java @@ -31,10 +31,10 @@ import org.apache.tools.ant.BuildFileRule; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static org.apache.tools.ant.AntAssert.assertContains; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertTrue; /** */ @@ -43,6 +43,9 @@ public class FixCrLfTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/taskdefs/fixcrlf/build.xml"); @@ -150,12 +153,9 @@ public class FixCrLfTest { @Test public void testFixFileExclusive() { - try { - buildRule.executeTarget("testFixFileExclusive"); - fail(FixCRLF.ERROR_FILE_AND_SRCDIR); - } catch (BuildException ex) { - assertContains(FixCRLF.ERROR_FILE_AND_SRCDIR, ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage(FixCRLF.ERROR_FILE_AND_SRCDIR); + buildRule.executeTarget("testFixFileExclusive"); } /** @@ -231,11 +231,8 @@ public class FixCrLfTest { // not used, but public so theoretically must remain for BC? @Deprecated - public void assertEqualContent(File expect, File result) - throws AssertionFailedError, IOException { - if (!result.exists()) { - fail("Expected file " + result + " doesn\'t exist"); - } + public void assertEqualContent(File expect, File result) throws AssertionFailedError, IOException { + assertTrue("Expected file " + result + " doesn\'t exist", result.exists()); try (InputStream inExpect = new BufferedInputStream(new FileInputStream(expect)); InputStream inResult = new BufferedInputStream(new FileInputStream(result))) { diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/GetTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/GetTest.java index f079adbf1..a011adaee 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/GetTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/GetTest.java @@ -24,11 +24,13 @@ import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static org.apache.tools.ant.AntAssert.assertContains; -import static org.apache.tools.ant.AntAssert.assertNotContains; +import static org.hamcrest.Matchers.both; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThat; /** */ @@ -37,6 +39,9 @@ public class GetTest { @Rule public final BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/taskdefs/get.xml"); @@ -47,54 +52,49 @@ public class GetTest { buildRule.executeTarget("cleanup"); } - @Test + /** + * Fail due to missing required argument + */ + @Test(expected = BuildException.class) public void test1() { - try { - buildRule.executeTarget("test1"); - fail("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("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("required argument missing"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("test3"); + // TODO assert value } - @Test + /** + * Fail due to invalid src argument + */ + @Test(expected = BuildException.class) public void test4() { - try { - buildRule.executeTarget("test4"); - fail("src invalid"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("test4"); + // TODO assert value } - @Test + /** + * Fail due to invalid dest argument or no HTTP server on localhost + */ + @Test(expected = BuildException.class) public void test5() { - try { - buildRule.executeTarget("test5"); - fail("dest invalid (or no http-server on local machine"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("test5"); + // TODO assert value } @Test @@ -102,15 +102,17 @@ public class GetTest { buildRule.executeTarget("test6"); } + /** + * Fail due to null or empty userAgent argument + */ @Test public void test7() { + thrown.expect(BuildException.class); try { buildRule.executeTarget("test7"); - assertNotContains("Adding header", buildRule.getLog()); - - fail("userAgent may not be null or empty"); - } catch (BuildException ex) { - //TODO assert value + } finally { + // post-mortem + assertThat(buildRule.getLog(), not(containsString("Adding header"))); } } @@ -127,22 +129,21 @@ public class GetTest { @Test public void testTwoHeadersAreAddedOK() { buildRule.executeTarget("testTwoHeadersAreAddedOK"); - String log = buildRule.getLog(); - assertContains("Adding header 'header1'", log); - assertContains("Adding header 'header2'", log); + assertThat(buildRule.getLog(), both(containsString("Adding header 'header1'")) + .and(containsString("Adding header 'header2'"))); } @Test public void testEmptyHeadersAreNeverAdded() { buildRule.executeTarget("testEmptyHeadersAreNeverAdded"); - assertNotContains("Adding header", buildRule.getLog()); + assertThat(buildRule.getLog(), not(containsString("Adding header"))); } @Test public void testThatWhenMoreThanOneHeaderHaveSameNameOnlyLastOneIsAdded() { buildRule.executeTarget("testThatWhenMoreThanOneHeaderHaveSameNameOnlyLastOneIsAdded"); String log = buildRule.getLog(); - assertContains("Adding header 'header1'", log); + assertThat(log, containsString("Adding header 'header1'")); int actualHeaderCount = log.split("Adding header ").length - 1; @@ -152,7 +153,7 @@ public class GetTest { @Test public void testHeaderSpaceTrimmed() { buildRule.executeTarget("testHeaderSpaceTrimmed"); - assertContains("Adding header 'header1'", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Adding header 'header1'")); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ImportTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ImportTest.java index ec53f6f3b..29680f0ca 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/ImportTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ImportTest.java @@ -18,33 +18,36 @@ package org.apache.tools.ant.taskdefs; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.hasProperty; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThat; import static org.junit.Assume.assumeTrue; import java.io.File; -import java.io.IOException; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildFileRule; -import org.apache.tools.ant.Location; import org.apache.tools.ant.Project; import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class ImportTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Test public void testSimpleImport() { buildRule.configureProject("src/etc/testcases/taskdefs/import/import.xml"); - assertContains("Before importIn imported topAfter import", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Before importIn imported topAfter import")); } @Test @@ -58,9 +61,9 @@ public class ImportTest { @Test public void testSerial() { buildRule.configureProject("src/etc/testcases/taskdefs/import/subdir/serial.xml"); - assertContains("Unnamed2.xmlUnnamed1.xml", buildRule.getLog()); - assertContains("Expected string was not found in log", - "Skipped already imported file", buildRule.getFullLog()); + assertThat(buildRule.getLog(), containsString("Unnamed2.xmlUnnamed1.xml")); + assertThat("Expected string was not found in log", + buildRule.getFullLog(), containsString("Skipped already imported file")); } // allow this as imported in targets are only tested when a target is run @@ -83,15 +86,10 @@ public class ImportTest { @Test public void testImportInTargetNotAllowed() { - buildRule.configureProject( - "src/etc/testcases/taskdefs/import/subdir/importintarget.xml"); - try { - buildRule.executeTarget("do-import"); - fail("Build exception should have been thrown as import only allowed in top level task"); - } catch (BuildException ex) { - assertContains("not a top level task", "import only allowed as a top-level task", - ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("import only allowed as a top-level task"); + buildRule.configureProject("src/etc/testcases/taskdefs/import/subdir/importintarget.xml"); + buildRule.executeTarget("do-import"); } @Test @@ -105,31 +103,18 @@ public class ImportTest { @Test public void testImportSameTargets() { - try { - buildRule.configureProject( - "src/etc/testcases/taskdefs/import/same_target.xml"); - fail("Expected build exception"); - } catch (BuildException ex) { - assertContains("Message did not contain expected contents", "Duplicate target", - ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Duplicate target"); + buildRule.configureProject("src/etc/testcases/taskdefs/import/same_target.xml"); } @Test public void testImportError() { - try { - buildRule.configureProject( - "src/etc/testcases/taskdefs/import/import_bad_import.xml"); - fail("Build exception should have been thrown"); - } catch (BuildException ex) { - Location lo = ex.getLocation(); - assertNotNull( - "expected location of build exception to be set", lo); - assertContains( - "expected location to contain calling file", "import_bad_import.xml", lo.getFileName()); - assertContains( - "expected message of ex to contain called file", "bad.xml", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("bad.xml"); + thrown.expect(hasProperty("location", + hasProperty("fileName", containsString("import_bad_import.xml")))); + buildRule.configureProject("src/etc/testcases/taskdefs/import/import_bad_import.xml"); } @Test @@ -140,21 +125,15 @@ public class ImportTest { } assumeTrue("Current system does not support Symlinks", new File(ln).exists()); String symlink = "src/etc/testcases/taskdefs/import/symlinks/d3b"; - File symlinkFile = new File(System.getProperty("root"), symlink); - if (Runtime.getRuntime().exec(new String[] {ln, "-s", "d3a", symlinkFile.getAbsolutePath()}).waitFor() != 0) { - throw new IOException("'" + ln + " -s d3a " + symlink + "' failed"); - } + File symlinkFile = new File(symlink); + assertEquals("'" + ln + " -s d3a " + symlink + "' failed", + Runtime.getRuntime().exec(new String[] {ln, "-s", "d3a", symlinkFile.getAbsolutePath()}).waitFor(), 0); try { - buildRule.configureProject( - "src/etc/testcases/taskdefs/import/symlinks/d1/p1.xml"); - assertEquals( - buildRule.getProject().getProperty("ant.file.p2"), - new File(System.getProperty("root"), "src/etc/testcases/taskdefs/import/symlinks/d2/p2.xml") - .getAbsolutePath()); - assertEquals( - buildRule.getProject().getProperty("ant.file.p3"), - new File(System.getProperty("root"), "src/etc/testcases/taskdefs/import/symlinks/d3b/p3.xml") - .getAbsolutePath()); + buildRule.configureProject("src/etc/testcases/taskdefs/import/symlinks/d1/p1.xml"); + assertEquals(buildRule.getProject().getProperty("ant.file.p2"), + new File("src/etc/testcases/taskdefs/import/symlinks/d2/p2.xml").getAbsolutePath()); + assertEquals(buildRule.getProject().getProperty("ant.file.p3"), + new File("src/etc/testcases/taskdefs/import/symlinks/d3b/p3.xml").getAbsolutePath()); } finally { symlinkFile.delete(); } @@ -163,7 +142,8 @@ public class ImportTest { @Test public void testTargetFirst() { buildRule.configureProject("src/etc/testcases/taskdefs/import/importtargetfirst.xml"); - assertContains("Importing targetfirstAfter target firstAfter importing", buildRule.getLog()); + assertThat(buildRule.getLog(), + containsString("Importing targetfirstAfter target firstAfter importing")); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java index 3ef9b3fad..53f63ddef 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java @@ -24,7 +24,6 @@ import java.io.FileReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; -import java.io.Reader; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -33,16 +32,17 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildFileRule; import org.apache.tools.ant.FileUtilities; import org.apache.tools.ant.util.FileUtils; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; 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.Assert.fail; import static org.junit.Assume.assumeTrue; /** @@ -52,11 +52,11 @@ public class JarTest { @Rule public final BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + private static String tempJar = "tmp.jar"; private static String tempDir = "jartmp/"; - private Reader r1; - private Reader r2; - @Before public void setUp() { @@ -64,28 +64,13 @@ public class JarTest { buildRule.executeTarget("setUp"); } - @After - public void tearDown() { - if (r1 != null) { - try { - r1.close(); - } catch (IOException e) { - } - } - if (r2 != null) { - try { - r2.close(); - } catch (IOException e) { - } - } - } - /** * Expected failure due to required argument not specified */ @Test(expected = BuildException.class) public void test1() { buildRule.executeTarget("test1"); + // TODO assert exception message } /** @@ -114,7 +99,6 @@ public class JarTest { public void test4() { buildRule.executeTarget("test4"); File jarFile = new File(getOutputDir(), tempJar); - assertTrue(jarFile.exists()); } @@ -154,14 +138,12 @@ public class JarTest { @Test public void testRecreateWithoutUpdateNewerFile() { - testRecreate("testRecreateNewerFileSetup", - "testRecreateWithoutUpdateNewerFile"); + testRecreate("testRecreateNewerFileSetup", "testRecreateWithoutUpdateNewerFile"); } @Test public void testRecreateWithUpdateNewerFile() { - testRecreate("testRecreateNewerFileSetup", - "testRecreateWithUpdateNewerFile"); + testRecreate("testRecreateNewerFileSetup", "testRecreateWithUpdateNewerFile"); } private void testRecreate(String firstTarget, String secondTarget) { @@ -177,22 +159,24 @@ public class JarTest { long jarModifiedDate = jarFile.lastModified(); buildRule.executeTarget(secondTarget); jarFile = new File(getOutputDir(), tempJar); - assertTrue("jar has been recreated in " + secondTarget, - jarModifiedDate < jarFile.lastModified()); + assertTrue("jar has been recreated in " + secondTarget, jarModifiedDate < jarFile.lastModified()); } @Test - public void testManifestStaysIntact() - throws IOException, ManifestException { + public void testManifestStaysIntact() throws IOException, ManifestException { buildRule.executeTarget("testManifestStaysIntact"); - r1 = new FileReader(new File(getOutputDir(), - tempDir + "manifest")); - r2 = new FileReader(new File(getOutputDir(), - tempDir + "META-INF/MANIFEST.MF")); + Manifest mf1; + try (FileReader r1 = new FileReader(new File(getOutputDir(), tempDir + "manifest"))) { + mf1 = new Manifest(r1); + } + + Manifest mf2; + try (FileReader r2 = new FileReader(new File(getOutputDir(), tempDir + + "META-INF/MANIFEST.MF"))) { + mf2 = new Manifest(r2); + } - Manifest mf1 = new Manifest(r1); - Manifest mf2 = new Manifest(r2); assertEquals(mf1, mf2); } @@ -218,26 +202,22 @@ public class JarTest { @Test public void testRecreateZipfilesetWithoutUpdateAdditionalFiles() { - testRecreate("test4", - "testRecreateZipfilesetWithoutUpdateAdditionalFiles"); + testRecreate("test4", "testRecreateZipfilesetWithoutUpdateAdditionalFiles"); } @Test public void testRecreateZipfilesetWithUpdateAdditionalFiles() { - testRecreate("test4", - "testRecreateZipfilesetWithUpdateAdditionalFiles"); + testRecreate("test4", "testRecreateZipfilesetWithUpdateAdditionalFiles"); } @Test public void testRecreateZipfilesetWithoutUpdateNewerFile() { - testRecreate("testRecreateNewerFileSetup", - "testRecreateZipfilesetWithoutUpdateNewerFile"); + testRecreate("testRecreateNewerFileSetup", "testRecreateZipfilesetWithoutUpdateNewerFile"); } @Test public void testRecreateZipfilesetWithUpdateNewerFile() { - testRecreate("testRecreateNewerFileSetup", - "testRecreateZipfilesetWithUpdateNewerFile"); + testRecreate("testRecreateNewerFileSetup", "testRecreateZipfilesetWithUpdateNewerFile"); } @Test @@ -306,9 +286,8 @@ public class JarTest { } @Test public void testManifestOnlyJar() { - buildRule.executeTarget("testManifestOnlyJar"); - assertContains("Building MANIFEST-only jar: ", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Building MANIFEST-only jar: ")); File manifestFile = new File(getOutputDir(), tempDir + "META-INF" + File.separator + "MANIFEST.MF"); assertTrue(manifestFile.exists()); } @@ -320,44 +299,41 @@ public class JarTest { @Test public void testNoVersionInfoFail() { - try { - buildRule.executeTarget("testNoVersionInfoFail"); - fail("BuildException expected: Manifest Implementation information missing."); - } catch (BuildException ex) { - assertContains("No Implementation-Title set.", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("No Implementation-Title set."); + buildRule.executeTarget("testNoVersionInfoFail"); } @Test public void testNoVersionInfoIgnore() { buildRule.executeTarget("testNoVersionInfoIgnore"); - assertTrue(buildRule.getFullLog().contains("No Implementation-Title set.")); - assertTrue(buildRule.getFullLog().contains("No Implementation-Version set.")); - assertTrue(buildRule.getFullLog().contains("No Implementation-Vendor set.")); + assertThat(buildRule.getFullLog(), containsString("No Implementation-Title set.")); + assertThat(buildRule.getFullLog(), containsString("No Implementation-Version set.")); + assertThat(buildRule.getFullLog(), containsString("No Implementation-Vendor set.")); } @Test public void testNoVersionInfoWarn() { buildRule.executeTarget("testNoVersionInfoWarn"); - assertTrue(buildRule.getLog().contains("No Implementation-Title set.")); - assertTrue(buildRule.getLog().contains("No Implementation-Version set.")); - assertTrue(buildRule.getLog().contains("No Implementation-Vendor set.")); + assertThat(buildRule.getLog(), containsString("No Implementation-Title set.")); + assertThat(buildRule.getLog(), containsString("No Implementation-Version set.")); + assertThat(buildRule.getLog(), containsString("No Implementation-Vendor set.")); } @Test public void testNoVersionInfoNoStrict() { buildRule.executeTarget("testNoVersionInfoNoStrict"); - assertFalse(buildRule.getLog().contains("No Implementation-Title set.")); - assertFalse(buildRule.getLog().contains("No Implementation-Version set.")); - assertFalse(buildRule.getLog().contains("No Implementation-Vendor set.")); + assertThat(buildRule.getLog(), not(containsString("No Implementation-Title set."))); + assertThat(buildRule.getLog(), not(containsString("No Implementation-Version set."))); + assertThat(buildRule.getLog(), not(containsString("No Implementation-Vendor set."))); } @Test public void testHasVersionInfo() { buildRule.executeTarget("testHasVersionInfo"); - assertFalse(buildRule.getLog().contains("No Implementation-Title set.")); - assertFalse(buildRule.getLog().contains("No Implementation-Version set.")); - assertFalse(buildRule.getLog().contains("No Implementation-Vendor set.")); + assertThat(buildRule.getLog(), not(containsString("No Implementation-Title set."))); + assertThat(buildRule.getLog(), not(containsString("No Implementation-Version set."))); + assertThat(buildRule.getLog(), not(containsString("No Implementation-Vendor set."))); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java index 80e9f435c..31b7ef9aa 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java @@ -38,13 +38,14 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.internal.AssumptionViolatedException; +import org.junit.rules.ExpectedException; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import static org.junit.Assume.assumeTrue; /** @@ -55,6 +56,9 @@ public class JavaTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + private static final int TIME_TO_WAIT = 1; // wait 1 second extra to allow for java to start ... // this time was OK on a Win NT machine and on nagoya @@ -87,62 +91,44 @@ public class JavaTest { @Test public void testNoJarNoClassname() { - try { - buildRule.executeTarget("testNoJarNoClassname"); - fail("Build exception should have been thrown - parameter validation"); - } catch (BuildException ex) { - assertContains("Classname must not be null.", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Classname must not be null."); + buildRule.executeTarget("testNoJarNoClassname"); } @Test public void testJarNoFork() { - try { - buildRule.executeTarget("testJarNoFork"); - fail("Build exception should have been thrown - parameter validation"); - } catch (BuildException ex) { - assertContains("Cannot execute a jar in non-forked mode. Please set fork='true'. ", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Cannot execute a jar in non-forked mode. Please set fork='true'. "); + buildRule.executeTarget("testJarNoFork"); } @Test public void testJarAndClassName() { - try { - buildRule.executeTarget("testJarAndClassName"); - fail("Build exception should have been thrown - both classname and JAR are not allowed"); - } catch (BuildException ex) { - assertEquals("Cannot use 'jar' and 'classname' attributes in same command", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Cannot use 'jar' and 'classname' attributes in same command"); + buildRule.executeTarget("testJarAndClassName"); } @Test public void testClassnameAndJar() { - try { - buildRule.executeTarget("testClassnameAndJar"); - fail("Build exception should have been thrown - both classname and JAR are not allowed"); - } catch (BuildException ex) { - assertEquals("Cannot use 'jar' with 'classname' or 'module' attributes in same command.", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Cannot use 'jar' with 'classname' or 'module' attributes in same command."); + buildRule.executeTarget("testClassnameAndJar"); } @Test public void testJarAndModule() { - try { - buildRule.executeTarget("testJarAndModule"); - fail("Build exception should have been thrown - both module and JAR are not allowed"); - } catch (BuildException ex) { - assertEquals("Cannot use 'jar' and 'module' attributes in same command", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Cannot use 'jar' and 'module' attributes in same command"); + buildRule.executeTarget("testJarAndModule"); } @Test public void testModuleAndJar() { - try { - buildRule.executeTarget("testModuleAndJar"); - fail("Build exception should have been thrown - both module and JAR are not allowed"); - } catch (BuildException ex) { - assertEquals("Cannot use 'jar' with 'classname' or 'module' attributes in same command.", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Cannot use 'jar' with 'classname' or 'module' attributes in same command."); + buildRule.executeTarget("testModuleAndJar"); } @Test @@ -214,8 +200,6 @@ public class JavaTest { buildRule.executeTarget("testRun"); } - - /** this test fails but we ignore the return value; * we verify that failure only matters when failonerror is set */ @@ -228,54 +212,42 @@ public class JavaTest { @Test public void testRunFailFoe() { assumeTrue("Fatal tests have not been set to run", runFatalTests); - try { - buildRule.executeTarget("testRunFailFoe"); - fail("Build exception should have been thrown - " + "java failures being propagated"); - } catch (BuildException ex) { - assertContains("Java returned:", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Java returned:"); + buildRule.executeTarget("testRunFailFoe"); } @Test public void testRunFailFoeFork() { - try { - buildRule.executeTarget("testRunFailFoeFork"); - fail("Build exception should have been thrown - " + "java failures being propagated"); - } catch (BuildException ex) { - assertContains("Java returned:", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Java returned:"); + buildRule.executeTarget("testRunFailFoeFork"); } @Test public void testExcepting() { buildRule.executeTarget("testExcepting"); - assertContains("Exception raised inside called program", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Exception raised inside called program")); } @Test public void testExceptingFork() { buildRule.executeTarget("testExceptingFork"); - assertContains("Java Result:", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Java Result:")); } @Test public void testExceptingFoe() { - try { - buildRule.executeTarget("testExceptingFoe"); - fail("Build exception should have been thrown - " + "passes exception through"); - } catch (BuildException ex) { - assertContains("Exception raised inside called program", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Exception raised inside called program"); + buildRule.executeTarget("testExceptingFoe"); } @Test public void testExceptingFoeFork() { - try { - buildRule.executeTarget("testExceptingFoeFork"); - fail("Build exception should have been thrown - " + "exceptions turned into error codes"); - } catch (BuildException ex) { - assertContains("Java returned:", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Java returned:"); + buildRule.executeTarget("testExceptingFoeFork"); } @Test @@ -304,12 +276,9 @@ public class JavaTest { @Test public void testRunFailWithFailOnError() { - try { - buildRule.executeTarget("testRunFailWithFailOnError"); - fail("Build exception should have been thrown - " + "non zero return code"); - } catch (BuildException ex) { - assertContains("Java returned:", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Java returned:"); + buildRule.executeTarget("testRunFailWithFailOnError"); } @Test @@ -329,7 +298,6 @@ public class JavaTest { Thread.sleep(TIME_TO_WAIT * 1000 + SECURITY_MARGIN); - // let's be nice with the next generation of developers if (!logFile.exists()) { System.out.println("suggestion: increase the constant" diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/JavacTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/JavacTest.java index ce302dcca..cead067b4 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/JavacTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/JavacTest.java @@ -28,12 +28,14 @@ import org.apache.tools.ant.taskdefs.compilers.JavacExternal; import org.junit.Before; import org.junit.Test; -import static org.apache.tools.ant.AntAssert.assertContains; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.Path; + +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; /** @@ -65,13 +67,12 @@ public class JavacTest { javac.setFork(true); assertNotNull("normal fork", javac.getJavacExecutable()); - assertContains("name should contain \"javac\"", "javac", - javac.getJavacExecutable()); + assertThat("name should contain \"javac\"", javac.getJavacExecutable(), containsString("javac")); project.setProperty("build.compiler", "extJavac"); javac.setFork(false); assertNotNull("fork via property", javac.getJavacExecutable()); - assertContains("name should contain \"javac\"", "javac", javac.getJavacExecutable()); + assertThat("name should contain \"javac\"", javac.getJavacExecutable(), containsString("javac")); project.setProperty("build.compiler", "whatever"); assertNull("no fork and not extJavac means no executable", diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/LoadFileTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/LoadFileTest.java index 136b45bdc..19571f1ef 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/LoadFileTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/LoadFileTest.java @@ -23,12 +23,13 @@ import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThat; /** * Test the load file task @@ -38,6 +39,9 @@ public class LoadFileTest { @Rule public final BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/taskdefs/loadfile.xml"); @@ -49,29 +53,21 @@ public class LoadFileTest { } /** - * A unit test for JUnit + * Fail due to source file not defined */ - @Test + @Test(expected = BuildException.class) public void testNoSourcefileDefined() { - try { - buildRule.executeTarget("testNoSourcefileDefined"); - fail("BuildException expected: source file not defined"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("testNoSourcefileDefined"); + // TODO assert value } /** - * A unit test for JUnit + * Fail due to output property not defined */ - @Test + @Test(expected = BuildException.class) public void testNoPropertyDefined() { - try { - buildRule.executeTarget("testNoPropertyDefined"); - fail("BuildException expected: output property not defined"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("testNoPropertyDefined"); + // TODO assert value } /** @@ -79,12 +75,9 @@ public class LoadFileTest { */ @Test public void testNoSourcefilefound() { - try { - buildRule.executeTarget("testNoSourcefilefound"); - fail("BuildException expected: File not found"); - } catch (BuildException ex) { - assertContains(" doesn't exist", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage(" doesn't exist"); + buildRule.executeTarget("testNoSourcefilefound"); } /** @@ -102,9 +95,8 @@ public class LoadFileTest { @Test public void testLoadAFile() throws BuildException { buildRule.executeTarget("testLoadAFile"); - if (!buildRule.getProject().getProperty("testLoadAFile").contains("eh?")) { - fail("property is not all in the file"); - } + assertThat("property is not all in the file", + buildRule.getProject().getProperty("testLoadAFile"), containsString("eh?")); } /** @@ -122,9 +114,8 @@ public class LoadFileTest { @Test public void testEvalProps() throws BuildException { buildRule.executeTarget("testEvalProps"); - if (!buildRule.getProject().getProperty("testEvalProps").contains("rain")) { - fail("property eval broken"); - } + assertThat("property eval broken", + buildRule.getProject().getProperty("testEvalProps"), containsString("rain")); } /** @@ -133,9 +124,8 @@ public class LoadFileTest { @Test public void testFilterChain() throws BuildException { buildRule.executeTarget("testFilterChain"); - if (!buildRule.getProject().getProperty("testFilterChain").contains("World!")) { - fail("Filter Chain broken"); - } + assertThat("Filter Chain broken", + buildRule.getProject().getProperty("testFilterChain"), containsString("World!")); } /** diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/MacroDefTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/MacroDefTest.java index 59b6bd74f..ccf34516a 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/MacroDefTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/MacroDefTest.java @@ -23,10 +23,12 @@ import org.apache.tools.ant.BuildFileRule; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThat; /** */ @@ -35,6 +37,9 @@ public class MacroDefTest { @Rule public final BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/taskdefs/macrodef.xml"); @@ -52,24 +57,16 @@ public class MacroDefTest { assertEquals("Inner Text", buildRule.getLog()); } - @Test + @Test(expected = BuildException.class) public void testDuplicateAttribute() { - try { - buildRule.executeTarget("duplicate.attribute"); - fail("BuildException expected: the attribute text has already been specified"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("duplicate.attribute"); + //TODO assert value } - @Test + @Test(expected = BuildException.class) public void testDuplicateElement() { - try { - buildRule.executeTarget("duplicate.element"); - fail("BuildException expected: the element text has already been specified"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("duplicate.element"); + //TODO assert value } @Test @@ -105,32 +102,31 @@ public class MacroDefTest { @Test public void testTextElement() { buildRule.executeTarget("textelement"); - assertContains("Hello world", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Hello world")); } @Test public void testTextTrim() { buildRule.executeTarget("text.trim"); - assertContains("[Hello world]", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("[Hello world]")); } - @Test + /** + * Fail due to the name "text" already used as an attribute + */ + @Test(expected = BuildException.class) public void testDuplicateTextName() { - try { - buildRule.executeTarget("duplicatetextname"); - fail("BuildException expected: the name \"text\" is already used as an attribute"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("duplicatetextname"); + // TODO assert value } - @Test + + /** + * Fail due to the attribute name "text" already used by a text element + */ + @Test(expected = BuildException.class) public void testDuplicateTextName2() { - try { - buildRule.executeTarget("duplicatetextname2"); - fail("BuildException expected: the attribute name \"text\" has already been used by the text element"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("duplicatetextname2"); + // TODO assert value } @Test @@ -159,12 +155,9 @@ public class MacroDefTest { @Test public void testImplicitNotOptional() { - try { - buildRule.executeTarget("implicit.notoptional"); - fail("BuildException expected: Missing nested elements for implicit element implicit"); - } catch (BuildException ex) { - assertEquals("Missing nested elements for implicit element implicit", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Missing nested elements for implicit element implicit"); + buildRule.executeTarget("implicit.notoptional"); } @Test @@ -175,38 +168,28 @@ public class MacroDefTest { @Test public void testImplicitExplicit() { - try { - buildRule.executeTarget("implicit.explicit"); - fail("BuildException expected: Only one element allowed when using implicit elements"); - } catch (BuildException ex) { - assertEquals("Only one element allowed when using implicit elements", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Only one element allowed when using implicit elements"); + buildRule.executeTarget("implicit.explicit"); } @Test public void testBackTraceOff() { - try { - buildRule.executeTarget("backtraceoff"); - } catch (BuildException ex) { - if (ex.getMessage().contains("following error occurred")) { - fail("error message contained backtrace - " + ex.getMessage()); - } - } + thrown.expect(BuildException.class); + thrown.expectMessage(not(containsString("following error occurred"))); + buildRule.executeTarget("backtraceoff"); } @Test public void testBackTrace() { - try { - buildRule.executeTarget("backtraceon"); - fail("BuildException expected: Checking if a back trace is created"); - } catch (BuildException ex) { - assertContains("following error occurred", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("following error occurred"); + buildRule.executeTarget("backtraceon"); } @Test public void testTopLevelText() { buildRule.executeTarget("top-level-text"); - assertContains("Hello World", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Hello World")); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/MakeUrlTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/MakeUrlTest.java index b60604b90..62f7e9ed1 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/MakeUrlTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/MakeUrlTest.java @@ -22,15 +22,16 @@ import org.apache.tools.ant.BuildFileRule; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import java.io.InputStream; import java.io.IOException; import java.net.URL; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; public class MakeUrlTest { @@ -38,6 +39,9 @@ public class MakeUrlTest { @Rule public final BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/taskdefs/makeurl.xml"); @@ -45,42 +49,30 @@ public class MakeUrlTest { @Test public void testEmpty() { - try { - buildRule.executeTarget("testEmpty"); - fail("BuildException expected: missing property"); - } catch (BuildException ex) { - assertContains("property", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("property"); + buildRule.executeTarget("testEmpty"); } @Test public void testNoProperty() { - try { - buildRule.executeTarget("testNoProperty"); - fail("BuildException expected: missing property"); - } catch (BuildException ex) { - assertContains("property", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("property"); + buildRule.executeTarget("testNoProperty"); } @Test public void testNoFile() { - try { - buildRule.executeTarget("testNoFile"); - fail("BuildException expected: missing file"); - } catch (BuildException ex) { - assertContains("file", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("file"); + buildRule.executeTarget("testNoFile"); } @Test public void testValidation() { - try { - buildRule.executeTarget("testValidation"); - fail("BuildException expected: " + MakeUrl.ERROR_MISSING_FILE); - } catch (BuildException ex) { - assertContains("file", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("file"); + buildRule.executeTarget("testValidation"); } @Test @@ -160,8 +152,8 @@ public class MakeUrlTest { protected void assertPropertyContains(String property, String contains) { String result = getProperty(property); - assertTrue("expected " + contains + " in " + result, - result != null && result.contains(contains)); + assertNotNull("expected non-null property value", result); + assertThat("expected " + contains + " in " + result, result, containsString(contains)); } /** diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestClassPathTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestClassPathTest.java index 20ddadd0f..b1a34d194 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestClassPathTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestClassPathTest.java @@ -17,10 +17,10 @@ */ package org.apache.tools.ant.taskdefs; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThat; import static org.junit.Assume.assumeFalse; import static org.junit.Assume.assumeNoException; import static org.junit.Assume.assumeTrue; @@ -35,6 +35,7 @@ import org.apache.tools.ant.util.regexp.RegexpMatcherFactory; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; /** * Tests <bm:manifestclasspath>. @@ -44,6 +45,9 @@ public class ManifestClassPathTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/taskdefs/manifestclasspath.xml"); @@ -51,57 +55,62 @@ public class ManifestClassPathTest { @Test public void testBadDirectory() { + thrown.expect(BuildException.class); + thrown.expectMessage("Jar's directory not found:"); try { buildRule.executeTarget("test-bad-directory"); - fail("Build exception should have been thrown on bad directory"); - } catch (BuildException ex) { - assertContains("Jar's directory not found:", ex.getMessage()); + } finally { + // post-mortem + assertNull(buildRule.getProject().getProperty("jar.classpath")); } - assertNull(buildRule.getProject().getProperty("jar.classpath")); } @Test public void testBadNoProperty() { + thrown.expect(BuildException.class); + thrown.expectMessage("Missing 'property' attribute!"); try { buildRule.executeTarget("test-bad-no-property"); - fail("Build exception should have been thrown on no property"); - } catch (BuildException ex) { - assertContains("Missing 'property' attribute!", ex.getMessage()); + } finally { + // post-mortem + assertNull(buildRule.getProject().getProperty("jar.classpath")); } - assertNull(buildRule.getProject().getProperty("jar.classpath")); } @Test public void testBadPropertyExists() { + thrown.expect(BuildException.class); + thrown.expectMessage("Property 'jar.classpath' already set!"); try { buildRule.executeTarget("test-bad-property-exists"); - fail("Build exception should have been thrown on bad property"); - } catch (BuildException ex) { - assertContains("Property 'jar.classpath' already set!", ex.getMessage()); + } finally { + // post-mortem + assertEquals("exists", buildRule.getProject().getProperty("jar.classpath")); } - assertEquals(buildRule.getProject().getProperty("jar.classpath"), "exists"); } @Test public void testBadNoJarfile() { + thrown.expect(BuildException.class); + thrown.expectMessage("Missing 'jarfile' attribute!"); try { buildRule.executeTarget("test-bad-no-jarfile"); - fail("Build exception should have been thrown on bad jar file"); - } catch (BuildException ex) { - assertContains("Missing 'jarfile' attribute!", ex.getMessage()); + } finally { + // post-mortem + assertNull(buildRule.getProject().getProperty("jar.classpath")); } - assertNull(buildRule.getProject().getProperty("jar.classpath")); } @Test public void testBadNoClassPath() { + thrown.expect(BuildException.class); + thrown.expectMessage("Missing nested !"); try { buildRule.executeTarget("test-bad-no-classpath"); - fail("Build exception should have been thrown on no classpath"); - } catch (BuildException ex) { - assertContains("Missing nested !", ex.getMessage()); + } finally { + // post-mortem + assertNull(buildRule.getProject().getProperty("jar.classpath")); } - assertNull(buildRule.getProject().getProperty("jar.classpath")); } @Test @@ -136,18 +145,20 @@ public class ManifestClassPathTest { @Test public void testParentLevel2TooDeep() { + thrown.expect(BuildException.class); + thrown.expectMessage("No suitable relative path from "); try { buildRule.executeTarget("test-parent-level2-too-deep"); - fail("Build exception should have been thrown on no suitable path"); - } catch (BuildException ex) { - assertContains("No suitable relative path from ", ex.getMessage()); + } finally { + // post-mortem + assertNull(buildRule.getProject().getProperty("jar.classpath")); } - assertNull(buildRule.getProject().getProperty("jar.classpath")); } @Test public void testPseudoTahoeRefid() { - assumeTrue("No regexp matcher is present", RegexpMatcherFactory.regexpMatcherPresent(buildRule.getProject())); + assumeTrue("No regexp matcher is present", + RegexpMatcherFactory.regexpMatcherPresent(buildRule.getProject())); buildRule.executeTarget("test-pseudo-tahoe-refid"); assertEquals(buildRule.getProject().getProperty("jar.classpath"), "classes/dsp-core/ " @@ -160,7 +171,8 @@ public class ManifestClassPathTest { @Test public void testPseudoTahoeNested() { - assumeTrue("No regexp matcher is present", RegexpMatcherFactory.regexpMatcherPresent(buildRule.getProject())); + assumeTrue("No regexp matcher is present", + RegexpMatcherFactory.regexpMatcherPresent(buildRule.getProject())); buildRule.executeTarget("test-pseudo-tahoe-nested"); assertEquals(buildRule.getProject().getProperty("jar.classpath"), "classes/dsp-core/ " @@ -192,7 +204,7 @@ public class ManifestClassPathTest { public void testInternationalGerman() { buildRule.executeTarget("international-german"); buildRule.executeTarget("run-two-jars"); - assertContains("beta alpha", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("beta alpha")); } @Test @@ -200,7 +212,7 @@ public class ManifestClassPathTest { assumeFalse("Test with hebrew path not attempted under Windows", Os.isFamily("windows")); buildRule.executeTarget("international-hebrew"); buildRule.executeTarget("run-two-jars"); - assertContains("beta alpha", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("beta alpha")); } @Test @@ -237,13 +249,13 @@ public class ManifestClassPathTest { } buildRule.getProject().setProperty("altDriveLetter", altDriveLetter); + thrown.expect(BuildException.class); + thrown.expectMessage("No suitable relative path from "); try { buildRule.executeTarget("testDifferentDrive"); - fail("Build exception should have been thrown on no alternative drive"); - } catch (BuildException ex) { - assertContains("No suitable relative path from ", ex.getMessage()); + } finally { + // post-mortem + assertNull(buildRule.getProject().getProperty("cp")); } - - assertNull(buildRule.getProject().getProperty("cp")); } } 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 f8bd3299b..283181e79 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java @@ -33,14 +33,15 @@ import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; /** * Testcase for the Manifest class used in the jar task. @@ -51,6 +52,9 @@ public class ManifestTest { @Rule public final BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + private File expandedManifest; private File outDir; @@ -109,12 +113,9 @@ public class ManifestTest { */ @Test public void test3() { - try { - buildRule.executeTarget("test3"); - fail("BuildException expected: Manifest is invalid - no colon on header line"); - } catch (BuildException ex) { - assertContains("Invalid Manifest", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Invalid Manifest"); + buildRule.executeTarget("test3"); } /** @@ -122,12 +123,9 @@ public class ManifestTest { */ @Test public void test4() { - try { - buildRule.executeTarget("test4"); - fail("BuildException expected: Manifest is invalid - section starts with continuation line"); - } catch (BuildException ex) { - assertContains("Invalid Manifest", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Invalid Manifest"); + buildRule.executeTarget("test4"); } /** @@ -136,8 +134,8 @@ public class ManifestTest { @Test public void test5() { buildRule.executeTarget("test5"); - assertTrue("Expected warning about Name in main section", buildRule.getLog() - .contains("Manifest warning: \"Name\" attributes should not occur in the main section")); + assertThat("Expected warning about Name in main section", buildRule.getLog(), + containsString("Manifest warning: \"Name\" attributes should not occur in the main section")); } /** @@ -145,25 +143,25 @@ public class ManifestTest { */ @Test public void test6() { + thrown.expect(BuildException.class); + thrown.expectMessage("Invalid Manifest"); try { buildRule.executeTarget("test6"); - fail("BuildException expected: Manifest is invalid - section starts with incorrect attribute"); - } catch (BuildException ex) { - assertContains("Invalid Manifest", ex.getMessage()); + } finally { + assertThat("Expected warning about section not starting with Name: attribute", buildRule.getLog(), + containsString("Manifest sections should start with a \"Name\" attribute")); + } - assertTrue("Expected warning about section not starting with Name: attribute", buildRule.getLog() - .contains("Manifest sections should start with a \"Name\" attribute")); } - /** * From attribute is illegal */ @Test public void test7() { buildRule.executeTarget("test7"); - assertTrue("Expected warning about From: attribute", buildRule.getLog() - .contains(Manifest.ERROR_FROM_FORBIDDEN)); + assertThat("Expected warning about From: attribute", buildRule.getLog(), + containsString(Manifest.ERROR_FROM_FORBIDDEN)); } /** @@ -187,13 +185,9 @@ public class ManifestTest { */ @Test public void test9() { - try { - buildRule.executeTarget("test9"); - fail("BuildException expected: Construction is invalid - Name attribute should not be used"); - } catch (BuildException ex) { - assertContains("Specify the section name using the \"name\" attribute of the
element", - ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Specify the section name using the \"name\" attribute of the
element"); + buildRule.executeTarget("test9"); } /** @@ -201,12 +195,9 @@ public class ManifestTest { */ @Test public void test10() { - try { - buildRule.executeTarget("test10"); - fail("BuildException expected: Attribute has no name"); - } catch (BuildException ex) { - assertContains("Attributes must have name and value", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Attributes must have name and value"); + buildRule.executeTarget("test10"); } /** @@ -214,12 +205,9 @@ public class ManifestTest { */ @Test public void test11() { - try { - buildRule.executeTarget("test11"); - fail("BuildException expected: Attribute has no value"); - } catch (BuildException ex) { - assertContains("Attributes must have name and value", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Attributes must have name and value"); + buildRule.executeTarget("test11"); } /** @@ -227,12 +215,9 @@ public class ManifestTest { */ @Test public void test12() { - try { - buildRule.executeTarget("test12"); - fail("BuildException expected: Section with no name"); - } catch (BuildException ex) { - assertContains("Sections must have a name", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Sections must have a name"); + buildRule.executeTarget("test12"); } /** @@ -240,13 +225,9 @@ public class ManifestTest { */ @Test public void test13() { - try { - buildRule.executeTarget("test13"); - fail("BuildException expected: Duplicate Attribute"); - } catch (BuildException ex) { - assertContains("The attribute \"Test\" may not occur more than once in the same section", - ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("The attribute \"Test\" may not occur more than once in the same section"); + buildRule.executeTarget("test13"); } /** @@ -361,14 +342,10 @@ public class ManifestTest { /** * file attribute for manifest task is required. */ - @Test + @Test(expected = BuildException.class) public void testNoFile() { - try { - buildRule.executeTarget("testNoFile"); - fail("BuildException expected: file is required"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("testNoFile"); + //TODO assert value } /** @@ -394,71 +371,51 @@ public class ManifestTest { String mfAsString = mf.toString(); assertNotNull(mfAsString); assertTrue(mfAsString.startsWith("Manifest-Version: 2.0")); - assertTrue(mfAsString.contains("Foo: Bar")); + assertThat(mfAsString, containsString("Foo: Bar")); mf = getManifest(new File(outDir, "mftest2.mf")); assertNotNull(mf); mfAsString = mf.toString(); assertNotNull(mfAsString); - assertTrue(mfAsString.contains("Foo: Baz")); - assertFalse(mfAsString.contains("Foo: Bar")); + assertThat(mfAsString, containsString("Foo: Baz")); + assertThat(mfAsString, not(containsString("Foo: Bar"))); } @Test public void testFrom() { buildRule.executeTarget("testFrom"); - assertContains(Manifest.ERROR_FROM_FORBIDDEN, buildRule.getLog()); + assertThat(buildRule.getLog(), containsString(Manifest.ERROR_FROM_FORBIDDEN)); } - @Test + @Test(expected = BuildException.class) public void testIllegalName() { - try { - buildRule.executeTarget("testIllegalName"); - fail("BuildException expected: Manifest attribute names must not contain ' '"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("testIllegalName"); + // TODO assert value } - @Test + @Test(expected = BuildException.class) public void testIllegalNameInSection() { - try { - buildRule.executeTarget("testIllegalNameInSection"); - fail("BuildException expected: Manifest attribute names must not contain ' '"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("testIllegalNameInSection"); + // TODO assert value } - @Test + @Test(expected = BuildException.class) public void testIllegalNameBegin() { - try { - buildRule.executeTarget("testIllegalNameInSection"); - fail("BuildException expected: Manifest attribute names must not start with '-' at the begin."); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("testIllegalNameInSection"); + // TODO assert value } - @Test + @Test(expected = BuildException.class) public void testIllegalName2() { - try { - buildRule.executeTarget("testIllegalName"); - fail("BuildException expected: Manifest attribute names must not contain '.'"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("testIllegalName"); + // TODO assert value } - @Test + @Test(expected = BuildException.class) public void testIllegalName3() { - try { - buildRule.executeTarget("testIllegalName"); - fail("BuildException expected: Manifest attribute names must not contain '*'"); - } catch (BuildException ex) { - //TODO assert value - } - } + buildRule.executeTarget("testIllegalName"); + // TODO assert value + } /** * Reads mftest.mf. diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/NiceTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/NiceTest.java index 635c2d5cf..e6da5a2a2 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/NiceTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/NiceTest.java @@ -23,9 +23,7 @@ import org.apache.tools.ant.BuildFileRule; import org.junit.Before; import org.junit.Rule; import org.junit.Test; - -import static org.apache.tools.ant.AntAssert.assertContains; -import static org.junit.Assert.fail; +import org.junit.rules.ExpectedException; /** * test nice @@ -35,6 +33,9 @@ public class NiceTest { @Rule public final BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/taskdefs/nice.xml"); @@ -62,22 +63,16 @@ public class NiceTest { @Test public void testTooSlow() { - try { - buildRule.executeTarget("too_slow"); - fail("BuildException expected: out of range"); - } catch (BuildException ex) { - assertContains("out of the range 1-10", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("out of the range 1-10"); + buildRule.executeTarget("too_slow"); } @Test public void testTooFast() { - try { - buildRule.executeTarget("too_fast"); - fail("BuildException expected: out of range"); - } catch (BuildException ex) { - assertContains("out of the range 1-10", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("out of the range 1-10"); + buildRule.executeTarget("too_fast"); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/PreSetDefTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/PreSetDefTest.java index 65b66a41a..f1dc4b50e 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/PreSetDefTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/PreSetDefTest.java @@ -25,10 +25,9 @@ import org.apache.tools.ant.types.FileSet; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static org.apache.tools.ant.AntAssert.assertContains; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; /** */ @@ -37,6 +36,9 @@ public class PreSetDefTest { @Rule public final BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/taskdefs/presetdef.xml"); @@ -98,22 +100,16 @@ public class PreSetDefTest { @Test public void testCorrectTaskNameBadAttr() { - try { - buildRule.executeTarget("correct_taskname_badattr"); - fail("BuildException expected: attribute message"); - } catch (BuildException ex) { - assertContains("javac doesn't support the", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("javac doesn't support the"); + buildRule.executeTarget("correct_taskname_badattr"); } @Test public void testCorrectTaskNameBadEl() { - try { - buildRule.executeTarget("correct_taskname_badel"); - fail("BuildException expected: element message"); - } catch (BuildException ex) { - assertContains("javac doesn't support the", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("javac doesn't support the"); + buildRule.executeTarget("correct_taskname_badel"); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java index 7dad1ab6a..080b63f62 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java @@ -18,10 +18,9 @@ package org.apache.tools.ant.taskdefs; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThat; import static org.junit.Assume.assumeNoException; import org.apache.tools.ant.BuildException; @@ -30,6 +29,7 @@ import org.apache.tools.ant.util.FileUtils; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; /** */ @@ -38,6 +38,9 @@ public class PropertyTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + /** Utilities used for file operations */ private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); @@ -58,25 +61,23 @@ public class PropertyTest { @Test public void test2() { buildRule.executeTarget("test2"); - assertContains("testprop1=aa, testprop3=xxyy, testprop4=aazz", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("testprop1=aa, testprop3=xxyy, testprop4=aazz")); } + /** + * Fail due to circular definition + */ @Test public void test3() { - try { - buildRule.executeTarget("test3"); - fail("Did not throw exception on circular exception"); - } catch (BuildException e) { - assertTrue("Circular definition not detected - ", - e.getMessage().contains("was circularly defined")); - } - + thrown.expect(BuildException.class); + thrown.expectMessage("was circularly defined"); + buildRule.executeTarget("test3"); } @Test public void test4() { buildRule.executeTarget("test4"); - assertContains("http.url is http://localhost:999", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("http.url is http://localhost:999")); } @Test @@ -86,7 +87,7 @@ public class PropertyTest { buildRule.getProject().setNewProperty("test5.url", uri); buildRule.executeTarget("test5"); - assertContains("http.url is http://localhost:999", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("http.url is http://localhost:999")); } @Test @@ -95,32 +96,30 @@ public class PropertyTest { assertEquals("80", buildRule.getProject().getProperty("server1.http.port")); } + /** + * Fail due to prefix allowed only non-resource/file load + */ @Test public void testPrefixFailure() { - try { - buildRule.executeTarget("prefix.fail"); - fail("Did not throw exception on invalid use of prefix"); - } catch (BuildException e) { - assertContains("Prefix allowed on non-resource/file load - ", - "Prefix is only valid", e.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Prefix is only valid"); + buildRule.executeTarget("prefix.fail"); } + /** + * Fail due to circular definition + */ @Test public void testCircularReference() { - try { - buildRule.executeTarget("testCircularReference"); - fail("Did not throw exception on circular exception"); - } catch (BuildException e) { - assertContains("Circular definition not detected - ", - "was circularly defined", e.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("was circularly defined"); + buildRule.executeTarget("testCircularReference"); } @Test public void testThisIsNotACircularReference() { buildRule.executeTarget("thisIsNotACircularReference"); - assertContains("b is A/A/A", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("b is A/A/A")); } @Test @@ -133,7 +132,6 @@ public class PropertyTest { buildRule.executeTarget("testXmlProperty"); assertEquals("ONE", buildRule.getProject().getProperty("xml.one")); assertEquals("TWO", buildRule.getProject().getProperty("xml.two")); - } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/RmicAdvancedTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/RmicAdvancedTest.java index 490ce9f41..5de5f2d41 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/RmicAdvancedTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/RmicAdvancedTest.java @@ -30,7 +30,8 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; import static org.junit.Assume.assumeFalse; import static org.junit.Assume.assumeTrue; @@ -46,7 +47,7 @@ public class RmicAdvancedTest { public BuildFileRule buildRule = new BuildFileRule(); @Rule - public ExpectedException tried = ExpectedException.none(); + public ExpectedException thrown = ExpectedException.none(); /** * The JUnit setup method @@ -202,8 +203,8 @@ public class RmicAdvancedTest { */ @Test public void testBadName() { - tried.expect(BuildException.class); - tried.expectMessage(RmicAdapterFactory.ERROR_UNKNOWN_COMPILER); + thrown.expect(BuildException.class); + thrown.expectMessage(RmicAdapterFactory.ERROR_UNKNOWN_COMPILER); buildRule.executeTarget("testBadName"); } @@ -220,8 +221,8 @@ public class RmicAdvancedTest { */ @Test public void testWrongClass() { - tried.expect(BuildException.class); - tried.expectMessage(RmicAdapterFactory.ERROR_NOT_RMIC_ADAPTER); + thrown.expect(BuildException.class); + thrown.expectMessage(RmicAdapterFactory.ERROR_NOT_RMIC_ADAPTER); buildRule.executeTarget("testWrongClass"); } @@ -230,13 +231,13 @@ public class RmicAdvancedTest { */ @Test public void testDefaultBadClass() { - tried.expect(BuildException.class); - tried.expectMessage(Rmic.ERROR_RMIC_FAILED); + thrown.expect(BuildException.class); + thrown.expectMessage(Rmic.ERROR_RMIC_FAILED); try { buildRule.executeTarget("testDefaultBadClass"); } finally { // don't look for much text here as it is vendor and version dependent - assertContains("unimplemented.class", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("unimplemented.class")); } } @@ -245,8 +246,8 @@ public class RmicAdvancedTest { */ @Test public void testMagicProperty() { - tried.expect(BuildException.class); - tried.expectMessage(RmicAdapterFactory.ERROR_UNKNOWN_COMPILER); + thrown.expect(BuildException.class); + thrown.expectMessage(RmicAdapterFactory.ERROR_UNKNOWN_COMPILER); buildRule.executeTarget("testMagicProperty"); } @@ -255,8 +256,8 @@ public class RmicAdvancedTest { */ @Test public void testMagicPropertyOverridesEmptyString() { - tried.expect(BuildException.class); - tried.expectMessage(RmicAdapterFactory.ERROR_UNKNOWN_COMPILER); + thrown.expect(BuildException.class); + thrown.expectMessage(RmicAdapterFactory.ERROR_UNKNOWN_COMPILER); buildRule.executeTarget("testMagicPropertyOverridesEmptyString"); } @@ -268,8 +269,8 @@ public class RmicAdvancedTest { @Test @Ignore("Previously named to prevent execution") public void NotestFailingAdapter() { - tried.expect(BuildException.class); - tried.expectMessage(Rmic.ERROR_RMIC_FAILED); + thrown.expect(BuildException.class); + thrown.expectMessage(Rmic.ERROR_RMIC_FAILED); buildRule.executeTarget("testFailingAdapter"); } @@ -336,8 +337,8 @@ public class RmicAdvancedTest { @Test public void testXnewForkedJava9plus() { assumeTrue("Current system is Java 8 or older", JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.JAVA_9)); - tried.expect(BuildException.class); - tried.expectMessage("JDK9 has removed support for -Xnew"); + thrown.expect(BuildException.class); + thrown.expectMessage("JDK9 has removed support for -Xnew"); buildRule.executeTarget("testXnewForked"); } @@ -356,8 +357,8 @@ public class RmicAdvancedTest { @Test public void testXnewForkedDestJava9plus() { assumeTrue("Current system is Java 8 or older", JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.JAVA_9)); - tried.expect(BuildException.class); - tried.expectMessage("JDK9 has removed support for -Xnew"); + thrown.expect(BuildException.class); + thrown.expectMessage("JDK9 has removed support for -Xnew"); buildRule.executeTarget("testXnewForkedDest"); } @@ -376,8 +377,8 @@ public class RmicAdvancedTest { @Test public void testXnewCompilerJava9plus() { assumeTrue("Current system is Java 8 or older", JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.JAVA_9)); - tried.expect(BuildException.class); - tried.expectMessage("JDK9 has removed support for -Xnew"); + thrown.expect(BuildException.class); + thrown.expectMessage("JDK9 has removed support for -Xnew"); buildRule.executeTarget("testXnewCompiler"); } @@ -396,8 +397,8 @@ public class RmicAdvancedTest { @Test public void testXnewCompilerDestJava9plus() { assumeTrue("Current system is Java 8 or older", JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.JAVA_9)); - tried.expect(BuildException.class); - tried.expectMessage("JDK9 has removed support for -Xnew"); + thrown.expect(BuildException.class); + thrown.expectMessage("JDK9 has removed support for -Xnew"); buildRule.executeTarget("testXnewCompilerDest"); } @@ -416,8 +417,8 @@ public class RmicAdvancedTest { @Test public void testIDLJava11plus() { assumeTrue("Current system is Java 10 or older", JavaEnvUtils.isAtLeastJavaVersion("11")); - tried.expect(BuildException.class); - tried.expectMessage("this rmic implementation doesn't support the -idl switch"); + thrown.expect(BuildException.class); + thrown.expectMessage("this rmic implementation doesn't support the -idl switch"); buildRule.executeTarget("testIDL"); } @@ -436,8 +437,8 @@ public class RmicAdvancedTest { @Test public void testIDLDestJava11plus() { assumeTrue("Current system is Java 10 or older", JavaEnvUtils.isAtLeastJavaVersion("11")); - tried.expect(BuildException.class); - tried.expectMessage("this rmic implementation doesn't support the -idl switch"); + thrown.expect(BuildException.class); + thrown.expectMessage("this rmic implementation doesn't support the -idl switch"); buildRule.executeTarget("testIDL"); } @@ -456,8 +457,8 @@ public class RmicAdvancedTest { @Test public void testIIOPJava11plus() { assumeTrue("Current system is Java 10 or older", JavaEnvUtils.isAtLeastJavaVersion("11")); - tried.expect(BuildException.class); - tried.expectMessage("this rmic implementation doesn't support the -iiop switch"); + thrown.expect(BuildException.class); + thrown.expectMessage("this rmic implementation doesn't support the -iiop switch"); buildRule.executeTarget("testIIOP"); } @@ -476,8 +477,8 @@ public class RmicAdvancedTest { @Test public void testIIOPDestJava11plus() { assumeTrue("Current system is Java 10 or older", JavaEnvUtils.isAtLeastJavaVersion("11")); - tried.expect(BuildException.class); - tried.expectMessage("this rmic implementation doesn't support the -iiop switch"); + thrown.expect(BuildException.class); + thrown.expectMessage("this rmic implementation doesn't support the -iiop switch"); buildRule.executeTarget("testIIOP"); } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java index 3bd5b0b44..b0dc876e1 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java @@ -30,14 +30,14 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.junit.Before; import org.junit.Ignore; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static org.apache.tools.ant.AntAssert.assertContains; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; /** * Simple testcase to test for driver caching. @@ -63,6 +63,9 @@ public class SQLExecTest { public static final String PATH = "path"; public static final String SQL = "sql"; + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { // make sure the cache is cleared. @@ -72,30 +75,29 @@ public class SQLExecTest { // simple test to ensure that the caching does work... @Test public void testDriverCaching() { + thrown.expect(BuildException.class); + thrown.expectMessage("No suitable Driver"); SQLExec sql = createTask(getProperties(NULL)); assertFalse(SQLExec.getLoaderMap().containsKey(NULL_DRIVER)); try { sql.execute(); - fail("BuildException should have been thrown"); - } catch (BuildException e) { - assertContains("No suitable Driver", e.getMessage()); - } - assertTrue(SQLExec.getLoaderMap().containsKey(NULL_DRIVER)); - assertSame(sql.getLoader(), JDBCTask.getLoaderMap().get(NULL_DRIVER)); - ClassLoader loader1 = sql.getLoader(); - - // 2nd run.. - sql = createTask(getProperties(NULL)); - // the driver must still be cached. - assertTrue(JDBCTask.getLoaderMap().containsKey(NULL_DRIVER)); - try { - sql.execute(); - } catch (BuildException e) { - assertTrue(e.getCause().getMessage().contains("No suitable Driver")); + } finally { + assertTrue(SQLExec.getLoaderMap().containsKey(NULL_DRIVER)); + assertSame(sql.getLoader(), JDBCTask.getLoaderMap().get(NULL_DRIVER)); + ClassLoader loader1 = sql.getLoader(); + + // 2nd run.. + sql = createTask(getProperties(NULL)); + // the driver must still be cached. + assertTrue(JDBCTask.getLoaderMap().containsKey(NULL_DRIVER)); + try { + sql.execute(); + } finally { + assertTrue(JDBCTask.getLoaderMap().containsKey(NULL_DRIVER)); + assertSame(sql.getLoader(), JDBCTask.getLoaderMap().get(NULL_DRIVER)); + assertSame(loader1, sql.getLoader()); + } } - assertTrue(JDBCTask.getLoaderMap().containsKey(NULL_DRIVER)); - assertSame(sql.getLoader(), JDBCTask.getLoaderMap().get(NULL_DRIVER)); - assertSame(loader1, sql.getLoader()); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/StyleTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/StyleTest.java index 1fcf57a43..cf3546e4d 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/StyleTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/StyleTest.java @@ -28,8 +28,10 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import static org.hamcrest.Matchers.containsString; 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.Assert.fail; @@ -228,11 +230,8 @@ public class StyleTest { private void assertFileContains(String filename, String contains) throws IOException { String content = getFileString(filename); - assertTrue( - "expecting file " + filename - + " to contain " + contains - + " but got " + content, - content.contains(contains)); + assertThat("expecting file " + filename + " to contain " + contains + " but got " + content, + content, containsString(contains)); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/SubAntTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/SubAntTest.java index 0a7321fad..fa2ee2c0f 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/SubAntTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/SubAntTest.java @@ -20,8 +20,6 @@ package org.apache.tools.ant.taskdefs; import java.io.File; -import junit.framework.AssertionFailedError; - import org.apache.tools.ant.BuildEvent; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildFileRule; @@ -29,17 +27,20 @@ import org.apache.tools.ant.BuildListener; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - +import static org.junit.Assert.assertThat; public class SubAntTest { @Rule public final BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/taskdefs/subant.xml"); @@ -84,33 +85,31 @@ public class SubAntTest { @Test public void testMultipleTargets() { buildRule.executeTarget("multipleTargets"); - assertContains("test1-one", buildRule.getLog()); - assertContains("test1-two", buildRule.getLog()); - assertContains("test2-one", buildRule.getLog()); - assertContains("test2-two", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("test1-one")); + assertThat(buildRule.getLog(), containsString("test1-two")); + assertThat(buildRule.getLog(), containsString("test2-one")); + assertThat(buildRule.getLog(), containsString("test2-two")); } @Test public void testMultipleTargetsOneDoesntExist_FOEfalse() { buildRule.executeTarget("multipleTargetsOneDoesntExist_FOEfalse"); - assertContains("Target \"three\" does not exist in the project \"subant\"", buildRule.getLog()); + assertThat(buildRule.getLog(), + containsString("Target \"three\" does not exist in the project \"subant\"")); } @Test public void testMultipleTargetsOneDoesntExist_FOEtrue() { - try { - buildRule.executeTarget("multipleTargetsOneDoesntExist_FOEtrue"); - fail("BuildException expected: Calling not existent target"); - } catch (BuildException ex) { - assertContains("Target \"three\" does not exist in the project \"subant\"", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Target \"three\" does not exist in the project \"subant\""); + buildRule.executeTarget("multipleTargetsOneDoesntExist_FOEtrue"); } protected void testBaseDirs(String target, String[] dirs) { SubAntTest.BasedirChecker bc = new SubAntTest.BasedirChecker(dirs); buildRule.getProject().addBuildListener(bc); buildRule.executeTarget(target); - AssertionFailedError ae = bc.getError(); + AssertionError ae = bc.getError(); if (ae != null) { throw ae; } @@ -120,7 +119,7 @@ public class SubAntTest { private class BasedirChecker implements BuildListener { private String[] expectedBasedirs; private int calls = 0; - private AssertionFailedError error; + private AssertionError error; BasedirChecker(String[] dirs) { expectedBasedirs = dirs; @@ -152,17 +151,15 @@ public class SubAntTest { try { assertEquals(expectedBasedirs[calls++], event.getProject().getBaseDir().getAbsolutePath()); - } catch (AssertionFailedError e) { + } catch (AssertionError e) { error = e; } } } - AssertionFailedError getError() { + AssertionError getError() { return error; } - } - } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/SyncTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/SyncTest.java index cb696f113..15d82a919 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/SyncTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/SyncTest.java @@ -23,8 +23,9 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import static org.apache.tools.ant.AntAssert.assertContains; -import static org.junit.Assert.assertFalse; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; public class SyncTest { @@ -42,7 +43,7 @@ public class SyncTest { buildRule.executeTarget("simplecopy"); String d = buildRule.getProject().getProperty("dest") + "/a/b/c/d"; assertFileIsPresent(d); - assertFalse(buildRule.getFullLog().contains("dangling")); + assertThat(buildRule.getFullLog(), not(containsString("dangling"))); } @Test @@ -52,7 +53,7 @@ public class SyncTest { assertFileIsNotPresent(d); String c = buildRule.getProject().getProperty("dest") + "/a/b/c"; assertFileIsNotPresent(c); - assertFalse(buildRule.getFullLog().contains("dangling")); + assertThat(buildRule.getFullLog(), not(containsString("dangling"))); } @Test @@ -62,7 +63,7 @@ public class SyncTest { assertFileIsNotPresent(d); String c = buildRule.getProject().getProperty("dest") + "/a/b/c"; assertFileIsPresent(c); - assertFalse(buildRule.getFullLog().contains("dangling")); + assertThat(buildRule.getFullLog(), not(containsString("dangling"))); } @Test @@ -86,9 +87,9 @@ public class SyncTest { assertFileIsPresent(d); String f = buildRule.getProject().getProperty("dest") + "/e/f"; assertFileIsNotPresent(f); - assertTrue(buildRule.getFullLog().contains("Removing orphan file:")); - assertContains("Removed 1 dangling file from", buildRule.getFullLog()); - assertContains("Removed 1 dangling directory from", buildRule.getFullLog()); + assertThat(buildRule.getFullLog(), containsString(("Removing orphan file:"))); + assertThat(buildRule.getFullLog(), containsString("Removed 1 dangling file from")); + assertThat(buildRule.getFullLog(), containsString("Removed 1 dangling directory from")); } @Test @@ -98,9 +99,9 @@ public class SyncTest { assertFileIsPresent(d); String f = buildRule.getProject().getProperty("dest") + "/e/f"; assertFileIsNotPresent(f); - assertTrue(buildRule.getFullLog().contains("Removing orphan file:")); - assertContains("Removed 1 dangling file from", buildRule.getFullLog()); - assertContains("Removed 1 dangling directory from", buildRule.getFullLog()); + assertThat(buildRule.getFullLog(), containsString(("Removing orphan file:"))); + assertThat(buildRule.getFullLog(), containsString("Removed 1 dangling file from")); + assertThat(buildRule.getFullLog(), containsString("Removed 1 dangling directory from")); } @Test @@ -112,9 +113,9 @@ public class SyncTest { assertFileIsPresent(c); String f = buildRule.getProject().getProperty("dest") + "/e/f"; assertFileIsNotPresent(f); - assertTrue(buildRule.getFullLog().contains("Removing orphan directory:")); - assertContains("NO dangling file to remove from", buildRule.getFullLog()); - assertContains("Removed 2 dangling directories from", buildRule.getFullLog()); + assertThat(buildRule.getFullLog(), containsString(("Removing orphan directory:"))); + assertThat(buildRule.getFullLog(), containsString("NO dangling file to remove from")); + assertThat(buildRule.getFullLog(), containsString("Removed 2 dangling directories from")); } @Test @@ -124,7 +125,7 @@ public class SyncTest { assertFileIsPresent(d); String f = buildRule.getProject().getProperty("dest") + "/e/f"; assertFileIsPresent(f); - assertFalse(buildRule.getFullLog().contains("Removing orphan file:")); + assertThat(buildRule.getFullLog(), not(containsString("Removing orphan file:"))); } @Test @@ -134,7 +135,7 @@ public class SyncTest { assertFileIsPresent(d); String f = buildRule.getProject().getProperty("dest") + "/e/f"; assertFileIsPresent(f); - assertFalse(buildRule.getFullLog().contains("Removing orphan file:")); + assertThat(buildRule.getFullLog(), not(containsString("Removing orphan file:"))); } public void assertFileIsPresent(String f) { diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/TaskdefTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/TaskdefTest.java index 24e509097..0faf16a2d 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/TaskdefTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/TaskdefTest.java @@ -25,8 +25,9 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; /** @@ -118,11 +119,11 @@ public class TaskdefTest { public void testOverride() { buildRule.executeTarget("testOverride"); String log = buildRule.getLog(); - assertTrue("override warning sent", - log.contains("Trying to override old definition of task copy")); - assertTrue("task inside target worked", - log.contains("In target")); - assertTrue("task inside target worked", - log.contains("In TaskContainer")); + assertThat("override warning sent", log, + containsString("Trying to override old definition of task copy")); + assertThat("task inside target worked", log, + containsString("In target")); + assertThat("task inside target worked", log, + containsString("In TaskContainer")); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/TouchTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/TouchTest.java index 80a08846a..700aec1d1 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/TouchTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/TouchTest.java @@ -25,24 +25,25 @@ import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import java.io.File; -import static org.apache.tools.ant.AntAssert.assertContains; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; public class TouchTest { @Rule public final BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + private static String TOUCH_FILE = "src/etc/testcases/taskdefs/touchtest"; /** Utilities used for file operations */ private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); - @Before public void setUp() { buildRule.configureProject("src/etc/testcases/taskdefs/touch.xml"); @@ -168,13 +169,9 @@ public class TouchTest { */ @Test public void testBadPattern() { - try { - buildRule.executeTarget("testBadPattern"); - fail("No parsing exception thrown"); - } catch (BuildException ex) { - assertContains("Unparseable", ex.getMessage()); - } - + thrown.expect(BuildException.class); + thrown.expectMessage("Unparseable"); + buildRule.executeTarget("testBadPattern"); } /** diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/TypeAdapterTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/TypeAdapterTest.java index 2751ea521..94b163e40 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/TypeAdapterTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/TypeAdapterTest.java @@ -28,11 +28,11 @@ import org.apache.tools.ant.TypeAdapter; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - +import static org.junit.Assert.assertThat; /** */ @@ -41,6 +41,8 @@ public class TypeAdapterTest { @Rule public final BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); @Before public void setUp() { @@ -50,35 +52,32 @@ public class TypeAdapterTest { @Test public void testTaskAdapter() { buildRule.executeTarget("taskadapter"); - assertContains("MyExec called", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("MyExec called")); } @Test public void testRunAdapter() { buildRule.executeTarget("runadapter"); - assertContains("MyRunnable called", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("MyRunnable called")); } @Test public void testRunAdapterError() { - try { - buildRule.executeTarget("runadaptererror"); - fail("BuildException expected: no public run method"); - } catch (BuildException ex) { - assertContains("No public run() method in", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("No public run() method in"); + buildRule.executeTarget("runadaptererror"); } @Test public void testDelay() { buildRule.executeTarget("delay"); - assertContains("MyTask called", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("MyTask called")); } @Test public void testOnErrorReport() { buildRule.executeTarget("onerror.report"); - assertContains("MyTaskNotPresent cannot be found", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("MyTaskNotPresent cannot be found")); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/TypedefTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/TypedefTest.java index 0b938261e..d8e58216b 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/TypedefTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/TypedefTest.java @@ -23,11 +23,12 @@ import org.apache.tools.ant.BuildFileRule; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThat; /** */ @@ -36,50 +37,36 @@ public class TypedefTest { @Rule public final BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); @Before public void setUp() { buildRule.configureProject("src/etc/testcases/taskdefs/typedef.xml"); } - @Test + @Test(expected = BuildException.class) public void testEmpty() { - try { - buildRule.executeTarget("empty"); - fail("BuildException expected: required argument not specified"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("empty"); + // TODO assert value } - @Test + @Test(expected = BuildException.class) public void testNoName() { - try { - buildRule.executeTarget("noName"); - fail("BuildException expected: required argument not specified"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("noName"); + // TODO assert value } - @Test + @Test(expected = BuildException.class) public void testNoClassname() { - try { - buildRule.executeTarget("noClassname"); - fail("BuildException expected: required argument not specified"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("noClassname"); + // TODO assert value } - @Test + @Test(expected = BuildException.class) public void testClassNotFound() { - try { - buildRule.executeTarget("classNotFound"); - fail("BuildException expected: classname specified doesn't exist"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("classNotFound"); + // TODO assert value } @Test @@ -88,8 +75,7 @@ public class TypedefTest { assertEquals("", buildRule.getLog()); Object ref = buildRule.getProject().getReferences().get("global"); assertNotNull("ref is not null", ref); - assertEquals("org.example.types.TypedefTestType", - ref.getClass().getName()); + assertEquals("org.example.types.TypedefTestType", ref.getClass().getName()); } @Test @@ -98,8 +84,7 @@ public class TypedefTest { assertEquals("", buildRule.getLog()); Object ref = buildRule.getProject().getReferences().get("local"); assertNotNull("ref is not null", ref); - assertEquals("org.example.types.TypedefTestType", - ref.getClass().getName()); + assertEquals("org.example.types.TypedefTestType", ref.getClass().getName()); } /** @@ -109,28 +94,25 @@ public class TypedefTest { @Test public void testDoubleNotPresent() { buildRule.executeTarget("double-notpresent"); - assertContains("hi", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("hi")); } @Test public void testNoResourceOnErrorFailAll() { - try { - buildRule.executeTarget("noresourcefailall"); - fail("BuildException expected: the requested resource does not exist"); - } catch (BuildException ex) { - assertContains("Could not load definitions from resource ", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Could not load definitions from resource "); + buildRule.executeTarget("noresourcefailall"); } @Test public void testNoResourceOnErrorFail() { buildRule.executeTarget("noresourcefail"); - assertContains("Could not load definitions from resource ", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Could not load definitions from resource ")); } @Test public void testNoResourceOnErrorNotFail() { - buildRule.executeTarget("noresourcenotfail"); - assertContains("Could not load definitions from resource ", buildRule.getLog()); + buildRule.executeTarget("noresourcenotfail"); + assertThat(buildRule.getLog(), containsString("Could not load definitions from resource ")); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ZipTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ZipTest.java index 60f2977bb..b15ef276f 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/ZipTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ZipTest.java @@ -33,12 +33,12 @@ import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; import static org.junit.Assume.assumeTrue; public class ZipTest { @@ -49,52 +49,47 @@ public class ZipTest { //instance variable to allow cleanup ZipFile zfPrefixAddsDir = null; - @Before public void setUp() { buildRule.configureProject("src/etc/testcases/taskdefs/zip.xml"); buildRule.executeTarget("setUp"); } - @Test + /** + * Fail due to required argument not specified + */ + @Test(expected = BuildException.class) public void test1() { - try { - buildRule.executeTarget("test1"); - fail("BuildException expected: required argument not specified"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("test1"); + //TODO assert value } - @Test + /** + * Fail due to required argument not specified + */ + @Test(expected = BuildException.class) public void test2() { - try { - buildRule.executeTarget("test2"); - fail("BuildException expected: required argument not specified"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("test2"); + //TODO assert value } - @Test + /** + * Fail because zip cannot include itself + */ + @Test(expected = BuildException.class) public void test3() { - try { - buildRule.executeTarget("test3"); - fail("BuildException expected: zip cannot include itself"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("test3"); + //TODO assert value } - @Test + /** + * Fail because zip cannot include itself + */ + @Test(expected = BuildException.class) @Ignore("Previously commented out") public void test4() { - try { - buildRule.executeTarget("test4"); - fail("BuildException expected: zip cannot include itself"); - } catch (BuildException ex) { - //TODO assert value - } + buildRule.executeTarget("test4"); + //TODO assert value } @After @@ -150,13 +145,13 @@ public class ZipTest { @Test public void testUpdateNotNecessary() { buildRule.executeTarget("testUpdateNotNecessary"); - assertFalse(buildRule.getLog().contains("Updating")); + assertThat(buildRule.getLog(), not(containsString("Updating"))); } @Test public void testUpdateIsNecessary() { buildRule.executeTarget("testUpdateIsNecessary"); - assertContains("Updating", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Updating")); } // Bugzilla Report 18403 @@ -204,7 +199,7 @@ public class ZipTest { @Test public void testZipEmptyCreate() { buildRule.executeTarget("zipEmptyCreate"); - assertContains("Note: creating empty", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Note: creating empty")); } // Bugzilla Report 25513 diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java index 7409b79d3..9c33cdfcb 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java @@ -34,10 +34,11 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.Path; import org.apache.tools.ant.util.FileUtils; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; public class DefaultCompilerAdapterTest { @@ -419,8 +420,8 @@ public class DefaultCompilerAdapterTest { sth.setJavac(javac); Commandline cmd = new Commandline(); sth.setupModernJavacCommandlineSwitches(cmd); - assertContains("Support for javac --release has been added in " - + "Java9 ignoring it", javac.getLog()); + assertThat(javac.getLog(), + containsString("Support for javac --release has been added in Java9 ignoring it")); String[] args = cmd.getCommandline(); assertEquals(7, args.length); assertEquals("-classpath", args[0]); @@ -445,8 +446,8 @@ public class DefaultCompilerAdapterTest { sth.setJavac(javac); Commandline cmd = new Commandline(); sth.setupModernJavacCommandlineSwitches(cmd); - assertContains("Ignoring source, target and bootclasspath as " - + "release has been set", javac.getLog()); + assertThat(javac.getLog(), + containsString("Ignoring source, target and bootclasspath as release has been set")); String[] args = cmd.getCommandline(); assertEquals(5, args.length); assertEquals("-classpath", args[0]); @@ -491,8 +492,7 @@ public class DefaultCompilerAdapterTest { if (expectedLog.isEmpty()) { assertEquals("", javac.getLog()); } else { - String l = javac.getLog(); - assertContains(expectedLog, l); + assertThat(javac.getLog(), containsString(expectedLog)); } String[] args = cmd.getCommandline(); assertEquals(expectedSource == null ? 0 : 2, args.length); diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/condition/IsFileSelectedTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/condition/IsFileSelectedTest.java index 10049fc0a..eaab2cea7 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/condition/IsFileSelectedTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/condition/IsFileSelectedTest.java @@ -23,9 +23,7 @@ import org.apache.tools.ant.BuildFileRule; import org.junit.Before; import org.junit.Rule; import org.junit.Test; - -import static org.apache.tools.ant.AntAssert.assertContains; -import static org.junit.Assert.fail; +import org.junit.rules.ExpectedException; /** * Testcase for the <isfileselected> condition. @@ -36,6 +34,9 @@ public class IsFileSelectedTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/taskdefs/conditions/isfileselected.xml"); @@ -63,12 +64,8 @@ public class IsFileSelectedTest { @Test public void testNotSelector() { - try { - buildRule.executeTarget("not.selector"); - fail("Exception should have been thrown: checking for use as a selector (not allowed)"); - } catch (BuildException ex) { - assertContains("fileset doesn't support the nested \"isfile", - ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("fileset doesn't support the nested \"isfileselected\""); + buildRule.executeTarget("not.selector"); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/condition/IsReachableTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/condition/IsReachableTest.java index 87e3712f2..a8847460f 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/condition/IsReachableTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/condition/IsReachableTest.java @@ -23,10 +23,7 @@ import org.junit.Before; import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; - -import static org.apache.tools.ant.AntAssert.assertContains; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import org.junit.rules.ExpectedException; /** * test for reachable things @@ -36,10 +33,12 @@ public class IsReachableTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { - buildRule.configureProject( - "src/etc/testcases/taskdefs/conditions/isreachable.xml"); + buildRule.configureProject("src/etc/testcases/taskdefs/conditions/isreachable.xml"); } @Test @@ -64,52 +63,37 @@ public class IsReachableTest { @Test public void testBoth() { - try { - buildRule.executeTarget("testBoth"); - fail("Build exception expected: error on two targets"); - } catch (BuildException ex) { - assertEquals(IsReachable.ERROR_BOTH_TARGETS, ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage(IsReachable.ERROR_BOTH_TARGETS); + buildRule.executeTarget("testBoth"); } @Test public void testNoTargets() { - try { - buildRule.executeTarget("testNoTargets"); - fail("Build exception expected: no params"); - } catch (BuildException ex) { - assertEquals(IsReachable.ERROR_NO_HOSTNAME, ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage(IsReachable.ERROR_NO_HOSTNAME); + buildRule.executeTarget("testNoTargets"); } @Test public void testBadTimeout() { - try { - buildRule.executeTarget("testBadTimeout"); - fail("Build exception expected: error on -ve timeout"); - } catch (BuildException ex) { - assertEquals(IsReachable.ERROR_BAD_TIMEOUT, ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage(IsReachable.ERROR_BAD_TIMEOUT); + buildRule.executeTarget("testBadTimeout"); } @Test @Ignore("Previously named in a way to prevent execution") public void NotestFile() { - try { - buildRule.executeTarget("testFile"); - fail("Build exception expected: error on file URL"); - } catch (BuildException ex) { - assertEquals(IsReachable.ERROR_NO_HOST_IN_URL, ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage(IsReachable.ERROR_NO_HOST_IN_URL); + buildRule.executeTarget("testFile"); } @Test public void testBadURL() { - try { - buildRule.executeTarget("testBadURL"); - fail("Build exception expected: error in URL"); - } catch (BuildException ex) { - assertContains(IsReachable.ERROR_BAD_URL, ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage(IsReachable.ERROR_BAD_URL); + buildRule.executeTarget("testBadURL"); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/condition/TypeFoundTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/condition/TypeFoundTest.java index 45df9a80f..fabb58952 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/condition/TypeFoundTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/condition/TypeFoundTest.java @@ -22,10 +22,9 @@ import org.apache.tools.ant.BuildFileRule; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static org.apache.tools.ant.AntAssert.assertContains; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; import static org.junit.Assert.assertNull; /** @@ -36,6 +35,9 @@ public class TypeFoundTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/taskdefs/conditions/typefound.xml"); @@ -49,12 +51,9 @@ public class TypeFoundTest { @Test public void testUndefined() { - try { - buildRule.executeTarget("testUndefined"); - fail("Build exception expected: left out the name attribute"); - } catch (BuildException ex) { - assertContains("No type specified", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("No type specified"); + buildRule.executeTarget("testUndefined"); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ANTLRTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ANTLRTest.java index f57e418e6..ba0b2924b 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ANTLRTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ANTLRTest.java @@ -28,8 +28,9 @@ import org.junit.Test; import java.io.File; import java.io.FilenameFilter; -import static org.apache.tools.ant.AntAssert.assertContains; -import static org.apache.tools.ant.AntAssert.assertNotContains; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -178,32 +179,32 @@ public class ANTLRTest { @Test public void testNoRecompile() { buildRule.executeTarget("test9"); - assertNotContains("Skipped grammar file.", buildRule.getFullLog()); + assertThat(buildRule.getFullLog(), not(containsString("Skipped grammar file."))); buildRule.executeTarget("noRecompile"); - assertContains("Skipped grammar file.", buildRule.getFullLog()); + assertThat(buildRule.getFullLog(), containsString("Skipped grammar file.")); } @Test public void testNormalRecompile() { buildRule.executeTarget("test9"); - assertNotContains("Skipped grammar file.", buildRule.getFullLog()); + assertThat(buildRule.getFullLog(), not(containsString("Skipped grammar file."))); FileUtilities.rollbackTimestamps(buildRule.getOutputDir(), 5); buildRule.executeTarget("normalRecompile"); - assertNotContains("Skipped grammar file.", buildRule.getFullLog()); + assertThat(buildRule.getFullLog(), not(containsString("Skipped grammar file."))); } @Test // Bugzilla Report 12961 public void testSupergrammarChangeRecompile() { buildRule.executeTarget("test9"); - assertNotContains("Skipped grammar file.", buildRule.getFullLog()); + assertThat(buildRule.getFullLog(), not(containsString("Skipped grammar file."))); FileUtilities.rollbackTimestamps(buildRule.getOutputDir(), 5); buildRule.executeTarget("supergrammarChangeRecompile"); - assertNotContains("Skipped grammar file.", buildRule.getFullLog()); + assertThat(buildRule.getFullLog(), not(containsString("Skipped grammar file."))); } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java index 68275f2d9..bc298f83a 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java @@ -18,11 +18,11 @@ package org.apache.tools.ant.taskdefs.optional; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import static org.junit.Assume.assumeTrue; import java.io.BufferedInputStream; @@ -41,6 +41,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; /** * Tests the EchoProperties task. @@ -59,6 +60,9 @@ public class EchoPropertiesTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject(TASKDEFS_DIR + "echoproperties.xml"); @@ -73,45 +77,39 @@ public class EchoPropertiesTest { @Test public void testEchoToLog() { buildRule.executeTarget("testEchoToLog"); - assertContains("test.property=" + TEST_VALUE, buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("test.property=" + TEST_VALUE)); } @Test public void testEchoWithEmptyPrefixToLog() { buildRule.executeTarget("testEchoWithEmptyPrefixToLog"); - assertContains("test.property=" + TEST_VALUE, buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("test.property=" + TEST_VALUE)); } @Test public void testReadBadFile() { - try { - buildRule.executeTarget("testReadBadFile"); - fail("BuildException should have been thrown on bad file"); - } catch (BuildException ex) { - assertContains("srcfile is a directory", "srcfile is a directory!", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("srcfile is a directory!"); + buildRule.executeTarget("testReadBadFile"); } @Test public void testReadBadFileNoFail() { buildRule.executeTarget("testReadBadFileNoFail"); - assertContains("srcfile is a directory!", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("srcfile is a directory!")); } @Test public void testEchoToBadFile() { - try { - buildRule.executeTarget("testEchoToBadFile"); - fail("BuildException should have been thrown on destination file being a directory"); - } catch (BuildException ex) { - assertContains("destfile is a directory", "destfile is a directory!", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("destfile is a directory!"); + buildRule.executeTarget("testEchoToBadFile"); } @Test public void testEchoToBadFileNoFail() { buildRule.executeTarget("testEchoToBadFileNoFail"); - assertContains("destfile is a directory!", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("destfile is a directory!")); } @Test @@ -127,14 +125,8 @@ public class EchoPropertiesTest { // read in the file File f = createRelativeFile(GOOD_OUTFILE_XML); try (BufferedReader br = new BufferedReader(new FileReader(f))) { - String read = null; - while ((read = br.readLine()) != null) { - if (read.contains("")) { - // found the property we set - it's good. - return; - } - } - fail("did not encounter set property in generated file."); + assertTrue("did not encounter set property in generated file.", br.lines().anyMatch(line + -> line.contains(""))); } } @@ -172,19 +164,15 @@ public class EchoPropertiesTest { @Test public void testWithPrefixAndRegex() { - try { - buildRule.executeTarget("testWithPrefixAndRegex"); - fail("BuildException should have been thrown on Prefix and RegEx being set"); - } catch (BuildException ex) { - assertEquals("The target must fail with prefix and regex attributes set", - "Please specify either prefix or regex, but not both", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Please specify either prefix or regex, but not both"); + buildRule.executeTarget("testWithPrefixAndRegex"); } @Test public void testWithEmptyPrefixAndRegex() { buildRule.executeTarget("testEchoWithEmptyPrefixToLog"); - assertContains("test.property=" + TEST_VALUE, buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("test.property=" + TEST_VALUE)); } @Test @@ -194,7 +182,7 @@ public class EchoPropertiesTest { buildRule.executeTarget("testWithRegex"); // the following line has been changed from checking ant.home to ant.version // so the test will still work when run outside of an Ant script - assertContains("ant.version=", buildRule.getFullLog()); + assertThat(buildRule.getFullLog(), containsString("ant.version=")); } private void testEchoPrefixVarious(String target) throws Exception { @@ -231,10 +219,7 @@ public class EchoPropertiesTest { } protected File createRelativeFile(String filename) { - if (filename.equals(".")) { - return buildRule.getProject().getBaseDir(); - } - // else - return new File(buildRule.getProject().getBaseDir(), filename); + return filename.equals(".") ? buildRule.getProject().getBaseDir() + : new File(buildRule.getProject().getBaseDir(), filename); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/RpmTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/RpmTest.java index 137003855..a18db0ed8 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/RpmTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/RpmTest.java @@ -23,25 +23,23 @@ import org.apache.tools.ant.taskdefs.Execute; import org.apache.tools.ant.taskdefs.ExecuteStreamHandler; import org.apache.tools.ant.types.Commandline; +import org.junit.Rule; import org.junit.Test; - -import static org.junit.Assert.fail; -import static org.apache.tools.ant.AntAssert.assertContains; +import org.junit.rules.ExpectedException; public class RpmTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Test public void testShouldThrowExceptionWhenRpmFails() { + thrown.expect(BuildException.class); + thrown.expectMessage("' failed with exit code 2"); Rpm rpm = new MyRpm(); rpm.setProject(new Project()); rpm.setFailOnError(true); - // execute - try { - rpm.execute(); - fail("should have thrown a build exception"); - } catch (BuildException ex) { - assertContains("' failed with exit code 2", ex.getMessage()); - } + rpm.execute(); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/SchemaValidateTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/SchemaValidateTest.java index 81215c8bc..1cb0451c6 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/SchemaValidateTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/SchemaValidateTest.java @@ -22,9 +22,7 @@ import org.apache.tools.ant.BuildFileRule; import org.junit.Before; import org.junit.Rule; import org.junit.Test; - -import static org.apache.tools.ant.AntAssert.assertContains; -import static org.junit.Assert.fail; +import org.junit.rules.ExpectedException; /** * Test schema validation @@ -40,6 +38,9 @@ public class SchemaValidateTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject(TASKDEFS_DIR + "schemavalidate.xml"); @@ -63,56 +64,37 @@ public class SchemaValidateTest { @Test public void testNoEmptySchemaNamespace() { - try { - buildRule.executeTarget("testNoEmptySchemaNamespace"); - fail("Empty namespace URI"); - } catch (BuildException ex) { - assertContains(SchemaValidate.SchemaLocation.ERROR_NO_URI, ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage(SchemaValidate.SchemaLocation.ERROR_NO_URI); + buildRule.executeTarget("testNoEmptySchemaNamespace"); } @Test public void testNoEmptySchemaLocation() { - try { - buildRule.executeTarget("testNoEmptySchemaLocation"); - fail("Empty schema location"); - } catch (BuildException ex) { - assertContains(SchemaValidate.SchemaLocation.ERROR_NO_LOCATION, - ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage(SchemaValidate.SchemaLocation.ERROR_NO_LOCATION); + buildRule.executeTarget("testNoEmptySchemaLocation"); } @Test public void testNoFile() { - try { - buildRule.executeTarget("testNoFile"); - fail("No file at file attribute"); - } catch (BuildException ex) { - assertContains(SchemaValidate.SchemaLocation.ERROR_NO_FILE, - ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage(SchemaValidate.SchemaLocation.ERROR_NO_FILE); + buildRule.executeTarget("testNoFile"); } @Test public void testNoDoubleSchemaLocation() { - try { - buildRule.executeTarget("testNoDoubleSchemaLocation"); - fail("Two locations for schemas"); - } catch (BuildException ex) { - assertContains(SchemaValidate.SchemaLocation.ERROR_TWO_LOCATIONS, - ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage(SchemaValidate.SchemaLocation.ERROR_TWO_LOCATIONS); + buildRule.executeTarget("testNoDoubleSchemaLocation"); } @Test public void testNoDuplicateSchema() { - try { - buildRule.executeTarget("testNoDuplicateSchema"); - fail("duplicate schemas with different values"); - } catch (BuildException ex) { - assertContains(SchemaValidate.ERROR_DUPLICATE_SCHEMA, - ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage(SchemaValidate.ERROR_DUPLICATE_SCHEMA); + buildRule.executeTarget("testNoDuplicateSchema"); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/XmlValidateTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/XmlValidateTest.java index 3e9c25866..8faaf5027 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/XmlValidateTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/XmlValidateTest.java @@ -22,10 +22,7 @@ import org.apache.tools.ant.BuildFileRule; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.internal.AssumptionViolatedException; - -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import org.junit.rules.ExpectedException; /** * Tests the XMLValidate optional task, by running targets in the test script @@ -45,6 +42,9 @@ public class XmlValidateTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject(TASKDEFS_DIR + "xmlvalidate.xml"); @@ -104,42 +104,23 @@ public class XmlValidateTest { } /** - * Test xml schema validation + * Test xml schema validation, + * implicitly assume schema-supporting parser (Java 5+) */ @Test public void testXmlSchemaGood() throws BuildException { - try { - buildRule.executeTarget("testSchemaGood"); - } catch (BuildException e) { - if (e.getMessage().endsWith( - " doesn't recognize feature http://apache.org/xml/features/validation/schema") - || e.getMessage().endsWith( - " doesn't support feature http://apache.org/xml/features/validation/schema")) { - throw new AssumptionViolatedException("parser doesn't support schema"); - } else { - throw e; - } - } + buildRule.executeTarget("testSchemaGood"); } + /** - * Test xml schema validation + * Test xml schema validation, + * implicitly assume schema-supporting parser (Java 5+) */ @Test public void testXmlSchemaBad() { - try { - buildRule.executeTarget("testSchemaBad"); - fail("Should throw BuildException because 'Bad Schema Validation'"); - } catch (BuildException e) { - if (e.getMessage().endsWith( - " doesn't recognize feature http://apache.org/xml/features/validation/schema") - || e.getMessage().endsWith( - " doesn't support feature http://apache.org/xml/features/validation/schema")) { - throw new AssumptionViolatedException("parser doesn't support schema"); - } else { - assertTrue( - e.getMessage().contains("not a valid XML document")); - } - } + thrown.expect(BuildException.class); + thrown.expectMessage("not a valid XML document"); + buildRule.executeTarget("testSchemaBad"); } /** @@ -160,30 +141,24 @@ public class XmlValidateTest { * * Bug 11279 */ - @Test + @Test(expected = BuildException.class) public void testUtf8() { - try { - buildRule.executeTarget("testUtf8"); - fail("Invalid characters in file"); - } catch (BuildException ex) { - //TODO assert exception message - } + buildRule.executeTarget("testUtf8"); + //TODO assert exception message } - // Tests property element, using XML schema properties as an example. + /** + * Tests property element, using XML schema properties as an example. + */ @Test public void testPropertySchemaForValidXML() { buildRule.executeTarget("testProperty.validXML"); } - @Test + @Test(expected = BuildException.class) public void testPropertySchemaForInvalidXML() { - try { - buildRule.executeTarget("testProperty.invalidXML"); - fail("XML file does not satisfy schema"); - } catch (BuildException ex) { - //TODO assert exception message - } + buildRule.executeTarget("testProperty.invalidXML"); + //TODO assert exception message } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/XsltTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/XsltTest.java index 66b488283..334485fe1 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/XsltTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/XsltTest.java @@ -23,7 +23,9 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import static org.junit.Assert.fail; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertThat; /** * Tests the {@link org.apache.tools.ant.taskdefs.XSLTProcess} task. @@ -45,14 +47,10 @@ public class XsltTest { buildRule.configureProject(TASKDEFS_DIR + "xslt.xml"); } - @Test + @Test(expected = BuildException.class) public void testCatchNoDtd() { - try { - buildRule.executeTarget("testCatchNoDtd"); - fail("Expected failure"); - } catch (BuildException ex) { - //TODO assert exception message - } + buildRule.executeTarget("testCatchNoDtd"); + // TODO assert exception message } @Test @@ -76,8 +74,7 @@ public class XsltTest { @Test public void testStyleSheetWithInclude() { buildRule.executeTarget("testStyleSheetWithInclude"); - if (buildRule.getLog().contains("java.io.FileNotFoundException")) { - fail("xsl:include was not found"); - } + assertThat("xsl:include was not found", buildRule.getLog(), + not(containsString("java.io.FileNotFoundException"))); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/depend/DependTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/depend/DependTest.java index d2fce93c5..65585074f 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/depend/DependTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/depend/DependTest.java @@ -29,11 +29,13 @@ import org.apache.tools.ant.types.FileSet; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.both; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; /** * Testcase for the Depend optional task. @@ -48,6 +50,9 @@ public class DependTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject(TEST_BUILD_FILE); @@ -104,7 +109,6 @@ public class DependTest { FileUtilities.rollbackTimestamps(new File(buildRule.getProject().getProperty("tempsrc.dir")), 5); FileUtilities.rollbackTimestamps(new File(buildRule.getProject().getProperty("classes.dir")), 5); - buildRule.executeTarget("testinner"); assertEquals("Depend did not leave correct number of files", 0, getResultFiles().size()); @@ -132,12 +136,9 @@ public class DependTest { */ @Test public void testNoSource() { - try { - buildRule.executeTarget("testnosource"); - fail("Build exception expected: No source specified"); - } catch (BuildException ex) { - assertContains("srcdir attribute must be set", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("srcdir attribute must be set"); + buildRule.executeTarget("testnosource"); } /** @@ -145,12 +146,9 @@ public class DependTest { */ @Test public void testEmptySource() { - try { - buildRule.executeTarget("testemptysource"); - fail("Build exception expected: No source specified"); - } catch (BuildException ex) { - assertContains("srcdir attribute must be non-empty", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("srcdir attribute must be non-empty"); + buildRule.executeTarget("testemptysource"); } /** @@ -201,12 +199,9 @@ public class DependTest { FileUtilities.rollbackTimestamps(new File(buildRule.getProject().getProperty("classes.dir")), 5); buildRule.executeTarget("testnonpublic"); - String log = buildRule.getLog(); - assertContains("Expected warning about APrivate", - "The class APrivate in file", log); - assertContains("but has not been deleted because its source file " - + "could not be determined", - "The class APrivate in file", log); + assertThat("Expected warning about APrivate", + buildRule.getLog(), both(containsString("The class APrivate in file")) + .and(containsString("but has not been deleted because its source file could not be determined"))); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/image/ImageTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/image/ImageTest.java index ac1c27cac..3fda82c75 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/image/ImageTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/image/ImageTest.java @@ -25,16 +25,17 @@ import org.junit.Before; import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import java.io.File; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeFalse; import static org.junit.Assume.assumeTrue; - /** * Tests the Image task. * @@ -49,6 +50,9 @@ public class ImageTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { /* JAI depends on internal API removed in Java 9 */ @@ -60,13 +64,13 @@ public class ImageTest { @Test public void testEchoToLog() { buildRule.executeTarget("testEchoToLog"); - assertContains("Processing File", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Processing File")); } @Test public void testSimpleScale() { buildRule.executeTarget("testSimpleScale"); - assertContains("Processing File", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Processing File")); File f = new File(buildRule.getOutputDir(), LARGEIMAGE); assertTrue("Did not create " + f.getAbsolutePath(), f.exists()); @@ -75,13 +79,13 @@ public class ImageTest { @Test public void testOverwriteTrue() { buildRule.executeTarget("testSimpleScale"); - assertContains("Processing File", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Processing File")); File f = new File(buildRule.getOutputDir(), LARGEIMAGE); assumeTrue("Could not change file modification date", f.setLastModified(f.lastModified() - FILE_UTILS.getFileTimestampGranularity() * 2)); long lastModified = f.lastModified(); buildRule.executeTarget("testOverwriteTrue"); - assertContains("Processing File", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Processing File")); f = new File(buildRule.getOutputDir(), LARGEIMAGE); long overwrittenLastModified = f.lastModified(); assertTrue("File was not overwritten.", lastModified < overwrittenLastModified); @@ -90,11 +94,11 @@ public class ImageTest { @Test public void testOverwriteFalse() { buildRule.executeTarget("testSimpleScale"); - assertContains("Processing File", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Processing File")); File f = new File(buildRule.getOutputDir(), LARGEIMAGE); long lastModified = f.lastModified(); buildRule.executeTarget("testOverwriteFalse"); - assertContains("Processing File", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Processing File")); f = new File(buildRule.getOutputDir(), LARGEIMAGE); long overwrittenLastModified = f.lastModified(); assertEquals("File was overwritten.", lastModified, overwrittenLastModified); @@ -103,7 +107,7 @@ public class ImageTest { @Test public void testSimpleScaleWithMapper() { buildRule.executeTarget("testSimpleScaleWithMapper"); - assertContains("Processing File", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Processing File")); File f = new File(buildRule.getOutputDir(), "scaled-" + LARGEIMAGE); assertTrue("Did not create " + f.getAbsolutePath(), f.exists()); } @@ -111,13 +115,13 @@ public class ImageTest { @Test @Ignore("Previously named in a manner to prevent execution") public void testFailOnError() { + final String message = "Unable to render RenderedOp for this operation."; + thrown.expect(RuntimeException.class); + thrown.expectMessage(message); try { buildRule.executeTarget("testFailOnError"); - assertContains("Unable to process image stream", buildRule.getLog()); - } catch (RuntimeException re) { - assertTrue("Run time exception should say 'Unable to process image stream'. :" - + re.toString(), - re.toString().contains("Unable to process image stream")); + } finally { + assertThat(buildRule.getLog(), containsString(message)); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTest.java index 5728e9298..55d4b0760 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTest.java @@ -23,7 +23,8 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; /** * Testcase for the JDepend optional task. @@ -46,8 +47,7 @@ public class JDependTest { @Test public void testSimple() { buildRule.executeTarget("simple"); - assertContains("Package: org.apache.tools.ant.util.facade", - buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("Package: org.apache.tools.ant.util.facade")); } /** @@ -56,7 +56,7 @@ public class JDependTest { @Test public void testXml() { buildRule.executeTarget("xml"); - assertContains("", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("")); } /** @@ -66,7 +66,7 @@ public class JDependTest { @Test public void testFork() { buildRule.executeTarget("fork"); - assertContains("Package: org.apache.tools.ant.util.facade", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Package: org.apache.tools.ant.util.facade")); } /** @@ -75,7 +75,7 @@ public class JDependTest { @Test public void testForkXml() { buildRule.executeTarget("fork-xml"); - assertContains("", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("")); } /** @@ -84,7 +84,7 @@ public class JDependTest { @Test public void testTimeout() { buildRule.executeTarget("fork-xml"); - assertContains("JDepend FAILED - Timed out", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("JDepend FAILED - Timed out")); } @@ -94,7 +94,7 @@ public class JDependTest { @Test public void testTimeoutNot() { buildRule.executeTarget("fork-timeout-not"); - assertContains("Package: org.apache.tools.ant.util.facade", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("Package: org.apache.tools.ant.util.facade")); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitReportTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitReportTest.java index 99709afbb..2355b7ace 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitReportTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitReportTest.java @@ -18,9 +18,10 @@ package org.apache.tools.ant.taskdefs.optional.junit; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.junit.Assert.assertThat; import static org.junit.Assume.assumeTrue; import java.io.ByteArrayInputStream; @@ -100,21 +101,21 @@ public class JUnitReportTest { public void testEmptyFile() { buildRule.executeTarget("testEmptyFile"); assertIndexCreated(); - assertContains("Required text not found in log", XMLResultAggregator.WARNING_EMPTY_FILE, buildRule.getLog()); + assertThat("Required text not found in log", buildRule.getLog(), containsString(XMLResultAggregator.WARNING_EMPTY_FILE)); } @Test public void testIncompleteFile() { buildRule.executeTarget("testIncompleteFile"); assertIndexCreated(); - assertContains("Required text not found in log", XMLResultAggregator.WARNING_IS_POSSIBLY_CORRUPTED, buildRule.getLog()); + assertThat("Required text not found in log", buildRule.getLog(), containsString(XMLResultAggregator.WARNING_IS_POSSIBLY_CORRUPTED)); } @Test public void testWrongElement() { buildRule.executeTarget("testWrongElement"); assertIndexCreated(); - assertContains("Required text not found in log", XMLResultAggregator.WARNING_INVALID_ROOT_ELEMENT, buildRule.getLog()); + assertThat("Required text not found in log", buildRule.getLog(), containsString(XMLResultAggregator.WARNING_INVALID_ROOT_ELEMENT)); } // Bugzilla Report 34963 @@ -126,8 +127,8 @@ public class JUnitReportTest { try { r = new FileReader(new File(buildRule.getOutputDir(), "html/sampleproject/coins/0_CoinTest.html")); String report = FileUtils.readFully(r); - assertContains("output must contain
:\n" + report, "junit.framework.AssertionFailedError: DOEG
", report); - assertContains("#51049: output must translate line breaks:\n" + report, "cur['line.separator'] = '\\r\\n';", report); + assertThat("output must contain
:\n" + report, report, containsString("junit.framework.AssertionFailedError: DOEG
")); + assertThat("#51049: output must translate line breaks:\n" + report, report, containsString("cur['line.separator'] = '\\r\\n';")); } finally { FileUtils.close(r); } @@ -179,7 +180,7 @@ public class JUnitReportTest { @Test public void testWithParams() throws Exception { buildRule.executeTarget("testWithParams"); - assertContains("key1=value1,key2=value2", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("key1=value1,key2=value2")); commonIndexFileAssertions(); } 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 6a9a9af26..5df984b8b 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 @@ -17,15 +17,16 @@ */ package org.apache.tools.ant.taskdefs.optional.junit; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; -import static org.apache.tools.ant.AntAssert.assertNotContains; -import static org.apache.tools.ant.AntAssert.assertContains; import java.io.BufferedReader; import java.io.ByteArrayInputStream; @@ -179,26 +180,26 @@ public class JUnitTaskTest { collectorFile.exists()); // the passing test cases buildRule.executeTarget("A.test01"); - assertContains("1st run: should run A.test01", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("1st run: should run A.test01")); buildRule.executeTarget("B.test05"); - assertContains("1st run: should run B.test05", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("1st run: should run B.test05")); buildRule.executeTarget("B.test06"); - assertContains("1st run: should run B.test06", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("1st run: should run B.test06")); buildRule.executeTarget("C.test07"); - assertContains("1st run: should run C.test07", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("1st run: should run C.test07")); buildRule.executeTarget("C.test08"); - assertContains("1st run: should run C.test08", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("1st run: should run C.test08")); buildRule.executeTarget("C.test09"); - assertContains("1st run: should run C.test09", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("1st run: should run C.test09")); // the failing test cases buildRule.executeTarget("A.test02"); - assertContains("1st run: should run A.test02", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("1st run: should run A.test02")); buildRule.executeTarget("A.test03"); - assertContains("1st run: should run A.test03", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("1st run: should run A.test03")); buildRule.executeTarget("B.test04"); - assertContains("1st run: should run B.test04", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("1st run: should run B.test04")); buildRule.executeTarget("D.test10"); - assertContains("1st run: should run D.test10", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("1st run: should run D.test10")); // 2nd junit run: should do only failing tests @@ -208,26 +209,26 @@ public class JUnitTaskTest { collectorFile.exists()); // the passing test cases buildRule.executeTarget("A.test01"); - assertNotContains("2nd run: should not run A.test01", buildRule.getOutput()); + assertThat(buildRule.getOutput(), not(containsString("2nd run: should not run A.test01"))); buildRule.executeTarget("B.test05"); - assertNotContains("2nd run: should not run A.test05", buildRule.getOutput()); + assertThat(buildRule.getOutput(), not(containsString("2nd run: should not run A.test05"))); buildRule.executeTarget("B.test06"); - assertNotContains("2nd run: should not run B.test06", buildRule.getOutput()); + assertThat(buildRule.getOutput(), not(containsString("2nd run: should not run B.test06"))); buildRule.executeTarget("C.test07"); - assertNotContains("2nd run: should not run C.test07", buildRule.getOutput()); + assertThat(buildRule.getOutput(), not(containsString("2nd run: should not run C.test07"))); buildRule.executeTarget("C.test08"); - assertNotContains("2nd run: should not run C.test08", buildRule.getOutput()); + assertThat(buildRule.getOutput(), not(containsString("2nd run: should not run C.test08"))); buildRule.executeTarget("C.test09"); - assertNotContains("2nd run: should not run C.test09", buildRule.getOutput()); + assertThat(buildRule.getOutput(), not(containsString("2nd run: should not run C.test09"))); // the failing test cases buildRule.executeTarget("A.test02"); - assertContains("2nd run: should run A.test02", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("2nd run: should run A.test02")); buildRule.executeTarget("A.test03"); - assertContains("2nd run: should run A.test03", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("2nd run: should run A.test03")); buildRule.executeTarget("B.test04"); - assertContains("2nd run: should run B.test04", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("2nd run: should run B.test04")); buildRule.executeTarget("D.test10"); - assertContains("2nd run: should run D.test10", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("2nd run: should run D.test10")); // "fix" errors in class A @@ -239,13 +240,13 @@ public class JUnitTaskTest { + "' should exist after the 3rd run.", collectorFile.exists()); buildRule.executeTarget("A.test02"); - assertContains("3rd run: should run A.test02", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("3rd run: should run A.test02")); buildRule.executeTarget("A.test03"); - assertContains("3rd run: should run A.test03", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("3rd run: should run A.test03")); buildRule.executeTarget("B.test04"); - assertContains("3rd run: should run B.test04", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("3rd run: should run B.test04")); buildRule.executeTarget("D.test10"); - assertContains("3rd run: should run D.test10", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("3rd run: should run D.test10")); // 4rd run: two running tests with errors @@ -254,12 +255,12 @@ public class JUnitTaskTest { + "' should exist after the 4th run.", collectorFile.exists()); //TODO: these two statements fail - //buildRule.executeTarget("A.test02");assertNotContains("4th run: should not run A.test02", buildRule.getOutput()); - //buildRule.executeTarget("A.test03");assertNotContains("4th run: should not run A.test03", buildRule.getOutput()); + //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"); - assertContains("4th run: should run B.test04", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("4th run: should run B.test04")); buildRule.executeTarget("D.test10"); - assertContains("4th run: should run D.test10", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("4th run: should run D.test10")); } @@ -272,16 +273,16 @@ public class JUnitTaskTest { @Test public void testMultilineAssertsNoFork() { buildRule.executeTarget("testMultilineAssertsNoFork"); - assertNotContains("messaged up", buildRule.getLog()); - assertNotContains("crashed)", buildRule.getLog()); + assertThat(buildRule.getLog(), not(containsString("messaged up"))); + assertThat(buildRule.getLog(), not(containsString("crashed)"))); } // Bugzilla Issue 45411 @Test public void testMultilineAssertsFork() { buildRule.executeTarget("testMultilineAssertsFork"); - assertNotContains("messaged up", buildRule.getLog()); - assertNotContains("crashed)", buildRule.getLog()); + assertThat(buildRule.getLog(), not(containsString("messaged up"))); + assertThat(buildRule.getLog(), not(containsString("crashed)"))); } private void assertResultFilesExist(String target, String extension) { @@ -301,8 +302,8 @@ public class JUnitTaskTest { } private void assertNoPrint(String result, String where) { - assertNotContains(where + " '" + result + "' must not contain print statement", - "print to System.", result); + assertThat(where + " '" + result + "' must not contain print statement", + result, not(containsString("print to System."))); } private void assertOutput() throws IOException { diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestListenerTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestListenerTest.java index a4798efc1..e9cfd1fd1 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestListenerTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestListenerTest.java @@ -18,8 +18,9 @@ package org.apache.tools.ant.taskdefs.optional.junit; -import static org.apache.tools.ant.AntAssert.assertContains; -import static org.apache.tools.ant.AntAssert.assertNotContains; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertThat; import org.apache.tools.ant.BuildFileRule; import org.junit.Before; @@ -48,55 +49,55 @@ public class JUnitTestListenerTest { public void testFullLogOutput() { buildRule.getProject().setProperty("enableEvents", "true"); buildRule.executeTarget(PASS_TEST_TARGET); - assertContains("expecting full log to have BuildListener events", - JUnitTask.TESTLISTENER_PREFIX, buildRule.getFullLog()); + assertThat("expecting full log to have BuildListener events", buildRule.getFullLog(), + containsString(JUnitTask.TESTLISTENER_PREFIX)); } @Test public void testNoLogOutput() { buildRule.getProject().setProperty("enableEvents", "true"); buildRule.executeTarget(PASS_TEST_TARGET); - assertNotContains("expecting log to not have BuildListener events", - JUnitTask.TESTLISTENER_PREFIX, buildRule.getLog()); + assertThat("expecting log to not have BuildListener events", buildRule.getLog(), + not(containsString(JUnitTask.TESTLISTENER_PREFIX))); } @Test public void testTestCountFired() { buildRule.getProject().setProperty("enableEvents", "true"); buildRule.executeTarget(PASS_TEST_TARGET); - assertContains("expecting test count message", JUnitTask.TESTLISTENER_PREFIX - + "tests to run: ", buildRule.getFullLog()); + assertThat("expecting test count message", buildRule.getFullLog(), + containsString(JUnitTask.TESTLISTENER_PREFIX + "tests to run: ")); } @Test public void testStartTestFired() { buildRule.getProject().setProperty("enableEvents", "true"); buildRule.executeTarget(PASS_TEST_TARGET); - assertContains("expecting test started message", JUnitTask.TESTLISTENER_PREFIX - + "startTest(" + PASS_TEST + ")", buildRule.getFullLog()); + assertThat("expecting test started message", buildRule.getFullLog(), + containsString(JUnitTask.TESTLISTENER_PREFIX + "startTest(" + PASS_TEST + ")")); } @Test public void testEndTestFired() { buildRule.getProject().setProperty("enableEvents", "true"); buildRule.executeTarget(PASS_TEST_TARGET); - assertContains("expecting test ended message", JUnitTask.TESTLISTENER_PREFIX - + "endTest(" + PASS_TEST + ")", buildRule.getFullLog()); + assertThat("expecting test ended message", buildRule.getFullLog(), + containsString(JUnitTask.TESTLISTENER_PREFIX + "endTest(" + PASS_TEST + ")")); } @Test public void testNoFullLogOutputByDefault() { buildRule.executeTarget(PASS_TEST_TARGET); - assertNotContains("expecting full log to not have BuildListener events", - JUnitTask.TESTLISTENER_PREFIX, buildRule.getFullLog()); + assertThat("expecting full log to not have BuildListener events", buildRule.getFullLog(), + not(containsString(JUnitTask.TESTLISTENER_PREFIX))); } @Test public void testFullLogOutputMagicProperty() { buildRule.getProject().setProperty(JUnitTask.ENABLE_TESTLISTENER_EVENTS, "true"); buildRule.executeTarget(PASS_TEST_TARGET); - assertContains("expecting full log to have BuildListener events", - JUnitTask.TESTLISTENER_PREFIX, buildRule.getFullLog()); + assertThat("expecting full log to have BuildListener events", buildRule.getFullLog(), + containsString(JUnitTask.TESTLISTENER_PREFIX)); } @Test @@ -104,8 +105,8 @@ public class JUnitTestListenerTest { buildRule.getProject().setProperty(JUnitTask.ENABLE_TESTLISTENER_EVENTS, "false"); buildRule.getProject().setProperty("enableEvents", "true"); buildRule.executeTarget(PASS_TEST_TARGET); - assertNotContains("expecting full log to not have BuildListener events", - JUnitTask.TESTLISTENER_PREFIX, buildRule.getFullLog()); + assertThat("expecting full log to not have BuildListener events", buildRule.getFullLog(), + not(containsString(JUnitTask.TESTLISTENER_PREFIX))); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java index e0d65e154..086d17cfd 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java @@ -28,9 +28,10 @@ import junit.framework.TestSuite; import org.apache.tools.ant.BuildException; import org.junit.Test; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; /** @@ -81,7 +82,7 @@ public class JUnitTestRunnerTest { runner.run(); String error = runner.getFormatter().getError(); assertEquals(error, JUnitTestRunner.ERRORS, runner.getRetCode()); - assertTrue(error, error.contains("thrown on purpose")); + assertThat(error, error, containsString("thrown on purpose")); } // check that something which is not a testcase generates no errors diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/TearDownOnVmCrashTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/TearDownOnVmCrashTest.java index eb202848a..5234e63c4 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/TearDownOnVmCrashTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/TearDownOnVmCrashTest.java @@ -18,9 +18,10 @@ package org.apache.tools.ant.taskdefs.optional.junit; -import static org.apache.tools.ant.AntAssert.assertContains; -import static org.apache.tools.ant.AntAssert.assertNotContains; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; import org.apache.tools.ant.BuildFileRule; import org.junit.Before; @@ -41,13 +42,13 @@ public class TearDownOnVmCrashTest { public void testNoTeardown() { buildRule.executeTarget("testNoTeardown"); assertEquals("true", buildRule.getProject().getProperty("error")); - assertNotContains("tearDown called on Timeout", buildRule.getOutput()); + assertThat(buildRule.getOutput(), not(containsString("tearDown called on Timeout"))); } @Test public void testTeardown() { buildRule.executeTarget("testTeardown"); assertEquals("true", buildRule.getProject().getProperty("error")); - assertContains("tearDown called on Timeout", buildRule.getOutput()); + assertThat(buildRule.getOutput(), containsString("tearDown called on Timeout")); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLFormatterWithCDATAOnSystemOut.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLFormatterWithCDATAOnSystemOut.java index 8d03a7287..64674a292 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLFormatterWithCDATAOnSystemOut.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLFormatterWithCDATAOnSystemOut.java @@ -26,7 +26,8 @@ import org.apache.tools.ant.util.FileUtils; import org.junit.Rule; import org.junit.Test; -import static org.junit.Assert.assertTrue; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; public class XMLFormatterWithCDATAOnSystemOut { @@ -66,8 +67,8 @@ public class XMLFormatterWithCDATAOnSystemOut { buildRule.executeTarget("run-junit"); File f = buildRule.getProject().resolveFile(REPORT); try (FileReader reader = new FileReader(f)) { - String content = FileUtils.readFully(reader); - assertTrue(content.contains("]]>")); + assertThat(FileUtils.readFully(reader), + containsString("]]>")); } finally { f.delete(); } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/net/FTPTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/net/FTPTest.java index a2dfe4f47..7628b272e 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/net/FTPTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/net/FTPTest.java @@ -17,8 +17,9 @@ */ package org.apache.tools.ant.taskdefs.optional.net; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.junit.Assume.assumeTrue; @@ -212,7 +213,7 @@ public class FTPTest { @Test public void testGetWithSelector() { buildRule.executeTarget("ftp-get-with-selector"); - assertContains("selectors are not supported in remote filesets", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("selectors are not supported in remote filesets")); FileSet fsDestination = buildRule.getProject().getReference("fileset-destination-without-selector"); DirectoryScanner dsDestination = fsDestination.getDirectoryScanner(buildRule.getProject()); dsDestination.scan(); diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java index 7b2d327c8..6ba512b82 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java @@ -24,13 +24,14 @@ import org.apache.tools.ant.types.FileSet; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import java.io.File; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; /** * Tests the examples of the <scriptdef> task. @@ -42,6 +43,9 @@ public class ScriptDefTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/taskdefs/optional/script/scriptdef.xml"); @@ -55,31 +59,24 @@ public class ScriptDefTest { FileSet fileset = p.getReference("testfileset"); File baseDir = fileset.getDir(p); String log = buildRule.getLog(); - assertTrue("Expecting attribute value printed", - log.contains("Attribute attr1 = test")); - - assertTrue("Expecting nested element value printed", - log.contains("Fileset basedir = " + baseDir.getAbsolutePath())); + assertThat("Expecting attribute value printed", log, + containsString("Attribute attr1 = test")); + assertThat("Expecting nested element value printed", log, + containsString("Fileset basedir = " + baseDir.getAbsolutePath())); } @Test public void testNoLang() { - try { - buildRule.executeTarget("nolang"); - fail("Absence of language attribute not detected"); - } catch (BuildException ex) { - assertContains("requires a language attribute", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("requires a language attribute"); + buildRule.executeTarget("nolang"); } @Test public void testNoName() { - try { - buildRule.executeTarget("noname"); - fail("Absence of name attribute not detected"); - } catch (BuildException ex) { - assertContains("scriptdef requires a name attribute", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("scriptdef requires a name attribute"); + buildRule.executeTarget("noname"); } @Test @@ -90,11 +87,10 @@ public class ScriptDefTest { FileSet fileset = p.getReference("testfileset"); File baseDir = fileset.getDir(p); String log = buildRule.getLog(); - assertTrue("Expecting attribute value to be printed", - log.contains("Attribute attr1 = test")); - - assertTrue("Expecting nested element value to be printed", - log.contains("Fileset basedir = " + baseDir.getAbsolutePath())); + assertThat("Expecting attribute value to be printed", log, + containsString("Attribute attr1 = test")); + assertThat("Expecting nested element value to be printed", log, + containsString("Fileset basedir = " + baseDir.getAbsolutePath())); } @Test @@ -105,39 +101,31 @@ public class ScriptDefTest { @Test public void testException() { - try { - buildRule.executeTarget("exception"); - fail("Should have thrown an exception in the script"); - } catch (BuildException ex) { - assertContains("TypeError", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("TypeError"); + buildRule.executeTarget("exception"); } @Test public void testDoubleDef() { buildRule.executeTarget("doubledef"); String log = buildRule.getLog(); - assertTrue("Task1 did not execute", log.contains("Task1")); - assertTrue("Task2 did not execute", log.contains("Task2")); + assertThat("Task1 did not execute", log, containsString("Task1")); + assertThat("Task2 did not execute", log, containsString("Task2")); } @Test public void testDoubleAttribute() { - try { - buildRule.executeTarget("doubleAttributeDef"); - fail("Should have detected duplicate attirbute definition"); - } catch (BuildException ex) { - assertContains("attr1 attribute more than once", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("attr1 attribute more than once"); + buildRule.executeTarget("doubleAttributeDef"); } @Test public void testProperty() { buildRule.executeTarget("property"); - // get the fileset and its basedir - String log = buildRule.getLog(); - assertTrue("Expecting property in attribute value replaced", - log.contains("Attribute value = test")); + assertThat("Expecting property in attribute value replaced", + buildRule.getLog(), containsString("Attribute value = test")); } @Test @@ -149,17 +137,11 @@ public class ScriptDefTest { @Test public void testUseSrcAndEncodingFailure() { + thrown.expect(BuildException.class); + thrown.expectMessage("expected but was "); final String readerEncoding = "ISO-8859-1"; buildRule.getProject().setProperty("useSrcAndEncoding.reader.encoding", readerEncoding); - try { - buildRule.executeTarget("useSrcAndEncoding"); - fail("should have failed with reader's encoding [" + readerEncoding + - "] different from the writer's encoding [" + - buildRule.getProject().getProperty("useSrcAndEncoding.encoding") + "]"); - } catch (BuildException e) { - assertTrue(e.getMessage().matches( - "expected but was ")); - } + buildRule.executeTarget("useSrcAndEncoding"); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/types/AddTypeTest.java b/src/tests/junit/org/apache/tools/ant/types/AddTypeTest.java index bebf627a1..88a5572c7 100644 --- a/src/tests/junit/org/apache/tools/ant/types/AddTypeTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/AddTypeTest.java @@ -26,15 +26,19 @@ import org.apache.tools.ant.taskdefs.condition.Condition; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static org.apache.tools.ant.AntAssert.assertContains; -import static org.junit.Assert.fail; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; public class AddTypeTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/types/addtype.xml"); @@ -63,71 +67,65 @@ public class AddTypeTest { @Test public void testNestedA() { buildRule.executeTarget("nested.a"); - assertContains("add A called", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("add A called")); } @Test public void testNestedB() { buildRule.executeTarget("nested.b"); - assertContains("add B called", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("add B called")); } @Test public void testNestedC() { buildRule.executeTarget("nested.c"); - assertContains("add C called", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("add C called")); } @Test public void testNestedAB() { - try { - buildRule.executeTarget("nested.ab"); - fail("Build exception expected: Should have got ambiguous"); - } catch (BuildException ex) { - assertContains("ambiguous", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("ambiguous"); + buildRule.executeTarget("nested.ab"); } @Test public void testConditionType() { buildRule.executeTarget("condition.type"); - assertContains("beforeafter", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("beforeafter")); } @Test public void testConditionTask() { buildRule.executeTarget("condition.task"); - assertContains("My Condition execution", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("My Condition execution")); } @Test public void testConditionConditionType() { buildRule.executeTarget("condition.condition.type"); - assertContains("My Condition eval", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("My Condition eval")); } @Test public void testConditionConditionTask() { - try { - buildRule.executeTarget("condition.condition.task"); - fail("Build exception expected: Task masking condition"); - } catch (BuildException ex) { - assertContains("doesn't support the nested", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("doesn't support the nested"); + buildRule.executeTarget("condition.condition.task"); } @Test public void testAddConfigured() { buildRule.executeTarget("myaddconfigured"); - assertContains("value is Value Setexecute: value is Value Set", - buildRule.getLog()); + assertThat(buildRule.getLog(), + containsString("value is Value Setexecute: value is Value Set")); } @Test public void testAddConfiguredValue() { buildRule.executeTarget("myaddconfiguredvalue"); - assertContains("value is Value Setexecute: value is Value Set", - buildRule.getLog()); + assertThat(buildRule.getLog(), + containsString("value is Value Setexecute: value is Value Set")); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/types/AssertionsTest.java b/src/tests/junit/org/apache/tools/ant/types/AssertionsTest.java index f96228f9b..c3768bf02 100644 --- a/src/tests/junit/org/apache/tools/ant/types/AssertionsTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/AssertionsTest.java @@ -22,9 +22,10 @@ import org.apache.tools.ant.BuildFileRule; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static org.apache.tools.ant.AntAssert.assertContains; -import static org.junit.Assert.fail; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; import static org.junit.Assume.assumeFalse; /** @@ -35,23 +36,22 @@ public class AssertionsTest { @Rule public BuildFileRule buildRule = new BuildFileRule(); + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/types/assertions.xml"); } - /** * runs a test and expects an assertion thrown in forked code * @param target String */ private void expectAssertion(String target) { - try { - buildRule.executeTarget(target); - fail("BuildException should have been thrown by assertion fail in task"); - } catch (BuildException ex) { - assertContains("assertion not thrown in " + target, "Java returned: 1", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Java returned: 1"); + buildRule.executeTarget(target); } @Test @@ -91,29 +91,25 @@ public class AssertionsTest { @Test public void testMultipleAssertions() { - try { - buildRule.executeTarget("test-multiple-assertions"); - fail("BuildException should have been thrown by assertion fail in task"); - } catch (BuildException ex) { - assertContains("multiple assertions rejected", "Only one assertion declaration is allowed", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("Only one assertion declaration is allowed"); + buildRule.executeTarget("test-multiple-assertions"); } @Test public void testReferenceAbuse() { - try { - buildRule.executeTarget("test-reference-abuse"); - fail("BuildException should have been thrown by reference abuse"); - } catch (BuildException ex) { - assertContains("reference abuse rejected", "You must not specify", ex.getMessage()); - } + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify"); + buildRule.executeTarget("test-reference-abuse"); } @Test public void testNofork() { - assumeFalse("ran Ant tests with -ea and this would fail spuriously", AssertionsTest.class.desiredAssertionStatus()); + assumeFalse("ran Ant tests with -ea and this would fail spuriously", + AssertionsTest.class.desiredAssertionStatus()); buildRule.executeTarget("test-nofork"); - assertContains("Assertion statements are currently ignored in non-forked mode", buildRule.getLog()); + assertThat(buildRule.getLog(), + containsString("Assertion statements are currently ignored in non-forked mode")); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/types/CommandlineJavaTest.java b/src/tests/junit/org/apache/tools/ant/types/CommandlineJavaTest.java index 3b137129e..596756b03 100644 --- a/src/tests/junit/org/apache/tools/ant/types/CommandlineJavaTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/CommandlineJavaTest.java @@ -18,18 +18,21 @@ package org.apache.tools.ant.types; - import org.apache.tools.ant.MagicNames; import org.apache.tools.ant.Project; import org.junit.After; import org.junit.Before; import org.junit.Test; +import java.io.File; + +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * JUnit testcases for org.apache.tools.ant.CommandlineJava @@ -93,8 +96,7 @@ public class CommandlineJavaTest { // assertEquals("with classpath", "java", s[0]); assertEquals("with classpath", "-Djava.compiler=NONE", s[1]); assertEquals("with classpath", "-classpath", s[2]); - assertTrue("build.xml contained", - s[3].contains("build.xml" + java.io.File.pathSeparator)); + assertThat("build.xml contained", s[3], containsString("build.xml" + File.pathSeparator)); assertTrue("ant.jar contained", s[3].endsWith("ant.jar")); assertEquals("with classpath", "junit.textui.TestRunner", s[4]); assertEquals("with classpath", diff --git a/src/tests/junit/org/apache/tools/ant/types/PathTest.java b/src/tests/junit/org/apache/tools/ant/types/PathTest.java index b75e9f506..46ac37e3c 100644 --- a/src/tests/junit/org/apache/tools/ant/types/PathTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/PathTest.java @@ -27,7 +27,9 @@ import org.apache.tools.ant.taskdefs.condition.Os; import org.junit.Before; import org.junit.Test; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -572,7 +574,7 @@ public class PathTest { assertEquals(0, p.list().length); } catch (BuildException x) { String m = x.toString(); - assertTrue(m, m.contains("circular")); + assertThat(m, m, containsString("circular")); } } diff --git a/src/tests/junit/org/apache/tools/ant/types/PolyTest.java b/src/tests/junit/org/apache/tools/ant/types/PolyTest.java index f0589f184..b42d577c5 100644 --- a/src/tests/junit/org/apache/tools/ant/types/PolyTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/PolyTest.java @@ -25,7 +25,8 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; public class PolyTest { @@ -40,25 +41,25 @@ public class PolyTest { @Test public void testFileSet() { buildRule.executeTarget("fileset"); - assertContains("types.FileSet", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("types.FileSet")); } @Test public void testFileSetAntType() { buildRule.executeTarget("fileset-ant-type"); - assertContains("types.PolyTest$MyFileSet", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("types.PolyTest$MyFileSet")); } @Test public void testPath() { buildRule.executeTarget("path"); - assertContains("types.Path", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("types.Path")); } @Test public void testPathAntType() { buildRule.executeTarget("path-ant-type"); - assertContains("types.PolyTest$MyPath", buildRule.getLog()); + assertThat(buildRule.getLog(), containsString("types.PolyTest$MyPath")); } public static class MyFileSet extends FileSet { diff --git a/src/tests/junit/org/apache/tools/ant/types/RedirectorElementTest.java b/src/tests/junit/org/apache/tools/ant/types/RedirectorElementTest.java index 0b10e3373..db1c4f8bb 100644 --- a/src/tests/junit/org/apache/tools/ant/types/RedirectorElementTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/RedirectorElementTest.java @@ -24,7 +24,9 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.both; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -73,9 +75,8 @@ public class RedirectorElementTest { @Test public void testLogInputString() { buildRule.executeTarget("testLogInputString"); - if (buildRule.getLog().contains("testLogInputString can-cat")) { - assertContains("Using input string", buildRule.getFullLog()); - } + assertThat(buildRule.getFullLog(), both(containsString("testLogInputString can-cat")) + .and(containsString("Using input string"))); } @Test 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 a75a66353..d1a8fe165 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 @@ -23,7 +23,8 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; /** @@ -45,7 +46,7 @@ public class ScriptSelectorTest { buildRule.executeTarget("testNolanguage"); fail("Absence of language attribute not detected"); } catch (BuildException ex) { - assertContains("script language must be specified", ex.getMessage()); + assertThat(ex.getMessage(), containsString("script language must be specified")); } } 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 f8c8e6c14..7d7b00ee0 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 @@ -46,11 +46,12 @@ import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; -import static org.apache.tools.ant.AntAssert.assertContains; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.junit.Assume.assumeNotNull; @@ -184,7 +185,8 @@ public class ModifiedSelectorTest { assertNotNull("'fs.full.value' must be set.", fsFullValue); assertNotEquals("'fs.full.value' must not be null.", "", fsFullValue); - assertTrue("'fs.full.value' must contain ant.bat.", fsFullValue.contains("ant.bat")); + assertThat("'fs.full.value' must contain ant.bat.", fsFullValue, + containsString("ant.bat")); assertNotNull("'fs.mod.value' must be set.", fsModValue); // must be empty according to the Mock* implementations @@ -569,7 +571,7 @@ public class ModifiedSelectorTest { public void testResourceSelectorSelresTrue() { BFT bft = new BFT(); bft.doTarget("modifiedselectortest-ResourceSelresTrue"); - assertContains("does not provide an InputStream", bft.getLog()); + assertThat(bft.getLog(), containsString("does not provide an InputStream")); bft.deleteCachefile(); } 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 f12f9aaa7..3aa855920 100644 --- a/src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java @@ -26,8 +26,10 @@ import java.util.Properties; import org.junit.Test; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; public class LayoutPreservingPropertiesTest { @@ -60,8 +62,8 @@ public class LayoutPreservingPropertiesTest { // and now make sure that the comments made it into the new file String s = readFile(tmp); - assertTrue("missing comment", s.contains("# a comment")); - assertTrue("missing comment", s.contains("! more comment")); + assertThat("missing comment", s, containsString(("# a comment"))); + assertThat("missing comment", s, containsString(("! more comment"))); } /** @@ -90,16 +92,15 @@ public class LayoutPreservingPropertiesTest { // and check that the resulting file looks okay String s = readFile(tmp); - assertTrue(s.contains("\\ prop\\ one\\ =\\ \\ leading and trailing" - + " spaces ")); - assertTrue(s.contains("prop\\ttwo=contains\\ttab")); - assertTrue(s.contains("prop\\nthree=contains\\nnewline")); - assertTrue(s.contains("prop\\rfour=contains\\rcarriage return")); - assertTrue(s.contains("prop\\\\six=contains\\\\backslash")); - assertTrue(s.contains("prop\\:seven=contains\\:colon")); - assertTrue(s.contains("prop\\=eight=contains\\=equals")); - assertTrue(s.contains("prop\\#nine=contains\\#hash")); - assertTrue(s.contains("prop\\!ten=contains\\!exclamation")); + assertThat(s, containsString("\\ prop\\ one\\ =\\ \\ leading and trailing spaces ")); + assertThat(s, containsString("prop\\ttwo=contains\\ttab")); + assertThat(s, containsString("prop\\nthree=contains\\nnewline")); + assertThat(s, containsString("prop\\rfour=contains\\rcarriage return")); + assertThat(s, containsString("prop\\\\six=contains\\\\backslash")); + assertThat(s, containsString("prop\\:seven=contains\\:colon")); + assertThat(s, containsString("prop\\=eight=contains\\=equals")); + assertThat(s, containsString("prop\\#nine=contains\\#hash")); + assertThat(s, containsString("prop\\!ten=contains\\!exclamation")); } /** @@ -126,13 +127,12 @@ public class LayoutPreservingPropertiesTest { // and check that the resulting file looks okay String s = readFile(tmp); - assertFalse(s.contains("\\ prop\\ one\\ =\\ \\ leading and" - + " trailing spaces ")); - assertTrue(s.contains("\\ prop\\ one\\ =new one")); - assertFalse(s.contains("prop\\ttwo=contains\\ttab")); - assertTrue(s.contains("prop\\ttwo=new two")); - assertFalse(s.contains("prop\\nthree=contains\\nnewline")); - assertTrue(s.contains("prop\\nthree=new three")); + assertThat(s, not(containsString("\\ prop\\ one\\ =\\ \\ leading and trailing spaces "))); + assertThat(s, containsString("\\ prop\\ one\\ =new one")); + assertThat(s, not(containsString("prop\\ttwo=contains\\ttab"))); + assertThat(s, containsString("prop\\ttwo=new two")); + assertThat(s, not(containsString("prop\\nthree=contains\\nnewline"))); + assertThat(s, containsString("prop\\nthree=new three")); } @Test @@ -172,13 +172,13 @@ public class LayoutPreservingPropertiesTest { // and check that the resulting file looks okay String s = readFile(tmp); - assertFalse("should have had no properties ", s.contains("prop.alpha")); - assertFalse("should have had no properties ", s.contains("prop.beta")); - assertFalse("should have had no properties ", s.contains("prop.gamma")); + assertThat("should have had no properties ", s, not(containsString(("prop.alpha")))); + assertThat("should have had no properties ", s, not(containsString(("prop.beta")))); + assertThat("should have had no properties ", s, not(containsString(("prop.gamma")))); - assertFalse("should have had no comments", s.contains("# a comment")); - assertFalse("should have had no comments", s.contains("! more comment")); - assertFalse("should have had no comments", s.contains("# now a line wrapping one")); + assertThat("should have had no comments", s, not(containsString(("# a comment")))); + assertThat("should have had no comments", s, not(containsString(("! more comment")))); + assertThat("should have had no comments", s, not(containsString(("# now a line wrapping one")))); } @Test @@ -198,9 +198,8 @@ public class LayoutPreservingPropertiesTest { // and check that the resulting file looks okay String s = readFile(tmp); - assertFalse("should not have had prop.beta", s.contains("prop.beta")); - assertTrue("should have had prop.beta's comment", - s.contains("! more comment")); + assertThat("should not have had prop.beta", s, not(containsString(("prop.beta")))); + assertThat("should have had prop.beta's comment", s, containsString("! more comment")); } @Test @@ -222,8 +221,8 @@ public class LayoutPreservingPropertiesTest { // and check that the resulting file looks okay String s = readFile(tmp); - assertFalse("should not have had prop.beta", s.contains("prop.beta")); - assertFalse("should not have had prop.beta's comment", s.contains("! more comment")); + assertThat("should not have had prop.beta", s, not(containsString(("prop.beta")))); + assertThat("should not have had prop.beta's comment", s, not(containsString(("! more comment")))); } @Test @@ -254,13 +253,12 @@ public class LayoutPreservingPropertiesTest { String s2 = readFile(tmp2); // check original is untouched - assertTrue("should have had 'simple'", s1.contains("simple")); - assertFalse("should not have had prop.new", s1.contains("prop.new")); + assertThat("should have had 'simple'", s1, containsString(("simple"))); + assertThat("should not have had prop.new", s1, not(containsString(("prop.new")))); // check clone has the changes - assertTrue("should have had 'a new value for beta'", - s2.contains("a new value for beta")); - assertTrue("should have had prop.new", s2.contains("prop.new")); + assertThat("should have had 'a new value for beta'", s2, containsString(("a new value for beta"))); + assertThat("should have had prop.new", s2, containsString(("prop.new"))); } @Test @@ -285,16 +283,16 @@ public class LayoutPreservingPropertiesTest { // and check that the resulting file looks okay String s = readFile(tmp); - assertTrue(s.contains("prop\\:seven=new value for seven")); - assertTrue(s.contains("prop\\=eight=new value for eight")); - assertTrue(s.contains("prop\\ eleven=new value for eleven")); - assertTrue(s.contains("alpha=new value for alpha")); - assertTrue(s.contains("beta=new value for beta")); + assertThat(s, containsString("prop\\:seven=new value for seven")); + assertThat(s, containsString("prop\\=eight=new value for eight")); + assertThat(s, containsString("prop\\ eleven=new value for eleven")); + assertThat(s, containsString("alpha=new value for alpha")); + assertThat(s, containsString("beta=new value for beta")); - assertFalse(s.contains("prop\\:seven=contains\\:colon")); - assertFalse(s.contains("prop\\=eight=contains\\=equals")); - assertFalse(s.contains("alpha:set with a colon")); - assertFalse(s.contains("beta set with a space")); + assertThat(s, not(containsString("prop\\:seven=contains\\:colon"))); + assertThat(s, not(containsString("prop\\=eight=contains\\=equals"))); + assertThat(s, not(containsString("alpha:set with a colon"))); + assertThat(s, not(containsString("beta set with a space"))); } private static String readFile(File f) throws IOException {