LineFeed0LineReader implements LineReader { public String readLine() { StringBuilder currentLineStringBuilder = new StringBuilder(); int chr = 0; int size = 0; try { while ((chr = reader.read()) != -1) { char currentChar = (char) chr; currentLineStringBuilder.append(currentChar); size += Character.toString(currentChar).getBytes(fileEncoding).length; if (size >= totalBytes) { break; } } } catch (IOException e) { throw new FileException("Reader control operation was failed.", e); } return currentLineStringBuilder.toString(); } LineFeed0LineReader(Reader reader, String fileEncoding,
            int totalBytes); String readLine();  }
@Test public void testReadLine01() throws Exception { String str = "111122223333444455556"; Reader reader = new BufferedReader(new StringReader(str)); String encoding = "UTF-8"; int totalBytes = 4; LineFeed0LineReader target = new LineFeed0LineReader(reader, encoding, totalBytes); assertEquals(target.readLine(), "1111"); assertEquals(target.readLine(), "2222"); assertEquals(target.readLine(), "3333"); assertEquals(target.readLine(), "4444"); assertEquals(target.readLine(), "5555"); assertEquals(target.readLine(), "6"); assertEquals(target.readLine(), ""); }
@Test public void testReadLine02() throws Exception { String str = "11112222333344445555"; Reader reader = new BufferedReader(new StringReader(str)); String encoding = "UTF-8"; int totalBytes = 4; LineFeed0LineReader target = new LineFeed0LineReader(reader, encoding, totalBytes); assertEquals(target.readLine(), "1111"); assertEquals(target.readLine(), "2222"); assertEquals(target.readLine(), "3333"); assertEquals(target.readLine(), "4444"); assertEquals(target.readLine(), "5555"); assertEquals(target.readLine(), ""); }
@Test public void testReadLine03() throws Exception { String str = "1あ2い３う４え五お"; Reader reader = new BufferedReader(new StringReader(str)); String encoding = "UTF-8"; int totalBytes = 4; LineFeed0LineReader target = new LineFeed0LineReader(reader, encoding, totalBytes); assertEquals(target.readLine(), "1あ"); assertEquals(target.readLine(), "2い"); assertEquals(target.readLine(), "３う"); assertEquals(target.readLine(), "４え"); assertEquals(target.readLine(), "五お"); assertEquals(target.readLine(), ""); }
@Test public void testReadLine04() throws Exception { String str = "\r\r\n\n\\\"\"\".,/@<>"; Reader reader = new BufferedReader(new StringReader(str)); String encoding = "UTF-8"; int totalBytes = 4; LineFeed0LineReader target = new LineFeed0LineReader(reader, encoding, totalBytes); assertEquals(target.readLine(), "\r\r\n\n"); assertEquals(target.readLine(), "\\\"\"\""); assertEquals(target.readLine(), ".,/@"); assertEquals(target.readLine(), "<>"); assertEquals(target.readLine(), ""); }
@Test public void testReadLine05() throws Exception { String str = "1あ12い2３う４え五お"; Reader reader = new BufferedReader(new StringReader(str)); String encoding = "Shift-JIS"; int totalBytes = 4; LineFeed0LineReader target = new LineFeed0LineReader(reader, encoding, totalBytes); assertEquals(target.readLine(), "1あ1"); assertEquals(target.readLine(), "2い2"); assertEquals(target.readLine(), "３う"); assertEquals(target.readLine(), "４え"); assertEquals(target.readLine(), "五お"); assertEquals(target.readLine(), ""); }
@Test public void testReadLine06() throws Exception { String str = "\r\r\n\n\\\"\"\".,/@<>"; Reader reader = new BufferedReader(new StringReader(str)); String encoding = "Shift-JIS"; int totalBytes = 4; LineFeed0LineReader target = new LineFeed0LineReader(reader, encoding, totalBytes); assertEquals(target.readLine(), "\r\r\n\n"); assertEquals(target.readLine(), "\\\"\"\""); assertEquals(target.readLine(), ".,/@"); assertEquals(target.readLine(), "<>"); assertEquals(target.readLine(), ""); }
@Test public void testReadLine07() throws Exception { String str = "1あ12い2３う４え五お"; Reader reader = new BufferedReader(new StringReader(str)); String encoding = "EUC-JP"; int totalBytes = 4; LineFeed0LineReader target = new LineFeed0LineReader(reader, encoding, totalBytes); assertEquals(target.readLine(), "1あ1"); assertEquals(target.readLine(), "2い2"); assertEquals(target.readLine(), "３う"); assertEquals(target.readLine(), "４え"); assertEquals(target.readLine(), "五お"); assertEquals(target.readLine(), ""); }
@Test public void testReadLine08() throws Exception { String str = "\r\r\n\n\\\"\"\".,/@<>"; Reader reader = new BufferedReader(new StringReader(str)); String encoding = "EUC-JP"; int totalBytes = 4; LineFeed0LineReader target = new LineFeed0LineReader(reader, encoding, totalBytes); assertEquals(target.readLine(), "\r\r\n\n"); assertEquals(target.readLine(), "\\\"\"\""); assertEquals(target.readLine(), ".,/@"); assertEquals(target.readLine(), "<>"); assertEquals(target.readLine(), ""); }
@Test public void testReadLine09() throws Exception { String str = "123456789012345678901234567890"; Reader reader = new BufferedReader(new StringReader(str)); String encoding = ""; int totalBytes = 10; LineFeed0LineReader target = new LineFeed0LineReader(reader, encoding, totalBytes); try { target.readLine(); fail(); } catch (Exception e) { assertTrue(e instanceof FileException); assertEquals("Reader control operation was failed.", e.getMessage()); assertTrue(e.getCause() instanceof IOException); } }
@Test public void testReadLine10() throws Exception { String encoding = "UTF-8"; int totalBytes = 10; Reader reader = mock(Reader.class); when(reader.read()).thenThrow(new IOException()); LineFeed0LineReader target = new LineFeed0LineReader(reader, encoding, totalBytes); try { target.readLine(); fail(); } catch (Exception e) { assertTrue(e instanceof FileException); assertEquals("Reader control operation was failed.", e.getMessage()); assertTrue(e.getCause() instanceof IOException); } }