Browse Source

Improve test coverage of AutoCRLF(In|Out)putStream

Bug: 405672
Change-Id: I3894e98617fcee16dc2ac9853c203c62eb30c3ab
Signed-off-by: Chris Aniszczyk <zx@twitter.com>
tags/v3.0.0.201305080800-m7
Robin Stocker 11 years ago
parent
commit
78fca8a099

+ 11
- 0
org.eclipse.jgit.test/tst/org/eclipse/jgit/util/io/AutoCRLFInputStreamTest.java View File

@@ -64,6 +64,17 @@ public class AutoCRLFInputStreamTest {
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 {

+ 22
- 18
org.eclipse.jgit.test/tst/org/eclipse/jgit/util/io/AutoCRLFOutputStreamTest.java View File

@@ -66,29 +66,25 @@ public class AutoCRLFOutputStreamTest {
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 @@ public class AutoCRLFOutputStreamTest {
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 @@ public class AutoCRLFOutputStreamTest {
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 @@ public class AutoCRLFOutputStreamTest {
in.close();
out.close();
byte[] actualBytes = bos.toByteArray();
Assert.assertEquals("bufsize=" + i, encode(expectBytes),
Assert.assertEquals("bufsize=" + size, encode(expectBytes),
encode(actualBytes));
}
}

+ 4
- 1
org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFInputStream.java View File

@@ -58,9 +58,12 @@ import org.eclipse.jgit.diff.RawText;
* 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;


Loading…
Cancel
Save