01: package de.regnis.q.sequence.line;
02:
03: import java.io.*;
04: import junit.framework.*;
05:
06: import de.regnis.q.sequence.line.simplifier.*;
07:
08: /**
09: * @author Marc Strapetz
10: */
11: public class QSequenceLineReaderTest extends TestCase {
12:
13: // Accessing ==============================================================
14:
15: public void test() throws IOException {
16: test("A simple string.", 1);
17: test("Two\nlines.", 2);
18: test("Three\nli\nnes.", 3);
19: test("\nLine\n", 2);
20: test("Line\r\n\r", 2);
21: test("Line\r\n\r\n", 2);
22: test("Line\r\r", 2);
23: test("Line\r\r ", 3);
24: test("Line\n\r\r", 3);
25: test("\n\n\n", 3);
26: test("", 0);
27:
28: test("Line", 1);
29: test("Line\n", 1);
30: test("Line\r\n", 1);
31: test("Line\n\r", 2);
32: test("Line\n\r\r", 3);
33: }
34:
35: // Utils ==================================================================
36:
37: private void test(String testString, int expectedLineCount)
38: throws IOException {
39: final byte[] bytes = testString.getBytes();
40: final QSequenceLineMemoryCache cache = new QSequenceLineMemoryCache();
41: final QSequenceLineReader reader = new QSequenceLineReader(4);
42: reader.read(new ByteArrayInputStream(bytes), cache,
43: new QSequenceLineDummySimplifier());
44: assertEquals(expectedLineCount, cache.getLineCount());
45:
46: for (int index = 0; index < cache.getLineCount(); index++) {
47: final QSequenceLine line = cache.getLine(index);
48: if (index == 0) {
49: assertEquals(0, line.getFrom());
50: } else if (index == cache.getLineCount() - 1) {
51: assertEquals(bytes.length, line.getFrom()
52: + line.getContentLength());
53: } else {
54: final int expectedTo = (int) cache.getLine(index - 1)
55: .getFrom()
56: + cache.getLine(index - 1).getContentLength();
57: assertEquals(expectedTo, line.getFrom());
58: }
59:
60: for (int byteIndex = (int) line.getFrom(); byteIndex < line
61: .getFrom()
62: + line.getContentLength(); byteIndex++) {
63: assertEquals(bytes[byteIndex],
64: line.getContentBytes()[byteIndex
65: - (int) line.getFrom()]);
66: }
67: }
68: }
69: }
|