FixedFileLineIterator extends AbstractFileLineIterator<T> { @Override protected String[] separateColumns(String fileLineString) { if (fileLineString == null || "".equals(fileLineString)) { return new String[0]; } int[] columnBytes = getColumnBytes(); String[] results = new String[columnBytes.length]; try { byte[] bytes = fileLineString.getBytes(getFileEncoding()); if (getTotalBytes() != bytes.length) { throw new FileException("Total Columns byte is different " + "from Total FileLineObject's columns byte.", new IllegalStateException(), this.getFileName()); } int byteIndex = 0; for (int i = 0; i < columnBytes.length; i++) { results[i] = new String(bytes, byteIndex, columnBytes[i], getFileEncoding()); byteIndex += columnBytes[i]; } } catch (UnsupportedEncodingException e) { throw new FileException( "fileEncoding which isn't supported was set.", e, this .getFileName()); } return results; } FixedFileLineIterator(String fileName, Class<T> clazz,
            Map<String, ColumnParser> columnParserMap); @Override char getDelimiter(); @Override char getEncloseChar();  }
@Test public void testSeparateColumns01() throws Exception { URL url = FixedFileLineIteratorTest.class.getResource("/aaa.txt"); String fileName = url.getPath(); Class<FixedFileLineIterator_Stub01> clazz = FixedFileLineIterator_Stub01.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); ColumnParser parser = new FixedFileLineIterator_ColumnParserStub01(); columnParserMap.put("java.lang.String", parser); columnParserMap.put("java.util.Date", parser); columnParserMap.put("java.math.BigDecimal", parser); columnParserMap.put("java.lang.int", parser); FixedFileLineIterator<FixedFileLineIterator_Stub01> lineIterator = new FixedFileLineIterator<FixedFileLineIterator_Stub01>(fileName, clazz, columnParserMap); String fileLineString = null; String[] columns = lineIterator.separateColumns(fileLineString); assertEquals(0, columns.length); }
@Test public void testSeparateColumns02() throws Exception { URL url = FixedFileLineIteratorTest.class.getResource("/aaa.txt"); String fileName = url.getPath(); Class<FixedFileLineIterator_Stub01> clazz = FixedFileLineIterator_Stub01.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); ColumnParser parser = new FixedFileLineIterator_ColumnParserStub01(); columnParserMap.put("java.lang.String", parser); columnParserMap.put("java.util.Date", parser); columnParserMap.put("java.math.BigDecimal", parser); columnParserMap.put("java.lang.int", parser); FixedFileLineIterator<FixedFileLineIterator_Stub01> lineIterator = new FixedFileLineIterator<FixedFileLineIterator_Stub01>(fileName, clazz, columnParserMap); String fileLineString = "aaa"; try { lineIterator.separateColumns(fileLineString); } catch (FileException e) { assertEquals(IllegalStateException.class, e.getCause().getClass()); assertEquals(fileName, e.getFileName()); String message = "Total Columns byte is different from " + "Total FileLineObject's columns byte."; assertEquals(message, e.getMessage()); } }
@Test public void testSeparateColumns03() throws Exception { URL url = FixedFileLineIteratorTest.class.getResource("/aaa.txt"); String fileName = url.getPath(); Class<FixedFileLineIterator_Stub03> clazz = FixedFileLineIterator_Stub03.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); ColumnParser columnParser = new FixedFileLineIterator_ColumnParserStub01(); columnParserMap.put("java.lang.String", columnParser); columnParserMap.put("java.util.Date", columnParser); columnParserMap.put("java.math.BigDecimal", columnParser); columnParserMap.put("java.lang.int", columnParser); FixedFileLineIterator<FixedFileLineIterator_Stub03> lineIterator = new FixedFileLineIterator<FixedFileLineIterator_Stub03>(fileName, clazz, columnParserMap); String fileLineString = "12345"; String[] columns = lineIterator.separateColumns(fileLineString); assertEquals(1, columns.length); assertEquals("12345", columns[0]); }
@Test public void testSeparateColumns04() throws Exception { URL url = FixedFileLineIteratorTest.class.getResource("/aaa.txt"); String fileName = url.getPath(); Class<FixedFileLineIterator_Stub03> clazz = FixedFileLineIterator_Stub03.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); ColumnParser columnParser = new FixedFileLineIterator_ColumnParserStub01(); columnParserMap.put("java.lang.String", columnParser); columnParserMap.put("java.util.Date", columnParser); columnParserMap.put("java.math.BigDecimal", columnParser); columnParserMap.put("java.lang.int", columnParser); FixedFileLineIterator<FixedFileLineIterator_Stub03> lineIterator = new FixedFileLineIterator<FixedFileLineIterator_Stub03>(fileName, clazz, columnParserMap); String fileLineString = "12345"; ReflectionTestUtils.setField(lineIterator, "fileEncoding", "aaa"); try { lineIterator.separateColumns(fileLineString); fail("FileExceptionがスローされませんでした。"); } catch (FileException e) { assertEquals(UnsupportedEncodingException.class, e.getCause() .getClass()); assertEquals(fileName, e.getFileName()); assertEquals("fileEncoding which isn't supported was set.", e .getMessage()); } }
@Test public void testSeparateColumns05() throws Exception { URL url = FixedFileLineIteratorTest.class.getResource("/aaa.txt"); String fileName = url.getPath(); Class<FixedFileLineIterator_Stub04> clazz = FixedFileLineIterator_Stub04.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); ColumnParser columnParser = new FixedFileLineIterator_ColumnParserStub01(); columnParserMap.put("java.lang.String", columnParser); columnParserMap.put("java.util.Date", columnParser); columnParserMap.put("java.math.BigDecimal", columnParser); columnParserMap.put("java.lang.int", columnParser); FixedFileLineIterator<FixedFileLineIterator_Stub04> lineIterator = new FixedFileLineIterator<FixedFileLineIterator_Stub04>(fileName, clazz, columnParserMap); String fileLineString = "2006/12/10aaaaa123456789"; String[] columns = lineIterator.separateColumns(fileLineString); assertEquals(3, columns.length); assertEquals("2006/12/10", columns[0]); assertEquals("aaaaa", columns[1]); assertEquals("123456789", columns[2]); }
@Test public void testSeparateColumns06() throws Exception { URL url = FixedFileLineIteratorTest.class.getResource("/aaa.txt"); String fileName = url.getPath(); Class<FixedFileLineIterator_Stub01> clazz = FixedFileLineIterator_Stub01.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); ColumnParser columnParser = new FixedFileLineIterator_ColumnParserStub01(); columnParserMap.put("java.lang.String", columnParser); columnParserMap.put("java.util.Date", columnParser); columnParserMap.put("java.math.BigDecimal", columnParser); columnParserMap.put("java.lang.int", columnParser); FixedFileLineIterator<FixedFileLineIterator_Stub01> lineIterator = new FixedFileLineIterator<FixedFileLineIterator_Stub01>(fileName, clazz, columnParserMap); String fileLineString = ""; String[] columns = lineIterator.separateColumns(fileLineString); assertEquals(0, columns.length); }
@Test public void testSeparateColumns07() throws Exception { URL url = FixedFileLineIteratorTest.class.getResource("/aaa.txt"); String fileName = url.getPath(); Class<FixedFileLineIterator_Stub04> clazz = FixedFileLineIterator_Stub04.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); ColumnParser columnParser = new FixedFileLineIterator_ColumnParserStub01(); columnParserMap.put("java.lang.String", columnParser); columnParserMap.put("java.util.Date", columnParser); columnParserMap.put("java.math.BigDecimal", columnParser); columnParserMap.put("java.lang.int", columnParser); FixedFileLineIterator<FixedFileLineIterator_Stub04> lineIterator = new FixedFileLineIterator<FixedFileLineIterator_Stub04>(fileName, clazz, columnParserMap); String fileLineString = "2006/01/01あああああ012345678"; try { lineIterator.separateColumns(fileLineString); fail("FileExceptionがスローされませんでした。"); } catch (FileException e) { assertEquals(IllegalStateException.class, e.getCause().getClass()); assertEquals(fileName, e.getFileName()); String message = "Total Columns byte is different from " + "Total FileLineObject's columns byte."; assertEquals(message, e.getMessage()); } }