From a390fd653ca45cb32cee7b25050ae70a4f1b55c9 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 2 Feb 2018 10:25:53 +0100 Subject: [PATCH] BZ 62071 - fix error message when fileset.setFile is called twice --- WHATSNEW | 5 +++++ .../tools/ant/types/AbstractFileSet.java | 11 +++++++++- .../tools/ant/types/AbstractFileSetTest.java | 20 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/WHATSNEW b/WHATSNEW index 1430c8d77..923f75a9a 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -30,6 +30,11 @@ Fixed bugs: streams of a process, could end up being truncated. Bugzilla Report 58833, 58451 + * // will now throw an exception + with a more useful error message when setFile is called twice on + the same instance. + Bugzilla Report 62071 + Other changes: -------------- diff --git a/src/main/org/apache/tools/ant/types/AbstractFileSet.java b/src/main/org/apache/tools/ant/types/AbstractFileSet.java index 7d1ac4aba..0c1d57d4e 100644 --- a/src/main/org/apache/tools/ant/types/AbstractFileSet.java +++ b/src/main/org/apache/tools/ant/types/AbstractFileSet.java @@ -232,7 +232,16 @@ public abstract class AbstractFileSet extends DataType if (isReference()) { throw tooManyAttributes(); } - if (getDir() != null) { + if (fileAttributeUsed) { + if (getDir().equals(file.getParentFile())) { + String[] includes = defaultPatterns.getIncludePatterns(getProject()); + if (includes.length == 1 && includes[0].equals(file.getName())) { + // NOOP, setFile has been invoked twice with the same parameter + return; + } + } + throw new BuildException("setFile cannot be called twice with different arguments"); + } else if (getDir() != null) { throw dirAndFileAreMutuallyExclusive(); } setDir(file.getParentFile()); diff --git a/src/tests/junit/org/apache/tools/ant/types/AbstractFileSetTest.java b/src/tests/junit/org/apache/tools/ant/types/AbstractFileSetTest.java index 56ee49819..a025b2bf1 100644 --- a/src/tests/junit/org/apache/tools/ant/types/AbstractFileSetTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/AbstractFileSetTest.java @@ -244,4 +244,24 @@ public abstract class AbstractFileSetTest { File dir = f1.getDir(project); assertEquals("Dir is basedir", dir, project.getBaseDir()); } + + @Test + public void canCallSetFileTwiceWithSameArgument() { + AbstractFileSet f = getInstance(); + f.setFile(new File("/a")); + f.setFile(new File("/a")); + // really only asserts no exception is thrown + } + + @Test + public void cantCallSetFileTwiceWithDifferentArguments() { + AbstractFileSet f = getInstance(); + f.setFile(new File("/a")); + try { + f.setFile(new File("/b")); + fail("expected an exception"); + } catch (BuildException ex) { + assertEquals("setFile cannot be called twice with different arguments", ex.getMessage()); + } + } }