From e62a76a40cb7a0d008bdc324cc3b096b0796295d Mon Sep 17 00:00:00 2001 From: Harsh J Date: Fri, 25 May 2012 06:38:16 +0000 Subject: [PATCH] HADOOP-8323. Add javadoc and tests for Text.clear() behavior (harsh) git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1342514 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-common/CHANGES.txt | 2 ++ .../main/java/org/apache/hadoop/io/Text.java | 5 ++++ .../java/org/apache/hadoop/io/TestText.java | 24 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index ba1c7d87b2..1551d5ea25 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -179,6 +179,8 @@ Release 2.0.1-alpha - UNRELEASED HADOOP-8422. Deprecate FileSystem#getDefault* and getServerDefault methods that don't take a Path argument. (eli) + HADOOP-8323. Add javadoc and tests for Text.clear() behavior (harsh) + BUG FIXES HADOOP-8372. NetUtils.normalizeHostName() incorrectly handles hostname diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/Text.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/Text.java index 0bee33236d..78748b05f8 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/Text.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/Text.java @@ -236,6 +236,11 @@ public void append(byte[] utf8, int start, int len) { /** * Clear the string to empty. + * + * Note: For performance reasons, this call does not clear the + * underlying byte array that is retrievable via {@link #getBytes()}. + * In order to free the byte-array memory, call {@link #set(byte[])} + * with an empty byte array (For example, new byte[0]). */ public void clear() { length = 0; diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestText.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestText.java index a86c532bad..9bf83b906f 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestText.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestText.java @@ -241,6 +241,30 @@ public void testValidate() throws Exception { Text.validateUTF8(utf8, 0, length); } + public void testClear() throws Exception { + // Test lengths on an empty text object + Text text = new Text(); + assertEquals( + "Actual string on an empty text object must be an empty string", + "", text.toString()); + assertEquals("Underlying byte array length must be zero", + 0, text.getBytes().length); + assertEquals("String's length must be zero", + 0, text.getLength()); + + // Test if clear works as intended + text = new Text("abcd\u20acbdcd\u20ac"); + int len = text.getLength(); + text.clear(); + assertEquals("String must be empty after clear()", + "", text.toString()); + assertTrue( + "Length of the byte array must not decrease after clear()", + text.getBytes().length >= len); + assertEquals("Length of the string must be reset to 0 after clear()", + 0, text.getLength()); + } + public void testTextText() throws CharacterCodingException { Text a=new Text("abc"); Text b=new Text("a");