EncloseCharLineFeed2LineReader implements LineReader { @Override public String readLine() { StringBuilder currentLineStringBuilder = new StringBuilder(); char previousChar = Character.MIN_VALUE; char currentChar = Character.MIN_VALUE; boolean isEnclosed = true; boolean isEscape = false; char lineFeedChar1 = lineFeedChar.charAt(0); char lineFeedChar2 = lineFeedChar.charAt(1); int chr = 0; int fieldCount = 0; try { while ((chr = reader.read()) != -1) { currentChar = (char) chr; if (previousChar == Character.MIN_VALUE) { previousChar = currentChar; } if (previousChar == getEncloseCharcter(fieldCount)) { if (isEnclosed) { if (currentChar == getEncloseCharcter(fieldCount)) { isEnclosed = false; currentLineStringBuilder.append(currentChar); } } else { if (currentChar == getEncloseCharcter(fieldCount) && !isEscape) { isEscape = true; } else if (currentChar == getEncloseCharcter(fieldCount) && isEscape) { isEscape = false; currentLineStringBuilder.append(currentChar); currentLineStringBuilder.append(currentChar); } else if (currentChar == delimiterCharacter) { if (isEscape) { currentLineStringBuilder .append(getEncloseCharcter(fieldCount)); previousChar = Character.MIN_VALUE; isEnclosed = true; isEscape = false; fieldCount++; } currentLineStringBuilder.append(currentChar); } else if (currentChar == lineFeedChar1) { if (isEscape) { currentLineStringBuilder .append(getEncloseCharcter(fieldCount)); previousChar = currentChar; isEnclosed = true; isEscape = false; } currentLineStringBuilder.append(currentChar); } else { if (isEscape) { currentLineStringBuilder .append(getEncloseCharcter(fieldCount)); previousChar = currentChar; isEnclosed = true; isEscape = false; } currentLineStringBuilder.append(currentChar); } } } else if (previousChar == lineFeedChar1) { if (currentChar == lineFeedChar2) { if (currentLineStringBuilder.length() - 1 >= 0 && (currentLineStringBuilder .charAt(currentLineStringBuilder .length() - 1) == lineFeedChar1)) { currentLineStringBuilder .deleteCharAt(currentLineStringBuilder .length() - 1); previousChar = Character.MIN_VALUE; isEnclosed = true; isEscape = false; break; } } currentLineStringBuilder.append(currentChar); } else { if (currentChar == delimiterCharacter) { currentLineStringBuilder.append(currentChar); previousChar = Character.MIN_VALUE; isEnclosed = true; isEscape = false; fieldCount++; } else if (currentChar == lineFeedChar2) { if (currentLineStringBuilder.length() - 1 >= 0 && (currentLineStringBuilder .charAt(currentLineStringBuilder .length() - 1)) == lineFeedChar1) { currentLineStringBuilder .deleteCharAt(currentLineStringBuilder .length() - 1); previousChar = Character.MIN_VALUE; isEnclosed = true; isEscape = false; break; } else { currentLineStringBuilder.append(currentChar); } } else { currentLineStringBuilder.append(currentChar); } } } } catch (IOException e) { throw new FileException("Reader control operation was failed.", e); } return currentLineStringBuilder.toString(); } EncloseCharLineFeed2LineReader(char delimiterCharacter,
            char encloseCharacter, char[] columnEncloseCharacter,
            Reader reader, String lineFeedChar); @Override String readLine();  }
