VariableFileLineIterator extends AbstractFileLineIterator<T> { @Override protected String[] separateColumns(String fileLineString) { if (fileLineString == null || "".equals(fileLineString)) { return new String[0]; } StringBuilder columnBuilder = new StringBuilder(); char previousChar = Character.MIN_VALUE; List<String> columnList = new ArrayList<String>(); boolean isEnclosed = true; boolean isEscaped = false; int fieldCount = 0; char[] columnEncloseChar = getColumnEncloseChar(); if (!isEnclosed()) { return StringUtils.splitByWholeSeparatorPreserveAllTokens( fileLineString, Character.toString(delimiter)); } else { for (char currentChar : fileLineString.toCharArray()) { if (previousChar == Character.MIN_VALUE) { previousChar = currentChar; } if (previousChar == getEncloseCharcter(columnEncloseChar, fieldCount)) { if (isEnclosed) { if (currentChar == getEncloseCharcter( columnEncloseChar, fieldCount)) { isEnclosed = false; } } else { if (currentChar == getEncloseCharcter( columnEncloseChar, fieldCount)) { if (isEscaped) { columnBuilder.append(currentChar); isEscaped = false; } else { isEscaped = true; } } else if (currentChar == getDelimiter()) { if (isEscaped) { columnList.add(columnBuilder.toString()); previousChar = Character.MIN_VALUE; columnBuilder.delete(0, columnBuilder.length()); isEnclosed = true; isEscaped = false; fieldCount++; } else { columnBuilder.append(currentChar); isEscaped = false; } } else { columnBuilder.append(currentChar); } } } else { if (currentChar != getDelimiter()) { columnBuilder.append(currentChar); } else { columnList.add(columnBuilder.toString()); previousChar = Character.MIN_VALUE; columnBuilder.delete(0, columnBuilder.length()); fieldCount++; } } } columnList.add(columnBuilder.toString()); return columnList.toArray(new String[columnList.size()]); } } VariableFileLineIterator(String fileName, Class<T> clazz,
            Map<String, ColumnParser> columnParserMap); @Override char getDelimiter(); @Override char getEncloseChar();  }
