1   /*
2    * Copyright (C) 2006  Tom Gibara
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
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  }