Improve test coverage of AutoCRLF(In|Out)putStream

Bug: 405672
Change-Id: I3894e98617fcee16dc2ac9853c203c62eb30c3ab
Signed-off-by: Chris Aniszczyk <zx@twitter.com>
This commit is contained in:
Robin Stocker 2013-04-18 21:55:35 +02:00 committed by Chris Aniszczyk
parent fa1bc6abb7
commit 78fca8a099
3 changed files with 37 additions and 19 deletions

View File

@ -64,6 +64,17 @@ public void test() throws IOException {
assertNoCrLf("\r\n\r\r", "\r\n\r\r");
assertNoCrLf("\r\n\r\n", "\r\n\r\n");
assertNoCrLf("\r\n\r\n\r", "\n\r\n\r");
assertNoCrLf("\0\n", "\0\n");
}
@Test
public void testBoundary() throws IOException {
for (int i = AutoCRLFInputStream.BUFFER_SIZE - 10; i < AutoCRLFInputStream.BUFFER_SIZE + 10; i++) {
String s1 = AutoCRLFOutputStreamTest.repeat("a", i);
assertNoCrLf(s1, s1);
String s2 = AutoCRLFOutputStreamTest.repeat("\0", i);
assertNoCrLf(s2, s2);
}
}
private void assertNoCrLf(String string, String string2) throws IOException {

View File

@ -66,29 +66,25 @@ public void test() throws IOException {
assertNoCrLf("\r\n\r\r", "\r\n\r\r");
assertNoCrLf("\r\n\r\n", "\r\n\r\n");
assertNoCrLf("\r\n\r\n\r", "\n\r\n\r");
assertNoCrLf("\0\n", "\0\n");
}
@Test
public void testBoundary() throws IOException {
assertBoundaryCorrect(AutoCRLFOutputStream.BUFFER_SIZE - 5);
assertBoundaryCorrect(AutoCRLFOutputStream.BUFFER_SIZE - 4);
assertBoundaryCorrect(AutoCRLFOutputStream.BUFFER_SIZE - 3);
assertBoundaryCorrect(AutoCRLFOutputStream.BUFFER_SIZE - 2);
assertBoundaryCorrect(AutoCRLFOutputStream.BUFFER_SIZE - 1);
assertBoundaryCorrect(AutoCRLFOutputStream.BUFFER_SIZE);
assertBoundaryCorrect(AutoCRLFOutputStream.BUFFER_SIZE + 1);
assertBoundaryCorrect(AutoCRLFOutputStream.BUFFER_SIZE + 2);
assertBoundaryCorrect(AutoCRLFOutputStream.BUFFER_SIZE + 3);
assertBoundaryCorrect(AutoCRLFOutputStream.BUFFER_SIZE + 4);
assertBoundaryCorrect(AutoCRLFOutputStream.BUFFER_SIZE + 5);
for (int i = AutoCRLFOutputStream.BUFFER_SIZE - 10; i < AutoCRLFOutputStream.BUFFER_SIZE + 10; i++) {
String s1 = repeat("a", i);
assertNoCrLf(s1, s1);
String s2 = repeat("\0", i);
assertNoCrLf(s2, s2);
}
}
private void assertBoundaryCorrect(int size) throws IOException {
StringBuilder sb = new StringBuilder(size);
public static String repeat(String input, int size) {
StringBuilder sb = new StringBuilder(input.length() * size);
for (int i = 0; i < size; i++)
sb.append('a');
sb.append(input);
String s = sb.toString();
assertNoCrLf(s, s);
return s;
}
private void assertNoCrLf(String string, String string2) throws IOException {
@ -105,8 +101,9 @@ private void assertNoCrLfHelper(String expect, String input)
throws IOException {
byte[] inbytes = input.getBytes();
byte[] expectBytes = expect.getBytes();
for (int i = 0; i < 5; ++i) {
byte[] buf = new byte[i];
for (int i = -4; i < 5; ++i) {
int size = Math.abs(i);
byte[] buf = new byte[size];
InputStream in = new ByteArrayInputStream(inbytes);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
OutputStream out = new AutoCRLFOutputStream(bos);
@ -115,6 +112,13 @@ private void assertNoCrLfHelper(String expect, String input)
while ((n = in.read(buf)) >= 0) {
out.write(buf, 0, n);
}
} else if (i < 0) {
int n;
while ((n = in.read(buf)) >= 0) {
byte[] b = new byte[n];
System.arraycopy(buf, 0, b, 0, n);
out.write(b);
}
} else {
int c;
while ((c = in.read()) != -1)
@ -124,7 +128,7 @@ private void assertNoCrLfHelper(String expect, String input)
in.close();
out.close();
byte[] actualBytes = bos.toByteArray();
Assert.assertEquals("bufsize=" + i, encode(expectBytes),
Assert.assertEquals("bufsize=" + size, encode(expectBytes),
encode(actualBytes));
}
}

View File

@ -58,9 +58,12 @@
* of binary files, canonicalization is turned off (for the complete file).
*/
public class AutoCRLFInputStream extends InputStream {
static final int BUFFER_SIZE = 8096;
private final byte[] single = new byte[1];
private final byte[] buf = new byte[8096];
private final byte[] buf = new byte[BUFFER_SIZE];
private final InputStream in;