@Test public void testSeparateColumns01() throws Exception { URL url = this.getClass().getResource("File_Empty.txt"); String fileName = url.getPath(); Class<VariableFileLineIterator_Stub10> clazz = VariableFileLineIterator_Stub10.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); VariableFileLineIterator<VariableFileLineIterator_Stub10> variableFileLineIterator = new VariableFileLineIterator<VariableFileLineIterator_Stub10>(fileName, clazz, columnParserMap); String fileLineString = null; String[] result = variableFileLineIterator.separateColumns( fileLineString); assertEquals(0, result.length); }
@Test public void testSeparateColumns02() throws Exception { URL url = this.getClass().getResource("File_Empty.txt"); String fileName = url.getPath(); Class<VariableFileLineIterator_Stub10> clazz = VariableFileLineIterator_Stub10.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); VariableFileLineIterator<VariableFileLineIterator_Stub10> variableFileLineIterator = new VariableFileLineIterator<VariableFileLineIterator_Stub10>(fileName, clazz, columnParserMap); String fileLineString = "aaa"; String[] result = variableFileLineIterator.separateColumns( fileLineString); assertEquals(1, result.length); assertEquals("aaa", result[0]); }
@Test public void testSeparateColumns03() throws Exception { URL url = this.getClass().getResource("File_Empty.txt"); String fileName = url.getPath(); Class<VariableFileLineIterator_Stub10> clazz = VariableFileLineIterator_Stub10.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); VariableFileLineIterator<VariableFileLineIterator_Stub10> variableFileLineIterator = new VariableFileLineIterator<VariableFileLineIterator_Stub10>(fileName, clazz, columnParserMap); String fileLineString = "aaa,aaa,aaa"; String[] result = variableFileLineIterator.separateColumns( fileLineString); assertEquals(3, result.length); assertEquals("aaa", result[0]); assertEquals("aaa", result[1]); assertEquals("aaa", result[2]); }
@Test public void testSeparateColumns04() throws Exception { URL url = this.getClass().getResource("File_Empty.txt"); String fileName = url.getPath(); Class<VariableFileLineIterator_Stub11> clazz = VariableFileLineIterator_Stub11.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); VariableFileLineIterator<VariableFileLineIterator_Stub11> variableFileLineIterator = new VariableFileLineIterator<VariableFileLineIterator_Stub11>(fileName, clazz, columnParserMap); String fileLineString = "\"aaa\""; ReflectionTestUtils.setField(variableFileLineIterator, "columnEncloseChar", new char[] { '\"', '\"' }); String[] result = variableFileLineIterator.separateColumns( fileLineString); assertEquals(1, result.length); assertEquals("aaa", result[0]); }
@Test public void testSeparateColumns05() throws Exception { URL url = this.getClass().getResource("File_Empty.txt"); String fileName = url.getPath(); Class<VariableFileLineIterator_Stub11> clazz = VariableFileLineIterator_Stub11.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); VariableFileLineIterator<VariableFileLineIterator_Stub11> variableFileLineIterator = new VariableFileLineIterator<VariableFileLineIterator_Stub11>(fileName, clazz, columnParserMap); String fileLineString = "\"aaa\",\"aaa\",\"aaa\""; ReflectionTestUtils.setField(variableFileLineIterator, "columnEncloseChar", new char[] { '\"', '\"', '\"' }); String[] result = variableFileLineIterator.separateColumns( fileLineString); assertEquals(3, result.length); assertEquals("aaa", result[0]); assertEquals("aaa", result[1]); assertEquals("aaa", result[2]); }
@Test public void testSeparateColumns06() throws Exception { URL url = this.getClass().getResource("File_Empty.txt"); String fileName = url.getPath(); Class<VariableFileLineIterator_Stub12> clazz = VariableFileLineIterator_Stub12.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); VariableFileLineIterator<VariableFileLineIterator_Stub12> variableFileLineIterator = new VariableFileLineIterator<VariableFileLineIterator_Stub12>(fileName, clazz, columnParserMap); String fileLineString = "\"aa\ra\",\"aa,a\",\"aa\"\"a\""; ReflectionTestUtils.setField(variableFileLineIterator, "columnEncloseChar", new char[] { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }); String[] result = variableFileLineIterator.separateColumns( fileLineString); assertEquals(3, result.length); assertEquals("aa\ra", result[0]); assertEquals("aa,a", result[1]); assertEquals("aa\"a", result[2]); }
@Test public void testSeparateColumns07() throws Exception { URL url = this.getClass().getResource("File_Empty.txt"); String fileName = url.getPath(); Class<VariableFileLineIterator_Stub13> clazz = VariableFileLineIterator_Stub13.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); VariableFileLineIterator<VariableFileLineIterator_Stub13> variableFileLineIterator = new VariableFileLineIterator<VariableFileLineIterator_Stub13>(fileName, clazz, columnParserMap); String fileLineString = ""; String[] result = variableFileLineIterator.separateColumns( fileLineString); assertEquals(0, result.length); }
@Test public void testSeparateColumns08() throws Exception { URL url = this.getClass().getResource("File_Empty.txt"); String fileName = url.getPath(); Class<VariableFileLineIterator_Stub11> clazz = VariableFileLineIterator_Stub11.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); VariableFileLineIterator<VariableFileLineIterator_Stub11> variableFileLineIterator = new VariableFileLineIterator<VariableFileLineIterator_Stub11>(fileName, clazz, columnParserMap); String fileLineString = "aaa,aaa,aaa"; ReflectionTestUtils.setField(variableFileLineIterator, "columnEncloseChar", new char[] { Character.MIN_VALUE, Character.MIN_VALUE, Character.MIN_VALUE }); String[] result = variableFileLineIterator.separateColumns( fileLineString); assertEquals(3, result.length); assertEquals("aaa", result[0]); assertEquals("aaa", result[1]); assertEquals("aaa", result[2]); }
@Test public void testSeparateColumns09() throws Exception { URL url = this.getClass().getResource("File_Empty.txt"); String fileName = url.getPath(); Class<VariableFileLineIterator_Stub10> clazz = VariableFileLineIterator_Stub10.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); VariableFileLineIterator<VariableFileLineIterator_Stub10> variableFileLineIterator = new VariableFileLineIterator<VariableFileLineIterator_Stub10>(fileName, clazz, columnParserMap); String fileLineString = ",,,,"; String[] result = variableFileLineIterator.separateColumns( fileLineString); assertEquals(5, result.length); assertEquals("", result[0]); assertEquals("", result[1]); assertEquals("", result[2]); assertEquals("", result[3]); assertEquals("", result[4]); }
@Test public void testSeparateColumns10() throws Exception { URL url = this.getClass().getResource("File_Empty.txt"); String fileName = url.getPath(); Class<VariableFileLineIterator_Stub14> clazz = VariableFileLineIterator_Stub14.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); VariableFileLineIterator<VariableFileLineIterator_Stub14> variableFileLineIterator = new VariableFileLineIterator<VariableFileLineIterator_Stub14>(fileName, clazz, columnParserMap); String fileLineString = "aaa#aaa#aaa"; String[] result = variableFileLineIterator.separateColumns( fileLineString); assertEquals(3, result.length); assertEquals("aaa", result[0]); assertEquals("aaa", result[1]); assertEquals("aaa", result[2]); }
@Test public void testSeparateColumns11() throws Exception { URL url = this.getClass().getResource("File_Empty.txt"); String fileName = url.getPath(); Class<VariableFileLineIterator_Stub10> clazz = VariableFileLineIterator_Stub10.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); VariableFileLineIterator<VariableFileLineIterator_Stub10> variableFileLineIterator = new VariableFileLineIterator<VariableFileLineIterator_Stub10>(fileName, clazz, columnParserMap); String fileLineString = ",bbb,ccc"; String[] result = variableFileLineIterator.separateColumns( fileLineString); assertEquals(3, result.length); assertEquals("", result[0]); assertEquals("bbb", result[1]); assertEquals("ccc", result[2]); }
@Test public void testSeparateColumns12() throws Exception { URL url = this.getClass().getResource("File_Empty.txt"); String fileName = url.getPath(); Class<VariableFileLineIterator_Stub10> clazz = VariableFileLineIterator_Stub10.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); VariableFileLineIterator<VariableFileLineIterator_Stub10> variableFileLineIterator = new VariableFileLineIterator<VariableFileLineIterator_Stub10>(fileName, clazz, columnParserMap); String fileLineString = "aaa,,ccc"; String[] result = variableFileLineIterator.separateColumns( fileLineString); assertEquals(3, result.length); assertEquals("aaa", result[0]); assertEquals("", result[1]); assertEquals("ccc", result[2]); }
@Test public void testSeparateColumns13() throws Exception { URL url = this.getClass().getResource("File_Empty.txt"); String fileName = url.getPath(); Class<VariableFileLineIterator_Stub10> clazz = VariableFileLineIterator_Stub10.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); VariableFileLineIterator<VariableFileLineIterator_Stub10> variableFileLineIterator = new VariableFileLineIterator<VariableFileLineIterator_Stub10>(fileName, clazz, columnParserMap); String fileLineString = "aaa,bbb,"; String[] result = variableFileLineIterator.separateColumns( fileLineString); assertEquals(3, result.length); assertEquals("aaa", result[0]); assertEquals("bbb", result[1]); assertEquals("", result[2]); }
@Test public void testSeparateColumns14() throws Exception { URL url = this.getClass().getResource("File_Empty.txt"); String fileName = url.getPath(); Class<VariableFileLineIterator_Stub11> clazz = VariableFileLineIterator_Stub11.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); VariableFileLineIterator<VariableFileLineIterator_Stub11> variableFileLineIterator = new VariableFileLineIterator<VariableFileLineIterator_Stub11>(fileName, clazz, columnParserMap); String fileLineString = "\"aa\"bb\""; ReflectionTestUtils.setField(variableFileLineIterator, "columnEncloseChar", new char[] { '\"', '\"' }); String[] result = variableFileLineIterator.separateColumns( fileLineString); assertEquals(1, result.length); assertEquals("aabb\"", result[0]); }
@Test public void testSeparateColumns15() throws Exception { URL url = this.getClass().getResource("File_Empty.txt"); String fileName = url.getPath(); Class<VariableFileLineIterator_Stub15> clazz = VariableFileLineIterator_Stub15.class; Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); VariableFileLineIterator<VariableFileLineIterator_Stub15> variableFileLineIterator = new VariableFileLineIterator<VariableFileLineIterator_Stub15>(fileName, clazz, columnParserMap); String fileLineString = "aaa|aaa|aaa"; String[] result = variableFileLineIterator.separateColumns( fileLineString); assertEquals(3, result.length); assertEquals("aaa", result[0]); assertEquals("aaa", result[1]); assertEquals("aaa", result[2]); }