1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.tomgibara.pronto.control.impl;
19
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.List;
23
24 import junit.framework.TestCase;
25
26 public class TestStdinControllerPart extends TestCase {
27
28 public void testSingleShort() {
29 doTest("only\n", 100, 0, "only");
30 }
31
32 public void testMultipleShort() {
33 doTest("two\nlines\n", 100, 0, "two", "lines");
34 }
35
36 public void testEmpty() {
37 doTest("", 100, 0);
38 }
39
40 public void testPartial() {
41 doTest("one\ntwo", 100, 3, "one");
42 }
43
44 public void testOverflow() {
45 doTest("1234567890", 10, 0, "1234567890");
46 }
47
48 public void testFullPartial() {
49 doTest("123\n123456", 10, 6, "123");
50 }
51
52 private void doTest(String input, int maxBytes, int expectedBytesRemaining, String... expectedLines) {
53 ArrayList<String> lines = new ArrayList<String>();
54 byte[] inputBytes = input.getBytes();
55 byte[] bytes = new byte[maxBytes];
56 int bytesRead = Math.min(inputBytes.length, bytes.length);
57 System.arraycopy(inputBytes, 0, bytes, 0, bytesRead);
58 int bytesRemaining = StdinControllerPart.extractLines(bytes, bytesRead, lines);
59 if (expectedBytesRemaining != -1) assertEquals(expectedBytesRemaining, bytesRemaining);
60 List<String> expected = Arrays.asList(expectedLines);
61 assertEquals(expected, lines);
62 }
63
64 }