Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 75A30200C16 for ; Thu, 9 Feb 2017 09:31:42 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 745DD160B4C; Thu, 9 Feb 2017 08:31:42 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 6EF94160B64 for ; Thu, 9 Feb 2017 09:31:41 +0100 (CET) Received: (qmail 20507 invoked by uid 500); 9 Feb 2017 08:31:40 -0000 Mailing-List: contact notifications-help@ant.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ant.apache.org Delivered-To: mailing list notifications@ant.apache.org Received: (qmail 20473 invoked by uid 99); 9 Feb 2017 08:31:40 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 09 Feb 2017 08:31:40 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 542ECDF9AB; Thu, 9 Feb 2017 08:31:40 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: jhm@apache.org To: notifications@ant.apache.org Date: Thu, 09 Feb 2017 08:31:42 -0000 Message-Id: <9a6df472437b4d688343d59d6e7f6882@git.apache.org> In-Reply-To: <6b3db7fcc4eb4567a5990ced73556a9b@git.apache.org> References: <6b3db7fcc4eb4567a5990ced73556a9b@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [3/4] ant git commit: [AA] Bugzilla Bug 60628: Change after code review by @janmaterne archived-at: Thu, 09 Feb 2017 08:31:42 -0000 [AA] Bugzilla Bug 60628: Change after code review by @janmaterne Project: http://git-wip-us.apache.org/repos/asf/ant/repo Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/198d7a2f Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/198d7a2f Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/198d7a2f Branch: refs/heads/master Commit: 198d7a2f449b91d7c559e17e2bc472269a8645ac Parents: 3d21510 Author: Arcadius Ahouansou Authored: Mon Jan 30 22:48:40 2017 +0000 Committer: Arcadius Ahouansou Committed: Mon Jan 30 22:48:40 2017 +0000 ---------------------------------------------------------------------- manual/Tasks/get.html | 11 +++---- src/etc/testcases/taskdefs/get.xml | 8 ++--- src/main/org/apache/tools/ant/taskdefs/Get.java | 32 ++++++-------------- .../org/apache/tools/ant/util/StringUtils.java | 21 +++++++++++++ .../org/apache/tools/ant/taskdefs/GetTest.java | 10 +++--- .../apache/tools/ant/util/StringUtilsTest.java | 27 ++++++++++++++--- 6 files changed, 67 insertions(+), 42 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ant/blob/198d7a2f/manual/Tasks/get.html ---------------------------------------------------------------------- diff --git a/manual/Tasks/get.html b/manual/Tasks/get.html index a6d89e9..e63b59d 100644 --- a/manual/Tasks/get.html +++ b/manual/Tasks/get.html @@ -183,12 +183,12 @@ plain text' authentication is used. This is only secure over an HTTPS link. name - The name or key of this header. + The name or key of this header. Cannot be null or empty. Leading and trailing spaces are removed Yes value - The value to assign to the. + The value to assign to the header. Cannot be null or empty. Leading and trailing spaces are removed Yes @@ -261,10 +261,9 @@ the input task to query for a password.

With custom HTTP headers

 <get src="http://ant.apache.org/index.html" dest="downloads">
-  <header name="header1" value=="headerValue1" />
-  <header name="header2" value=="headerValue2" />
-  <header name="header3" value=="headerValue3" />
-
+  <header name="header1" value="headerValue1" />
+  <header name="header2" value="headerValue2" />
+  <header name="header3" value="headerValue3" />
 </get>
 
