LineFeed2LineReader implements LineReader { @Override public String readLine() { StringBuilder currentLineStringBuilder = new StringBuilder(); char previousChar = Character.MIN_VALUE; char currentChar = Character.MIN_VALUE; char lineFeedChar1 = lineFeedChar.charAt(0); char lineFeedChar2 = lineFeedChar.charAt(1); int chr = 0; try { while ((chr = reader.read()) != -1) { currentChar = (char) chr; if (currentChar == lineFeedChar2) { if (previousChar == lineFeedChar1) { currentLineStringBuilder.delete( (currentLineStringBuilder.length() - 1), (currentLineStringBuilder.length())); break; } } previousChar = currentChar; currentLineStringBuilder.append(currentChar); } } catch (IOException e) { throw new FileException("Reader control operation was failed.", e); } return currentLineStringBuilder.toString(); } LineFeed2LineReader(Reader reader, String lineFeedChar); @Override String readLine();  }
@Test public void testReadLine01() throws Exception { Reader reader = mock(Reader.class); when(reader.read()).thenThrow(new IOException()); String lineFeedChar = "\r\n"; LineFeed2LineReader lineFeed2LineReader = new LineFeed2LineReader(reader, lineFeedChar); try { lineFeed2LineReader.readLine(); fail("FileExceptionがスローされませんでした"); } catch (FileException e) { assertSame(FileException.class, e.getClass()); assertEquals("Reader control operation was failed.", e .getMessage()); assertSame(IOException.class, e.getCause().getClass()); } }
@Test public void testReadLine02() throws Exception { byte[] buf = "".getBytes(); InputStream inputStream = new ByteArrayInputStream(buf); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader reader = new BufferedReader(inputStreamReader); String lineFeedChar = "\r\n"; LineFeed2LineReader lineFeed2LineReader = new LineFeed2LineReader(reader, lineFeedChar); String result = lineFeed2LineReader.readLine(); assertEquals("", result); }
@Test public void testReadLine03() throws Exception { String byteParm = "aaa,aaa,aaa\r\n"; InputStream inputStream = new ByteArrayInputStream(byteParm.getBytes()); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader reader = new BufferedReader(inputStreamReader); String lineFeedChar = "\r\n"; LineFeed2LineReader lineFeed2LineReader = new LineFeed2LineReader(reader, lineFeedChar); String result = lineFeed2LineReader.readLine(); assertEquals("aaa,aaa,aaa", result); }
@Test public void testReadLine04() throws Exception { String byteParm = "\"aaa\",\"aaa\",\"aa\r\na\"\r\n"; InputStream inputStream = new ByteArrayInputStream(byteParm.getBytes()); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader reader = new BufferedReader(inputStreamReader); String lineFeedChar = "\r\n"; LineFeed2LineReader lineFeed2LineReader = new LineFeed2LineReader(reader, lineFeedChar); String result = lineFeed2LineReader.readLine(); assertEquals("\"aaa\",\"aaa\",\"aa", result); }
@Test public void testReadLine05() throws Exception { Reader reader = mock(Reader.class); when(reader.read()).thenThrow(new IOException()); String lineFeedChar = "\r\n"; LineFeed2LineReader lineFeed2LineReader = new LineFeed2LineReader(reader, lineFeedChar); try { lineFeed2LineReader.readLine(); fail("FileExceptionがスローされませんでした"); } catch (FileException e) { assertSame(FileException.class, e.getClass()); assertEquals("Reader control operation was failed.", e .getMessage()); assertSame(IOException.class, e.getCause().getClass()); } }
@Test public void testReadLine06() throws Exception { String byteParm = "999,999,999"; InputStream inputStream = new ByteArrayInputStream(byteParm.getBytes()); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader reader = new BufferedReader(inputStreamReader); String lineFeedChar = "\r\n"; LineFeed2LineReader lineFeed2LineReader = new LineFeed2LineReader(reader, lineFeedChar); String result = lineFeed2LineReader.readLine(); assertEquals(byteParm, result); }
@Test public void testReadLine07() throws Exception { String byteParm = "101010,101010,101010\rn"; InputStream inputStream = new ByteArrayInputStream(byteParm.getBytes()); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader reader = new BufferedReader(inputStreamReader); String lineFeedChar = "\r\n"; LineFeed2LineReader lineFeed2LineReader = new LineFeed2LineReader(reader, lineFeedChar); String result = lineFeed2LineReader.readLine(); assertEquals("101010,101010,101010\rn", result); }
@Test public void testReadLine08() throws Exception { String byteParm = "あいう\r\n"; InputStream inputStream = new ByteArrayInputStream(byteParm.getBytes()); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader reader = new BufferedReader(inputStreamReader); String lineFeedChar = "\r\n"; LineFeed2LineReader lineFeed2LineReader = new LineFeed2LineReader(reader, lineFeedChar); String result = lineFeed2LineReader.readLine(); assertEquals("あいう", result); }