@Test public void testReadLine01() throws Exception { Reader reader = mock(Reader.class); when(reader.read()).thenThrow(new IOException()); char dChar = 'a'; char[] eChar = { 'b' }; String parm = "\r\n"; EncloseCharLineFeed2LineReader target = new EncloseCharLineFeed2LineReader(dChar, Character.MIN_VALUE, eChar, reader, parm); try { target.readLine(); fail("FileExceptionがスローされませんでした。失敗です。"); } catch (Exception e) { assertEquals(FileException.class, e.getClass()); assertEquals(IOException.class.getName(), e.getCause().getClass() .getName()); assertEquals("Reader control operation was failed.", e .getMessage()); } }
@Test public void testReadLine02() throws Exception { char dChar = 'a'; char[] eChar = { 'b' }; byte[] buf = "".getBytes(); InputStream inputStream = new ByteArrayInputStream(buf); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String parm = "\r\n"; EncloseCharLineFeed2LineReader testCalss = new EncloseCharLineFeed2LineReader(dChar, Character.MIN_VALUE, eChar, bufferedReader, parm); String result = testCalss.readLine(); assertEquals("", result); }
@Test public void testReadLine03() throws Exception { char dChar = ','; char[] eChar = { '\"', '\"', '\"', '\"' }; byte[] buf = "aaa,aaa,aaa\r\n".getBytes(); InputStream inputStream = new ByteArrayInputStream(buf); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String parm = "\r\n"; EncloseCharLineFeed2LineReader testCalss = new EncloseCharLineFeed2LineReader(dChar, Character.MIN_VALUE, eChar, bufferedReader, parm); String result = testCalss.readLine(); assertEquals("aaa,aaa,aaa", result); }
@Test public void testReadLine04() throws Exception { char dChar = ','; char[] eChar = { '\"', '\"', '\"', '\"' }; byte[] buf = "\"aaa\",\"aaa\",\"aaa\"\r\n".getBytes(); InputStream inputStream = new ByteArrayInputStream(buf); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String parm = "\r\n"; EncloseCharLineFeed2LineReader testCalss = new EncloseCharLineFeed2LineReader(dChar, Character.MIN_VALUE, eChar, bufferedReader, parm); String result = testCalss.readLine(); assertEquals("\"aaa\",\"aaa\",\"aaa\"", result); }
@Test public void testReadLine05() throws Exception { char dChar = ','; char[] eChar = { '\"', '\"', '\"', '\"' }; byte[] buf = "\"aa,a\",\"aa\"\"a\",\"aa\r\na\"\r\n".getBytes(); InputStream inputStream = new ByteArrayInputStream(buf); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String parm = "\r\n"; EncloseCharLineFeed2LineReader testCalss = new EncloseCharLineFeed2LineReader(dChar, Character.MIN_VALUE, eChar, bufferedReader, parm); String result = testCalss.readLine(); assertEquals("\"aa,a\",\"aa\"\"a\",\"aa\r\na\"", result); }
@Test public void testReadLine06() throws Exception { char dChar = ','; char[] eChar = { '\"', '\"', '\"', '\"' }; byte[] buf = "\"aaa\",\"aaa\",\"aaa\"aaa\r\n".getBytes(); InputStream inputStream = new ByteArrayInputStream(buf); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String parm = "\r\n"; EncloseCharLineFeed2LineReader testCalss = new EncloseCharLineFeed2LineReader(dChar, Character.MIN_VALUE, eChar, bufferedReader, parm); String result = testCalss.readLine(); assertEquals("\"aaa\",\"aaa\",\"aaa\"aaa", result); }
@Test public void testReadLine07() throws Exception { char dChar = ','; char[] eChar = { '\"', '\"', '\"', '\"' }; byte[] buf = "\r\n".getBytes(); InputStream inputStream = new ByteArrayInputStream(buf); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String parm = "\r\n"; EncloseCharLineFeed2LineReader testCalss = new EncloseCharLineFeed2LineReader(dChar, Character.MIN_VALUE, eChar, bufferedReader, parm); String result = testCalss.readLine(); assertEquals("", result); }
@Test public void testReadLine08() throws Exception { char dChar = ','; char[] eChar = { '\"', '\"', '\"', '\"' }; byte[] buf = "\n\r\n".getBytes(); InputStream inputStream = new ByteArrayInputStream(buf); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String parm = "\r\n"; EncloseCharLineFeed2LineReader testCalss = new EncloseCharLineFeed2LineReader(dChar, Character.MIN_VALUE, eChar, bufferedReader, parm); String result = testCalss.readLine(); assertEquals("\n", result); }
@Test public void testReadLine09() throws Exception { char dChar = ','; char[] eChar = { '\"', '\"', '\"', '\"' }; byte[] buf = "aaa".getBytes(); InputStream inputStream = new ByteArrayInputStream(buf); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String parm = "\r\n"; EncloseCharLineFeed2LineReader testCalss = new EncloseCharLineFeed2LineReader(dChar, Character.MIN_VALUE, eChar, bufferedReader, parm); String result = testCalss.readLine(); assertEquals("aaa", result); }
@Test public void testReadLine10() throws Exception { char dChar = ','; char[] eChar = { '\"', '\"', '\"', '\"' }; byte[] buf = "aaa,aaa,aaa\r\naaa,aaa".getBytes(); InputStream inputStream = new ByteArrayInputStream(buf); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String parm = "\r\n"; EncloseCharLineFeed2LineReader testCalss = new EncloseCharLineFeed2LineReader(dChar, Character.MIN_VALUE, eChar, bufferedReader, parm); String result = testCalss.readLine(); assertEquals("aaa,aaa,aaa", result); }
@Test public void testReadLine11() throws Exception { char dChar = ','; char[] eChar = { '\"', '\"', '\"', '\"' }; byte[] buf = "aaa,aa\ra,aaa\r\n".getBytes(); InputStream inputStream = new ByteArrayInputStream(buf); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String parm = "\r\n"; EncloseCharLineFeed2LineReader testCalss = new EncloseCharLineFeed2LineReader(dChar, Character.MIN_VALUE, eChar, bufferedReader, parm); String result = testCalss.readLine(); assertEquals("aaa,aa\ra,aaa", result); }
@Test public void testReadLine12() throws Exception { char dChar = ','; char[] eChar = { '\"', '\"', '\"', '\"' }; byte[] buf = "aaa,ａａａ,ａａａ\r\n".getBytes(); InputStream inputStream = new ByteArrayInputStream(buf); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String parm = "\r\n"; EncloseCharLineFeed2LineReader testCalss = new EncloseCharLineFeed2LineReader(dChar, Character.MIN_VALUE, eChar, bufferedReader, parm); String result = testCalss.readLine(); assertEquals("aaa,ａａａ,ａａａ", result); }
@Test public void testReadLine13() throws Exception { char dChar = ','; char[] eChar = { '\"', Character.MIN_VALUE, '|' }; byte[] buf = "\"aaa\",\"aaa\",|aaaaaa|\r\naaa".getBytes(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buf))); String parm = "\r\n"; EncloseCharLineFeed2LineReader testCalss = new EncloseCharLineFeed2LineReader(dChar, Character.MIN_VALUE, eChar, bufferedReader, parm); String result = testCalss.readLine(); assertEquals("\"aaa\",\"aaa\",|aaaaaa|", result); }
@Test public void testReadLine14() throws Exception { char dChar = ','; char[] eChar = { '\"', Character.MIN_VALUE, '|' }; byte[] buf = "\"a\r\naa\",\"aaa\",|aaa\r\naaa|\r\naaa".getBytes(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buf))); String parm = "\r\n"; EncloseCharLineFeed2LineReader testCalss = new EncloseCharLineFeed2LineReader(dChar, Character.MIN_VALUE, eChar, bufferedReader, parm); String result = testCalss.readLine(); assertEquals("\"a\r\naa\",\"aaa\",|aaa\r\naaa|", result); }
@Test public void testReadLine15() throws Exception { char dChar = ','; char[] eColChar = {}; char eChar = '\"'; byte[] buf = "\"a\r\naa\",\"aaa\",|aaaaaa|\r\naaa".getBytes(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buf))); String parm = "\r\n"; EncloseCharLineFeed2LineReader testCalss = new EncloseCharLineFeed2LineReader(dChar, eChar, eColChar, bufferedReader, parm); String result = testCalss.readLine(); assertEquals("\"a\r\naa\",\"aaa\",|aaaaaa|", result); }