Browse Source

Deprecate AntAssert

master
Gintas Grigelionis 7 years ago
parent
commit
81c3e6e3dd
76 changed files with 1247 additions and 1537 deletions
  1. +6
    -0
      WHATSNEW
  2. +4
    -0
      src/tests/junit/org/apache/tools/ant/AntAssert.java
  3. +6
    -8
      src/tests/junit/org/apache/tools/ant/BuildFileRule.java
  4. +8
    -10
      src/tests/junit/org/apache/tools/ant/CaseTest.java
  5. +16
    -16
      src/tests/junit/org/apache/tools/ant/ExtendedTaskdefTest.java
  6. +27
    -41
      src/tests/junit/org/apache/tools/ant/IncludeTest.java
  7. +7
    -9
      src/tests/junit/org/apache/tools/ant/LoaderRefTest.java
  8. +6
    -5
      src/tests/junit/org/apache/tools/ant/LocationTest.java
  9. +9
    -22
      src/tests/junit/org/apache/tools/ant/ProjectHelperRepositoryTest.java
  10. +3
    -2
      src/tests/junit/org/apache/tools/ant/ProjectTest.java
  11. +3
    -2
      src/tests/junit/org/apache/tools/ant/PropertyFileCLITest.java
  12. +16
    -15
      src/tests/junit/org/apache/tools/ant/TaskContainerTest.java
  13. +3
    -2
      src/tests/junit/org/apache/tools/ant/filters/DynamicFilterTest.java
  14. +56
    -48
      src/tests/junit/org/apache/tools/ant/filters/TokenFilterTest.java
  15. +11
    -14
      src/tests/junit/org/apache/tools/ant/launch/LocatorTest.java
  16. +4
    -3
      src/tests/junit/org/apache/tools/ant/taskdefs/AbstractCvsTaskTest.java
  17. +3
    -2
      src/tests/junit/org/apache/tools/ant/taskdefs/AntStructureTest.java
  18. +9
    -8
      src/tests/junit/org/apache/tools/ant/taskdefs/AntTest.java
  19. +4
    -3
      src/tests/junit/org/apache/tools/ant/taskdefs/CallTargetTest.java
  20. +14
    -14
      src/tests/junit/org/apache/tools/ant/taskdefs/ConcatTest.java
  21. +28
    -37
      src/tests/junit/org/apache/tools/ant/taskdefs/CopyTest.java
  22. +4
    -3
      src/tests/junit/org/apache/tools/ant/taskdefs/EchoXMLTest.java
  23. +10
    -13
      src/tests/junit/org/apache/tools/ant/taskdefs/FixCrLfTest.java
  24. +50
    -49
      src/tests/junit/org/apache/tools/ant/taskdefs/GetTest.java
  25. +33
    -53
      src/tests/junit/org/apache/tools/ant/taskdefs/ImportTest.java
  26. +42
    -66
      src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java
  27. +41
    -73
      src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java
  28. +5
    -4
      src/tests/junit/org/apache/tools/ant/taskdefs/JavacTest.java
  29. +23
    -33
      src/tests/junit/org/apache/tools/ant/taskdefs/LoadFileTest.java
  30. +41
    -58
      src/tests/junit/org/apache/tools/ant/taskdefs/MacroDefTest.java
  31. +21
    -29
      src/tests/junit/org/apache/tools/ant/taskdefs/MakeUrlTest.java
  32. +47
    -35
      src/tests/junit/org/apache/tools/ant/taskdefs/ManifestClassPathTest.java
  33. +61
    -104
      src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java
  34. +10
    -15
      src/tests/junit/org/apache/tools/ant/taskdefs/NiceTest.java
  35. +10
    -14
      src/tests/junit/org/apache/tools/ant/taskdefs/PreSetDefTest.java
  36. +28
    -30
      src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java
  37. +32
    -31
      src/tests/junit/org/apache/tools/ant/taskdefs/RmicAdvancedTest.java
  38. +23
    -21
      src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java
  39. +4
    -5
      src/tests/junit/org/apache/tools/ant/taskdefs/StyleTest.java
  40. +19
    -22
      src/tests/junit/org/apache/tools/ant/taskdefs/SubAntTest.java
  41. +17
    -16
      src/tests/junit/org/apache/tools/ant/taskdefs/SyncTest.java
  42. +8
    -7
      src/tests/junit/org/apache/tools/ant/taskdefs/TaskdefTest.java
  43. +7
    -10
      src/tests/junit/org/apache/tools/ant/taskdefs/TouchTest.java
  44. +12
    -13
      src/tests/junit/org/apache/tools/ant/taskdefs/TypeAdapterTest.java
  45. +26
    -44
      src/tests/junit/org/apache/tools/ant/taskdefs/TypedefTest.java
  46. +31
    -36
      src/tests/junit/org/apache/tools/ant/taskdefs/ZipTest.java
  47. +7
    -7
      src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java
  48. +7
    -10
      src/tests/junit/org/apache/tools/ant/taskdefs/condition/IsFileSelectedTest.java
  49. +20
    -36
      src/tests/junit/org/apache/tools/ant/taskdefs/condition/IsReachableTest.java
  50. +7
    -8
      src/tests/junit/org/apache/tools/ant/taskdefs/condition/TypeFoundTest.java
  51. +9
    -8
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/ANTLRTest.java
  52. +25
    -40
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java
  53. +8
    -10
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/RpmTest.java
  54. +19
    -37
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/SchemaValidateTest.java
  55. +22
    -47
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/XmlValidateTest.java
  56. +8
    -11
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/XsltTest.java
  57. +16
    -21
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/depend/DependTest.java
  58. +18
    -14
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/image/ImageTest.java
  59. +8
    -8
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTest.java
  60. +8
    -7
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitReportTest.java
  61. +37
    -36
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTaskTest.java
  62. +19
    -18
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestListenerTest.java
  63. +3
    -2
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java
  64. +5
    -4
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/TearDownOnVmCrashTest.java
  65. +4
    -3
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLFormatterWithCDATAOnSystemOut.java
  66. +3
    -2
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/net/FTPTest.java
  67. +33
    -51
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java
  68. +22
    -24
      src/tests/junit/org/apache/tools/ant/types/AddTypeTest.java
  69. +19
    -23
      src/tests/junit/org/apache/tools/ant/types/AssertionsTest.java
  70. +7
    -5
      src/tests/junit/org/apache/tools/ant/types/CommandlineJavaTest.java
  71. +3
    -1
      src/tests/junit/org/apache/tools/ant/types/PathTest.java
  72. +6
    -5
      src/tests/junit/org/apache/tools/ant/types/PolyTest.java
  73. +5
    -4
      src/tests/junit/org/apache/tools/ant/types/RedirectorElementTest.java
  74. +3
    -2
      src/tests/junit/org/apache/tools/ant/types/optional/ScriptSelectorTest.java
  75. +5
    -3
      src/tests/junit/org/apache/tools/ant/types/selectors/ModifiedSelectorTest.java
  76. +43
    -45
      src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java

+ 6
- 0
WHATSNEW View File

@@ -13,6 +13,12 @@ Fixed bugs:
* The junit task when used with includeantruntime="no" was incorrectly * The junit task when used with includeantruntime="no" was incorrectly
printing a warning about multiple versions of ant detected in path 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 Changes from Ant 1.10.2 TO Ant 1.10.3
===================================== =====================================




+ 4
- 0
src/tests/junit/org/apache/tools/ant/AntAssert.java View File

@@ -23,7 +23,11 @@ import static org.junit.Assert.assertTrue;
/** /**
* Provides common assert functions for use across multiple tests, similar to the <tt>Assert</tt>s * Provides common assert functions for use across multiple tests, similar to the <tt>Assert</tt>s
* within JUnit. * within JUnit.
*
* @deprecated use assertThat() in JUnit 4.4+ in combination with containsString() matcher;
* for exception messages, use ExpectedException rule.
*/ */
@Deprecated
public class AntAssert { public class AntAssert {


/** /**


+ 6
- 8
src/tests/junit/org/apache/tools/ant/BuildFileRule.java View File

@@ -35,6 +35,9 @@ import org.junit.rules.ExternalResource;
* \@Rule * \@Rule
* public BuildFileRule rule = new BuildFileRule(); * public BuildFileRule rule = new BuildFileRule();
* *
* \@Rule
* public ExpectedException thrown = ExpectedException.none();
*
* \@Before * \@Before
* public void setUp() { * public void setUp() {
* rule.configureProject("my/and/file.xml"); * rule.configureProject("my/and/file.xml");
@@ -48,12 +51,9 @@ import org.junit.rules.ExternalResource;
* *
* \@Test * \@Test
* public void testException() { * 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 outputBuffer;
private StringBuffer errorBuffer; private StringBuffer errorBuffer;




/** /**
* Tidies up following a test execution. If the currently configured * Tidies up following a test execution. If the currently configured
* project has a <tt>tearDown</tt> target then this will automatically * project has a <tt>tearDown</tt> target then this will automatically


+ 8
- 10
src/tests/junit/org/apache/tools/ant/CaseTest.java View File

@@ -21,9 +21,7 @@ package org.apache.tools.ant;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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 * Simple tests of build file processing
@@ -33,6 +31,9 @@ public class CaseTest {
@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/core/case.xml"); buildRule.configureProject("src/etc/testcases/core/case.xml");
@@ -49,15 +50,12 @@ public class CaseTest {


/** /**
* Test whether the build file uses case when determining * Test whether the build file uses case when determining
* task names.
* task names. Task name should be case sensitive.
*/ */
@Test @Test
public void testTaskCase() { 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");
} }
} }

+ 16
- 16
src/tests/junit/org/apache/tools/ant/ExtendedTaskdefTest.java View File

@@ -17,14 +17,11 @@
*/ */
package org.apache.tools.ant; 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.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;


/** /**
* created 16-Mar-2006 12:25:12 * created 16-Mar-2006 12:25:12
@@ -35,6 +32,9 @@ public class ExtendedTaskdefTest {
@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/core/extended-taskdef.xml"); buildRule.configureProject("src/etc/testcases/core/extended-taskdef.xml");
@@ -45,24 +45,24 @@ public class ExtendedTaskdefTest {
buildRule.executeTarget("teardown"); buildRule.executeTarget("teardown");
} }


/**
* Exception should be thrown by a subclass
*/
@Test @Test
public void testRun() { 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 @Test
public void testRun2() { 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");
} }


} }

+ 27
- 41
src/tests/junit/org/apache/tools/ant/IncludeTest.java View File

@@ -18,13 +18,16 @@


package org.apache.tools.ant; 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.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;


/** /**
* Test the build file inclusion using XML entities. * Test the build file inclusion using XML entities.
@@ -35,6 +38,9 @@ public class IncludeTest {
@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Test @Test
public void test1() { public void test1() {
buildRule.configureProject("src/etc/testcases/core/include/basic/include.xml"); buildRule.configureProject("src/etc/testcases/core/include/basic/include.xml");
@@ -72,59 +78,39 @@ public class IncludeTest {


@Test @Test
public void testParseErrorInIncluding() { 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"); 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 @Test
public void testTaskErrorInIncluding() { 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"); 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 @Test
public void testParseErrorInIncluded() { 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 @Test
public void testTaskErrorInIncluded() { 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"); 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 @Test


+ 7
- 9
src/tests/junit/org/apache/tools/ant/LoaderRefTest.java View File

@@ -18,12 +18,10 @@


package org.apache.tools.ant; 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.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;


/** /**
*/ */
@@ -32,6 +30,9 @@ public class LoaderRefTest {
@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/core/loaderref/loaderref.xml"); buildRule.configureProject("src/etc/testcases/core/loaderref/loaderref.xml");
@@ -41,11 +42,8 @@ public class LoaderRefTest {
// override allowed on <available> // override allowed on <available>
@Test @Test
public void testBadRef() { 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");
} }
} }

+ 6
- 5
src/tests/junit/org/apache/tools/ant/LocationTest.java View File

@@ -25,10 +25,11 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;


import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThat;


