PlainFileLineIterator extends AbstractFileLineIterator<Object> { public void skip(int skipLines) { for (int i = 0; i < skipLines; i++) { if (!hasNext()) { throw new FileLineException("The data which can be acquired doesn't exist.", new NoSuchElementException(), getFileName(), currentLineCount + 1); } readLine(); currentLineCount++; } } @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 testSkipint01() throws Exception { URL url = PlainFileLineIteratorTest.class.getResource( "PlainFileLineIterator_skip01.txt"); String fileName = url.getPath(); Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); PlainFileLineIterator fileLineIterator = Mockito.spy( new PlainFileLineIterator(fileName, PlainFileLineIterator_Stub01.class, columnParserMap)); ReflectionTestUtils.setField(fileLineIterator, "currentLineCount", 0); int skipLines = 0; fileLineIterator.init(); fileLineIterator.skip(skipLines); Mockito.verify(fileLineIterator, Mockito.never()).readLine(); assertEquals(0, ReflectionTestUtils.getField(fileLineIterator, "currentLineCount")); }
@Test public void testSkipint02() throws Exception { URL url = AbstractFileLineIteratorTest.class.getResource( "PlainFileLineIterator_skip01.txt"); String fileName = url.getPath(); Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); PlainFileLineIterator fileLineIterator = Mockito.spy( new PlainFileLineIterator(fileName, PlainFileLineIterator_Stub01.class, columnParserMap)); ReflectionTestUtils.setField(fileLineIterator, "currentLineCount", 0); int skipLines = 1; fileLineIterator.init(); fileLineIterator.skip(skipLines); Mockito.verify(fileLineIterator).readLine(); assertEquals(1, ReflectionTestUtils.getField(fileLineIterator, "currentLineCount")); }
@Test public void testSkipint03() throws Exception { URL url = AbstractFileLineIteratorTest.class.getResource( "PlainFileLineIterator_skip01.txt"); String fileName = url.getPath(); Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); PlainFileLineIterator fileLineIterator = Mockito.spy( new PlainFileLineIterator(fileName, PlainFileLineIterator_Stub01.class, columnParserMap)); ReflectionTestUtils.setField(fileLineIterator, "currentLineCount", 0); int skipLines = 3; fileLineIterator.init(); fileLineIterator.skip(skipLines); Mockito.verify(fileLineIterator, Mockito.times(3)).readLine(); assertEquals(3, ReflectionTestUtils.getField(fileLineIterator, "currentLineCount")); }
@Test public void testSkipint04() throws Exception { URL url = AbstractFileLineIteratorTest.class.getResource( "PlainFileLineIterator_skip01.txt"); String fileName = url.getPath(); Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); PlainFileLineIterator fileLineIterator = Mockito.spy( new PlainFileLineIterator(fileName, PlainFileLineIterator_Stub01.class, columnParserMap)); ReflectionTestUtils.setField(fileLineIterator, "currentLineCount", 0); int skipLines = 1; fileLineIterator.init(); FileException exception = new FileException("readLineからの例外です"); Mockito.doThrow(exception).when(fileLineIterator).readLine(); try { fileLineIterator.skip(skipLines); fail("FileExceptionがスローされませんでした"); } catch (FileException e) { assertSame(exception, e); assertEquals(0, ReflectionTestUtils.getField(fileLineIterator, "currentLineCount")); } }
@Test public void testSkipint05() throws Exception { URL url = AbstractFileLineIteratorTest.class.getResource( "PlainFileLineIterator_skip01.txt"); String fileName = url.getPath(); Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); PlainFileLineIterator fileLineIterator = Mockito.spy( new PlainFileLineIterator(fileName, PlainFileLineIterator_Stub01.class, columnParserMap)); ReflectionTestUtils.setField(fileLineIterator, "currentLineCount", 0); int skipLines = 100; fileLineIterator.init(); try { fileLineIterator.skip(skipLines); fail("FileLineExceptionがスローされませんでした"); } catch (FileLineException e) { assertEquals("The data which can be acquired doesn't exist.", e .getMessage()); assertTrue(NoSuchElementException.class.isAssignableFrom(e .getCause().getClass())); assertEquals(fileName, e.getFileName()); assertEquals(4, e.getLineNo()); Mockito.verify(fileLineIterator, Mockito.times(3)).readLine(); assertEquals(3, ReflectionTestUtils.getField(fileLineIterator, "currentLineCount")); } }