http://git-wip-us.apache.org/repos/asf/ant/blob/198d7a2f/src/etc/testcases/taskdefs/get.xml ---------------------------------------------------------------------- diff --git a/src/etc/testcases/taskdefs/get.xml b/src/etc/testcases/taskdefs/get.xml index 569d833..188febd 100644 --- a/src/etc/testcases/taskdefs/get.xml +++ b/src/etc/testcases/taskdefs/get.xml @@ -98,21 +98,21 @@ - +
- +
- +
@@ -120,7 +120,7 @@ - +
http://git-wip-us.apache.org/repos/asf/ant/blob/198d7a2f/src/main/org/apache/tools/ant/taskdefs/Get.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/taskdefs/Get.java b/src/main/org/apache/tools/ant/taskdefs/Get.java index 2200bd8..674a535 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Get.java +++ b/src/main/org/apache/tools/ant/taskdefs/Get.java @@ -45,6 +45,8 @@ import org.apache.tools.ant.types.resources.URLProvider; import org.apache.tools.ant.types.resources.URLResource; import org.apache.tools.ant.util.FileNameMapper; import org.apache.tools.ant.util.FileUtils; +import org.apache.tools.ant.util.StringUtils; + import java.util.LinkedHashMap; import java.util.Map; @@ -495,28 +497,14 @@ public class Get extends Task { */ public void addConfiguredHeader(Header header) { if (header != null) { - String key = trimToNull(header.getName()); - String value = trimToNull(header.getValue()); + String key = StringUtils.trimToNull(header.getName()); + String value = StringUtils.trimToNull(header.getValue()); if (key != null && value != null) { this.headers.put(key, value); } } } - private String trimToNull(String inputString) { - - if (inputString == null) { - return null; - } - - inputString = inputString.trim(); - if ("".equals(inputString)) { - return null; - } - return inputString; - } - - /** * Define the mapper to map source to destination files. * @return a mapper to be configured. @@ -761,14 +749,14 @@ public class Get extends Task { connection.setRequestProperty("Accept-Encoding", GZIP_CONTENT_ENCODING); } - if (!headers.isEmpty()) { - for (final Map.Entry header : headers.entrySet()) { - //we do not log the header value as it may contain sensitive data like passwords - log(String.format("Adding header '%s' ", header.getKey())); - connection.setRequestProperty(header.getKey(), header.getValue()); - } + + for (final Map.Entry header : headers.entrySet()) { + //we do not log the header value as it may contain sensitive data like passwords + log(String.format("Adding header '%s' ", header.getKey())); + connection.setRequestProperty(header.getKey(), header.getValue()); } + if (connection instanceof HttpURLConnection) { ((HttpURLConnection) connection) .setInstanceFollowRedirects(false); http://git-wip-us.apache.org/repos/asf/ant/blob/198d7a2f/src/main/org/apache/tools/ant/util/StringUtils.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/tools/ant/util/StringUtils.java b/src/main/org/apache/tools/ant/util/StringUtils.java index 04f1ce8..6ee9c45 100644 --- a/src/main/org/apache/tools/ant/util/StringUtils.java +++ b/src/main/org/apache/tools/ant/util/StringUtils.java @@ -306,4 +306,25 @@ public final class StringUtils { private static Collector joining(CharSequence separator) { return separator == null ? Collectors.joining() : Collectors.joining(separator); } + + + /** + * @param inputString String to trim + * @return null if the input string is null or empty or contain only empty spaces. + * It returns the input string without leading and trailing spaces otherwise. + * + */ + public static String trimToNull(String inputString) { + + if (inputString == null) { + return null; + } + + String tmpString = inputString.trim(); + if ("".equals(tmpString)) { + return null; + } + return tmpString; + } + } http://git-wip-us.apache.org/repos/asf/ant/blob/198d7a2f/src/tests/junit/org/apache/tools/ant/taskdefs/GetTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/GetTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/GetTest.java index e18fdcd..fb78937 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/GetTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/GetTest.java @@ -124,8 +124,8 @@ public class GetTest { } @Test - public void testTwoHeaders() { - buildRule.executeTarget("testTwoHeaders"); + public void testTwoHeadersAreAddedOK() { + buildRule.executeTarget("testTwoHeadersAreAddedOK"); String log = buildRule.getLog(); AntAssert.assertContains("Adding header 'header1'", log); AntAssert.assertContains("Adding header 'header2'", log); @@ -133,13 +133,13 @@ public class GetTest { @Test public void testEmptyHeadersAreNeverAdded() { - buildRule.executeTarget("testEmptyHeaders"); + buildRule.executeTarget("testEmptyHeadersAreNeverAdded"); AntAssert.assertNotContains("Adding header", buildRule.getLog()); } @Test public void testThatWhenMoreThanOneHeaderHaveSameNameOnlyLastOneIsAdded() { - buildRule.executeTarget("testDuplicateHeaderNames"); + buildRule.executeTarget("testThatWhenMoreThanOneHeaderHaveSameNameOnlyLastOneIsAdded"); String log = buildRule.getLog(); AntAssert.assertContains("Adding header 'header1'", log); @@ -150,7 +150,7 @@ public class GetTest { @Test public void testHeaderSpaceTrimmed() { - buildRule.executeTarget("testHeaderSpacesTrimmed"); + buildRule.executeTarget("testHeaderSpaceTrimmed"); AntAssert.assertContains("Adding header 'header1'", buildRule.getLog()); } http://git-wip-us.apache.org/repos/asf/ant/blob/198d7a2f/src/tests/junit/org/apache/tools/ant/util/StringUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/ant/util/StringUtilsTest.java b/src/tests/junit/org/apache/tools/ant/util/StringUtilsTest.java index d2187c4..612c6ec 100644 --- a/src/tests/junit/org/apache/tools/ant/util/StringUtilsTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/StringUtilsTest.java @@ -17,16 +17,14 @@ */ package org.apache.tools.ant.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import java.util.Arrays; import java.util.Collection; import java.util.Vector; import org.junit.Test; +import static org.junit.Assert.*; + /** * Test for StringUtils */ @@ -195,5 +193,24 @@ public class StringUtilsTest { public void testJoinNullSeparator() { assertEquals("abc", StringUtils.join(Arrays.asList("a", "b", "c"), null)); } - + + @Test + public void testTrimToNullWithNullInput(){ + assertNull(StringUtils.trimToNull(null)); + } + + @Test + public void testTrimToNullWithEmptyInput(){ + assertNull(StringUtils.trimToNull("")); + } + + @Test + public void testTrimToNullWithBlankSpaceInput(){ + assertNull(StringUtils.trimToNull(" ")); + } + + @Test + public void testTrimToNullWithInputPaddedWithSpace(){ + assertEquals("aaBcDeF",StringUtils.trimToNull(" aaBcDeF ")); + } }