From 4b1d9ae3a6bf6ebdec5fd1d6e347da2f286cbb7a Mon Sep 17 00:00:00 2001 From: Gintas Grigelionis Date: Sun, 22 Apr 2018 23:55:41 +0200 Subject: [PATCH] More matchers/fixtures/optimizations --- .../tools/ant/DirectoryScannerTest.java | 21 +- .../org/apache/tools/ant/ProjectTest.java | 3 +- .../tools/ant/filters/TokenFilterTest.java | 43 +-- .../ant/taskdefs/ManifestClassPathTest.java | 3 +- .../ant/taskdefs/ProtectedJarMethodsTest.java | 18 +- .../apache/tools/ant/taskdefs/SyncTest.java | 7 +- .../compilers/DefaultCompilerAdapterTest.java | 7 +- .../optional/junit/JUnitTaskTest.java | 8 +- .../optional/junit/JUnitTestRunnerTest.java | 2 +- .../ant/taskdefs/optional/ssh/ScpTest.java | 4 +- .../taskdefs/optional/unix/SymlinkTest.java | 36 +-- .../ant/taskdefs/optional/vss/MSVSSTest.java | 8 +- .../apache/tools/ant/types/FileListTest.java | 179 ++++++------ .../apache/tools/ant/types/FilterSetTest.java | 26 +- .../apache/tools/ant/types/MapperTest.java | 15 +- .../tools/ant/types/PatternSetTest.java | 255 ++++++++--------- .../ant/types/resources/ResourceListTest.java | 119 ++++---- .../ant/types/selectors/DateSelectorTest.java | 1 - .../ant/types/selectors/TypeSelectorTest.java | 1 - .../apache/tools/ant/util/FileUtilsTest.java | 259 +++++++++--------- 20 files changed, 502 insertions(+), 513 deletions(-) diff --git a/src/tests/junit/org/apache/tools/ant/DirectoryScannerTest.java b/src/tests/junit/org/apache/tools/ant/DirectoryScannerTest.java index a44480797..33fe2a9f9 100644 --- a/src/tests/junit/org/apache/tools/ant/DirectoryScannerTest.java +++ b/src/tests/junit/org/apache/tools/ant/DirectoryScannerTest.java @@ -18,8 +18,11 @@ package org.apache.tools.ant; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeFalse; import static org.junit.Assume.assumeTrue; @@ -395,7 +398,7 @@ public class DirectoryScannerTest { Set set = ds.getScannedDirs(); assertFalse("empty set", set.isEmpty()); String s = "alpha/beta/gamma/".replace('/', File.separatorChar); - assertFalse("scanned " + s, set.contains(s)); + assertThat("scanned " + s, set, not(hasItem(s))); } @Test @@ -515,16 +518,16 @@ public class DirectoryScannerTest { ds.scan(); List dirs = Arrays.asList(ds.getExcludedDirectories()); assertEquals(2, dirs.size()); - assertTrue("beta is excluded", dirs.contains("alpha/beta" - .replace('/', File.separatorChar))); - assertTrue("gamma is excluded", dirs.contains("alpha/beta/gamma" - .replace('/', File.separatorChar))); + assertThat("beta is excluded", dirs, + hasItem("alpha/beta".replace('/', File.separatorChar))); + assertThat("gamma is excluded", dirs, + hasItem("alpha/beta/gamma".replace('/', File.separatorChar))); List files = Arrays.asList(ds.getExcludedFiles()); assertEquals(2, files.size()); - assertTrue("beta.xml is excluded", files.contains("alpha/beta/beta.xml" - .replace('/', File.separatorChar))); - assertTrue("gamma.xml is excluded", files.contains("alpha/beta/gamma/gamma.xml" - .replace('/', File.separatorChar))); + assertThat("beta.xml is excluded", files, + hasItem("alpha/beta/beta.xml".replace('/', File.separatorChar))); + assertThat("gamma.xml is excluded", files, + hasItem("alpha/beta/gamma/gamma.xml".replace('/', File.separatorChar))); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/ProjectTest.java b/src/tests/junit/org/apache/tools/ant/ProjectTest.java index 62d59ddf4..ef1f8d5f2 100644 --- a/src/tests/junit/org/apache/tools/ant/ProjectTest.java +++ b/src/tests/junit/org/apache/tools/ant/ProjectTest.java @@ -34,6 +34,7 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.hasValue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -262,7 +263,7 @@ public class ProjectTest { @Test public void testTaskDefinitionContains() { - assertTrue(p.getTaskDefinitions().contains(org.apache.tools.ant.taskdefs.Echo.class)); + assertThat(p.getTaskDefinitions(), hasValue(org.apache.tools.ant.taskdefs.Echo.class)); } /** diff --git a/src/tests/junit/org/apache/tools/ant/filters/TokenFilterTest.java b/src/tests/junit/org/apache/tools/ant/filters/TokenFilterTest.java index 30f5bc0be..aff9de819 100644 --- a/src/tests/junit/org/apache/tools/ant/filters/TokenFilterTest.java +++ b/src/tests/junit/org/apache/tools/ant/filters/TokenFilterTest.java @@ -21,10 +21,11 @@ package org.apache.tools.ant.filters; import static org.hamcrest.Matchers.both; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertThat; -import static org.junit.Assume.assumeTrue; +import static org.junit.Assume.assumeThat; import java.io.FileReader; import java.io.IOException; @@ -115,8 +116,9 @@ public class TokenFilterTest { public void testReplaceRegex() throws IOException { buildRule.executeTarget("hasregex"); - assumeTrue("Regex not present", - getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp").contains("bye world")); + assumeThat("Regex not present", + getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp"), + containsString("bye world")); buildRule.executeTarget("replaceregex"); String contents = getFileString(buildRule.getProject().getProperty("output") + "/replaceregex"); @@ -131,8 +133,9 @@ public class TokenFilterTest { @Test public void testFilterReplaceRegex() throws IOException { buildRule.executeTarget("hasregex"); - assumeTrue("Regex not present", - getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp").contains("bye world")); + assumeThat("Regex not present", + getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp"), + containsString("bye world")); buildRule.executeTarget("filterreplaceregex"); assertThat(getFileString(buildRule.getProject().getProperty("output") + "/filterreplaceregex"), @@ -143,8 +146,9 @@ public class TokenFilterTest { @Test public void testHandleDollerMatch() throws IOException { buildRule.executeTarget("hasregex"); - assumeTrue("Regex not present", - getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp").contains("bye world")); + assumeThat("Regex not present", + getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp"), + containsString("bye world")); buildRule.executeTarget("dollermatch"); } @@ -184,9 +188,12 @@ public class TokenFilterTest { @Test public void testContainsRegex() throws IOException { buildRule.executeTarget("hasregex"); - assumeTrue("Regex not present", getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp").contains("bye world")); + assertThat("Regex not present", + getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp"), + containsString("bye world")); - //expectFileContains(buildRule.getProject().getProperty("output") + "/replaceregexp", "bye world"); + // assertThat(buildRule.getProject().getProperty("output") + "/replaceregexp", + // containsString("bye world")); buildRule.executeTarget("containsregex"); String contents = getFileString(buildRule.getProject().getProperty("output") + "/containsregex"); @@ -198,8 +205,9 @@ public class TokenFilterTest { @Test public void testFilterContainsRegex() throws IOException { buildRule.executeTarget("hasregex"); - assumeTrue("Regex not present", - getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp").contains("bye world")); + assumeThat("Regex not present", + getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp"), + containsString("bye world")); buildRule.executeTarget("filtercontainsregex"); String contents = getFileString(buildRule.getProject().getProperty("output") + "/filtercontainsregex"); @@ -211,8 +219,9 @@ public class TokenFilterTest { @Test public void testContainsRegex2() throws IOException { buildRule.executeTarget("hasregex"); - assumeTrue("Regex not present", - getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp").contains("bye world")); + assumeThat("Regex not present", + getFileString(buildRule.getProject().getProperty("output") + "/replaceregexp"), + containsString("bye world")); buildRule.executeTarget("containsregex2"); assertThat(getFileString(buildRule.getProject().getProperty("output") + "/containsregex2"), @@ -230,8 +239,8 @@ public class TokenFilterTest { @Test public void testScriptFilter() throws IOException { - assumeTrue("Project does not have 'testScriptFilter' target", - buildRule.getProject().getTargets().contains("testScriptFilter")); + assumeThat("Project does not have 'testScriptFilter' target", + buildRule.getProject().getTargets(), hasKey("testScriptFilter")); buildRule.executeTarget("scriptfilter"); assertThat(getFileString(buildRule.getProject().getProperty("output") + "/scriptfilter"), containsString("HELLO WORLD")); @@ -240,8 +249,8 @@ public class TokenFilterTest { @Test public void testScriptFilter2() throws IOException { - assumeTrue("Project does not have 'testScriptFilter' target", - buildRule.getProject().getTargets().contains("testScriptFilter")); + assumeThat("Project does not have 'testScriptFilter' target", + buildRule.getProject().getTargets(), hasKey("testScriptFilter")); buildRule.executeTarget("scriptfilter2"); assertThat(getFileString(buildRule.getProject().getProperty("output") + "/scriptfilter2"), containsString("HELLO MOON")); diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestClassPathTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestClassPathTest.java index b1a34d194..2698e6e28 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestClassPathTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestClassPathTest.java @@ -234,7 +234,8 @@ public class ManifestClassPathTest { String tmpCanonicalPath = tmpdir.getCanonicalPath(); driveLetter = tmpCanonicalPath.substring(0, 1).toUpperCase(); } catch (IOException ioe) { - System.out.println("exception happened getting canonical path of java.io.tmpdir : " + ioe.getMessage()); + System.out.println("exception happened getting canonical path of java.io.tmpdir : " + + ioe.getMessage()); } String altDriveLetter = null; try { diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ProtectedJarMethodsTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ProtectedJarMethodsTest.java index 1d1a6706a..032203a8a 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/ProtectedJarMethodsTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ProtectedJarMethodsTest.java @@ -21,6 +21,7 @@ package org.apache.tools.ant.taskdefs; import java.io.File; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.apache.tools.ant.BuildFileRule; @@ -28,9 +29,10 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import static org.hamcrest.Matchers.hasItem; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertThat; /** */ @@ -61,16 +63,14 @@ public class ProtectedJarMethodsTest { "foo", }; Jar.grabFilesAndDirs(archive, dirs, files); + assertEquals(expectedDirs.length, dirs.size()); - for (String expectedDir : expectedDirs) { - assertTrue("Found " + expectedDir, - dirs.contains(expectedDir)); - } + Arrays.stream(expectedDirs).forEach(expectedDir -> assertThat("Found " + expectedDir, + dirs, hasItem(expectedDir))); + assertEquals(expectedFiles.length, files.size()); - for (String expectedFile : expectedFiles) { - assertTrue("Found " + expectedFile, - files.contains(expectedFile)); - } + Arrays.stream(expectedFiles).forEach(expectedFile -> assertThat("Found " + expectedFile, + files, hasItem(expectedFile))); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/SyncTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/SyncTest.java index 15d82a919..e98b56d60 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/SyncTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/SyncTest.java @@ -25,6 +25,7 @@ import org.junit.Test; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @@ -139,12 +140,10 @@ public class SyncTest { } public void assertFileIsPresent(String f) { - assertTrue("Expected file " + f, - buildRule.getProject().resolveFile(f).exists()); + assertTrue("Expected file " + f, buildRule.getProject().resolveFile(f).exists()); } public void assertFileIsNotPresent(String f) { - assertTrue("Didn't expect file " + f, - !buildRule.getProject().resolveFile(f).exists()); + assertFalse("Didn't expect file " + f, buildRule.getProject().resolveFile(f).exists()); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java index 9c33cdfcb..139a53cd5 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/compilers/DefaultCompilerAdapterTest.java @@ -35,8 +35,9 @@ import org.apache.tools.ant.types.Path; import org.apache.tools.ant.util.FileUtils; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @@ -270,7 +271,7 @@ public class DefaultCompilerAdapterTest { assertNotNull(cmd[0]); final List cmdLine = Arrays.asList(cmd[0].getCommandline()); //No modulesourcepath - assertFalse(cmdLine.contains("--module-source-path")); + assertThat(cmdLine, not(hasItem("--module-source-path"))); //The -sourcepath has to be followed by src int index = cmdLine.indexOf("-sourcepath"); assertTrue(index != -1 && index < cmdLine.size() - 1); @@ -328,7 +329,7 @@ public class DefaultCompilerAdapterTest { assertNotNull(cmd[0]); final List cmdLine = Arrays.asList(cmd[0].getCommandline()); //No sourcepath - assertFalse(cmdLine.contains("-sourcepath")); + assertThat(cmdLine, not(hasItem("-sourcepath"))); //The --module-source-path has to be followed by the pattern int index = cmdLine.indexOf("--module-source-path"); assertTrue(index != -1 && index < cmdLine.size() - 1); diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTaskTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTaskTest.java index 0eeb833f4..1f2c0d7ad 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTaskTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTaskTest.java @@ -588,8 +588,8 @@ public class JUnitTaskTest { public void testCheckDuplicateAntJar() throws Exception { setupCheckDuplicateTest(); buildRule.executeTarget("testCheckForkedPath"); - assertTrue("Expecting the warning about the duplicate ant jar", - buildRule.getLog().contains("WARNING: multiple versions of ant detected in path for junit")); + assertThat("Expecting the warning about the duplicate ant jar", buildRule.getLog(), + containsString("WARNING: multiple versions of ant detected in path for junit")); } @Test @@ -597,8 +597,8 @@ public class JUnitTaskTest { setupCheckDuplicateTest(); buildRule.getProject().setProperty("includeantruntime", "no"); buildRule.executeTarget("testCheckForkedPath"); - assertFalse("Unexpected warning about the duplicate ant jar", - buildRule.getLog().contains("WARNING: multiple versions of ant detected in path for junit")); + assertThat("Unexpected warning about the duplicate ant jar", buildRule.getLog(), + not(containsString("WARNING: multiple versions of ant detected in path for junit"))); } private void delete(File f) { diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java index e68eb3063..ef5c28629 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java @@ -135,7 +135,7 @@ public class JUnitTestRunnerTest { public void testInvalidTestCase() { TestRunner runner = createRunner(InvalidTestCase.class); runner.run(); - // On junit3 this is a FAILURE, on junit4 this is an ERROR + // For JUnit 3 this is a FAILURE, for JUnit 4 this is an ERROR int ret = runner.getRetCode(); if (ret != JUnitTestRunner.FAILURES && ret != JUnitTestRunner.ERRORS) { fail("Unexpected result " + ret + " from junit runner"); diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ssh/ScpTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ssh/ScpTest.java index 6df995417..80283ef51 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ssh/ScpTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/ssh/ScpTest.java @@ -78,9 +78,7 @@ public class ScpTest { @After public void tearDown() { - for (File file : cleanUpList) { - file.delete(); - } + cleanUpList.forEach(File::delete); } @Test diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/unix/SymlinkTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/unix/SymlinkTest.java index 717baecb0..0bc3f6b1c 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/unix/SymlinkTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/unix/SymlinkTest.java @@ -42,8 +42,8 @@ import org.junit.Test; 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.assertTrue; -import static org.junit.Assert.fail; import static org.junit.Assume.assumeTrue; import java.io.File; @@ -88,12 +88,10 @@ public class SymlinkTest { public void testDelete() { buildRule.executeTarget("test-delete"); Project p = buildRule.getProject(); - String linkDeleted = p.getProperty("test.delete.link.still.there"); assertNotNull("Actual file deleted by symlink", p.getProperty("test.delete.file.still.there")); - if (linkDeleted != null) { - fail(linkDeleted); - } + String linkDeleted = p.getProperty("test.delete.link.still.there"); + assertNull(linkDeleted, linkDeleted); } @Test @@ -144,11 +142,7 @@ public class SymlinkTest { p.getProperty("test.record.dir2.recorded")); String dir3rec = p.getProperty("test.record.dir3.recorded"); - - if (dir3rec != null) { - fail(dir3rec); - } - + assertNull(dir3rec, dir3rec); } @Test @@ -159,18 +153,11 @@ public class SymlinkTest { String link2Rem = p.getProperty("test.recreate.link2.not.removed"); String link3Rem = p.getProperty("test.recreate.link3.not.removed"); String dirlinkRem = p.getProperty("test.recreate.dirlink.not.removed"); - if (link1Rem != null) { - fail(link1Rem); - } - if (link2Rem != null) { - fail(link2Rem); - } - if (link3Rem != null) { - fail(link3Rem); - } - if (dirlinkRem != null) { - fail(dirlinkRem); - } + + assertNull(link1Rem, link1Rem); + assertNull(link2Rem ,link2Rem); + assertNull(link3Rem ,link3Rem); + assertNull(dirlinkRem ,dirlinkRem); assertNotNull("Failed to recreate link1", p.getProperty("test.recreate.link1.recreated")); @@ -182,10 +169,7 @@ public class SymlinkTest { p.getProperty("test.recreate.dirlink.recreated")); String doubleRecreate = p.getProperty("test.recreate.dirlink2.recreated.twice"); - - if (doubleRecreate != null) { - fail(doubleRecreate); - } + assertNull(doubleRecreate, doubleRecreate); assertNotNull("Failed to alter dirlink3", p.getProperty("test.recreate.dirlink3.was.altered")); diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/vss/MSVSSTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/vss/MSVSSTest.java index 77d32ab83..72504a485 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/vss/MSVSSTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/vss/MSVSSTest.java @@ -19,6 +19,7 @@ package org.apache.tools.ant.taskdefs.optional.vss; import java.io.File; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; @@ -461,12 +462,7 @@ public class MSVSSTest implements MSVSSConstants { } // Count the number of empty strings - int cnt = 0; - for (String argument : sGeneratedCmdLine) { - if (argument.isEmpty()) { - cnt++; - } - } + int cnt = (int) Arrays.stream(sGeneratedCmdLine).filter(String::isEmpty).count(); // We have extra elements assertFalse("extra args", genLength - cnt > sTestCmdLine.length); } diff --git a/src/tests/junit/org/apache/tools/ant/types/FileListTest.java b/src/tests/junit/org/apache/tools/ant/types/FileListTest.java index 0fe59f72a..f7e2e982f 100644 --- a/src/tests/junit/org/apache/tools/ant/types/FileListTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/FileListTest.java @@ -23,11 +23,11 @@ import org.apache.tools.ant.BuildFileRule; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import java.io.File; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; /** * Some tests for filelist. @@ -35,111 +35,118 @@ import static org.junit.Assert.fail; public class FileListTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Rule public BuildFileRule buildRule = new BuildFileRule(); + private FileList f; + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/types/filelist.xml"); + f = new FileList(); } + /** + * Can add reference to FileList with directory attribute set. + */ @Test - public void testEmptyElementIfIsReference() { - FileList f = new FileList(); + public void testEmptyElementSetDirThenRefid() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify more than one attribute when using refid"); f.setDir(buildRule.getProject().resolveFile(".")); - try { - f.setRefid(new Reference(buildRule.getProject(), "dummyref")); - fail("Can add reference to FileList with directory attribute set."); - } catch (BuildException be) { - assertEquals("You must not specify more than one attribute when using refid", - be.getMessage()); - } + f.setRefid(new Reference(buildRule.getProject(), "dummyref")); + } - f = new FileList(); + @Test + public void testEmptyElementSetFilesThenRefid() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify more than one attribute when using refid"); f.setFiles("foo.xml,c/d/bar.xml"); - try { - f.setRefid(new Reference(buildRule.getProject(), "dummyref")); - fail("Can add reference to FileList with file attribute set."); - } catch (BuildException be) { - assertEquals("You must not specify more than one attribute when using refid", - be.getMessage()); - } + f.setRefid(new Reference(buildRule.getProject(), "dummyref")); + } - f = new FileList(); + @Test + public void testEmptyElementSetRefidThenFiles() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify more than one attribute when using refid"); + f.setRefid(new Reference(buildRule.getProject(), "dummyref")); + f.setFiles("a/b/foo.java"); + } + + @Test + public void testEmptyElementSetRefidThenDir() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify more than one attribute when using refid"); f.setRefid(new Reference(buildRule.getProject(), "dummyref")); - try { - f.setFiles("a/b/foo.java"); - fail("Can set files in FileList that is a reference."); - } catch (BuildException be) { - assertEquals("You must not specify more than one attribute when using refid", - be.getMessage()); - } - try { - f.setDir(buildRule.getProject().resolveFile(".")); - fail("Can set dir in FileList that is a reference."); - } catch (BuildException be) { - assertEquals("You must not specify more than one attribute when using refid", - be.getMessage()); - } + f.setDir(buildRule.getProject().resolveFile(".")); } @Test - public void testCircularReferenceCheck() { - FileList f = new FileList(); + public void testCircularReferenceCheckDir() { + thrown.expect(BuildException.class); + thrown.expectMessage("This data type contains a circular reference."); buildRule.getProject().addReference("dummy", f); f.setRefid(new Reference(buildRule.getProject(), "dummy")); - try { - f.getDir(buildRule.getProject()); - fail("Can make FileList a Reference to itself."); - } catch (BuildException be) { - assertEquals("This data type contains a circular reference.", - be.getMessage()); - } - try { - f.getFiles(buildRule.getProject()); - fail("Can make FileList a Reference to itself."); - } catch (BuildException be) { - assertEquals("This data type contains a circular reference.", - be.getMessage()); - } - - // dummy1 --> dummy2 --> dummy3 --> dummy1 - FileList f1 = new FileList(); - buildRule.getProject().addReference("dummy1", f1); - f1.setRefid(new Reference(buildRule.getProject(), "dummy2")); - FileList f2 = new FileList(); - buildRule.getProject().addReference("dummy2", f2); - f2.setRefid(new Reference(buildRule.getProject(), "dummy3")); - FileList f3 = new FileList(); - buildRule.getProject().addReference("dummy3", f3); - f3.setRefid(new Reference(buildRule.getProject(), "dummy1")); - try { - f1.getDir(buildRule.getProject()); - fail("Can make circular reference."); - } catch (BuildException be) { - assertEquals("This data type contains a circular reference.", - be.getMessage()); - } - try { - f1.getFiles(buildRule.getProject()); - fail("Can make circular reference."); - } catch (BuildException be) { - assertEquals("This data type contains a circular reference.", - be.getMessage()); - } - - // dummy1 --> dummy2 --> dummy3 + f.getDir(buildRule.getProject()); + } + + @Test + public void testCircularReferenceCheckFiles() { + thrown.expect(BuildException.class); + thrown.expectMessage("This data type contains a circular reference."); + buildRule.getProject().addReference("dummy", f); + f.setRefid(new Reference(buildRule.getProject(), "dummy")); + f.getFiles(buildRule.getProject()); + } + + @Test + public void testLoopReferenceCheckDir() { + thrown.expect(BuildException.class); + thrown.expectMessage("This data type contains a circular reference."); + // dummy --> dummyA --> dummyB --> dummy + buildRule.getProject().addReference("dummy", f); + f.setRefid(new Reference(buildRule.getProject(), "dummyA")); + FileList fa = new FileList(); + buildRule.getProject().addReference("dummyA", fa); + fa.setRefid(new Reference(buildRule.getProject(), "dummyB")); + FileList fb = new FileList(); + buildRule.getProject().addReference("dummyB", fb); + fb.setRefid(new Reference(buildRule.getProject(), "dummy")); + f.getDir(buildRule.getProject()); + } + + @Test + public void testLoopReferenceCheckFiles() { + thrown.expect(BuildException.class); + thrown.expectMessage("This data type contains a circular reference."); + // dummy --> dummyA --> dummyB --> dummy + buildRule.getProject().addReference("dummy", f); + f.setRefid(new Reference(buildRule.getProject(), "dummyA")); + FileList fa = new FileList(); + buildRule.getProject().addReference("dummyA", fa); + fa.setRefid(new Reference(buildRule.getProject(), "dummyB")); + FileList fb = new FileList(); + buildRule.getProject().addReference("dummyB", fb); + fb.setRefid(new Reference(buildRule.getProject(), "dummy")); + f.getFiles(buildRule.getProject()); + } + + @Test + public void testLoopReferenceCheck() { + // dummy --> dummyA --> dummyB // (which has the Project's basedir as root). - f1 = new FileList(); - buildRule.getProject().addReference("dummy1", f1); - f1.setRefid(new Reference(buildRule.getProject(), "dummy2")); - f2 = new FileList(); - buildRule.getProject().addReference("dummy2", f2); - f2.setRefid(new Reference(buildRule.getProject(), "dummy3")); - f3 = new FileList(); - buildRule.getProject().addReference("dummy3", f3); - f3.setDir(buildRule.getProject().resolveFile(".")); - File dir = f1.getDir(buildRule.getProject()); + buildRule.getProject().addReference("dummy", f); + f.setRefid(new Reference(buildRule.getProject(), "dummyA")); + FileList fa = new FileList(); + buildRule.getProject().addReference("dummyA", fa); + fa.setRefid(new Reference(buildRule.getProject(), "dummyB")); + FileList fb = new FileList(); + buildRule.getProject().addReference("dummyB", fb); + fb.setDir(buildRule.getProject().resolveFile(".")); + File dir = f.getDir(buildRule.getProject()); assertEquals("Dir is basedir", dir, buildRule.getProject().getBaseDir()); } diff --git a/src/tests/junit/org/apache/tools/ant/types/FilterSetTest.java b/src/tests/junit/org/apache/tools/ant/types/FilterSetTest.java index 92010bbf8..9f01a7a3b 100644 --- a/src/tests/junit/org/apache/tools/ant/types/FilterSetTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/FilterSetTest.java @@ -208,12 +208,7 @@ public class FilterSetTest { File file1 = new File(System.getProperty("root"), name1); File file2 = new File(System.getProperty("root"), name2); - - if (!file1.exists() || !file2.exists()) { - return false; - } - - if (file1.length() != file2.length()) { + if (!file1.exists() || !file2.exists() || file1.length() != file2.length()) { return false; } @@ -221,16 +216,15 @@ public class FilterSetTest { byte[] buffer1 = new byte[BUF_SIZE]; byte[] buffer2 = new byte[BUF_SIZE]; - @SuppressWarnings("resource") - FileInputStream fis1 = new FileInputStream(file1); - @SuppressWarnings("resource") - FileInputStream fis2 = new FileInputStream(file2); - int read = 0; - while ((read = fis1.read(buffer1)) != -1) { - fis2.read(buffer2); - for (int i = 0; i < read; ++i) { - if (buffer1[i] != buffer2[i]) { - return false; + try (FileInputStream fis1 = new FileInputStream(file1); + FileInputStream fis2 = new FileInputStream(file2)) { + int read = 0; + while ((read = fis1.read(buffer1)) != -1) { + fis2.read(buffer2); + for (int i = 0; i < read; ++i) { + if (buffer1[i] != buffer2[i]) { + return false; + } } } } diff --git a/src/tests/junit/org/apache/tools/ant/types/MapperTest.java b/src/tests/junit/org/apache/tools/ant/types/MapperTest.java index 2a17c30f4..b47eb26bf 100644 --- a/src/tests/junit/org/apache/tools/ant/types/MapperTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/MapperTest.java @@ -34,9 +34,11 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import static org.hamcrest.Matchers.hasItem; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; /** @@ -169,12 +171,9 @@ public class MapperTest { assertNotNull("no filenames mapped", targets); assertEquals("wrong number of filenames mapped", 3, targets.length); List list = Arrays.asList(targets); - assertTrue("cannot find expected target \"tofilename\"", - list.contains("tofilename")); - assertTrue("cannot find expected target \"fromfilename\"", - list.contains("fromfilename")); - assertTrue("cannot find expected target \"mergefile\"", - list.contains("mergefile")); + assertThat("cannot find expected target \"tofilename\"", list, hasItem("tofilename")); + assertThat("cannot find expected target \"fromfilename\"", list, hasItem("fromfilename")); + assertThat("cannot find expected target \"mergefile\"", list, hasItem("mergefile")); } /** @@ -216,8 +215,8 @@ public class MapperTest { assertNotNull("no filenames mapped", targets); assertEquals("wrong number of filenames mapped", 2, targets.length); List list = Arrays.asList(targets); - assertTrue("cannot find expected target \"def\"", list.contains("def")); - assertTrue("cannot find expected target \"ghi\"", list.contains("ghi")); + assertThat("cannot find expected target \"def\"", list, hasItem("def")); + assertThat("cannot find expected target \"ghi\"", list, hasItem("ghi")); targets = fileNameMapper.mapFileName("z"); assertNull(targets); diff --git a/src/tests/junit/org/apache/tools/ant/types/PatternSetTest.java b/src/tests/junit/org/apache/tools/ant/types/PatternSetTest.java index a4f27b0ce..1cd61e4fe 100644 --- a/src/tests/junit/org/apache/tools/ant/types/PatternSetTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/PatternSetTest.java @@ -21,12 +21,13 @@ package org.apache.tools.ant.types; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import java.io.File; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; /** * JUnit 4 testcases for org.apache.tools.ant.types.PatternSet. @@ -36,151 +37,160 @@ import static org.junit.Assert.fail; public class PatternSetTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + private Project project; + private PatternSet p; + @Before public void setUp() { project = new Project(); project.setBasedir("."); + p = new PatternSet(); } @Test - public void testEmptyElementIfIsReference() { - PatternSet p = new PatternSet(); + public void testEmptyElementSetIncludesThenRefid() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify more than one attribute when using refid"); p.setIncludes("**/*.java"); - try { - p.setRefid(new Reference(project, "dummyref")); - fail("Can add reference to PatternSet with elements from setIncludes"); - } catch (BuildException be) { - assertEquals("You must not specify more than one attribute when using refid", - be.getMessage()); - } + p.setRefid(new Reference(project, "dummyref")); + } - p = new PatternSet(); + @Test + public void testEmptyElementSetRefidThenIncludes() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify more than one attribute when using refid"); p.setRefid(new Reference(project, "dummyref")); - try { - p.setIncludes("**/*.java"); - fail("Can set includes in PatternSet that is a reference."); - } catch (BuildException be) { - assertEquals("You must not specify more than one attribute when using refid", - be.getMessage()); - } + p.setIncludes("**/*.java"); + } - p = new PatternSet(); + @Test + public void testEmptyElementSetRefidThenIncludesfile() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify more than one attribute when using refid"); p.setRefid(new Reference(project, "dummyref")); - try { p.setIncludesfile(new File("/a")); - fail("Can set includesfile in PatternSet that is a reference."); - } catch (BuildException be) { - assertEquals("You must not specify more than one attribute when using refid", - be.getMessage()); - } - try { - p.setExcludes("**/*.java"); - fail("Can set excludes in PatternSet that is a reference."); - } catch (BuildException be) { - assertEquals("You must not specify more than one attribute when using refid", - be.getMessage()); - } - try { - p.setExcludesfile(new File("/a")); - fail("Can set excludesfile in PatternSet that is a reference."); - } catch (BuildException be) { - assertEquals("You must not specify more than one attribute when using refid", - be.getMessage()); - } - try { - p.createInclude(); - fail("Can add nested include in PatternSet that is a reference."); - } catch (BuildException be) { - assertEquals("You must not specify nested elements when using refid", - be.getMessage()); - } - try { - p.createExclude(); - fail("Can add nested exclude in PatternSet that is a reference."); - } catch (BuildException be) { - assertEquals("You must not specify nested elements when using refid", - be.getMessage()); - } - try { - p.createIncludesFile(); - fail("Can add nested includesfile in PatternSet that is a reference."); - } catch (BuildException be) { - assertEquals("You must not specify nested elements when using refid", - be.getMessage()); - } - try { - p.createExcludesFile(); - fail("Can add nested excludesfile in PatternSet that is a reference."); - } catch (BuildException be) { - assertEquals("You must not specify nested elements when using refid", - be.getMessage()); - } } @Test - public void testCircularReferenceCheck() { - PatternSet p = new PatternSet(); + public void testEmptyElementSetRefidThenExclude() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify more than one attribute when using refid"); + p.setRefid(new Reference(project, "dummyref")); + p.setExcludes("**/*.java"); + } + + @Test + public void testEmptyElementSetRefidThenExcludesfile() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify more than one attribute when using refid"); + p.setRefid(new Reference(project, "dummyref")); + p.setExcludesfile(new File("/a")); + } + + @Test + public void testEmptyElementSetRefidThenAddInclude() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify nested elements when using refid"); + p.setRefid(new Reference(project, "dummyref")); + p.createInclude(); + } + + @Test + public void testEmptyElementSetRefidThenAddExclude() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify nested elements when using refid"); + p.setRefid(new Reference(project, "dummyref")); + p.createExclude(); + } + + @Test + public void testEmptyElementSetRefidThenAddIncludesfile() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify nested elements when using refid"); + p.setRefid(new Reference(project, "dummyref")); + p.createIncludesFile(); + } + + @Test + public void testEmptyElementSetRefidThenAddExcludesfile() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify nested elements when using refid"); + p.setRefid(new Reference(project, "dummyref")); + p.createExcludesFile(); + } + + @Test + public void testCircularReferenceCheckIncludePaterns() { + thrown.expect(BuildException.class); + thrown.expectMessage("This data type contains a circular reference."); + project.addReference("dummy", p); + p.setRefid(new Reference(project, "dummy")); + p.getIncludePatterns(project); + } + + @Test + public void testCircularReferenceCheckExcludePatterns() { + thrown.expect(BuildException.class); + thrown.expectMessage("This data type contains a circular reference."); project.addReference("dummy", p); p.setRefid(new Reference(project, "dummy")); - try { - p.getIncludePatterns(project); - fail("Can make PatternSet a Reference to itself."); - } catch (BuildException be) { - assertEquals("This data type contains a circular reference.", - be.getMessage()); - } - try { - p.getExcludePatterns(project); - fail("Can make PatternSet a Reference to itself."); - } catch (BuildException be) { - assertEquals("This data type contains a circular reference.", - be.getMessage()); - } - - // dummy1 --> dummy2 --> dummy3 --> dummy1 - PatternSet p1 = new PatternSet(); - project.addReference("dummy1", p1); - p1.setRefid(new Reference(project, "dummy2")); - PatternSet p2 = new PatternSet(); - project.addReference("dummy2", p2); - p2.setRefid(new Reference(project, "dummy3")); - PatternSet p3 = new PatternSet(); - project.addReference("dummy3", p3); - p3.setRefid(new Reference(project, "dummy1")); - try { - p1.getIncludePatterns(project); - fail("Can make circular reference."); - } catch (BuildException be) { - assertEquals("This data type contains a circular reference.", - be.getMessage()); - } - try { - p1.getExcludePatterns(project); - fail("Can make circular reference."); - } catch (BuildException be) { - assertEquals("This data type contains a circular reference.", - be.getMessage()); - } - - // dummy1 --> dummy2 --> dummy3 + p.getExcludePatterns(project); + } + + @Test + public void testLoopReferenceCheckIncludePaterns() { + thrown.expect(BuildException.class); + thrown.expectMessage("This data type contains a circular reference."); + // dummy --> dummyA --> dummyB --> dummy + project.addReference("dummy", p); + p.setRefid(new Reference(project, "dummyA")); + PatternSet pa = new PatternSet(); + project.addReference("dummyA", pa); + pa.setRefid(new Reference(project, "dummyB")); + PatternSet pb = new PatternSet(); + project.addReference("dummyB", pb); + pb.setRefid(new Reference(project, "dummy")); + p.getIncludePatterns(project); + } + + @Test + public void testLoopReferenceCheckExcludePaterns() { + thrown.expect(BuildException.class); + thrown.expectMessage("This data type contains a circular reference."); + // dummy --> dummyA --> dummyB --> dummy + project.addReference("dummy", p); + p.setRefid(new Reference(project, "dummyA")); + PatternSet pa = new PatternSet(); + project.addReference("dummyA", pa); + pa.setRefid(new Reference(project, "dummyB")); + PatternSet pb = new PatternSet(); + project.addReference("dummyB", pb); + pb.setRefid(new Reference(project, "dummy")); + p.getExcludePatterns(project); + } + + @Test + public void testLoopReferenceCheck() { + // dummy --> dummyA --> dummyB // (which holds patterns "include" and "exclude") - p1 = new PatternSet(); - project.addReference("dummy1", p1); - p1.setRefid(new Reference(project, "dummy2")); - p2 = new PatternSet(); - project.addReference("dummy2", p2); - p2.setRefid(new Reference(project, "dummy3")); - p3 = new PatternSet(); - project.addReference("dummy3", p3); - p3.setIncludes("include"); - p3.createExclude().setName("exclude"); - String[] i = p1.getIncludePatterns(project); + project.addReference("dummy", p); + p.setRefid(new Reference(project, "dummyA")); + PatternSet pa = new PatternSet(); + project.addReference("dummyA", pa); + pa.setRefid(new Reference(project, "dummyB")); + PatternSet pb = new PatternSet(); + project.addReference("dummyB", pb); + pb.setIncludes("include"); + pb.createExclude().setName("exclude"); + String[] i = p.getIncludePatterns(project); assertEquals("One include pattern buried deep inside a nested patternset structure", 1, i.length); assertEquals("include", i[0]); - i = p3.getExcludePatterns(project); + i = pb.getExcludePatterns(project); assertEquals("One exclude pattern buried deep inside a nested patternset structure", 1, i.length); assertEquals("exclude", i[0]); @@ -188,7 +198,6 @@ public class PatternSetTest { @Test public void testNestedPatternset() { - PatternSet p = new PatternSet(); p.setIncludes("**/*.java"); PatternSet nested = new PatternSet(); diff --git a/src/tests/junit/org/apache/tools/ant/types/resources/ResourceListTest.java b/src/tests/junit/org/apache/tools/ant/types/resources/ResourceListTest.java index e7c8d78b1..0c266ac74 100644 --- a/src/tests/junit/org/apache/tools/ant/types/resources/ResourceListTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/resources/ResourceListTest.java @@ -25,18 +25,22 @@ import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import org.junit.rules.ExpectedException; public class ResourceListTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Rule public BuildFileRule buildRule = new BuildFileRule(); + private ResourceList rl; + @Before public void setUp() { buildRule.configureProject("src/etc/testcases/types/resources/resourcelist.xml"); + rl = new ResourceList(); } @After @@ -45,90 +49,69 @@ public class ResourceListTest { } @Test - public void testEmptyElementWithReference() { - ResourceList rl = new ResourceList(); + public void testEmptyElementSetEncodingThenRefid() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify more than one attribute when using refid"); rl.setEncoding("foo"); - try { - rl.setRefid(new Reference(buildRule.getProject(), "dummyref")); - fail("Can add reference to ResourceList with encoding attribute set."); - } catch (BuildException be) { - assertEquals("You must not specify more than one attribute when using refid", - be.getMessage()); - } + rl.setRefid(new Reference(buildRule.getProject(), "dummyref")); + } - rl = new ResourceList(); + @Test + public void testEmptyElementSetRefidThenEncoding() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify more than one attribute when using refid"); rl.setRefid(new Reference(buildRule.getProject(), "dummyref")); - try { - rl.setEncoding("foo"); - fail("Can set encoding in ResourceList that is a reference"); - } catch (BuildException be) { - assertEquals("You must not specify more than one attribute when using refid", - be.getMessage()); - } + rl.setEncoding("foo"); + } - rl = new ResourceList(); + @Test + public void testEmptyElementAddFileResourceThenRefid() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify nested elements when using refid"); rl.add(new FileResource(buildRule.getProject(), ".")); - try { - rl.setRefid(new Reference(buildRule.getProject(), "dummyref")); - fail("Can add reference to ResourceList with nested resource collection."); - } catch (BuildException be) { - assertEquals("You must not specify nested elements when using refid", - be.getMessage()); - } + rl.setRefid(new Reference(buildRule.getProject(), "dummyref")); + } - rl = new ResourceList(); + @Test + public void testEmptyElementAddRefidThenFileResource() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify nested elements when using refid"); rl.setRefid(new Reference(buildRule.getProject(), "dummyref")); - try { - rl.add(new FileResource(buildRule.getProject(), ".")); - fail("Can add reference to ResourceList with nested resource collection."); - } catch (BuildException be) { - assertEquals("You must not specify nested elements when using refid", - be.getMessage()); - } + rl.add(new FileResource(buildRule.getProject(), ".")); + } - rl = new ResourceList(); + @Test + public void testEmptyElementAddFilterChainThenRefid() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify nested elements when using refid"); rl.addFilterChain(new FilterChain()); - try { - rl.setRefid(new Reference(buildRule.getProject(), "dummyref")); - fail("Can add reference to ResourceList with nested filter chain."); - } catch (BuildException be) { - assertEquals("You must not specify nested elements when using refid", - be.getMessage()); - } + rl.setRefid(new Reference(buildRule.getProject(), "dummyref")); + } - rl = new ResourceList(); + @Test + public void testEmptyElementAddRefidThenFilterChain() { + thrown.expect(BuildException.class); + thrown.expectMessage("You must not specify nested elements when using refid"); rl.setRefid(new Reference(buildRule.getProject(), "dummyref")); - try { - rl.addFilterChain(new FilterChain()); - fail("Can add reference to ResourceList with nested filter chain."); - } catch (BuildException be) { - assertEquals("You must not specify nested elements when using refid", - be.getMessage()); - } + rl.addFilterChain(new FilterChain()); } @Test public void testCircularReference() { - ResourceList rl1 = new ResourceList(); - rl1.setProject(buildRule.getProject()); - rl1.setRefid(new Reference(buildRule.getProject(), "foo")); + thrown.expect(BuildException.class); + thrown.expectMessage("This data type contains a circular reference."); + rl.setProject(buildRule.getProject()); + rl.setRefid(new Reference(buildRule.getProject(), "foo")); - ResourceList rl2 = new ResourceList(); - rl2.setProject(buildRule.getProject()); - buildRule.getProject().addReference("foo", rl2); + ResourceList resourceList = new ResourceList(); + resourceList.setProject(buildRule.getProject()); + buildRule.getProject().addReference("foo", resourceList); Union u = new Union(); - u.add(rl1); + u.add(rl); u.setProject(buildRule.getProject()); - rl2.add(u); - - try { - rl2.size(); - fail("Can make ResourceList a Reference to itself."); - } catch (BuildException be) { - assertEquals("This data type contains a circular reference.", - be.getMessage()); - } + resourceList.add(u); + resourceList.size(); } } diff --git a/src/tests/junit/org/apache/tools/ant/types/selectors/DateSelectorTest.java b/src/tests/junit/org/apache/tools/ant/types/selectors/DateSelectorTest.java index 4b9341302..3f7f3b16f 100644 --- a/src/tests/junit/org/apache/tools/ant/types/selectors/DateSelectorTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/selectors/DateSelectorTest.java @@ -26,7 +26,6 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; import static org.junit.Assume.assumeTrue; /** diff --git a/src/tests/junit/org/apache/tools/ant/types/selectors/TypeSelectorTest.java b/src/tests/junit/org/apache/tools/ant/types/selectors/TypeSelectorTest.java index 881411d22..449cc5d8b 100644 --- a/src/tests/junit/org/apache/tools/ant/types/selectors/TypeSelectorTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/selectors/TypeSelectorTest.java @@ -25,7 +25,6 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; /** * Tests Type Selectors. diff --git a/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java b/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java index 43520adc0..1945ad43d 100644 --- a/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java @@ -26,7 +26,9 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.taskdefs.condition.Os; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.startsWith; @@ -35,7 +37,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assume.assumeFalse; import static org.junit.Assume.assumeTrue; /** @@ -44,6 +46,9 @@ import static org.junit.Assume.assumeTrue; */ public class FileUtilsTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); private File removeThis; private String root; @@ -77,7 +82,8 @@ public class FileUtilsTest { FileOutputStream fos = new FileOutputStream(removeThis); fos.write(new byte[0]); fos.close(); - assumeTrue("Could not change file modified time", removeThis.setLastModified(removeThis.lastModified() - 2000)); + assumeTrue("Could not change file modified time", + removeThis.setLastModified(removeThis.lastModified() - 2000)); long modTime = removeThis.lastModified(); assertNotEquals(0, modTime); @@ -103,82 +109,90 @@ public class FileUtilsTest { } @Test - public void testResolveFile() { - if (!Os.isFamily("dos") && !Os.isFamily("netware")) { - /* - * Start with simple absolute file names. - */ - assertEquals(File.separator, - FILE_UTILS.resolveFile(null, "/").getPath()); - assertEquals(File.separator, - FILE_UTILS.resolveFile(null, "\\").getPath()); - } else { - assertEqualsIgnoreDriveCase(localize(File.separator), + public void testResolveFilePosix() { + assumeTrue("DOS or NetWare", !Os.isFamily("dos") && !Os.isFamily("netware")); + /* + * Start with simple absolute file names. + */ + assertEquals(File.separator, FILE_UTILS.resolveFile(null, "/").getPath()); + assertEquals(File.separator, FILE_UTILS.resolveFile(null, "\\").getPath()); + } + + @Test + public void testResolveFileDosOrNetware() { + assumeTrue("Not DOS or Netware", Os.isFamily("dos") || Os.isFamily("netware")); + assertEqualsIgnoreDriveCase(localize(File.separator), FILE_UTILS.resolveFile(null, "/").getPath()); - assertEqualsIgnoreDriveCase(localize(File.separator), + assertEqualsIgnoreDriveCase(localize(File.separator), FILE_UTILS.resolveFile(null, "\\").getPath()); - /* - * throw in drive letters - */ - String driveSpec = "C:"; - assertEquals(driveSpec + "\\", - FILE_UTILS.resolveFile(null, driveSpec + "/").getPath()); - assertEquals(driveSpec + "\\", - FILE_UTILS.resolveFile(null, driveSpec + "\\").getPath()); - String driveSpecLower = "c:"; - assertEquals(driveSpecLower + "\\", - FILE_UTILS.resolveFile(null, driveSpecLower + "/").getPath()); - assertEquals(driveSpecLower + "\\", - FILE_UTILS.resolveFile(null, driveSpecLower + "\\").getPath()); - /* - * promised to eliminate consecutive slashes after drive letter. - */ - assertEquals(driveSpec + "\\", - FILE_UTILS.resolveFile(null, driveSpec + "/////").getPath()); - assertEquals(driveSpec + "\\", - FILE_UTILS.resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath()); - } + /* + * throw in drive letters + */ + String driveSpec = "C:"; + assertEquals(driveSpec + "\\", + FILE_UTILS.resolveFile(null, driveSpec + "/").getPath()); + assertEquals(driveSpec + "\\", + FILE_UTILS.resolveFile(null, driveSpec + "\\").getPath()); + String driveSpecLower = "c:"; + assertEquals(driveSpecLower + "\\", + FILE_UTILS.resolveFile(null, driveSpecLower + "/").getPath()); + assertEquals(driveSpecLower + "\\", + FILE_UTILS.resolveFile(null, driveSpecLower + "\\").getPath()); + /* + * promised to eliminate consecutive slashes after drive letter. + */ + assertEquals(driveSpec + "\\", + FILE_UTILS.resolveFile(null, driveSpec + "/////").getPath()); + assertEquals(driveSpec + "\\", + FILE_UTILS.resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath()); + if (Os.isFamily("netware")) { /* * throw in NetWare volume names */ - String driveSpec = "SYS:"; + driveSpec = "SYS:"; assertEquals(driveSpec, - FILE_UTILS.resolveFile(null, driveSpec + "/").getPath()); + FILE_UTILS.resolveFile(null, driveSpec + "/").getPath()); assertEquals(driveSpec, - FILE_UTILS.resolveFile(null, driveSpec + "\\").getPath()); - String driveSpecLower = "sys:"; + FILE_UTILS.resolveFile(null, driveSpec + "\\").getPath()); + driveSpecLower = "sys:"; assertEquals(driveSpec, - FILE_UTILS.resolveFile(null, driveSpecLower + "/").getPath()); + FILE_UTILS.resolveFile(null, driveSpecLower + "/").getPath()); assertEquals(driveSpec, - FILE_UTILS.resolveFile(null, driveSpecLower + "\\").getPath()); + FILE_UTILS.resolveFile(null, driveSpecLower + "\\").getPath()); /* * promised to eliminate consecutive slashes after drive letter. */ assertEquals(driveSpec, - FILE_UTILS.resolveFile(null, driveSpec + "/////").getPath()); + FILE_UTILS.resolveFile(null, driveSpec + "/////").getPath()); assertEquals(driveSpec, - FILE_UTILS.resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath()); - } else if (!Os.isFamily("dos")) { - /* - * drive letters must be considered just normal filenames. - */ - String driveSpec = "C:"; - String udir = System.getProperty("user.dir"); - assertEquals(udir + File.separator + driveSpec, - FILE_UTILS.resolveFile(null, driveSpec + "/").getPath()); - assertEquals(udir + File.separator + driveSpec, - FILE_UTILS.resolveFile(null, driveSpec + "\\").getPath()); - String driveSpecLower = "c:"; - assertEquals(udir + File.separator + driveSpecLower, - FILE_UTILS.resolveFile(null, driveSpecLower + "/").getPath()); - assertEquals(udir + File.separator + driveSpecLower, - FILE_UTILS.resolveFile(null, driveSpecLower + "\\").getPath()); + FILE_UTILS.resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath()); } - + } + @Test + public void testResolveFileNotDos() { + assumeFalse("is DOS", Os.isFamily("dos")); /* - * Now test some relative file name magic. + * drive letters must be considered just normal filenames. */ + String driveSpec = "C:"; + String udir = System.getProperty("user.dir"); + assertEquals(udir + File.separator + driveSpec, + FILE_UTILS.resolveFile(null, driveSpec + "/").getPath()); + assertEquals(udir + File.separator + driveSpec, + FILE_UTILS.resolveFile(null, driveSpec + "\\").getPath()); + String driveSpecLower = "c:"; + assertEquals(udir + File.separator + driveSpecLower, + FILE_UTILS.resolveFile(null, driveSpecLower + "/").getPath()); + assertEquals(udir + File.separator + driveSpecLower, + FILE_UTILS.resolveFile(null, driveSpecLower + "\\").getPath()); + } + + /* + * Test some relative file name magic. + */ + @Test + public void testResolveRelativeFile() { assertEquals(localize("/1/2/3/4"), FILE_UTILS.resolveFile(new File(localize("/1/2/3")), "4").getPath()); assertEquals(localize("/1/2/3/4"), @@ -203,95 +217,94 @@ public class FileUtilsTest { } @Test - public void testNormalize() { - if (!Os.isFamily("dos") && !Os.isFamily("netware")) { - /* - * Start with simple absolute file names. - */ - assertEquals(File.separator, - FILE_UTILS.normalize("/").getPath()); - assertEquals(File.separator, - FILE_UTILS.normalize("\\").getPath()); - } else { - try { - FILE_UTILS.normalize("/").getPath(); - fail("normalized \"/\" on dos or netware"); - } catch (Exception e) { - } - try { - FILE_UTILS.normalize("\\").getPath(); - fail("normalized \"\\\" on dos or netware"); - } catch (Exception e) { - } + public void testNormalizePosix() { + assumeTrue("DOS or NetWare", !Os.isFamily("dos") && !Os.isFamily("netware")); + /* + * Start with simple absolute file names. + */ + assertEquals(File.separator, FILE_UTILS.normalize("/").getPath()); + assertEquals(File.separator, FILE_UTILS.normalize("\\").getPath()); + + // Expected exception caught + thrown.expect(BuildException.class); + String driveSpec = "C:"; + assertEquals(driveSpec, FILE_UTILS.normalize(driveSpec).getPath()); + } + + @Test + public void testNormalizeDosOrNetwareFailures() { + assumeTrue("Not DOS or Netware", Os.isFamily("dos") || Os.isFamily("netware")); + thrown.expect(IOException.class); + try { + FILE_UTILS.normalize("/").getPath(); + } finally { + FILE_UTILS.normalize("\\").getPath(); } + } + @Test + public void testNormalizeDosOrNetware() { + assumeTrue("Not DOS or Netware", Os.isFamily("dos") || Os.isFamily("netware")); if (Os.isFamily("dos")) { /* * throw in drive letters */ String driveSpec = "C:"; - try { - FILE_UTILS.normalize(driveSpec).getPath(); - fail(driveSpec + " is not an absolute path"); - } catch (Exception e) { - } assertEquals(driveSpec + "\\", - FILE_UTILS.normalize(driveSpec + "/").getPath()); + FILE_UTILS.normalize(driveSpec + "/").getPath()); assertEquals(driveSpec + "\\", - FILE_UTILS.normalize(driveSpec + "\\").getPath()); + FILE_UTILS.normalize(driveSpec + "\\").getPath()); String driveSpecLower = "c:"; assertEquals(driveSpecLower + "\\", - FILE_UTILS.normalize(driveSpecLower + "/").getPath()); + FILE_UTILS.normalize(driveSpecLower + "/").getPath()); assertEquals(driveSpecLower + "\\", - FILE_UTILS.normalize(driveSpecLower + "\\").getPath()); + FILE_UTILS.normalize(driveSpecLower + "\\").getPath()); /* * promised to eliminate consecutive slashes after drive letter. */ assertEquals(driveSpec + "\\", - FILE_UTILS.normalize(driveSpec + "/////").getPath()); + FILE_UTILS.normalize(driveSpec + "/////").getPath()); assertEquals(driveSpec + "\\", - FILE_UTILS.normalize(driveSpec + "\\\\\\\\\\\\").getPath()); + FILE_UTILS.normalize(driveSpec + "\\\\\\\\\\\\").getPath()); + + // Expected exception caught + thrown.expect(BuildException.class); + FILE_UTILS.normalize(driveSpec).getPath(); } else if (Os.isFamily("netware")) { /* * throw in NetWare volume names */ String driveSpec = "SYS:"; assertEquals(driveSpec, - FILE_UTILS.normalize(driveSpec).getPath()); + FILE_UTILS.normalize(driveSpec).getPath()); assertEquals(driveSpec, - FILE_UTILS.normalize(driveSpec + "/").getPath()); + FILE_UTILS.normalize(driveSpec + "/").getPath()); assertEquals(driveSpec, - FILE_UTILS.normalize(driveSpec + "\\").getPath()); + FILE_UTILS.normalize(driveSpec + "\\").getPath()); String driveSpecLower = "sys:"; assertEquals(driveSpec, - FILE_UTILS.normalize(driveSpecLower).getPath()); + FILE_UTILS.normalize(driveSpecLower).getPath()); assertEquals(driveSpec, - FILE_UTILS.normalize(driveSpecLower + "/").getPath()); + FILE_UTILS.normalize(driveSpecLower + "/").getPath()); assertEquals(driveSpec, - FILE_UTILS.normalize(driveSpecLower + "\\").getPath()); + FILE_UTILS.normalize(driveSpecLower + "\\").getPath()); assertEquals(driveSpec + "\\junk", - FILE_UTILS.normalize(driveSpecLower + "\\junk").getPath()); + FILE_UTILS.normalize(driveSpecLower + "\\junk").getPath()); /* * promised to eliminate consecutive slashes after drive letter. */ assertEquals(driveSpec, - FILE_UTILS.normalize(driveSpec + "/////").getPath()); + FILE_UTILS.normalize(driveSpec + "/////").getPath()); assertEquals(driveSpec, - FILE_UTILS.normalize(driveSpec + "\\\\\\\\\\\\").getPath()); - } else { - try { - String driveSpec = "C:"; - assertEquals(driveSpec, - FILE_UTILS.normalize(driveSpec).getPath()); - fail("Expected failure, C: isn't an absolute path on other os's"); - } catch (BuildException e) { - // Passed test - } + FILE_UTILS.normalize(driveSpec + "\\\\\\\\\\\\").getPath()); } + } - /* - * Now test some relative file name magic. - */ + /** + * Test some relative file name magic. + */ + @Test + public void testNormalizeRelativeFile() { assertEquals(localize("/1/2/3/4"), FILE_UTILS.normalize(localize("/1/2/3/4")).getPath()); assertEquals(localize("/1/2/3/4"), @@ -309,16 +322,13 @@ public class FileUtilsTest { assertEquals(localize("/1/2/3/4"), FILE_UTILS.normalize(localize("/1/2/3/..\\../5/..\\./2/./3/6\\../4")).getPath()); - try { - FILE_UTILS.normalize("foo"); - fail("foo is not an absolute path"); - } catch (BuildException e) { - // Expected exception caught - } - assertEquals("will not go outside FS root (but will not throw an exception either)", new File(localize("/1/../../b")), FILE_UTILS.normalize(localize("/1/../../b"))); + + // Expected exception caught + thrown.expect(BuildException.class); + FILE_UTILS.normalize("foo"); } /** @@ -326,15 +336,12 @@ public class FileUtilsTest { */ @Test public void testNullArgs() { - try { - FILE_UTILS.normalize(null); - fail("successfully normalized a null-file"); - } catch (NullPointerException npe) { - // Expected exception caught - } - File f = FILE_UTILS.resolveFile(null, "a"); assertEquals(f, new File("a").getAbsoluteFile()); + + // Expected exception caught + thrown.expect(NullPointerException.class); + FILE_UTILS.normalize(null); }