svn merge -c 1342514 from trunk. Backports HADOOP-8323 (harsh).

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1342516 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Harsh J 2012-05-25 06:44:31 +00:00
parent 92f0d2be53
commit dda59a5ef9
3 changed files with 31 additions and 0 deletions

View File

@ -24,6 +24,8 @@ Release 2.0.1-alpha - UNRELEASED
HADOOP-8403. bump up POMs version to 2.0.1-SNAPSHOT. (tucu)
HADOOP-8323. Add javadoc and tests for Text.clear() behavior (harsh)
BUG FIXES
HADOOP-8372. NetUtils.normalizeHostName() incorrectly handles hostname

View File

@ -236,6 +236,11 @@ public class Text extends BinaryComparable
/**
* Clear the string to empty.
*
* <em>Note</em>: 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, <code>new byte[0]</code>).
*/
public void clear() {
length = 0;

View File

@ -241,6 +241,30 @@ public class TestText extends TestCase {
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");