-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw IOException when the line is over the limit
Fail the read when the text file line is over the maxLineLength limit.
- Loading branch information
Ying Su
committed
Sep 11, 2018
1 parent
5fe57e5
commit 95bc0d1
Showing
2 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/test/java/com/facebook/presto/hadoop/TestLineReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.facebook.presto.hadoop; | ||
|
||
import org.apache.hadoop.io.Text; | ||
import org.apache.hadoop.util.LineReader; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
public class TestLineReader { | ||
|
||
@Test | ||
public void testDefaultReaderMaxBytesConsumed() | ||
{ | ||
byte[] input = "Hello world! Goodbye world!\n".getBytes(UTF_8); | ||
InputStream in = new ByteArrayInputStream(input); | ||
// Set the LineReader internal read buffer size 4 bytes | ||
LineReader reader = new LineReader(in, 4); | ||
Text str = new Text(); | ||
try { | ||
reader.readLine(str, 0, 10); | ||
} | ||
catch (IOException e) { | ||
// It should be 3 reads of 4 bytes each, so the final bytesConsumed is 12 | ||
assertEquals(e.getMessage(), "Too many bytes before newline: " + 12); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDefaultReaderMaxLineLength() | ||
{ | ||
byte[] input = "Hello world! Goodbye world!\n".getBytes(UTF_8); | ||
InputStream in = new ByteArrayInputStream(input); | ||
// Set the LineReader internal read buffer size 4 bytes | ||
LineReader reader = new LineReader(in, 4); | ||
Text str = new Text(); | ||
try { | ||
reader.readLine(str, 10, 100); | ||
} | ||
catch (IOException e) { | ||
assertEquals(e.getMessage(), "Too many bytes before newline: " + 10); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCustomReaderMaxBytesConsumed() | ||
{ | ||
byte[] input = "Hello world! Goodbye world!\n".getBytes(UTF_8); | ||
byte[] delimiter = "!".getBytes(UTF_8); | ||
InputStream in = new ByteArrayInputStream(input); | ||
// Set the LineReader internal read buffer size 4 bytes | ||
LineReader reader = new LineReader(in, 4, delimiter); | ||
Text str = new Text(); | ||
try { | ||
reader.readLine(str, 0, 5); | ||
} | ||
catch (IOException e) { | ||
// It should be 2 reads of 4 bytes each, so the final bytesConsumed is 8 | ||
assertEquals(e.getMessage(), "Too many bytes before delimiter: " + 8); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCustomReaderMaxLineLength() | ||
{ | ||
byte[] input = "Hello world! Goodbye world!\n".getBytes(UTF_8); | ||
byte[] delimiter = "!".getBytes(UTF_8); | ||
InputStream in = new ByteArrayInputStream(input); | ||
// Set the LineReader internal read buffer size 4 bytes | ||
LineReader reader = new LineReader(in, 4, delimiter); | ||
Text str = new Text(); | ||
try { | ||
reader.readLine(str, 10, 100); | ||
} | ||
catch (IOException e) { | ||
assertEquals(e.getMessage(), "Too many bytes before delimiter: " + 10); | ||
} | ||
} | ||
} |