HADOOP-11165. TestUTF8 fails when run against java 8. Contributed by Stephen Chu.

(cherry picked from commit 85da71c2d3)
This commit is contained in:
cnauroth 2014-11-04 10:27:41 -08:00
parent b1fd0b6678
commit f4ed787669
2 changed files with 19 additions and 7 deletions

View File

@ -35,6 +35,9 @@ Release 2.7.0 - UNRELEASED
HADOOP-11186. documentation should talk about HADOOP-11186. documentation should talk about
hadoop.htrace.spanreceiver.classes, not hadoop.trace.spanreceiver.classes (cmccabe) hadoop.htrace.spanreceiver.classes, not hadoop.trace.spanreceiver.classes (cmccabe)
HADOOP-11165. TestUTF8 fails when run against java 8.
(Stephen Chu via cnauroth)
Release 2.6.0 - UNRELEASED Release 2.6.0 - UNRELEASED
INCOMPATIBLE CHANGES INCOMPATIBLE CHANGES

View File

@ -19,8 +19,11 @@
package org.apache.hadoop.io; package org.apache.hadoop.io;
import junit.framework.TestCase; import junit.framework.TestCase;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.UTFDataFormatException; import java.io.UTFDataFormatException;
import java.nio.ByteBuffer;
import java.util.Random; import java.util.Random;
import org.apache.hadoop.test.GenericTestUtils; import org.apache.hadoop.test.GenericTestUtils;
@ -54,11 +57,22 @@ public void testGetBytes() throws Exception {
// generate a random string // generate a random string
String before = getTestString(); String before = getTestString();
// check its utf8 // Check that the bytes are stored correctly in Modified-UTF8 format.
assertEquals(before, new String(UTF8.getBytes(before), "UTF-8")); // Note that the DataInput and DataOutput interfaces convert between
// bytes and Strings using the Modified-UTF8 format.
assertEquals(before, readModifiedUTF(UTF8.getBytes(before)));
} }
} }
private String readModifiedUTF(byte[] bytes) throws IOException {
final short lengthBytes = (short)2;
ByteBuffer bb = ByteBuffer.allocate(bytes.length + lengthBytes);
bb.putShort((short)bytes.length).put(bytes);
ByteArrayInputStream bis = new ByteArrayInputStream(bb.array());
DataInputStream dis = new DataInputStream(bis);
return dis.readUTF();
}
public void testIO() throws Exception { public void testIO() throws Exception {
DataOutputBuffer out = new DataOutputBuffer(); DataOutputBuffer out = new DataOutputBuffer();
DataInputBuffer in = new DataInputBuffer(); DataInputBuffer in = new DataInputBuffer();
@ -80,11 +94,6 @@ public void testIO() throws Exception {
in.reset(out.getData(), out.getLength()); in.reset(out.getData(), out.getLength());
String after2 = in.readUTF(); String after2 = in.readUTF();
assertEquals(before, after2); assertEquals(before, after2);
// test that it is compatible with Java's other decoder
String after3 = new String(out.getData(), 2, out.getLength()-2, "UTF-8");
assertEquals(before, after3);
} }
} }