PlainFileLineIterator extends AbstractFileLineIterator<Object> { @Override public String next() { if (readTrailer) { throw new FileLineException("Data part should be called before trailer part.", new IllegalStateException(), getFileName(), currentLineCount); } if (!hasNext()) { throw new FileLineException("The data which can be acquired doesn't exist.", new NoSuchElementException(), getFileName(), currentLineCount + 1, null, -1); } currentLineCount++; return readLine(); } @SuppressWarnings("unchecked")  PlainFileLineIterator(String fileName,
            @SuppressWarnings("rawtypes") Class clazz,
            Map<String, ColumnParser> columnParserMap); @Override String[] separateColumns(String fileLineString); @Override String next(); void skip(int skipLines); @Override List<String> getTrailer(); @Override char getDelimiter(); @Override char getEncloseChar(); @Override int getCurrentLineCount();  }
@Test public void testNext01() throws Exception { URL url = this.getClass().getResource("PlainFileLineIterator01.txt"); String fileName = url.getPath(); Class<PlainFileLineIterator_Stub01> clazz = PlainFileLineIterator_Stub01.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); PlainFileLineIterator plainFileLineIterator = Mockito.spy( new PlainFileLineIterator(fileName, clazz, columnParserMap)); String result = plainFileLineIterator.next(); assertEquals("Line data 1", result); Mockito.verify(plainFileLineIterator).readLine(); assertEquals(1, ReflectionTestUtils.getField(plainFileLineIterator, "currentLineCount")); }
@Test public void testNext02() throws Exception { URL url = this.getClass().getResource("File_Empty.txt"); String fileName = url.getPath(); Class<PlainFileLineIterator_Stub01> clazz = PlainFileLineIterator_Stub01.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); PlainFileLineIterator plainFileLineIterator = Mockito.spy( new PlainFileLineIterator(fileName, clazz, columnParserMap)); try { plainFileLineIterator.next(); fail("FileLineExceptionがスローされませんでした"); } catch (FileLineException e) { assertSame(FileLineException.class, e.getClass()); assertEquals("The data which can be acquired doesn't exist.", e .getMessage()); assertSame(NoSuchElementException.class, e.getCause().getClass()); assertEquals(fileName, e.getFileName()); assertEquals(1, e.getLineNo()); assertNull(e.getColumnName()); assertEquals(-1, e.getColumnIndex()); Mockito.verify(plainFileLineIterator, Mockito.never()).readLine(); assertEquals(0, ReflectionTestUtils.getField(plainFileLineIterator, "currentLineCount")); } }
@Test public void testNext03() throws Exception { URL url = this.getClass().getResource("PlainFileLineIterator03.txt"); String fileName = url.getPath(); Class<PlainFileLineIterator_Stub03> clazz = PlainFileLineIterator_Stub03.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); PlainFileLineIterator plainFileLineIterator = Mockito.spy( new PlainFileLineIterator(fileName, clazz, columnParserMap)); ReflectionTestUtils.setField(plainFileLineIterator, "readTrailer", true); try { plainFileLineIterator.next(); fail("FileLineExceptionがスローされませんでした"); } catch (FileLineException e) { assertSame(FileLineException.class, e.getClass()); assertEquals("Data part should be called before trailer part.", e .getMessage()); assertSame(IllegalStateException.class, e.getCause().getClass()); assertEquals(fileName, e.getFileName()); assertEquals(0, e.getLineNo()); Mockito.verify(plainFileLineIterator, Mockito.never()).readLine(); assertEquals(0, ReflectionTestUtils.getField(plainFileLineIterator, "currentLineCount")); } }
@Test public void testNext04() throws Exception { URL url = this.getClass().getResource("CsvFileLineIterator_next01.txt"); String fileName = url.getPath(); Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); PlainFileLineIterator fileLineIterator = new PlainFileLineIterator(fileName, PlainFileLineIterator_Stub02.class, columnParserMap); String result1 = fileLineIterator.next(); String result2 = fileLineIterator.next(); String result3 = fileLineIterator.next(); assertEquals("\"1\",22,333,|4444|", result1); assertEquals("\"5\",66,777,|8888|", result2); assertEquals("\"9\",AA,BBB,|CCCC|", result3); }