public class LocationTest { public class LocationTest {


@@ -71,16 +72,16 @@ public class LocationTest {
public void testMacrodefWrappedTask() { public void testMacrodefWrappedTask() {
buildRule.executeTarget("testMacrodefWrappedTask"); buildRule.executeTarget("testMacrodefWrappedTask");
Echo e = buildRule.getProject().getReference("echo3"); 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 @Test
public void testPresetdefWrappedTask() { public void testPresetdefWrappedTask() {
buildRule.executeTarget("testPresetdefWrappedTask"); buildRule.executeTarget("testPresetdefWrappedTask");
Echo e = buildRule.getProject().getReference("echo4"); 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 { public static class EchoLocation extends Task {


+ 9
- 22
src/tests/junit/org/apache/tools/ant/ProjectHelperRepositoryTest.java View File

@@ -26,7 +26,6 @@ import org.apache.tools.ant.types.resources.StringResource;
import org.junit.Test; import org.junit.Test;


import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


/** /**
* Testing around the management of the project helpers * Testing around the management of the project helpers
@@ -74,32 +73,20 @@ public class ProjectHelperRepositoryTest {
assertTrue(helper instanceof ProjectHelper2); 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 // 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
} }
} }

+ 3
- 2
src/tests/junit/org/apache/tools/ant/ProjectTest.java View File

@@ -32,12 +32,13 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


@@ -268,7 +269,7 @@ public class ProjectTest {
// overriding target from imported buildfile is allowed // overriding target from imported buildfile is allowed
buildRule.configureProject("src/etc/testcases/core/duplicate-target2.xml"); buildRule.configureProject("src/etc/testcases/core/duplicate-target2.xml");
buildRule.executeTarget("once"); buildRule.executeTarget("once");
assertContains("once from buildfile", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("once from buildfile"));
} }


@Test @Test


+ 3
- 2
src/tests/junit/org/apache/tools/ant/PropertyFileCLITest.java View File

@@ -24,7 +24,8 @@ import java.io.FileWriter;
import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.FileUtils;
import org.junit.Test; 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 { public class PropertyFileCLITest {


@@ -54,7 +55,7 @@ public class PropertyFileCLITest {
"-l", log.getAbsolutePath() "-l", log.getAbsolutePath()
}, null, null); }, null, null);
String l = FileUtils.safeReadFully(fr = new FileReader(log)); String l = FileUtils.safeReadFully(fr = new FileReader(log));
assertContains("Hello, world", l);
assertThat(l, containsString("Hello, world"));
} finally { } finally {
FileUtils.close(fw); FileUtils.close(fw);
FileUtils.close(fr); FileUtils.close(fr);


+ 16
- 15
src/tests/junit/org/apache/tools/ant/TaskContainerTest.java View File

@@ -22,7 +22,8 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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 { public class TaskContainerTest {


@@ -37,30 +38,30 @@ public class TaskContainerTest {
@Test @Test
public void testPropertyExpansion() { public void testPropertyExpansion() {
buildRule.executeTarget("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 @Test
public void testTaskdef() { public void testTaskdef() {
buildRule.executeTarget("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 @Test
public void testCaseInsensitive() { public void testCaseInsensitive() {
buildRule.executeTarget("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")));
} }


} }

+ 3
- 2
src/tests/junit/org/apache/tools/ant/filters/DynamicFilterTest.java View File

@@ -28,7 +28,8 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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 { public class DynamicFilterTest {


@@ -46,7 +47,7 @@ public class DynamicFilterTest {
buildRule.executeTarget("dynamicfilter"); buildRule.executeTarget("dynamicfilter");
String content = FileUtilities.getFileContents( String content = FileUtilities.getFileContents(
new File(buildRule.getProject().getProperty("output") + "/dynamicfilter")); new File(buildRule.getProject().getProperty("output") + "/dynamicfilter"));
assertContains("hellO wOrld", content);
assertThat(content, containsString("hellO wOrld"));
} }


public static class CustomFilter implements ChainableReader { public static class CustomFilter implements ChainableReader {


+ 56
- 48
src/tests/junit/org/apache/tools/ant/filters/TokenFilterTest.java View File

@@ -18,9 +18,10 @@


package org.apache.tools.ant.filters; 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.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;


@@ -58,57 +59,57 @@ public class TokenFilterTest {
@Test @Test
public void testTrimignore() { public void testTrimignore() {
buildRule.executeTarget("trimignore"); buildRule.executeTarget("trimignore");
assertContains("Hello-World", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("Hello-World"));
} }


@Test @Test
public void testStringTokenizer() { public void testStringTokenizer() {
buildRule.executeTarget("stringtokenizer"); buildRule.executeTarget("stringtokenizer");
assertContains("#This#is#a#number#of#words#", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("#This#is#a#number#of#words#"));
} }


@Test @Test
public void testUnixLineOutput() throws IOException { public void testUnixLineOutput() throws IOException {
buildRule.executeTarget("unixlineoutput"); 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 @Test
public void testDosLineOutput() throws IOException { public void testDosLineOutput() throws IOException {


buildRule.executeTarget("doslineoutput"); 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 @Test
public void testFileTokenizer() throws IOException { public void testFileTokenizer() throws IOException {
buildRule.executeTarget("filetokenizer"); buildRule.executeTarget("filetokenizer");
String contents = getFileString(buildRule.getProject().getProperty("output") + "/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 @Test
public void testReplaceString() throws IOException { public void testReplaceString() throws IOException {
buildRule.executeTarget("replacestring"); 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 @Test
public void testReplaceStrings() { public void testReplaceStrings() {
buildRule.executeTarget("replacestrings"); buildRule.executeTarget("replacestrings");
assertContains("bar bar bar", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("bar bar bar"));
} }


@Test @Test
public void testContainsString() throws IOException { public void testContainsString() throws IOException {
buildRule.executeTarget("containsstring"); buildRule.executeTarget("containsstring");
String contents = getFileString(buildRule.getProject().getProperty("output") + "/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 @Test
@@ -120,12 +121,12 @@ public class TokenFilterTest {


buildRule.executeTarget("replaceregex"); buildRule.executeTarget("replaceregex");
String contents = getFileString(buildRule.getProject().getProperty("output") + "/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 @Test
@@ -135,15 +136,16 @@ public class TokenFilterTest {
getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp").contains("bye world")); getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp").contains("bye world"));


buildRule.executeTarget("filterreplaceregex"); 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 @Test
public void testHandleDollerMatch() throws IOException { public void testHandleDollerMatch() throws IOException {
buildRule.executeTarget("hasregex"); 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"); buildRule.executeTarget("dollermatch");
} }
@@ -154,7 +156,7 @@ public class TokenFilterTest {
String contents = getFileString(buildRule.getProject().getProperty("output") + "/trimfile"); String contents = getFileString(buildRule.getProject().getProperty("output") + "/trimfile");
assertTrue("no ws at start", contents.startsWith("This is th")); assertTrue("no ws at start", contents.startsWith("This is th"));
assertTrue("no ws at end", contents.endsWith("second line.")); assertTrue("no ws at end", contents.endsWith("second line."));
assertContains(" This is the second", contents);
assertThat(contents, containsString(" This is the second"));
} }


@Test @Test
@@ -163,21 +165,21 @@ public class TokenFilterTest {
String contents = getFileString(buildRule.getProject().getProperty("output") + "/trimfilebyline"); String contents = getFileString(buildRule.getProject().getProperty("output") + "/trimfilebyline");
assertFalse("no ws at start", contents.startsWith("This is th")); assertFalse("no ws at start", contents.startsWith("This is th"));
assertFalse("no ws at end", contents.endsWith("second line.")); 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 @Test
public void testFilterReplaceString() throws IOException { public void testFilterReplaceString() throws IOException {
buildRule.executeTarget("filterreplacestring"); 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 @Test
public void testFilterReplaceStrings() { public void testFilterReplaceStrings() {
buildRule.executeTarget("filterreplacestrings"); buildRule.executeTarget("filterreplacestrings");
assertContains("bar bar bar", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("bar bar bar"));
} }


@Test @Test
@@ -189,40 +191,42 @@ public class TokenFilterTest {


buildRule.executeTarget("containsregex"); buildRule.executeTarget("containsregex");
String contents = getFileString(buildRule.getProject().getProperty("output") + "/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 @Test
public void testFilterContainsRegex() throws IOException { public void testFilterContainsRegex() throws IOException {
buildRule.executeTarget("hasregex"); 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"); buildRule.executeTarget("filtercontainsregex");
String contents = getFileString(buildRule.getProject().getProperty("output") + "/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 @Test
public void testContainsRegex2() throws IOException { public void testContainsRegex2() throws IOException {
buildRule.executeTarget("hasregex"); 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"); 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 @Test
public void testDeleteCharacters() throws IOException { public void testDeleteCharacters() throws IOException {
buildRule.executeTarget("deletecharacters"); buildRule.executeTarget("deletecharacters");
String contents = getFileString(buildRule.getProject().getProperty("output") + "/deletechars"); 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 @Test
@@ -230,21 +234,25 @@ public class TokenFilterTest {
assumeTrue("Project does not have 'testScriptFilter' target", assumeTrue("Project does not have 'testScriptFilter' target",
buildRule.getProject().getTargets().contains("testScriptFilter")); buildRule.getProject().getTargets().contains("testScriptFilter"));
buildRule.executeTarget("scriptfilter"); buildRule.executeTarget("scriptfilter");
assertContains("HELLO WORLD", getFileString(buildRule.getProject().getProperty("output") + "/scriptfilter"));
assertThat(getFileString(buildRule.getProject().getProperty("output") + "/scriptfilter"),
containsString("HELLO WORLD"));


} }


@Test @Test
public void testScriptFilter2() throws IOException { 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"); buildRule.executeTarget("scriptfilter2");
assertContains("HELLO MOON", getFileString(buildRule.getProject().getProperty("output") + "/scriptfilter2"));
assertThat(getFileString(buildRule.getProject().getProperty("output") + "/scriptfilter2"),
containsString("HELLO MOON"));
} }


@Test @Test
public void testCustomTokenFilter() throws IOException { public void testCustomTokenFilter() throws IOException {
buildRule.executeTarget("customtokenfilter"); 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; Reader r = null;
try { try {
r = new FileReader(FILE_UTILS.resolveFile(buildRule.getProject().getBaseDir(),filename)); r = new FileReader(FILE_UTILS.resolveFile(buildRule.getProject().getBaseDir(),filename));
return FileUtils.readFully(r);
return FileUtils.readFully(r);
} }
finally { finally {
FileUtils.close(r); FileUtils.close(r);


+ 11
- 14
src/tests/junit/org/apache/tools/ant/launch/LocatorTest.java View File

@@ -22,11 +22,12 @@ import java.io.File;
import org.apache.tools.ant.taskdefs.condition.Os; import org.apache.tools.ant.taskdefs.condition.Os;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test; 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 */ /** Test the locator in the ant-launch JAR */
public class LocatorTest { public class LocatorTest {
@@ -36,6 +37,8 @@ public class LocatorTest {
private static final String SHARED_JAR_URI = "jar:file:" + LAUNCHER_JAR private static final String SHARED_JAR_URI = "jar:file:" + LAUNCHER_JAR
+ "!/org/apache/tools/ant/launch/Launcher.class"; + "!/org/apache/tools/ant/launch/Launcher.class";


@Rule
public ExpectedException thrown = ExpectedException.none();


@Before @Before
public void setUp() { public void setUp() {
@@ -136,25 +139,19 @@ public class LocatorTest {
*/ */
@Test @Test
public void testFileFromRemoteShare() { public void testFileFromRemoteShare() {
assumeTrue("not Windows", windows);
String resolved = Locator.fromJarURI(SHARED_JAR_URI); String resolved = Locator.fromJarURI(SHARED_JAR_URI);
File f = new File(resolved); File f = new File(resolved);
String path = f.getAbsolutePath(); String path = f.getAbsolutePath();
if (windows) {
assertEquals(0, path.indexOf("\\\\"));
}
assertEquals(0, path.indexOf("\\\\"));
} }


@Test @Test
public void testHttpURI() { public void testHttpURI() {
String url = "http://ant.apache.org"; 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 @Test


+ 4
- 3
src/tests/junit/org/apache/tools/ant/taskdefs/AbstractCvsTaskTest.java View File

@@ -25,8 +25,9 @@ import org.junit.Test;


import java.io.File; 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.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;


/** /**
@@ -57,7 +58,7 @@ public class AbstractCvsTaskTest {
File f = new File(buildRule.getProject().getProperty("output") + "/src/Makefile"); File f = new File(buildRule.getProject().getProperty("output") + "/src/Makefile");
assertFalse("starting empty", f.exists()); assertFalse("starting empty", f.exists());
buildRule.executeTarget("package-attribute"); buildRule.executeTarget("package-attribute");
assertContains("U src/Makefile", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("U src/Makefile"));
assertTrue("now it is there", f.exists()); assertTrue("now it is there", f.exists());
} }


@@ -66,7 +67,7 @@ public class AbstractCvsTaskTest {
File f = new File(buildRule.getProject().getProperty("output") + "/src/Makefile"); File f = new File(buildRule.getProject().getProperty("output") + "/src/Makefile");
assertFalse("starting empty", f.exists()); assertFalse("starting empty", f.exists());
buildRule.executeTarget("tag-attribute"); buildRule.executeTarget("tag-attribute");
assertContains("OPENBSD_5_3", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("OPENBSD_5_3"));
assertTrue("now it is there", f.exists()); assertTrue("now it is there", f.exists());
} }
} }

+ 3
- 2
src/tests/junit/org/apache/tools/ant/taskdefs/AntStructureTest.java View File

@@ -29,9 +29,10 @@ import org.junit.Test;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.Hashtable; 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.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


@@ -70,7 +71,7 @@ public class AntStructureTest {
// the test has likely been loaded via a different classloader // the test has likely been loaded via a different classloader
// than this class. Therefore we make the printer assert its // than this class. Therefore we make the printer assert its
// state and only check for the tail invocation. // 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 { public static class MyPrinter implements AntStructure.StructurePrinter {


+ 9
- 8
src/tests/junit/org/apache/tools/ant/taskdefs/AntTest.java View File

@@ -34,12 +34,13 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


@@ -290,7 +291,7 @@ public class AntTest {
buildRule.getProject().setUserProperty("test", "7"); buildRule.getProject().setUserProperty("test", "7");
buildRule.executeTarget("test-property-override-inheritall-start"); 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 @Test
@@ -298,28 +299,28 @@ public class AntTest {
buildRule.getProject().setUserProperty("test", "7"); buildRule.getProject().setUserProperty("test", "7");
buildRule.executeTarget("test-property-override-no-inheritall-start"); 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 @Test
public void testOverrideWinsInheritAll() { public void testOverrideWinsInheritAll() {
buildRule.executeTarget("test-property-override-inheritall-start"); 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 @Test
public void testOverrideWinsNoInheritAll() { public void testOverrideWinsNoInheritAll() {
buildRule.executeTarget("test-property-override-no-inheritall-start"); 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 @Test
public void testPropertySet() { public void testPropertySet() {
buildRule.executeTarget("test-propertyset"); 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 @Test


+ 4
- 3
src/tests/junit/org/apache/tools/ant/taskdefs/CallTargetTest.java View File

@@ -26,8 +26,9 @@ import org.junit.Test;


import java.util.Vector; 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.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


/** /**
@@ -47,7 +48,7 @@ public class CallTargetTest {
@Test @Test
public void testInheritRefFileSet() { public void testInheritRefFileSet() {
buildRule.executeTarget("testinheritreffileset"); buildRule.executeTarget("testinheritreffileset");
assertContains("calltarget.xml", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("calltarget.xml"));
} }


// see bugrep 21724 (references not passing through with antcall) // see bugrep 21724 (references not passing through with antcall)
@@ -64,7 +65,7 @@ public class CallTargetTest {
v.add("call-multi"); v.add("call-multi");
v.add("call-multi"); v.add("call-multi");
buildRule.getProject().executeTargets(v); buildRule.getProject().executeTargets(v);
assertContains("multi is SETmulti is SET", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("multi is SETmulti is SET"));
} }


@Test @Test


+ 14
- 14
src/tests/junit/org/apache/tools/ant/taskdefs/ConcatTest.java View File

@@ -29,9 +29,10 @@ import org.junit.Test;
import java.io.File; import java.io.File;
import java.io.IOException; 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.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


@@ -150,7 +151,7 @@ public class ConcatTest {
String filename = "src/etc/testcases/taskdefs/thisfiledoesnotexist" String filename = "src/etc/testcases/taskdefs/thisfiledoesnotexist"
.replace('/', File.separatorChar); .replace('/', File.separatorChar);
buildRule.executeTarget("test6"); buildRule.executeTarget("test6");
assertContains(filename + " does not exist", buildRule.getLog());
assertThat(buildRule.getLog(), containsString(filename + " does not exist"));
} }


@Test @Test
@@ -200,7 +201,7 @@ public class ConcatTest {
@Test @Test
public void testFilter() { public void testFilter() {
buildRule.executeTarget("testfilter"); buildRule.executeTarget("testfilter");
assertTrue(buildRule.getLog().contains("REPLACED"));
assertThat(buildRule.getLog(), containsString("REPLACED"));
} }


@Test @Test
@@ -252,7 +253,7 @@ public class ConcatTest {
@Test @Test
public void testfilterinline() { public void testfilterinline() {
buildRule.executeTarget("testfilterinline"); buildRule.executeTarget("testfilterinline");
assertTrue(buildRule.getLog().contains("REPLACED"));
assertThat(buildRule.getLog(), containsString("REPLACED"));
} }


/** /**
@@ -261,8 +262,8 @@ public class ConcatTest {
@Test @Test
public void testmultireader() { public void testmultireader() {
buildRule.executeTarget("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 * Check if fixlastline works
@@ -270,8 +271,8 @@ public class ConcatTest {
@Test @Test
public void testfixlastline() throws IOException { public void testfixlastline() throws IOException {
buildRule.executeTarget("testfixlastline"); 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 @Test
public void testfixlastlineeol() throws IOException { public void testfixlastlineeol() throws IOException {
buildRule.executeTarget("testfixlastlineeol"); 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 @Test
@@ -300,10 +302,8 @@ public class ConcatTest {
throws IOException { throws IOException {
buildRule.executeTarget(target); buildRule.executeTarget(target);
String content = FileUtilities.getFileContents(buildRule.getProject(), filename); 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));
} }


} }

+ 28
- 37
src/tests/junit/org/apache/tools/ant/taskdefs/CopyTest.java View File

@@ -27,18 +27,21 @@ import org.junit.Before;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;


import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; 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.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;


/** /**
@@ -50,6 +53,8 @@ public class CopyTest {
@Rule @Rule
public final BuildFileRule buildRule = new BuildFileRule(); public final BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();


@Before @Before
public void setUp() { public void setUp() {
@@ -61,18 +66,14 @@ public class CopyTest {
public void test1() { public void test1() {
buildRule.executeTarget("test1"); buildRule.executeTarget("test1");
File f = new File(buildRule.getProject().getProperty("output"), "copytest1.tmp"); File f = new File(buildRule.getProject().getProperty("output"), "copytest1.tmp");
if (!f.exists()) {
fail("Copy failed");
}
assertTrue("Copy failed", f.exists());
} }


@Test @Test
public void test2() { public void test2() {
buildRule.executeTarget("test2"); buildRule.executeTarget("test2");
File f = new File(buildRule.getProject().getProperty("output"), "copytest1dir/copy.xml"); File f = new File(buildRule.getProject().getProperty("output"), "copytest1dir/copy.xml");
if (!f.exists()) {
fail("Copy failed");
}
assertTrue("Copy failed", f.exists());
} }


@Test @Test
@@ -100,19 +101,18 @@ public class CopyTest {
// file time checks for java1.2+ // file time checks for java1.2+
assertEquals(file3a.lastModified(), file3.lastModified()); assertEquals(file3a.lastModified(), file3.lastModified());
assertTrue(file3c.lastModified() < file3a.lastModified()); assertTrue(file3c.lastModified() < file3a.lastModified());

} }


@Test @Test
public void testFilterTest() { public void testFilterTest() {
buildRule.executeTarget("filtertest"); buildRule.executeTarget("filtertest");
assertFalse(buildRule.getLog().contains("loop in tokens"));
assertThat(buildRule.getLog(), not(containsString("loop in tokens")));
} }


@Test @Test
public void testInfiniteFilter() { public void testInfiniteFilter() {
buildRule.executeTarget("infinitetest"); buildRule.executeTarget("infinitetest");
assertContains("loop in tokens", buildRule.getOutput());
assertThat(buildRule.getOutput(), containsString("loop in tokens"));
} }


@Test @Test
@@ -128,7 +128,7 @@ public class CopyTest {
public void testFilterChain() throws IOException { public void testFilterChain() throws IOException {
buildRule.executeTarget("testFilterChain"); buildRule.executeTarget("testFilterChain");
File tmp = new File(buildRule.getProject().getProperty("output"), "copy.filterchain.tmp"); 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()); assertTrue(tmp.exists());
assertEquals(FileUtilities.getFileContents(tmp), FileUtilities.getFileContents(check)); assertEquals(FileUtilities.getFileContents(tmp), FileUtilities.getFileContents(check));
} }
@@ -136,16 +136,16 @@ public class CopyTest {
@Test @Test
public void testSingleFileFileset() { public void testSingleFileFileset() {
buildRule.executeTarget("test_single_file_fileset"); 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()); assertTrue(file.exists());
} }


@Test @Test
public void testSingleFilePath() { public void testSingleFilePath() {
buildRule.executeTarget("test_single_file_path"); 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()); assertTrue(file.exists());
} }


@@ -160,34 +160,27 @@ public class CopyTest {
@Test @Test
public void testMissingFileIgnore() { public void testMissingFileIgnore() {
buildRule.executeTarget("testMissingFileIgnore"); buildRule.executeTarget("testMissingFileIgnore");
assertContains("Warning: Could not find file", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("Warning: Could not find file"));
} }


@Test @Test
public void testMissingFileBail() { 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 @Test
public void testMissingDirIgnore() { public void testMissingDirIgnore() {
buildRule.executeTarget("testMissingDirIgnore"); buildRule.executeTarget("testMissingDirIgnore");
assertContains("Warning: ", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("Warning: "));
} }


@Test @Test
public void testMissingDirBail() { 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 @Test
@@ -218,11 +211,9 @@ public class CopyTest {
buildRule.executeTarget("testFileResourceWithFilter"); buildRule.executeTarget("testFileResourceWithFilter");
File file1 = new File(buildRule.getProject().getProperty("to.dir") + "/fileNR.txt"); File file1 = new File(buildRule.getProject().getProperty("to.dir") + "/fileNR.txt");
assertTrue(file1.exists()); 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) { } catch (IOException e) {
// no-op: not a real business error // no-op: not a real business error
} }


+ 4
- 3
src/tests/junit/org/apache/tools/ant/taskdefs/EchoXMLTest.java View File

@@ -25,7 +25,8 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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; import static org.junit.Assert.fail;


public class EchoXMLTest { public class EchoXMLTest {
@@ -54,7 +55,7 @@ public class EchoXMLTest {
buildRule.executeTarget("testFail"); buildRule.executeTarget("testFail");
fail("BuildException expected: must fail"); fail("BuildException expected: must fail");
} catch (BuildException ex) { } catch (BuildException ex) {
assertContains("${foo}=bar", ex.getMessage());
assertThat(ex.getMessage(), containsString("${foo}=bar"));
} }
} }


@@ -64,7 +65,7 @@ public class EchoXMLTest {
buildRule.executeTarget("testEmpty"); buildRule.executeTarget("testEmpty");
fail("BuildException expected: must fail"); fail("BuildException expected: must fail");
} catch (BuildException ex) { } catch (BuildException ex) {
assertContains("No nested XML specified", ex.getMessage());
assertThat(ex.getMessage(), containsString("No nested XML specified"));
} }
} }




+ 10
- 13
src/tests/junit/org/apache/tools/ant/taskdefs/FixCrLfTest.java View File

@@ -31,10 +31,10 @@ import org.apache.tools.ant.BuildFileRule;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertTrue;


/** /**
*/ */
@@ -43,6 +43,9 @@ public class FixCrLfTest {
@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/taskdefs/fixcrlf/build.xml"); buildRule.configureProject("src/etc/testcases/taskdefs/fixcrlf/build.xml");
@@ -150,12 +153,9 @@ public class FixCrLfTest {


@Test @Test
public void testFixFileExclusive() { 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? // not used, but public so theoretically must remain for BC?
@Deprecated @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)); try (InputStream inExpect = new BufferedInputStream(new FileInputStream(expect));
InputStream inResult = new BufferedInputStream(new FileInputStream(result))) { InputStream inResult = new BufferedInputStream(new FileInputStream(result))) {


+ 50
- 49
src/tests/junit/org/apache/tools/ant/taskdefs/GetTest.java View File

@@ -24,11 +24,13 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThat;


/** /**
*/ */
@@ -37,6 +39,9 @@ public class GetTest {
@Rule @Rule
public final BuildFileRule buildRule = new BuildFileRule(); public final BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/taskdefs/get.xml"); buildRule.configureProject("src/etc/testcases/taskdefs/get.xml");
@@ -47,54 +52,49 @@ public class GetTest {
buildRule.executeTarget("cleanup"); buildRule.executeTarget("cleanup");
} }


@Test
/**
* Fail due to missing required argument
*/
@Test(expected = BuildException.class)
public void test1() { 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() { 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() { 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() { 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() { 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 @Test
@@ -102,15 +102,17 @@ public class GetTest {
buildRule.executeTarget("test6"); buildRule.executeTarget("test6");
} }


/**
* Fail due to null or empty userAgent argument
*/
@Test @Test
public void test7() { public void test7() {
thrown.expect(BuildException.class);
try { try {
buildRule.executeTarget("test7"); 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 @Test
public void testTwoHeadersAreAddedOK() { public void testTwoHeadersAreAddedOK() {
buildRule.executeTarget("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 @Test
public void testEmptyHeadersAreNeverAdded() { public void testEmptyHeadersAreNeverAdded() {
buildRule.executeTarget("testEmptyHeadersAreNeverAdded"); buildRule.executeTarget("testEmptyHeadersAreNeverAdded");
assertNotContains("Adding header", buildRule.getLog());
assertThat(buildRule.getLog(), not(containsString("Adding header")));
} }


@Test @Test
public void testThatWhenMoreThanOneHeaderHaveSameNameOnlyLastOneIsAdded() { public void testThatWhenMoreThanOneHeaderHaveSameNameOnlyLastOneIsAdded() {
buildRule.executeTarget("testThatWhenMoreThanOneHeaderHaveSameNameOnlyLastOneIsAdded"); buildRule.executeTarget("testThatWhenMoreThanOneHeaderHaveSameNameOnlyLastOneIsAdded");
String log = buildRule.getLog(); String log = buildRule.getLog();
assertContains("Adding header 'header1'", log);
assertThat(log, containsString("Adding header 'header1'"));


int actualHeaderCount = log.split("Adding header ").length - 1; int actualHeaderCount = log.split("Adding header ").length - 1;


@@ -152,7 +153,7 @@ public class GetTest {
@Test @Test
public void testHeaderSpaceTrimmed() { public void testHeaderSpaceTrimmed() {
buildRule.executeTarget("testHeaderSpaceTrimmed"); buildRule.executeTarget("testHeaderSpaceTrimmed");
assertContains("Adding header 'header1'", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("Adding header 'header1'"));
} }


} }

+ 33
- 53
src/tests/junit/org/apache/tools/ant/taskdefs/ImportTest.java View File

@@ -18,33 +18,36 @@


package org.apache.tools.ant.taskdefs; 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.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; 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 static org.junit.Assume.assumeTrue;


import java.io.File; import java.io.File;
import java.io.IOException;


import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileRule; import org.apache.tools.ant.BuildFileRule;
import org.apache.tools.ant.Location;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;


public class ImportTest { public class ImportTest {


@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Test @Test
public void testSimpleImport() { public void testSimpleImport() {
buildRule.configureProject("src/etc/testcases/taskdefs/import/import.xml"); 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 @Test
@@ -58,9 +61,9 @@ public class ImportTest {
@Test @Test
public void testSerial() { public void testSerial() {
buildRule.configureProject("src/etc/testcases/taskdefs/import/subdir/serial.xml"); 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 // allow this as imported in targets are only tested when a target is run
@@ -83,15 +86,10 @@ public class ImportTest {


@Test @Test
public void testImportInTargetNotAllowed() { 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 @Test
@@ -105,31 +103,18 @@ public class ImportTest {


@Test @Test
public void testImportSameTargets() { 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 @Test
public void testImportError() { 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 @Test
@@ -140,21 +125,15 @@ public class ImportTest {
} }
assumeTrue("Current system does not support Symlinks", new File(ln).exists()); assumeTrue("Current system does not support Symlinks", new File(ln).exists());
String symlink = "src/etc/testcases/taskdefs/import/symlinks/d3b"; 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 { 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 { } finally {
symlinkFile.delete(); symlinkFile.delete();
} }
@@ -163,7 +142,8 @@ public class ImportTest {
@Test @Test
public void testTargetFirst() { public void testTargetFirst() {
buildRule.configureProject("src/etc/testcases/taskdefs/import/importtargetfirst.xml"); 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 @Test


+ 42
- 66
src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java View File

@@ -24,7 +24,6 @@ import java.io.FileReader;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.IOException; import java.io.IOException;
import java.io.Reader;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; 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.BuildFileRule;
import org.apache.tools.ant.FileUtilities; import org.apache.tools.ant.FileUtilities;
import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.FileUtils;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;


/** /**
@@ -52,11 +52,11 @@ public class JarTest {
@Rule @Rule
public final BuildFileRule buildRule = new BuildFileRule(); public final BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

private static String tempJar = "tmp.jar"; private static String tempJar = "tmp.jar";
private static String tempDir = "jartmp/"; private static String tempDir = "jartmp/";
private Reader r1;
private Reader r2;



@Before @Before
public void setUp() { public void setUp() {
@@ -64,28 +64,13 @@ public class JarTest {
buildRule.executeTarget("setUp"); 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 * Expected failure due to required argument not specified
*/ */
@Test(expected = BuildException.class) @Test(expected = BuildException.class)
public void test1() { public void test1() {
buildRule.executeTarget("test1"); buildRule.executeTarget("test1");
// TODO assert exception message
} }


/** /**
@@ -114,7 +99,6 @@ public class JarTest {
public void test4() { public void test4() {
buildRule.executeTarget("test4"); buildRule.executeTarget("test4");
File jarFile = new File(getOutputDir(), tempJar); File jarFile = new File(getOutputDir(), tempJar);

assertTrue(jarFile.exists()); assertTrue(jarFile.exists());
} }


@@ -154,14 +138,12 @@ public class JarTest {


@Test @Test
public void testRecreateWithoutUpdateNewerFile() { public void testRecreateWithoutUpdateNewerFile() {
testRecreate("testRecreateNewerFileSetup",
"testRecreateWithoutUpdateNewerFile");
testRecreate("testRecreateNewerFileSetup", "testRecreateWithoutUpdateNewerFile");
} }


@Test @Test
public void testRecreateWithUpdateNewerFile() { public void testRecreateWithUpdateNewerFile() {
testRecreate("testRecreateNewerFileSetup",
"testRecreateWithUpdateNewerFile");
testRecreate("testRecreateNewerFileSetup", "testRecreateWithUpdateNewerFile");
} }


private void testRecreate(String firstTarget, String secondTarget) { private void testRecreate(String firstTarget, String secondTarget) {
@@ -177,22 +159,24 @@ public class JarTest {
long jarModifiedDate = jarFile.lastModified(); long jarModifiedDate = jarFile.lastModified();
buildRule.executeTarget(secondTarget); buildRule.executeTarget(secondTarget);
jarFile = new File(getOutputDir(), tempJar); 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 @Test
public void testManifestStaysIntact()
throws IOException, ManifestException {
public void testManifestStaysIntact() throws IOException, ManifestException {
buildRule.executeTarget("testManifestStaysIntact"); 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); assertEquals(mf1, mf2);
} }


@@ -218,26 +202,22 @@ public class JarTest {


@Test @Test
public void testRecreateZipfilesetWithoutUpdateAdditionalFiles() { public void testRecreateZipfilesetWithoutUpdateAdditionalFiles() {
testRecreate("test4",
"testRecreateZipfilesetWithoutUpdateAdditionalFiles");
testRecreate("test4", "testRecreateZipfilesetWithoutUpdateAdditionalFiles");
} }


@Test @Test
public void testRecreateZipfilesetWithUpdateAdditionalFiles() { public void testRecreateZipfilesetWithUpdateAdditionalFiles() {
testRecreate("test4",
"testRecreateZipfilesetWithUpdateAdditionalFiles");
testRecreate("test4", "testRecreateZipfilesetWithUpdateAdditionalFiles");
} }


@Test @Test
public void testRecreateZipfilesetWithoutUpdateNewerFile() { public void testRecreateZipfilesetWithoutUpdateNewerFile() {
testRecreate("testRecreateNewerFileSetup",
"testRecreateZipfilesetWithoutUpdateNewerFile");
testRecreate("testRecreateNewerFileSetup", "testRecreateZipfilesetWithoutUpdateNewerFile");
} }


@Test @Test
public void testRecreateZipfilesetWithUpdateNewerFile() { public void testRecreateZipfilesetWithUpdateNewerFile() {
testRecreate("testRecreateNewerFileSetup",
"testRecreateZipfilesetWithUpdateNewerFile");
testRecreate("testRecreateNewerFileSetup", "testRecreateZipfilesetWithUpdateNewerFile");
} }


@Test @Test
@@ -306,9 +286,8 @@ public class JarTest {
} }
@Test @Test
public void testManifestOnlyJar() { public void testManifestOnlyJar() {

buildRule.executeTarget("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"); File manifestFile = new File(getOutputDir(), tempDir + "META-INF" + File.separator + "MANIFEST.MF");
assertTrue(manifestFile.exists()); assertTrue(manifestFile.exists());
} }
@@ -320,44 +299,41 @@ public class JarTest {


@Test @Test
public void testNoVersionInfoFail() { 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 @Test
public void testNoVersionInfoIgnore() { public void testNoVersionInfoIgnore() {
buildRule.executeTarget("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 @Test
public void testNoVersionInfoWarn() { public void testNoVersionInfoWarn() {
buildRule.executeTarget("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 @Test
public void testNoVersionInfoNoStrict() { public void testNoVersionInfoNoStrict() {
buildRule.executeTarget("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 @Test
public void testHasVersionInfo() { public void testHasVersionInfo() {
buildRule.executeTarget("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.")));
} }


} }

+ 41
- 73
src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java View File

@@ -38,13 +38,14 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.internal.AssumptionViolatedException; 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.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;


/** /**
@@ -55,6 +56,9 @@ public class JavaTest {
@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

private static final int TIME_TO_WAIT = 1; private static final int TIME_TO_WAIT = 1;
// wait 1 second extra to allow for java to start ... // wait 1 second extra to allow for java to start ...
// this time was OK on a Win NT machine and on nagoya // this time was OK on a Win NT machine and on nagoya
@@ -87,62 +91,44 @@ public class JavaTest {


@Test @Test
public void testNoJarNoClassname() { 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 @Test
public void testJarNoFork() { 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 @Test
public void testJarAndClassName() { 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 @Test
public void testClassnameAndJar() { 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 @Test
public void testJarAndModule() { 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 @Test
public void testModuleAndJar() { 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 @Test
@@ -214,8 +200,6 @@ public class JavaTest {
buildRule.executeTarget("testRun"); buildRule.executeTarget("testRun");
} }




/** this test fails but we ignore the return value; /** this test fails but we ignore the return value;
* we verify that failure only matters when failonerror is set * we verify that failure only matters when failonerror is set
*/ */
@@ -228,54 +212,42 @@ public class JavaTest {
@Test @Test
public void testRunFailFoe() { public void testRunFailFoe() {
assumeTrue("Fatal tests have not been set to run", runFatalTests); 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 @Test
public void testRunFailFoeFork() { 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 @Test
public void testExcepting() { public void testExcepting() {
buildRule.executeTarget("testExcepting"); buildRule.executeTarget("testExcepting");
assertContains("Exception raised inside called program", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("Exception raised inside called program"));
} }


@Test @Test
public void testExceptingFork() { public void testExceptingFork() {
buildRule.executeTarget("testExceptingFork"); buildRule.executeTarget("testExceptingFork");
assertContains("Java Result:", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("Java Result:"));
} }


@Test @Test
public void testExceptingFoe() { 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 @Test
public void testExceptingFoeFork() { 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 @Test
@@ -304,12 +276,9 @@ public class JavaTest {


@Test @Test
public void testRunFailWithFailOnError() { 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 @Test
@@ -329,7 +298,6 @@ public class JavaTest {


Thread.sleep(TIME_TO_WAIT * 1000 + SECURITY_MARGIN); Thread.sleep(TIME_TO_WAIT * 1000 + SECURITY_MARGIN);



// let's be nice with the next generation of developers // let's be nice with the next generation of developers
if (!logFile.exists()) { if (!logFile.exists()) {
System.out.println("suggestion: increase the constant" System.out.println("suggestion: increase the constant"


+ 5
- 4
src/tests/junit/org/apache/tools/ant/taskdefs/JavacTest.java View File

@@ -28,12 +28,14 @@ import org.apache.tools.ant.taskdefs.compilers.JavacExternal;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;


import static org.apache.tools.ant.AntAssert.assertContains;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.Path;

import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;


/** /**
@@ -65,13 +67,12 @@ public class JavacTest {


javac.setFork(true); javac.setFork(true);
assertNotNull("normal fork", javac.getJavacExecutable()); 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"); project.setProperty("build.compiler", "extJavac");
javac.setFork(false); javac.setFork(false);
assertNotNull("fork via property", javac.getJavacExecutable()); 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"); project.setProperty("build.compiler", "whatever");
assertNull("no fork and not extJavac means no executable", assertNull("no fork and not extJavac means no executable",


+ 23
- 33
src/tests/junit/org/apache/tools/ant/taskdefs/LoadFileTest.java View File

@@ -23,12 +23,13 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThat;


/** /**
* Test the load file task * Test the load file task
@@ -38,6 +39,9 @@ public class LoadFileTest {
@Rule @Rule
public final BuildFileRule buildRule = new BuildFileRule(); public final BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/taskdefs/loadfile.xml"); 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() { 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() { 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 @Test
public void testNoSourcefilefound() { 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 @Test
public void testLoadAFile() throws BuildException { public void testLoadAFile() throws BuildException {
buildRule.executeTarget("testLoadAFile"); 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 @Test
public void testEvalProps() throws BuildException { public void testEvalProps() throws BuildException {
buildRule.executeTarget("testEvalProps"); 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 @Test
public void testFilterChain() throws BuildException { public void testFilterChain() throws BuildException {
buildRule.executeTarget("testFilterChain"); buildRule.executeTarget("testFilterChain");
if (!buildRule.getProject().getProperty("testFilterChain").contains("World!")) {
fail("Filter Chain broken");
}
assertThat("Filter Chain broken",
buildRule.getProject().getProperty("testFilterChain"), containsString("World!"));
} }


/** /**


+ 41
- 58
src/tests/junit/org/apache/tools/ant/taskdefs/MacroDefTest.java View File

@@ -23,10 +23,12 @@ import org.apache.tools.ant.BuildFileRule;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThat;


/** /**
*/ */
@@ -35,6 +37,9 @@ public class MacroDefTest {
@Rule @Rule
public final BuildFileRule buildRule = new BuildFileRule(); public final BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/taskdefs/macrodef.xml"); buildRule.configureProject("src/etc/testcases/taskdefs/macrodef.xml");
@@ -52,24 +57,16 @@ public class MacroDefTest {
assertEquals("Inner Text", buildRule.getLog()); assertEquals("Inner Text", buildRule.getLog());
} }


@Test
@Test(expected = BuildException.class)
public void testDuplicateAttribute() { 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() { 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 @Test
@@ -105,32 +102,31 @@ public class MacroDefTest {
@Test @Test
public void testTextElement() { public void testTextElement() {
buildRule.executeTarget("textelement"); buildRule.executeTarget("textelement");
assertContains("Hello world", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("Hello world"));
} }


@Test @Test
public void testTextTrim() { public void testTextTrim() {
buildRule.executeTarget("text.trim"); 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() { 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() { 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 @Test
@@ -159,12 +155,9 @@ public class MacroDefTest {


@Test @Test
public void testImplicitNotOptional() { 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 @Test
@@ -175,38 +168,28 @@ public class MacroDefTest {


@Test @Test
public void testImplicitExplicit() { 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 @Test
public void testBackTraceOff() { 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 @Test
public void testBackTrace() { 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 @Test
public void testTopLevelText() { public void testTopLevelText() {
buildRule.executeTarget("top-level-text"); buildRule.executeTarget("top-level-text");
assertContains("Hello World", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("Hello World"));
} }
} }

+ 21
- 29
src/tests/junit/org/apache/tools/ant/taskdefs/MakeUrlTest.java View File

@@ -22,15 +22,16 @@ import org.apache.tools.ant.BuildFileRule;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;


import java.io.InputStream; import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URL; 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.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 { public class MakeUrlTest {
@@ -38,6 +39,9 @@ public class MakeUrlTest {
@Rule @Rule
public final BuildFileRule buildRule = new BuildFileRule(); public final BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/taskdefs/makeurl.xml"); buildRule.configureProject("src/etc/testcases/taskdefs/makeurl.xml");
@@ -45,42 +49,30 @@ public class MakeUrlTest {


@Test @Test
public void testEmpty() { 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 @Test
public void testNoProperty() { 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 @Test
public void testNoFile() { 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 @Test
public void testValidation() { 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 @Test
@@ -160,8 +152,8 @@ public class MakeUrlTest {
protected void assertPropertyContains(String property, String contains) { protected void assertPropertyContains(String property, String contains) {
String result = getProperty(property); 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));
} }


/** /**


+ 47
- 35
src/tests/junit/org/apache/tools/ant/taskdefs/ManifestClassPathTest.java View File

@@ -17,10 +17,10 @@
*/ */
package org.apache.tools.ant.taskdefs; 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.assertEquals;
import static org.junit.Assert.assertNull; 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.assumeFalse;
import static org.junit.Assume.assumeNoException; import static org.junit.Assume.assumeNoException;
import static org.junit.Assume.assumeTrue; 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.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;


/** /**
* Tests &lt;bm:manifestclasspath&gt;. * Tests &lt;bm:manifestclasspath&gt;.
@@ -44,6 +45,9 @@ public class ManifestClassPathTest {
@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/taskdefs/manifestclasspath.xml"); buildRule.configureProject("src/etc/testcases/taskdefs/manifestclasspath.xml");
@@ -51,57 +55,62 @@ public class ManifestClassPathTest {


@Test @Test
public void testBadDirectory() { public void testBadDirectory() {
thrown.expect(BuildException.class);
thrown.expectMessage("Jar's directory not found:");
try { try {
buildRule.executeTarget("test-bad-directory"); 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 @Test
public void testBadNoProperty() { public void testBadNoProperty() {
thrown.expect(BuildException.class);
thrown.expectMessage("Missing 'property' attribute!");
try { try {
buildRule.executeTarget("test-bad-no-property"); 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 @Test
public void testBadPropertyExists() { public void testBadPropertyExists() {
thrown.expect(BuildException.class);
thrown.expectMessage("Property 'jar.classpath' already set!");
try { try {
buildRule.executeTarget("test-bad-property-exists"); 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 @Test
public void testBadNoJarfile() { public void testBadNoJarfile() {
thrown.expect(BuildException.class);
thrown.expectMessage("Missing 'jarfile' attribute!");
try { try {
buildRule.executeTarget("test-bad-no-jarfile"); 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 @Test
public void testBadNoClassPath() { public void testBadNoClassPath() {
thrown.expect(BuildException.class);
thrown.expectMessage("Missing nested <classpath>!");
try { try {
buildRule.executeTarget("test-bad-no-classpath"); buildRule.executeTarget("test-bad-no-classpath");
fail("Build exception should have been thrown on no classpath");
} catch (BuildException ex) {
assertContains("Missing nested <classpath>!", ex.getMessage());
} finally {
// post-mortem
assertNull(buildRule.getProject().getProperty("jar.classpath"));
} }
assertNull(buildRule.getProject().getProperty("jar.classpath"));
} }


@Test @Test
@@ -136,18 +145,20 @@ public class ManifestClassPathTest {


@Test @Test
public void testParentLevel2TooDeep() { public void testParentLevel2TooDeep() {
thrown.expect(BuildException.class);
thrown.expectMessage("No suitable relative path from ");
try { try {
buildRule.executeTarget("test-parent-level2-too-deep"); 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 @Test
public void testPseudoTahoeRefid() { 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"); buildRule.executeTarget("test-pseudo-tahoe-refid");
assertEquals(buildRule.getProject().getProperty("jar.classpath"), "classes/dsp-core/ " assertEquals(buildRule.getProject().getProperty("jar.classpath"), "classes/dsp-core/ "
@@ -160,7 +171,8 @@ public class ManifestClassPathTest {


@Test @Test
public void testPseudoTahoeNested() { 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"); buildRule.executeTarget("test-pseudo-tahoe-nested");
assertEquals(buildRule.getProject().getProperty("jar.classpath"), "classes/dsp-core/ " assertEquals(buildRule.getProject().getProperty("jar.classpath"), "classes/dsp-core/ "
@@ -192,7 +204,7 @@ public class ManifestClassPathTest {
public void testInternationalGerman() { public void testInternationalGerman() {
buildRule.executeTarget("international-german"); buildRule.executeTarget("international-german");
buildRule.executeTarget("run-two-jars"); buildRule.executeTarget("run-two-jars");
assertContains("beta alpha", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("beta alpha"));
} }


@Test @Test
@@ -200,7 +212,7 @@ public class ManifestClassPathTest {
assumeFalse("Test with hebrew path not attempted under Windows", Os.isFamily("windows")); assumeFalse("Test with hebrew path not attempted under Windows", Os.isFamily("windows"));
buildRule.executeTarget("international-hebrew"); buildRule.executeTarget("international-hebrew");
buildRule.executeTarget("run-two-jars"); buildRule.executeTarget("run-two-jars");
assertContains("beta alpha", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("beta alpha"));
} }


@Test @Test
@@ -237,13 +249,13 @@ public class ManifestClassPathTest {
} }
buildRule.getProject().setProperty("altDriveLetter", altDriveLetter); buildRule.getProject().setProperty("altDriveLetter", altDriveLetter);


thrown.expect(BuildException.class);
thrown.expectMessage("No suitable relative path from ");
try { try {
buildRule.executeTarget("testDifferentDrive"); 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"));
} }
} }

+ 61
- 104
src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java View File

@@ -33,14 +33,15 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


/** /**
* Testcase for the Manifest class used in the jar task. * Testcase for the Manifest class used in the jar task.
@@ -51,6 +52,9 @@ public class ManifestTest {
@Rule @Rule
public final BuildFileRule buildRule = new BuildFileRule(); public final BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

private File expandedManifest; private File expandedManifest;
private File outDir; private File outDir;


@@ -109,12 +113,9 @@ public class ManifestTest {
*/ */
@Test @Test
public void test3() { 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 @Test
public void test4() { 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 @Test
public void test5() { public void test5() {
buildRule.executeTarget("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 @Test
public void test6() { public void test6() {
thrown.expect(BuildException.class);
thrown.expectMessage("Invalid Manifest");
try { try {
buildRule.executeTarget("test6"); 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 * From attribute is illegal
*/ */
@Test @Test
public void test7() { public void test7() {
buildRule.executeTarget("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 @Test
public void test9() { 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 <section> element",
ex.getMessage());
}
thrown.expect(BuildException.class);
thrown.expectMessage("Specify the section name using the \"name\" attribute of the <section> element");
buildRule.executeTarget("test9");
} }


/** /**
@@ -201,12 +195,9 @@ public class ManifestTest {
*/ */
@Test @Test
public void test10() { 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 @Test
public void test11() { 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 @Test
public void test12() { 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 @Test
public void test13() { 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. * file attribute for manifest task is required.
*/ */
@Test
@Test(expected = BuildException.class)
public void testNoFile() { 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(); String mfAsString = mf.toString();
assertNotNull(mfAsString); assertNotNull(mfAsString);
assertTrue(mfAsString.startsWith("Manifest-Version: 2.0")); assertTrue(mfAsString.startsWith("Manifest-Version: 2.0"));
assertTrue(mfAsString.contains("Foo: Bar"));
assertThat(mfAsString, containsString("Foo: Bar"));


mf = getManifest(new File(outDir, "mftest2.mf")); mf = getManifest(new File(outDir, "mftest2.mf"));
assertNotNull(mf); assertNotNull(mf);
mfAsString = mf.toString(); mfAsString = mf.toString();
assertNotNull(mfAsString); assertNotNull(mfAsString);
assertTrue(mfAsString.contains("Foo: Baz"));
assertFalse(mfAsString.contains("Foo: Bar"));
assertThat(mfAsString, containsString("Foo: Baz"));
assertThat(mfAsString, not(containsString("Foo: Bar")));
} }


@Test @Test
public void testFrom() { public void testFrom() {
buildRule.executeTarget("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() { 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() { 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() { 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() { 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() { 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. * Reads mftest.mf.


+ 10
- 15
src/tests/junit/org/apache/tools/ant/taskdefs/NiceTest.java View File

@@ -23,9 +23,7 @@ import org.apache.tools.ant.BuildFileRule;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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 * test nice
@@ -35,6 +33,9 @@ public class NiceTest {
@Rule @Rule
public final BuildFileRule buildRule = new BuildFileRule(); public final BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/taskdefs/nice.xml"); buildRule.configureProject("src/etc/testcases/taskdefs/nice.xml");
@@ -62,22 +63,16 @@ public class NiceTest {


@Test @Test
public void testTooSlow() { 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 @Test
public void testTooFast() { 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");
} }


} }

+ 10
- 14
src/tests/junit/org/apache/tools/ant/taskdefs/PreSetDefTest.java View File

@@ -25,10 +25,9 @@ import org.apache.tools.ant.types.FileSet;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.fail;


/** /**
*/ */
@@ -37,6 +36,9 @@ public class PreSetDefTest {
@Rule @Rule
public final BuildFileRule buildRule = new BuildFileRule(); public final BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/taskdefs/presetdef.xml"); buildRule.configureProject("src/etc/testcases/taskdefs/presetdef.xml");
@@ -98,22 +100,16 @@ public class PreSetDefTest {


@Test @Test
public void testCorrectTaskNameBadAttr() { 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 @Test
public void testCorrectTaskNameBadEl() { 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 @Test


+ 28
- 30
src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java View File

@@ -18,10 +18,9 @@


package org.apache.tools.ant.taskdefs; 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.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 static org.junit.Assume.assumeNoException;


import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
@@ -30,6 +29,7 @@ import org.apache.tools.ant.util.FileUtils;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;


/** /**
*/ */
@@ -38,6 +38,9 @@ public class PropertyTest {
@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

/** Utilities used for file operations */ /** Utilities used for file operations */
private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();


@@ -58,25 +61,23 @@ public class PropertyTest {
@Test @Test
public void test2() { public void test2() {
buildRule.executeTarget("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 @Test
public void test3() { 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 @Test
public void test4() { public void test4() {
buildRule.executeTarget("test4"); buildRule.executeTarget("test4");
assertContains("http.url is http://localhost:999", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("http.url is http://localhost:999"));
} }


@Test @Test
@@ -86,7 +87,7 @@ public class PropertyTest {
buildRule.getProject().setNewProperty("test5.url", uri); buildRule.getProject().setNewProperty("test5.url", uri);


buildRule.executeTarget("test5"); buildRule.executeTarget("test5");
assertContains("http.url is http://localhost:999", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("http.url is http://localhost:999"));
} }


@Test @Test
@@ -95,32 +96,30 @@ public class PropertyTest {
assertEquals("80", buildRule.getProject().getProperty("server1.http.port")); assertEquals("80", buildRule.getProject().getProperty("server1.http.port"));
} }


/**
* Fail due to prefix allowed only non-resource/file load
*/
@Test @Test
public void testPrefixFailure() { 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 @Test
public void testCircularReference() { 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 @Test
public void testThisIsNotACircularReference() { public void testThisIsNotACircularReference() {
buildRule.executeTarget("thisIsNotACircularReference"); buildRule.executeTarget("thisIsNotACircularReference");
assertContains("b is A/A/A", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("b is A/A/A"));
} }


@Test @Test
@@ -133,7 +132,6 @@ public class PropertyTest {
buildRule.executeTarget("testXmlProperty"); buildRule.executeTarget("testXmlProperty");
assertEquals("ONE", buildRule.getProject().getProperty("xml.one")); assertEquals("ONE", buildRule.getProject().getProperty("xml.one"));
assertEquals("TWO", buildRule.getProject().getProperty("xml.two")); assertEquals("TWO", buildRule.getProject().getProperty("xml.two"));

} }


} }

+ 32
- 31
src/tests/junit/org/apache/tools/ant/taskdefs/RmicAdvancedTest.java View File

@@ -30,7 +30,8 @@ import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; 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.assumeFalse;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;


@@ -46,7 +47,7 @@ public class RmicAdvancedTest {
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule @Rule
public ExpectedException tried = ExpectedException.none();
public ExpectedException thrown = ExpectedException.none();


/** /**
* The JUnit setup method * The JUnit setup method
@@ -202,8 +203,8 @@ public class RmicAdvancedTest {
*/ */
@Test @Test
public void testBadName() { 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"); buildRule.executeTarget("testBadName");
} }


@@ -220,8 +221,8 @@ public class RmicAdvancedTest {
*/ */
@Test @Test
public void testWrongClass() { 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"); buildRule.executeTarget("testWrongClass");
} }


@@ -230,13 +231,13 @@ public class RmicAdvancedTest {
*/ */
@Test @Test
public void testDefaultBadClass() { public void testDefaultBadClass() {
tried.expect(BuildException.class);
tried.expectMessage(Rmic.ERROR_RMIC_FAILED);
thrown.expect(BuildException.class);
thrown.expectMessage(Rmic.ERROR_RMIC_FAILED);
try { try {
buildRule.executeTarget("testDefaultBadClass"); buildRule.executeTarget("testDefaultBadClass");
} finally { } finally {
// don't look for much text here as it is vendor and version dependent // 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 @Test
public void testMagicProperty() { 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"); buildRule.executeTarget("testMagicProperty");
} }


@@ -255,8 +256,8 @@ public class RmicAdvancedTest {
*/ */
@Test @Test
public void testMagicPropertyOverridesEmptyString() { 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"); buildRule.executeTarget("testMagicPropertyOverridesEmptyString");
} }


@@ -268,8 +269,8 @@ public class RmicAdvancedTest {
@Test @Test
@Ignore("Previously named to prevent execution") @Ignore("Previously named to prevent execution")
public void NotestFailingAdapter() { 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"); buildRule.executeTarget("testFailingAdapter");
} }


@@ -336,8 +337,8 @@ public class RmicAdvancedTest {
@Test @Test
public void testXnewForkedJava9plus() { public void testXnewForkedJava9plus() {
assumeTrue("Current system is Java 8 or older", JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.JAVA_9)); 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"); buildRule.executeTarget("testXnewForked");
} }


@@ -356,8 +357,8 @@ public class RmicAdvancedTest {
@Test @Test
public void testXnewForkedDestJava9plus() { public void testXnewForkedDestJava9plus() {
assumeTrue("Current system is Java 8 or older", JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.JAVA_9)); 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"); buildRule.executeTarget("testXnewForkedDest");
} }


@@ -376,8 +377,8 @@ public class RmicAdvancedTest {
@Test @Test
public void testXnewCompilerJava9plus() { public void testXnewCompilerJava9plus() {
assumeTrue("Current system is Java 8 or older", JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.JAVA_9)); 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"); buildRule.executeTarget("testXnewCompiler");
} }


@@ -396,8 +397,8 @@ public class RmicAdvancedTest {
@Test @Test
public void testXnewCompilerDestJava9plus() { public void testXnewCompilerDestJava9plus() {
assumeTrue("Current system is Java 8 or older", JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.JAVA_9)); 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"); buildRule.executeTarget("testXnewCompilerDest");
} }


@@ -416,8 +417,8 @@ public class RmicAdvancedTest {
@Test @Test
public void testIDLJava11plus() { public void testIDLJava11plus() {
assumeTrue("Current system is Java 10 or older", JavaEnvUtils.isAtLeastJavaVersion("11")); 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"); buildRule.executeTarget("testIDL");
} }


@@ -436,8 +437,8 @@ public class RmicAdvancedTest {
@Test @Test
public void testIDLDestJava11plus() { public void testIDLDestJava11plus() {
assumeTrue("Current system is Java 10 or older", JavaEnvUtils.isAtLeastJavaVersion("11")); 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"); buildRule.executeTarget("testIDL");
} }


@@ -456,8 +457,8 @@ public class RmicAdvancedTest {
@Test @Test
public void testIIOPJava11plus() { public void testIIOPJava11plus() {
assumeTrue("Current system is Java 10 or older", JavaEnvUtils.isAtLeastJavaVersion("11")); 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"); buildRule.executeTarget("testIIOP");
} }


@@ -476,8 +477,8 @@ public class RmicAdvancedTest {
@Test @Test
public void testIIOPDestJava11plus() { public void testIIOPDestJava11plus() {
assumeTrue("Current system is Java 10 or older", JavaEnvUtils.isAtLeastJavaVersion("11")); 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"); buildRule.executeTarget("testIIOP");
} }




+ 23
- 21
src/tests/junit/org/apache/tools/ant/taskdefs/SQLExecTest.java View File

@@ -30,14 +30,14 @@ import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


/** /**
* Simple testcase to test for driver caching. * Simple testcase to test for driver caching.
@@ -63,6 +63,9 @@ public class SQLExecTest {
public static final String PATH = "path"; public static final String PATH = "path";
public static final String SQL = "sql"; public static final String SQL = "sql";


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
// make sure the cache is cleared. // make sure the cache is cleared.
@@ -72,30 +75,29 @@ public class SQLExecTest {
// simple test to ensure that the caching does work... // simple test to ensure that the caching does work...
@Test @Test
public void testDriverCaching() { public void testDriverCaching() {
thrown.expect(BuildException.class);
thrown.expectMessage("No suitable Driver");
SQLExec sql = createTask(getProperties(NULL)); SQLExec sql = createTask(getProperties(NULL));
assertFalse(SQLExec.getLoaderMap().containsKey(NULL_DRIVER)); assertFalse(SQLExec.getLoaderMap().containsKey(NULL_DRIVER));
try { try {
sql.execute(); 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 @Test


+ 4
- 5
src/tests/junit/org/apache/tools/ant/taskdefs/StyleTest.java View File

@@ -28,8 +28,10 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;


import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


@@ -228,11 +230,8 @@ public class StyleTest {


private void assertFileContains(String filename, String contains) throws IOException { private void assertFileContains(String filename, String contains) throws IOException {
String content = getFileString(filename); 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));
} }


} }

+ 19
- 22
src/tests/junit/org/apache/tools/ant/taskdefs/SubAntTest.java View File

@@ -20,8 +20,6 @@ package org.apache.tools.ant.taskdefs;


import java.io.File; import java.io.File;


import junit.framework.AssertionFailedError;

import org.apache.tools.ant.BuildEvent; import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileRule; import org.apache.tools.ant.BuildFileRule;
@@ -29,17 +27,20 @@ import org.apache.tools.ant.BuildListener;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.fail;

import static org.junit.Assert.assertThat;


public class SubAntTest { public class SubAntTest {


@Rule @Rule
public final BuildFileRule buildRule = new BuildFileRule(); public final BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/taskdefs/subant.xml"); buildRule.configureProject("src/etc/testcases/taskdefs/subant.xml");
@@ -84,33 +85,31 @@ public class SubAntTest {
@Test @Test
public void testMultipleTargets() { public void testMultipleTargets() {
buildRule.executeTarget("multipleTargets"); 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 @Test
public void testMultipleTargetsOneDoesntExist_FOEfalse() { public void testMultipleTargetsOneDoesntExist_FOEfalse() {
buildRule.executeTarget("multipleTargetsOneDoesntExist_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 @Test
public void testMultipleTargetsOneDoesntExist_FOEtrue() { 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) { protected void testBaseDirs(String target, String[] dirs) {
SubAntTest.BasedirChecker bc = new SubAntTest.BasedirChecker(dirs); SubAntTest.BasedirChecker bc = new SubAntTest.BasedirChecker(dirs);
buildRule.getProject().addBuildListener(bc); buildRule.getProject().addBuildListener(bc);
buildRule.executeTarget(target); buildRule.executeTarget(target);
AssertionFailedError ae = bc.getError();
AssertionError ae = bc.getError();
if (ae != null) { if (ae != null) {
throw ae; throw ae;
} }
@@ -120,7 +119,7 @@ public class SubAntTest {
private class BasedirChecker implements BuildListener { private class BasedirChecker implements BuildListener {
private String[] expectedBasedirs; private String[] expectedBasedirs;
private int calls = 0; private int calls = 0;
private AssertionFailedError error;
private AssertionError error;


BasedirChecker(String[] dirs) { BasedirChecker(String[] dirs) {
expectedBasedirs = dirs; expectedBasedirs = dirs;
@@ -152,17 +151,15 @@ public class SubAntTest {
try { try {
assertEquals(expectedBasedirs[calls++], assertEquals(expectedBasedirs[calls++],
event.getProject().getBaseDir().getAbsolutePath()); event.getProject().getBaseDir().getAbsolutePath());
} catch (AssertionFailedError e) {
} catch (AssertionError e) {
error = e; error = e;
} }
} }
} }


AssertionFailedError getError() {
AssertionError getError() {
return error; return error;
} }

} }



} }

+ 17
- 16
src/tests/junit/org/apache/tools/ant/taskdefs/SyncTest.java View File

@@ -23,8 +23,9 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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; import static org.junit.Assert.assertTrue;


public class SyncTest { public class SyncTest {
@@ -42,7 +43,7 @@ public class SyncTest {
buildRule.executeTarget("simplecopy"); buildRule.executeTarget("simplecopy");
String d = buildRule.getProject().getProperty("dest") + "/a/b/c/d"; String d = buildRule.getProject().getProperty("dest") + "/a/b/c/d";
assertFileIsPresent(d); assertFileIsPresent(d);
assertFalse(buildRule.getFullLog().contains("dangling"));
assertThat(buildRule.getFullLog(), not(containsString("dangling")));
} }


@Test @Test
@@ -52,7 +53,7 @@ public class SyncTest {
assertFileIsNotPresent(d); assertFileIsNotPresent(d);
String c = buildRule.getProject().getProperty("dest") + "/a/b/c"; String c = buildRule.getProject().getProperty("dest") + "/a/b/c";
assertFileIsNotPresent(c); assertFileIsNotPresent(c);
assertFalse(buildRule.getFullLog().contains("dangling"));
assertThat(buildRule.getFullLog(), not(containsString("dangling")));
} }


@Test @Test
@@ -62,7 +63,7 @@ public class SyncTest {
assertFileIsNotPresent(d); assertFileIsNotPresent(d);
String c = buildRule.getProject().getProperty("dest") + "/a/b/c"; String c = buildRule.getProject().getProperty("dest") + "/a/b/c";
assertFileIsPresent(c); assertFileIsPresent(c);
assertFalse(buildRule.getFullLog().contains("dangling"));
assertThat(buildRule.getFullLog(), not(containsString("dangling")));
} }


@Test @Test
@@ -86,9 +87,9 @@ public class SyncTest {
assertFileIsPresent(d); assertFileIsPresent(d);
String f = buildRule.getProject().getProperty("dest") + "/e/f"; String f = buildRule.getProject().getProperty("dest") + "/e/f";
assertFileIsNotPresent(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 @Test
@@ -98,9 +99,9 @@ public class SyncTest {
assertFileIsPresent(d); assertFileIsPresent(d);
String f = buildRule.getProject().getProperty("dest") + "/e/f"; String f = buildRule.getProject().getProperty("dest") + "/e/f";
assertFileIsNotPresent(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 @Test
@@ -112,9 +113,9 @@ public class SyncTest {
assertFileIsPresent(c); assertFileIsPresent(c);
String f = buildRule.getProject().getProperty("dest") + "/e/f"; String f = buildRule.getProject().getProperty("dest") + "/e/f";
assertFileIsNotPresent(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 @Test
@@ -124,7 +125,7 @@ public class SyncTest {
assertFileIsPresent(d); assertFileIsPresent(d);
String f = buildRule.getProject().getProperty("dest") + "/e/f"; String f = buildRule.getProject().getProperty("dest") + "/e/f";
assertFileIsPresent(f); assertFileIsPresent(f);
assertFalse(buildRule.getFullLog().contains("Removing orphan file:"));
assertThat(buildRule.getFullLog(), not(containsString("Removing orphan file:")));
} }


@Test @Test
@@ -134,7 +135,7 @@ public class SyncTest {
assertFileIsPresent(d); assertFileIsPresent(d);
String f = buildRule.getProject().getProperty("dest") + "/e/f"; String f = buildRule.getProject().getProperty("dest") + "/e/f";
assertFileIsPresent(f); assertFileIsPresent(f);
assertFalse(buildRule.getFullLog().contains("Removing orphan file:"));
assertThat(buildRule.getFullLog(), not(containsString("Removing orphan file:")));
} }


public void assertFileIsPresent(String f) { public void assertFileIsPresent(String f) {


+ 8
- 7
src/tests/junit/org/apache/tools/ant/taskdefs/TaskdefTest.java View File

@@ -25,8 +25,9 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;


import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


/** /**
@@ -118,11 +119,11 @@ public class TaskdefTest {
public void testOverride() { public void testOverride() {
buildRule.executeTarget("testOverride"); buildRule.executeTarget("testOverride");
String log = buildRule.getLog(); 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"));
} }
} }

+ 7
- 10
src/tests/junit/org/apache/tools/ant/taskdefs/TouchTest.java View File

@@ -25,24 +25,25 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;


import java.io.File; import java.io.File;


import static org.apache.tools.ant.AntAssert.assertContains;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


public class TouchTest { public class TouchTest {


@Rule @Rule
public final BuildFileRule buildRule = new BuildFileRule(); public final BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

private static String TOUCH_FILE = "src/etc/testcases/taskdefs/touchtest"; private static String TOUCH_FILE = "src/etc/testcases/taskdefs/touchtest";


/** Utilities used for file operations */ /** Utilities used for file operations */
private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();



@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/taskdefs/touch.xml"); buildRule.configureProject("src/etc/testcases/taskdefs/touch.xml");
@@ -168,13 +169,9 @@ public class TouchTest {
*/ */
@Test @Test
public void testBadPattern() { 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");
} }


/** /**


+ 12
- 13
src/tests/junit/org/apache/tools/ant/taskdefs/TypeAdapterTest.java View File

@@ -28,11 +28,11 @@ import org.apache.tools.ant.TypeAdapter;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.fail;

import static org.junit.Assert.assertThat;


/** /**
*/ */
@@ -41,6 +41,8 @@ public class TypeAdapterTest {
@Rule @Rule
public final BuildFileRule buildRule = new BuildFileRule(); public final BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();


@Before @Before
public void setUp() { public void setUp() {
@@ -50,35 +52,32 @@ public class TypeAdapterTest {
@Test @Test
public void testTaskAdapter() { public void testTaskAdapter() {
buildRule.executeTarget("taskadapter"); buildRule.executeTarget("taskadapter");
assertContains("MyExec called", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("MyExec called"));
} }


@Test @Test
public void testRunAdapter() { public void testRunAdapter() {
buildRule.executeTarget("runadapter"); buildRule.executeTarget("runadapter");
assertContains("MyRunnable called", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("MyRunnable called"));
} }


@Test @Test
public void testRunAdapterError() { 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 @Test
public void testDelay() { public void testDelay() {
buildRule.executeTarget("delay"); buildRule.executeTarget("delay");
assertContains("MyTask called", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("MyTask called"));
} }


@Test @Test
public void testOnErrorReport() { public void testOnErrorReport() {
buildRule.executeTarget("onerror.report"); buildRule.executeTarget("onerror.report");
assertContains("MyTaskNotPresent cannot be found", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("MyTaskNotPresent cannot be found"));
} }


@Test @Test


+ 26
- 44
src/tests/junit/org/apache/tools/ant/taskdefs/TypedefTest.java View File

@@ -23,11 +23,12 @@ import org.apache.tools.ant.BuildFileRule;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.assertNotNull; 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 @Rule
public final BuildFileRule buildRule = new BuildFileRule(); public final BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();


@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/taskdefs/typedef.xml"); buildRule.configureProject("src/etc/testcases/taskdefs/typedef.xml");
} }


@Test
@Test(expected = BuildException.class)
public void testEmpty() { 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() { 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() { 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() { 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 @Test
@@ -88,8 +75,7 @@ public class TypedefTest {
assertEquals("", buildRule.getLog()); assertEquals("", buildRule.getLog());
Object ref = buildRule.getProject().getReferences().get("global"); Object ref = buildRule.getProject().getReferences().get("global");
assertNotNull("ref is not null", ref); assertNotNull("ref is not null", ref);
assertEquals("org.example.types.TypedefTestType",
ref.getClass().getName());
assertEquals("org.example.types.TypedefTestType", ref.getClass().getName());
} }


@Test @Test
@@ -98,8 +84,7 @@ public class TypedefTest {
assertEquals("", buildRule.getLog()); assertEquals("", buildRule.getLog());
Object ref = buildRule.getProject().getReferences().get("local"); Object ref = buildRule.getProject().getReferences().get("local");
assertNotNull("ref is not null", ref); 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 @Test
public void testDoubleNotPresent() { public void testDoubleNotPresent() {
buildRule.executeTarget("double-notpresent"); buildRule.executeTarget("double-notpresent");
assertContains("hi", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("hi"));
} }


@Test @Test
public void testNoResourceOnErrorFailAll() { 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 @Test
public void testNoResourceOnErrorFail() { public void testNoResourceOnErrorFail() {
buildRule.executeTarget("noresourcefail"); buildRule.executeTarget("noresourcefail");
assertContains("Could not load definitions from resource ", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("Could not load definitions from resource "));
} }


@Test @Test
public void testNoResourceOnErrorNotFail() { 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 "));
} }
} }

+ 31
- 36
src/tests/junit/org/apache/tools/ant/taskdefs/ZipTest.java View File

@@ -33,12 +33,12 @@ import org.junit.Ignore;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; 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; import static org.junit.Assume.assumeTrue;


public class ZipTest { public class ZipTest {
@@ -49,52 +49,47 @@ public class ZipTest {
//instance variable to allow cleanup //instance variable to allow cleanup
ZipFile zfPrefixAddsDir = null; ZipFile zfPrefixAddsDir = null;



@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/taskdefs/zip.xml"); buildRule.configureProject("src/etc/testcases/taskdefs/zip.xml");
buildRule.executeTarget("setUp"); buildRule.executeTarget("setUp");
} }


@Test
/**
* Fail due to required argument not specified
*/
@Test(expected = BuildException.class)
public void test1() { 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() { 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() { 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") @Ignore("Previously commented out")
public void test4() { 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 @After
@@ -150,13 +145,13 @@ public class ZipTest {
@Test @Test
public void testUpdateNotNecessary() { public void testUpdateNotNecessary() {
buildRule.executeTarget("testUpdateNotNecessary"); buildRule.executeTarget("testUpdateNotNecessary");
assertFalse(buildRule.getLog().contains("Updating"));
assertThat(buildRule.getLog(), not(containsString("Updating")));
} }


@Test @Test
public void testUpdateIsNecessary() { public void testUpdateIsNecessary() {
buildRule.executeTarget("testUpdateIsNecessary"); buildRule.executeTarget("testUpdateIsNecessary");
assertContains("Updating", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("Updating"));
} }


// Bugzilla Report 18403 // Bugzilla Report 18403
@@ -204,7 +199,7 @@ public class ZipTest {
@Test @Test
public void testZipEmptyCreate() { public void testZipEmptyCreate() {
buildRule.executeTarget("zipEmptyCreate"); buildRule.executeTarget("zipEmptyCreate");
assertContains("Note: creating empty", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("Note: creating empty"));
} }


// Bugzilla Report 25513 // Bugzilla Report 25513


+ 7
- 7
src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java View File

@@ -34,10 +34,11 @@ import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.util.FileUtils; 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.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;


public class DefaultCompilerAdapterTest { public class DefaultCompilerAdapterTest {
@@ -419,8 +420,8 @@ public class DefaultCompilerAdapterTest {
sth.setJavac(javac); sth.setJavac(javac);
Commandline cmd = new Commandline(); Commandline cmd = new Commandline();
sth.setupModernJavacCommandlineSwitches(cmd); 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(); String[] args = cmd.getCommandline();
assertEquals(7, args.length); assertEquals(7, args.length);
assertEquals("-classpath", args[0]); assertEquals("-classpath", args[0]);
@@ -445,8 +446,8 @@ public class DefaultCompilerAdapterTest {
sth.setJavac(javac); sth.setJavac(javac);
Commandline cmd = new Commandline(); Commandline cmd = new Commandline();
sth.setupModernJavacCommandlineSwitches(cmd); 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(); String[] args = cmd.getCommandline();
assertEquals(5, args.length); assertEquals(5, args.length);
assertEquals("-classpath", args[0]); assertEquals("-classpath", args[0]);
@@ -491,8 +492,7 @@ public class DefaultCompilerAdapterTest {
if (expectedLog.isEmpty()) { if (expectedLog.isEmpty()) {
assertEquals("", javac.getLog()); assertEquals("", javac.getLog());
} else { } else {
String l = javac.getLog();
assertContains(expectedLog, l);
assertThat(javac.getLog(), containsString(expectedLog));
} }
String[] args = cmd.getCommandline(); String[] args = cmd.getCommandline();
assertEquals(expectedSource == null ? 0 : 2, args.length); assertEquals(expectedSource == null ? 0 : 2, args.length);


+ 7
- 10
src/tests/junit/org/apache/tools/ant/taskdefs/condition/IsFileSelectedTest.java View File

@@ -23,9 +23,7 @@ import org.apache.tools.ant.BuildFileRule;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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 &lt;isfileselected&gt; condition. * Testcase for the &lt;isfileselected&gt; condition.
@@ -36,6 +34,9 @@ public class IsFileSelectedTest {
@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/taskdefs/conditions/isfileselected.xml"); buildRule.configureProject("src/etc/testcases/taskdefs/conditions/isfileselected.xml");
@@ -63,12 +64,8 @@ public class IsFileSelectedTest {


@Test @Test
public void testNotSelector() { 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");
} }
} }

+ 20
- 36
src/tests/junit/org/apache/tools/ant/taskdefs/condition/IsReachableTest.java View File

@@ -23,10 +23,7 @@ import org.junit.Before;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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 * test for reachable things
@@ -36,10 +33,12 @@ public class IsReachableTest {
@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject(
"src/etc/testcases/taskdefs/conditions/isreachable.xml");
buildRule.configureProject("src/etc/testcases/taskdefs/conditions/isreachable.xml");
} }


@Test @Test
@@ -64,52 +63,37 @@ public class IsReachableTest {


@Test @Test
public void testBoth() { 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 @Test
public void testNoTargets() { 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 @Test
public void testBadTimeout() { 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 @Test
@Ignore("Previously named in a way to prevent execution") @Ignore("Previously named in a way to prevent execution")
public void NotestFile() { 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 @Test
public void testBadURL() { 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");
} }
} }

+ 7
- 8
src/tests/junit/org/apache/tools/ant/taskdefs/condition/TypeFoundTest.java View File

@@ -22,10 +22,9 @@ import org.apache.tools.ant.BuildFileRule;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;


/** /**
@@ -36,6 +35,9 @@ public class TypeFoundTest {
@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/taskdefs/conditions/typefound.xml"); buildRule.configureProject("src/etc/testcases/taskdefs/conditions/typefound.xml");
@@ -49,12 +51,9 @@ public class TypeFoundTest {


@Test @Test
public void testUndefined() { 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 @Test


+ 9
- 8
src/tests/junit/org/apache/tools/ant/taskdefs/optional/ANTLRTest.java View File

@@ -28,8 +28,9 @@ import org.junit.Test;
import java.io.File; import java.io.File;
import java.io.FilenameFilter; 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.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


@@ -178,32 +179,32 @@ public class ANTLRTest {
@Test @Test
public void testNoRecompile() { public void testNoRecompile() {
buildRule.executeTarget("test9"); buildRule.executeTarget("test9");
assertNotContains("Skipped grammar file.", buildRule.getFullLog());
assertThat(buildRule.getFullLog(), not(containsString("Skipped grammar file.")));
buildRule.executeTarget("noRecompile"); buildRule.executeTarget("noRecompile");
assertContains("Skipped grammar file.", buildRule.getFullLog());
assertThat(buildRule.getFullLog(), containsString("Skipped grammar file."));
} }


@Test @Test
public void testNormalRecompile() { public void testNormalRecompile() {
buildRule.executeTarget("test9"); buildRule.executeTarget("test9");
assertNotContains("Skipped grammar file.", buildRule.getFullLog());
assertThat(buildRule.getFullLog(), not(containsString("Skipped grammar file.")));


FileUtilities.rollbackTimestamps(buildRule.getOutputDir(), 5); FileUtilities.rollbackTimestamps(buildRule.getOutputDir(), 5);


buildRule.executeTarget("normalRecompile"); buildRule.executeTarget("normalRecompile");
assertNotContains("Skipped grammar file.", buildRule.getFullLog());
assertThat(buildRule.getFullLog(), not(containsString("Skipped grammar file.")));
} }


@Test @Test
// Bugzilla Report 12961 // Bugzilla Report 12961
public void testSupergrammarChangeRecompile() { public void testSupergrammarChangeRecompile() {
buildRule.executeTarget("test9"); buildRule.executeTarget("test9");
assertNotContains("Skipped grammar file.", buildRule.getFullLog());
assertThat(buildRule.getFullLog(), not(containsString("Skipped grammar file.")));


FileUtilities.rollbackTimestamps(buildRule.getOutputDir(), 5); FileUtilities.rollbackTimestamps(buildRule.getOutputDir(), 5);


buildRule.executeTarget("supergrammarChangeRecompile"); buildRule.executeTarget("supergrammarChangeRecompile");
assertNotContains("Skipped grammar file.", buildRule.getFullLog());
assertThat(buildRule.getFullLog(), not(containsString("Skipped grammar file.")));


} }




+ 25
- 40
src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java View File

@@ -18,11 +18,11 @@


package org.apache.tools.ant.taskdefs.optional; 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.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;


import java.io.BufferedInputStream; import java.io.BufferedInputStream;
@@ -41,6 +41,7 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;


/** /**
* Tests the EchoProperties task. * Tests the EchoProperties task.
@@ -59,6 +60,9 @@ public class EchoPropertiesTest {
@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject(TASKDEFS_DIR + "echoproperties.xml"); buildRule.configureProject(TASKDEFS_DIR + "echoproperties.xml");
@@ -73,45 +77,39 @@ public class EchoPropertiesTest {
@Test @Test
public void testEchoToLog() { public void testEchoToLog() {
buildRule.executeTarget("testEchoToLog"); buildRule.executeTarget("testEchoToLog");
assertContains("test.property=" + TEST_VALUE, buildRule.getLog());
assertThat(buildRule.getLog(), containsString("test.property=" + TEST_VALUE));
} }


@Test @Test
public void testEchoWithEmptyPrefixToLog() { public void testEchoWithEmptyPrefixToLog() {
buildRule.executeTarget("testEchoWithEmptyPrefixToLog"); buildRule.executeTarget("testEchoWithEmptyPrefixToLog");
assertContains("test.property=" + TEST_VALUE, buildRule.getLog());
assertThat(buildRule.getLog(), containsString("test.property=" + TEST_VALUE));
} }


@Test @Test
public void testReadBadFile() { 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 @Test
public void testReadBadFileNoFail() { public void testReadBadFileNoFail() {
buildRule.executeTarget("testReadBadFileNoFail"); buildRule.executeTarget("testReadBadFileNoFail");
assertContains("srcfile is a directory!", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("srcfile is a directory!"));
} }


@Test @Test
public void testEchoToBadFile() { 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 @Test
public void testEchoToBadFileNoFail() { public void testEchoToBadFileNoFail() {
buildRule.executeTarget("testEchoToBadFileNoFail"); buildRule.executeTarget("testEchoToBadFileNoFail");
assertContains("destfile is a directory!", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("destfile is a directory!"));
} }


@Test @Test
@@ -127,14 +125,8 @@ public class EchoPropertiesTest {
// read in the file // read in the file
File f = createRelativeFile(GOOD_OUTFILE_XML); File f = createRelativeFile(GOOD_OUTFILE_XML);
try (BufferedReader br = new BufferedReader(new FileReader(f))) { try (BufferedReader br = new BufferedReader(new FileReader(f))) {
String read = null;
while ((read = br.readLine()) != null) {
if (read.contains("<property name=\"test.property\" value=\"" + TEST_VALUE + "\" />")) {
// 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("<property name=\"test.property\" value=\"" + TEST_VALUE + "\" />")));
} }
} }


@@ -172,19 +164,15 @@ public class EchoPropertiesTest {


@Test @Test
public void testWithPrefixAndRegex() { 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 @Test
public void testWithEmptyPrefixAndRegex() { public void testWithEmptyPrefixAndRegex() {
buildRule.executeTarget("testEchoWithEmptyPrefixToLog"); buildRule.executeTarget("testEchoWithEmptyPrefixToLog");
assertContains("test.property=" + TEST_VALUE, buildRule.getLog());
assertThat(buildRule.getLog(), containsString("test.property=" + TEST_VALUE));
} }


@Test @Test
@@ -194,7 +182,7 @@ public class EchoPropertiesTest {
buildRule.executeTarget("testWithRegex"); buildRule.executeTarget("testWithRegex");
// the following line has been changed from checking ant.home to ant.version // 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 // 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 { private void testEchoPrefixVarious(String target) throws Exception {
@@ -231,10 +219,7 @@ public class EchoPropertiesTest {
} }


protected File createRelativeFile(String filename) { 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);
} }
} }

+ 8
- 10
src/tests/junit/org/apache/tools/ant/taskdefs/optional/RpmTest.java View File

@@ -23,25 +23,23 @@ import org.apache.tools.ant.taskdefs.Execute;
import org.apache.tools.ant.taskdefs.ExecuteStreamHandler; import org.apache.tools.ant.taskdefs.ExecuteStreamHandler;
import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.Commandline;


import org.junit.Rule;
import org.junit.Test; 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 { public class RpmTest {


@Rule
public ExpectedException thrown = ExpectedException.none();

@Test @Test
public void testShouldThrowExceptionWhenRpmFails() { public void testShouldThrowExceptionWhenRpmFails() {
thrown.expect(BuildException.class);
thrown.expectMessage("' failed with exit code 2");
Rpm rpm = new MyRpm(); Rpm rpm = new MyRpm();
rpm.setProject(new Project()); rpm.setProject(new Project());
rpm.setFailOnError(true); 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 @Test


+ 19
- 37
src/tests/junit/org/apache/tools/ant/taskdefs/optional/SchemaValidateTest.java View File

@@ -22,9 +22,7 @@ import org.apache.tools.ant.BuildFileRule;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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 * Test schema validation
@@ -40,6 +38,9 @@ public class SchemaValidateTest {
@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject(TASKDEFS_DIR + "schemavalidate.xml"); buildRule.configureProject(TASKDEFS_DIR + "schemavalidate.xml");
@@ -63,56 +64,37 @@ public class SchemaValidateTest {


@Test @Test
public void testNoEmptySchemaNamespace() { 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 @Test
public void testNoEmptySchemaLocation() { 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 @Test
public void testNoFile() { 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 @Test
public void testNoDoubleSchemaLocation() { 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 @Test
public void testNoDuplicateSchema() { 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 @Test


+ 22
- 47
src/tests/junit/org/apache/tools/ant/taskdefs/optional/XmlValidateTest.java View File

@@ -22,10 +22,7 @@ import org.apache.tools.ant.BuildFileRule;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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 * Tests the XMLValidate optional task, by running targets in the test script
@@ -45,6 +42,9 @@ public class XmlValidateTest {
@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject(TASKDEFS_DIR + "xmlvalidate.xml"); 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 @Test
public void testXmlSchemaGood() throws BuildException { 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 @Test
public void testXmlSchemaBad() { 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 * Bug 11279
*/ */
@Test
@Test(expected = BuildException.class)
public void testUtf8() { 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 @Test
public void testPropertySchemaForValidXML() { public void testPropertySchemaForValidXML() {
buildRule.executeTarget("testProperty.validXML"); buildRule.executeTarget("testProperty.validXML");
} }


@Test
@Test(expected = BuildException.class)
public void testPropertySchemaForInvalidXML() { 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
} }


} }

+ 8
- 11
src/tests/junit/org/apache/tools/ant/taskdefs/optional/XsltTest.java View File

@@ -23,7 +23,9 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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. * Tests the {@link org.apache.tools.ant.taskdefs.XSLTProcess} task.
@@ -45,14 +47,10 @@ public class XsltTest {
buildRule.configureProject(TASKDEFS_DIR + "xslt.xml"); buildRule.configureProject(TASKDEFS_DIR + "xslt.xml");
} }


@Test
@Test(expected = BuildException.class)
public void testCatchNoDtd() { 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 @Test
@@ -76,8 +74,7 @@ public class XsltTest {
@Test @Test
public void testStyleSheetWithInclude() { public void testStyleSheetWithInclude() {
buildRule.executeTarget("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")));
} }
} }

+ 16
- 21
src/tests/junit/org/apache/tools/ant/taskdefs/optional/depend/DependTest.java View File

@@ -29,11 +29,13 @@ import org.apache.tools.ant.types.FileSet;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


/** /**
* Testcase for the Depend optional task. * Testcase for the Depend optional task.
@@ -48,6 +50,9 @@ public class DependTest {
@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject(TEST_BUILD_FILE); 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("tempsrc.dir")), 5);
FileUtilities.rollbackTimestamps(new File(buildRule.getProject().getProperty("classes.dir")), 5); FileUtilities.rollbackTimestamps(new File(buildRule.getProject().getProperty("classes.dir")), 5);



buildRule.executeTarget("testinner"); buildRule.executeTarget("testinner");
assertEquals("Depend did not leave correct number of files", 0, assertEquals("Depend did not leave correct number of files", 0,
getResultFiles().size()); getResultFiles().size());
@@ -132,12 +136,9 @@ public class DependTest {
*/ */
@Test @Test
public void testNoSource() { 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 @Test
public void testEmptySource() { 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); FileUtilities.rollbackTimestamps(new File(buildRule.getProject().getProperty("classes.dir")), 5);


buildRule.executeTarget("testnonpublic"); 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")));
} }


} }

+ 18
- 14
src/tests/junit/org/apache/tools/ant/taskdefs/optional/image/ImageTest.java View File

@@ -25,16 +25,17 @@ import org.junit.Before;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;


import java.io.File; 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.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse; import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;



/** /**
* Tests the Image task. * Tests the Image task.
* *
@@ -49,6 +50,9 @@ public class ImageTest {
@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
/* JAI depends on internal API removed in Java 9 */ /* JAI depends on internal API removed in Java 9 */
@@ -60,13 +64,13 @@ public class ImageTest {
@Test @Test
public void testEchoToLog() { public void testEchoToLog() {
buildRule.executeTarget("testEchoToLog"); buildRule.executeTarget("testEchoToLog");
assertContains("Processing File", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("Processing File"));
} }


@Test @Test
public void testSimpleScale() { public void testSimpleScale() {
buildRule.executeTarget("testSimpleScale"); buildRule.executeTarget("testSimpleScale");
assertContains("Processing File", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("Processing File"));


File f = new File(buildRule.getOutputDir(), LARGEIMAGE); File f = new File(buildRule.getOutputDir(), LARGEIMAGE);
assertTrue("Did not create " + f.getAbsolutePath(), f.exists()); assertTrue("Did not create " + f.getAbsolutePath(), f.exists());
@@ -75,13 +79,13 @@ public class ImageTest {
@Test @Test
public void testOverwriteTrue() { public void testOverwriteTrue() {
buildRule.executeTarget("testSimpleScale"); buildRule.executeTarget("testSimpleScale");
assertContains("Processing File", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("Processing File"));
File f = new File(buildRule.getOutputDir(), LARGEIMAGE); File f = new File(buildRule.getOutputDir(), LARGEIMAGE);
assumeTrue("Could not change file modification date", assumeTrue("Could not change file modification date",
f.setLastModified(f.lastModified() - FILE_UTILS.getFileTimestampGranularity() * 2)); f.setLastModified(f.lastModified() - FILE_UTILS.getFileTimestampGranularity() * 2));
long lastModified = f.lastModified(); long lastModified = f.lastModified();
buildRule.executeTarget("testOverwriteTrue"); buildRule.executeTarget("testOverwriteTrue");
assertContains("Processing File", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("Processing File"));
f = new File(buildRule.getOutputDir(), LARGEIMAGE); f = new File(buildRule.getOutputDir(), LARGEIMAGE);
long overwrittenLastModified = f.lastModified(); long overwrittenLastModified = f.lastModified();
assertTrue("File was not overwritten.", lastModified < overwrittenLastModified); assertTrue("File was not overwritten.", lastModified < overwrittenLastModified);
@@ -90,11 +94,11 @@ public class ImageTest {
@Test @Test
public void testOverwriteFalse() { public void testOverwriteFalse() {
buildRule.executeTarget("testSimpleScale"); buildRule.executeTarget("testSimpleScale");
assertContains("Processing File", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("Processing File"));
File f = new File(buildRule.getOutputDir(), LARGEIMAGE); File f = new File(buildRule.getOutputDir(), LARGEIMAGE);
long lastModified = f.lastModified(); long lastModified = f.lastModified();
buildRule.executeTarget("testOverwriteFalse"); buildRule.executeTarget("testOverwriteFalse");
assertContains("Processing File", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("Processing File"));
f = new File(buildRule.getOutputDir(), LARGEIMAGE); f = new File(buildRule.getOutputDir(), LARGEIMAGE);
long overwrittenLastModified = f.lastModified(); long overwrittenLastModified = f.lastModified();
assertEquals("File was overwritten.", lastModified, overwrittenLastModified); assertEquals("File was overwritten.", lastModified, overwrittenLastModified);
@@ -103,7 +107,7 @@ public class ImageTest {
@Test @Test
public void testSimpleScaleWithMapper() { public void testSimpleScaleWithMapper() {
buildRule.executeTarget("testSimpleScaleWithMapper"); buildRule.executeTarget("testSimpleScaleWithMapper");
assertContains("Processing File", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("Processing File"));
File f = new File(buildRule.getOutputDir(), "scaled-" + LARGEIMAGE); File f = new File(buildRule.getOutputDir(), "scaled-" + LARGEIMAGE);
assertTrue("Did not create " + f.getAbsolutePath(), f.exists()); assertTrue("Did not create " + f.getAbsolutePath(), f.exists());
} }
@@ -111,13 +115,13 @@ public class ImageTest {
@Test @Test
@Ignore("Previously named in a manner to prevent execution") @Ignore("Previously named in a manner to prevent execution")
public void testFailOnError() { public void testFailOnError() {
final String message = "Unable to render RenderedOp for this operation.";
thrown.expect(RuntimeException.class);
thrown.expectMessage(message);
try { try {
buildRule.executeTarget("testFailOnError"); 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));
} }
} }




+ 8
- 8
src/tests/junit/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTest.java View File

@@ -23,7 +23,8 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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. * Testcase for the JDepend optional task.
@@ -46,8 +47,7 @@ public class JDependTest {
@Test @Test
public void testSimple() { public void testSimple() {
buildRule.executeTarget("simple"); 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 @Test
public void testXml() { public void testXml() {
buildRule.executeTarget("xml"); buildRule.executeTarget("xml");
assertContains("<DependsUpon>", buildRule.getOutput());
assertThat(buildRule.getOutput(), containsString("<DependsUpon>"));
} }


/** /**
@@ -66,7 +66,7 @@ public class JDependTest {
@Test @Test
public void testFork() { public void testFork() {
buildRule.executeTarget("fork"); 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 @Test
public void testForkXml() { public void testForkXml() {
buildRule.executeTarget("fork-xml"); buildRule.executeTarget("fork-xml");
assertContains("<DependsUpon>", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("<DependsUpon>"));
} }


/** /**
@@ -84,7 +84,7 @@ public class JDependTest {
@Test @Test
public void testTimeout() { public void testTimeout() {
buildRule.executeTarget("fork-xml"); 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 @Test
public void testTimeoutNot() { public void testTimeoutNot() {
buildRule.executeTarget("fork-timeout-not"); 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"));
} }


} }

+ 8
- 7
src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitReportTest.java View File

@@ -18,9 +18,10 @@


package org.apache.tools.ant.taskdefs.optional.junit; 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.assertFalse;
import static org.junit.Assert.assertTrue; 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 static org.junit.Assume.assumeTrue;


import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
@@ -100,21 +101,21 @@ public class JUnitReportTest {
public void testEmptyFile() { public void testEmptyFile() {
buildRule.executeTarget("testEmptyFile"); buildRule.executeTarget("testEmptyFile");
assertIndexCreated(); 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 @Test
public void testIncompleteFile() { public void testIncompleteFile() {
buildRule.executeTarget("testIncompleteFile"); buildRule.executeTarget("testIncompleteFile");
assertIndexCreated(); 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 @Test
public void testWrongElement() { public void testWrongElement() {
buildRule.executeTarget("testWrongElement"); buildRule.executeTarget("testWrongElement");
assertIndexCreated(); 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 // Bugzilla Report 34963
@@ -126,8 +127,8 @@ public class JUnitReportTest {
try { try {
r = new FileReader(new File(buildRule.getOutputDir(), "html/sampleproject/coins/0_CoinTest.html")); r = new FileReader(new File(buildRule.getOutputDir(), "html/sampleproject/coins/0_CoinTest.html"));
String report = FileUtils.readFully(r); String report = FileUtils.readFully(r);
assertContains("output must contain <br>:\n" + report, "junit.framework.AssertionFailedError: DOEG<br>", report);
assertContains("#51049: output must translate line breaks:\n" + report, "cur['line.separator'] = '\\r\\n';", report);
assertThat("output must contain <br>:\n" + report, report, containsString("junit.framework.AssertionFailedError: DOEG<br>"));
assertThat("#51049: output must translate line breaks:\n" + report, report, containsString("cur['line.separator'] = '\\r\\n';"));
} finally { } finally {
FileUtils.close(r); FileUtils.close(r);
} }
@@ -179,7 +180,7 @@ public class JUnitReportTest {
@Test @Test
public void testWithParams() throws Exception { public void testWithParams() throws Exception {
buildRule.executeTarget("testWithParams"); buildRule.executeTarget("testWithParams");
assertContains("key1=value1,key2=value2", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("key1=value1,key2=value2"));
commonIndexFileAssertions(); commonIndexFileAssertions();
} }




+ 37
- 36
src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTaskTest.java View File

@@ -17,15 +17,16 @@
*/ */
package org.apache.tools.ant.taskdefs.optional.junit; 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.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue; 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.BufferedReader;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
@@ -179,26 +180,26 @@ public class JUnitTaskTest {
collectorFile.exists()); collectorFile.exists());
// the passing test cases // the passing test cases
buildRule.executeTarget("A.test01"); 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"); 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"); 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"); 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"); 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"); 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 // the failing test cases
buildRule.executeTarget("A.test02"); 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"); 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"); 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"); 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 // 2nd junit run: should do only failing tests
@@ -208,26 +209,26 @@ public class JUnitTaskTest {
collectorFile.exists()); collectorFile.exists());
// the passing test cases // the passing test cases
buildRule.executeTarget("A.test01"); 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"); 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"); 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"); 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"); 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"); 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 // the failing test cases
buildRule.executeTarget("A.test02"); 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"); 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"); 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"); 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 // "fix" errors in class A
@@ -239,13 +240,13 @@ public class JUnitTaskTest {
+ "' should exist after the 3rd run.", + "' should exist after the 3rd run.",
collectorFile.exists()); collectorFile.exists());
buildRule.executeTarget("A.test02"); 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"); 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"); 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"); 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 // 4rd run: two running tests with errors
@@ -254,12 +255,12 @@ public class JUnitTaskTest {
+ "' should exist after the 4th run.", + "' should exist after the 4th run.",
collectorFile.exists()); collectorFile.exists());
//TODO: these two statements fail //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"); 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"); 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 @Test
public void testMultilineAssertsNoFork() { public void testMultilineAssertsNoFork() {
buildRule.executeTarget("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 // Bugzilla Issue 45411
@Test @Test
public void testMultilineAssertsFork() { public void testMultilineAssertsFork() {
buildRule.executeTarget("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) { private void assertResultFilesExist(String target, String extension) {
@@ -301,8 +302,8 @@ public class JUnitTaskTest {
} }


private void assertNoPrint(String result, String where) { 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 { private void assertOutput() throws IOException {


+ 19
- 18
src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestListenerTest.java View File

@@ -18,8 +18,9 @@


package org.apache.tools.ant.taskdefs.optional.junit; 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.apache.tools.ant.BuildFileRule;
import org.junit.Before; import org.junit.Before;
@@ -48,55 +49,55 @@ public class JUnitTestListenerTest {
public void testFullLogOutput() { public void testFullLogOutput() {
buildRule.getProject().setProperty("enableEvents", "true"); buildRule.getProject().setProperty("enableEvents", "true");
buildRule.executeTarget(PASS_TEST_TARGET); 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 @Test
public void testNoLogOutput() { public void testNoLogOutput() {
buildRule.getProject().setProperty("enableEvents", "true"); buildRule.getProject().setProperty("enableEvents", "true");
buildRule.executeTarget(PASS_TEST_TARGET); 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 @Test
public void testTestCountFired() { public void testTestCountFired() {
buildRule.getProject().setProperty("enableEvents", "true"); buildRule.getProject().setProperty("enableEvents", "true");
buildRule.executeTarget(PASS_TEST_TARGET); 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 @Test
public void testStartTestFired() { public void testStartTestFired() {
buildRule.getProject().setProperty("enableEvents", "true"); buildRule.getProject().setProperty("enableEvents", "true");
buildRule.executeTarget(PASS_TEST_TARGET); 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 @Test
public void testEndTestFired() { public void testEndTestFired() {
buildRule.getProject().setProperty("enableEvents", "true"); buildRule.getProject().setProperty("enableEvents", "true");
buildRule.executeTarget(PASS_TEST_TARGET); 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 @Test
public void testNoFullLogOutputByDefault() { public void testNoFullLogOutputByDefault() {
buildRule.executeTarget(PASS_TEST_TARGET); 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 @Test
public void testFullLogOutputMagicProperty() { public void testFullLogOutputMagicProperty() {
buildRule.getProject().setProperty(JUnitTask.ENABLE_TESTLISTENER_EVENTS, "true"); buildRule.getProject().setProperty(JUnitTask.ENABLE_TESTLISTENER_EVENTS, "true");
buildRule.executeTarget(PASS_TEST_TARGET); 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 @Test
@@ -104,8 +105,8 @@ public class JUnitTestListenerTest {
buildRule.getProject().setProperty(JUnitTask.ENABLE_TESTLISTENER_EVENTS, "false"); buildRule.getProject().setProperty(JUnitTask.ENABLE_TESTLISTENER_EVENTS, "false");
buildRule.getProject().setProperty("enableEvents", "true"); buildRule.getProject().setProperty("enableEvents", "true");
buildRule.executeTarget(PASS_TEST_TARGET); 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)));
} }


} }

+ 3
- 2
src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java View File

@@ -28,9 +28,10 @@ import junit.framework.TestSuite;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.junit.Test; import org.junit.Test;


import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


/** /**
@@ -81,7 +82,7 @@ public class JUnitTestRunnerTest {
runner.run(); runner.run();
String error = runner.getFormatter().getError(); String error = runner.getFormatter().getError();
assertEquals(error, JUnitTestRunner.ERRORS, runner.getRetCode()); 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 // check that something which is not a testcase generates no errors


+ 5
- 4
src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/TearDownOnVmCrashTest.java View File

@@ -18,9 +18,10 @@


package org.apache.tools.ant.taskdefs.optional.junit; 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.assertEquals;
import static org.junit.Assert.assertThat;


import org.apache.tools.ant.BuildFileRule; import org.apache.tools.ant.BuildFileRule;
import org.junit.Before; import org.junit.Before;
@@ -41,13 +42,13 @@ public class TearDownOnVmCrashTest {
public void testNoTeardown() { public void testNoTeardown() {
buildRule.executeTarget("testNoTeardown"); buildRule.executeTarget("testNoTeardown");
assertEquals("true", buildRule.getProject().getProperty("error")); assertEquals("true", buildRule.getProject().getProperty("error"));
assertNotContains("tearDown called on Timeout", buildRule.getOutput());
assertThat(buildRule.getOutput(), not(containsString("tearDown called on Timeout")));
} }


@Test @Test
public void testTeardown() { public void testTeardown() {
buildRule.executeTarget("testTeardown"); buildRule.executeTarget("testTeardown");
assertEquals("true", buildRule.getProject().getProperty("error")); assertEquals("true", buildRule.getProject().getProperty("error"));
assertContains("tearDown called on Timeout", buildRule.getOutput());
assertThat(buildRule.getOutput(), containsString("tearDown called on Timeout"));
} }
} }

+ 4
- 3
src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLFormatterWithCDATAOnSystemOut.java View File

@@ -26,7 +26,8 @@ import org.apache.tools.ant.util.FileUtils;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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 { public class XMLFormatterWithCDATAOnSystemOut {


@@ -66,8 +67,8 @@ public class XMLFormatterWithCDATAOnSystemOut {
buildRule.executeTarget("run-junit"); buildRule.executeTarget("run-junit");
File f = buildRule.getProject().resolveFile(REPORT); File f = buildRule.getProject().resolveFile(REPORT);
try (FileReader reader = new FileReader(f)) { try (FileReader reader = new FileReader(f)) {
String content = FileUtils.readFully(reader);
assertTrue(content.contains("</RESPONSE>&#x5d;&#x5d;&gt;</ERROR>"));
assertThat(FileUtils.readFully(reader),
containsString("</RESPONSE>&#x5d;&#x5d;&gt;</ERROR>"));
} finally { } finally {
f.delete(); f.delete();
} }


+ 3
- 2
src/tests/junit/org/apache/tools/ant/taskdefs/optional/net/FTPTest.java View File

@@ -17,8 +17,9 @@
*/ */
package org.apache.tools.ant.taskdefs.optional.net; 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.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;
@@ -212,7 +213,7 @@ public class FTPTest {
@Test @Test
public void testGetWithSelector() { public void testGetWithSelector() {
buildRule.executeTarget("ftp-get-with-selector"); 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"); FileSet fsDestination = buildRule.getProject().getReference("fileset-destination-without-selector");
DirectoryScanner dsDestination = fsDestination.getDirectoryScanner(buildRule.getProject()); DirectoryScanner dsDestination = fsDestination.getDirectoryScanner(buildRule.getProject());
dsDestination.scan(); dsDestination.scan();


+ 33
- 51
src/tests/junit/org/apache/tools/ant/taskdefs/optional/script/ScriptDefTest.java View File

@@ -24,13 +24,14 @@ import org.apache.tools.ant.types.FileSet;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;


import java.io.File; 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.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


/** /**
* Tests the examples of the &lt;scriptdef&gt; task. * Tests the examples of the &lt;scriptdef&gt; task.
@@ -42,6 +43,9 @@ public class ScriptDefTest {
@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/taskdefs/optional/script/scriptdef.xml"); buildRule.configureProject("src/etc/testcases/taskdefs/optional/script/scriptdef.xml");
@@ -55,31 +59,24 @@ public class ScriptDefTest {
FileSet fileset = p.getReference("testfileset"); FileSet fileset = p.getReference("testfileset");
File baseDir = fileset.getDir(p); File baseDir = fileset.getDir(p);
String log = buildRule.getLog(); 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 @Test
public void testNoLang() { 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 @Test
public void testNoName() { 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 @Test
@@ -90,11 +87,10 @@ public class ScriptDefTest {
FileSet fileset = p.getReference("testfileset"); FileSet fileset = p.getReference("testfileset");
File baseDir = fileset.getDir(p); File baseDir = fileset.getDir(p);
String log = buildRule.getLog(); 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 @Test
@@ -105,39 +101,31 @@ public class ScriptDefTest {


@Test @Test
public void testException() { 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 @Test
public void testDoubleDef() { public void testDoubleDef() {
buildRule.executeTarget("doubledef"); buildRule.executeTarget("doubledef");
String log = buildRule.getLog(); 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 @Test
public void testDoubleAttribute() { 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 @Test
public void testProperty() { public void testProperty() {
buildRule.executeTarget("property"); 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 @Test
@@ -149,17 +137,11 @@ public class ScriptDefTest {


@Test @Test
public void testUseSrcAndEncodingFailure() { public void testUseSrcAndEncodingFailure() {
thrown.expect(BuildException.class);
thrown.expectMessage("expected <eacute [\u00e9]> but was <eacute [\u00c3\u00a9]>");
final String readerEncoding = "ISO-8859-1"; final String readerEncoding = "ISO-8859-1";
buildRule.getProject().setProperty("useSrcAndEncoding.reader.encoding", readerEncoding); 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 <eacute \\[\u00e9]> but was <eacute \\[\u00c3\u00a9]>"));
}
buildRule.executeTarget("useSrcAndEncoding");
} }


@Test @Test


+ 22
- 24
src/tests/junit/org/apache/tools/ant/types/AddTypeTest.java View File

@@ -26,15 +26,19 @@ import org.apache.tools.ant.taskdefs.condition.Condition;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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 { public class AddTypeTest {


@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/types/addtype.xml"); buildRule.configureProject("src/etc/testcases/types/addtype.xml");
@@ -63,71 +67,65 @@ public class AddTypeTest {
@Test @Test
public void testNestedA() { public void testNestedA() {
buildRule.executeTarget("nested.a"); buildRule.executeTarget("nested.a");
assertContains("add A called", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("add A called"));
} }


@Test @Test
public void testNestedB() { public void testNestedB() {
buildRule.executeTarget("nested.b"); buildRule.executeTarget("nested.b");
assertContains("add B called", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("add B called"));
} }


@Test @Test
public void testNestedC() { public void testNestedC() {
buildRule.executeTarget("nested.c"); buildRule.executeTarget("nested.c");
assertContains("add C called", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("add C called"));
} }


@Test @Test
public void testNestedAB() { 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 @Test
public void testConditionType() { public void testConditionType() {
buildRule.executeTarget("condition.type"); buildRule.executeTarget("condition.type");
assertContains("beforeafter", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("beforeafter"));
} }


@Test @Test
public void testConditionTask() { public void testConditionTask() {
buildRule.executeTarget("condition.task"); buildRule.executeTarget("condition.task");
assertContains("My Condition execution", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("My Condition execution"));
} }


@Test @Test
public void testConditionConditionType() { public void testConditionConditionType() {
buildRule.executeTarget("condition.condition.type"); buildRule.executeTarget("condition.condition.type");
assertContains("My Condition eval", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("My Condition eval"));
} }


@Test @Test
public void testConditionConditionTask() { 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 @Test
public void testAddConfigured() { public void testAddConfigured() {
buildRule.executeTarget("myaddconfigured"); 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 @Test
public void testAddConfiguredValue() { public void testAddConfiguredValue() {
buildRule.executeTarget("myaddconfiguredvalue"); 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 @Test


+ 19
- 23
src/tests/junit/org/apache/tools/ant/types/AssertionsTest.java View File

@@ -22,9 +22,10 @@ import org.apache.tools.ant.BuildFileRule;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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; import static org.junit.Assume.assumeFalse;


/** /**
@@ -35,23 +36,22 @@ public class AssertionsTest {
@Rule @Rule
public BuildFileRule buildRule = new BuildFileRule(); public BuildFileRule buildRule = new BuildFileRule();


@Rule
public ExpectedException thrown = ExpectedException.none();

@Before @Before
public void setUp() { public void setUp() {
buildRule.configureProject("src/etc/testcases/types/assertions.xml"); buildRule.configureProject("src/etc/testcases/types/assertions.xml");
} }



/** /**
* runs a test and expects an assertion thrown in forked code * runs a test and expects an assertion thrown in forked code
* @param target String * @param target String
*/ */
private void expectAssertion(String target) { 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 @Test
@@ -91,29 +91,25 @@ public class AssertionsTest {


@Test @Test
public void testMultipleAssertions() { 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 @Test
public void testReferenceAbuse() { 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 @Test
public void testNofork() { 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"); 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 @Test


+ 7
- 5
src/tests/junit/org/apache/tools/ant/types/CommandlineJavaTest.java View File

@@ -18,18 +18,21 @@


package org.apache.tools.ant.types; package org.apache.tools.ant.types;



import org.apache.tools.ant.MagicNames; import org.apache.tools.ant.MagicNames;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; 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 * JUnit testcases for org.apache.tools.ant.CommandlineJava
@@ -93,8 +96,7 @@ public class CommandlineJavaTest {
// assertEquals("with classpath", "java", s[0]); // assertEquals("with classpath", "java", s[0]);
assertEquals("with classpath", "-Djava.compiler=NONE", s[1]); assertEquals("with classpath", "-Djava.compiler=NONE", s[1]);
assertEquals("with classpath", "-classpath", s[2]); 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")); assertTrue("ant.jar contained", s[3].endsWith("ant.jar"));
assertEquals("with classpath", "junit.textui.TestRunner", s[4]); assertEquals("with classpath", "junit.textui.TestRunner", s[4]);
assertEquals("with classpath", assertEquals("with classpath",


+ 3
- 1
src/tests/junit/org/apache/tools/ant/types/PathTest.java View File

@@ -27,7 +27,9 @@ import org.apache.tools.ant.taskdefs.condition.Os;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;


import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


@@ -572,7 +574,7 @@ public class PathTest {
assertEquals(0, p.list().length); assertEquals(0, p.list().length);
} catch (BuildException x) { } catch (BuildException x) {
String m = x.toString(); String m = x.toString();
assertTrue(m, m.contains("circular"));
assertThat(m, m, containsString("circular"));
} }
} }




+ 6
- 5
src/tests/junit/org/apache/tools/ant/types/PolyTest.java View File

@@ -25,7 +25,8 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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 { public class PolyTest {


@@ -40,25 +41,25 @@ public class PolyTest {
@Test @Test
public void testFileSet() { public void testFileSet() {
buildRule.executeTarget("fileset"); buildRule.executeTarget("fileset");
assertContains("types.FileSet", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("types.FileSet"));
} }


@Test @Test
public void testFileSetAntType() { public void testFileSetAntType() {
buildRule.executeTarget("fileset-ant-type"); buildRule.executeTarget("fileset-ant-type");
assertContains("types.PolyTest$MyFileSet", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("types.PolyTest$MyFileSet"));
} }


@Test @Test
public void testPath() { public void testPath() {
buildRule.executeTarget("path"); buildRule.executeTarget("path");
assertContains("types.Path", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("types.Path"));
} }


@Test @Test
public void testPathAntType() { public void testPathAntType() {
buildRule.executeTarget("path-ant-type"); buildRule.executeTarget("path-ant-type");
assertContains("types.PolyTest$MyPath", buildRule.getLog());
assertThat(buildRule.getLog(), containsString("types.PolyTest$MyPath"));
} }


public static class MyFileSet extends FileSet { public static class MyFileSet extends FileSet {


+ 5
- 4
src/tests/junit/org/apache/tools/ant/types/RedirectorElementTest.java View File

@@ -24,7 +24,9 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


@@ -73,9 +75,8 @@ public class RedirectorElementTest {
@Test @Test
public void testLogInputString() { public void testLogInputString() {
buildRule.executeTarget("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 @Test


+ 3
- 2
src/tests/junit/org/apache/tools/ant/types/optional/ScriptSelectorTest.java View File

@@ -23,7 +23,8 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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; import static org.junit.Assert.fail;


/** /**
@@ -45,7 +46,7 @@ public class ScriptSelectorTest {
buildRule.executeTarget("testNolanguage"); buildRule.executeTarget("testNolanguage");
fail("Absence of language attribute not detected"); fail("Absence of language attribute not detected");
} catch (BuildException ex) { } catch (BuildException ex) {
assertContains("script language must be specified", ex.getMessage());
assertThat(ex.getMessage(), containsString("script language must be specified"));


} }
} }


+ 5
- 3
src/tests/junit/org/apache/tools/ant/types/selectors/ModifiedSelectorTest.java View File

@@ -46,11 +46,12 @@ import org.junit.Ignore;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.junit.Assume.assumeNotNull; import static org.junit.Assume.assumeNotNull;
@@ -184,7 +185,8 @@ public class ModifiedSelectorTest {


assertNotNull("'fs.full.value' must be set.", fsFullValue); assertNotNull("'fs.full.value' must be set.", fsFullValue);
assertNotEquals("'fs.full.value' must not be null.", "", 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); assertNotNull("'fs.mod.value' must be set.", fsModValue);
// must be empty according to the Mock* implementations // must be empty according to the Mock* implementations
@@ -569,7 +571,7 @@ public class ModifiedSelectorTest {
public void testResourceSelectorSelresTrue() { public void testResourceSelectorSelresTrue() {
BFT bft = new BFT(); BFT bft = new BFT();
bft.doTarget("modifiedselectortest-ResourceSelresTrue"); bft.doTarget("modifiedselectortest-ResourceSelresTrue");
assertContains("does not provide an InputStream", bft.getLog());
assertThat(bft.getLog(), containsString("does not provide an InputStream"));
bft.deleteCachefile(); bft.deleteCachefile();
} }




+ 43
- 45
src/tests/junit/org/apache/tools/ant/util/LayoutPreservingPropertiesTest.java View File

@@ -26,8 +26,10 @@ import java.util.Properties;


import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;


public class LayoutPreservingPropertiesTest { public class LayoutPreservingPropertiesTest {
@@ -60,8 +62,8 @@ public class LayoutPreservingPropertiesTest {


// and now make sure that the comments made it into the new file // and now make sure that the comments made it into the new file
String s = readFile(tmp); 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 // and check that the resulting file looks okay
String s = readFile(tmp); 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 // and check that the resulting file looks okay
String s = readFile(tmp); 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 @Test
@@ -172,13 +172,13 @@ public class LayoutPreservingPropertiesTest {
// and check that the resulting file looks okay // and check that the resulting file looks okay
String s = readFile(tmp); 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 @Test
@@ -198,9 +198,8 @@ public class LayoutPreservingPropertiesTest {
// and check that the resulting file looks okay // and check that the resulting file looks okay
String s = readFile(tmp); 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 @Test
@@ -222,8 +221,8 @@ public class LayoutPreservingPropertiesTest {
// and check that the resulting file looks okay // and check that the resulting file looks okay
String s = readFile(tmp); 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 @Test
@@ -254,13 +253,12 @@ public class LayoutPreservingPropertiesTest {
String s2 = readFile(tmp2); String s2 = readFile(tmp2);


// check original is untouched // 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 // 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 @Test
@@ -285,16 +283,16 @@ public class LayoutPreservingPropertiesTest {
// and check that the resulting file looks okay // and check that the resulting file looks okay
String s = readFile(tmp); 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 { private static String readFile(File f) throws IOException {


Loading…
Cancel
Save