org.supercsv.exception.SuperCsvException Java Examples

The following examples show how to use org.supercsv.exception.SuperCsvException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BeanMappingFactoryHelper.java    From super-csv-annotation with Apache License 2.0 6 votes vote down vote up
/**
 * カラム番号が決定していないカラムをチェックする。
 * <p>{@link LazyCsvAnnotationBeanReader}/{@link LazyCsvAnnotationBeanWriter}において、
 *    CSVファイルや初期化時のヘッダーが不正により、該当するラベルがヘッダーに見つからないときをチェックする。
 * </p>
 * 
 * @since 2.2
 * @param beanType Beanタイプ
 * @param list カラム情報の一覧
 * @param headers ヘッダー
 * @throws SuperCsvException カラム番号が決定していないとき
 */
public static void validateNonDeterminedColumnNumber(final Class<?> beanType, final List<ColumnMapping> list,
        String[] headers) {
    
    final List<String> nonDeterminedLabels = list.stream()
            .filter(col -> !col.isDeterminedNumber())
            .map(col -> col.getLabel())
            .collect(Collectors.toList());
    
    if(!nonDeterminedLabels.isEmpty()) {
        
        throw new SuperCsvException(MessageBuilder.create("lazy.noDeteminedColumns")
                .var("property", beanType.getName())
                .var("labels", nonDeterminedLabels)
                .var("headers", headers)
                .format());
        
    }
    
}
 
Example #2
Source File: BeanMappingFactoryTest.java    From super-csv-annotation with Apache License 2.0 6 votes vote down vote up
/**
 * カラム番号が未決定のラベルをチェックする
 */
@Test
public void testValidateNonDeterminedColumns() {
    
    BeanMapping<NonDeterminedColumnBean> beanMapping = lazyFactory.create(NonDeterminedColumnBean.class, groupEmpty);
    
    Class<?> beanType = beanMapping.getType();
    List<ColumnMapping> list = beanMapping.getColumns();
    String[] headers = new String[] {"カラム", "カラム2", "カラム三"};
    
    assertThatThrownBy(() -> BeanMappingFactoryHelper.validateNonDeterminedColumnNumber(beanType, list, headers))
        .isInstanceOf(SuperCsvException.class)
        .hasMessage("'%s' のヘッダー([カラム, カラム2, カラム三])において、定義しているが一致しないラベル([カラム1, カラム3])があります。",
                NonDeterminedColumnBean.class.getName());
    
}
 
Example #3
Source File: TokenizerTest.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * Tests the readColumns() method when EOF is reached within quote scope.
 */
@Test
public void testQuotedFieldWithUnexpectedEOF() throws Exception {
	
	// EOF reached within quote scope
	final String input = "\"quoted spanning\ntwo lines with EOF reached before another quote";
	tokenizer = createTokenizer(input, EXCEL_PREFERENCE);
	try {
		tokenizer.readColumns(columns);
		fail("should have thrown SuperCsvException");
	}
	catch(SuperCsvException e) {
		assertEquals("unexpected end of file while reading quoted column beginning on line 1 and ending on line 2",
			e.getMessage());
	}
}
 
Example #4
Source File: TokenizerTest.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * Tests the readColumns() method when a newline is reached in quote
 * scoped when a single line is only supposed to be read
 */
@Test
public void testQuotedFieldWithUnexpectedNewline() throws Exception {

	// Row 2 has a missing trailing quote
	final String input = "col1,col2\n" +
			"\"foo\",\"bar\n" +
			"\"baz\",\"zoo\"\n" +
			"\"aaa\",\"bbb\"";
	CsvPreference pref = new CsvPreference.Builder(EXCEL_PREFERENCE)
			.maxLinesPerRow(1).build();

	tokenizer = createTokenizer(input, pref);
	try {
		boolean first = tokenizer.readColumns(columns);
		assertEquals(true , first);

		tokenizer.readColumns(columns);
		fail("should have thrown SuperCsvException");
	}
	catch(SuperCsvException e) {
		assertEquals("unexpected end of line while reading quoted column on line 2",
				e.getMessage());
	}
}
 
Example #5
Source File: CsvExceptionConverter.java    From super-csv-annotation with Apache License 2.0 6 votes vote down vote up
private List<CsvError> convertDefault(final SuperCsvException exception, final BeanMapping<?> beanMapping) {
    
    final CsvContext context = exception.getCsvContext();
    
    final Map<String, Object> variables = new HashMap<>();
    variables.put("lineNumber", context.getLineNumber());
    variables.put("rowNumber", context.getRowNumber());
    variables.put("columnNumber", context.getColumnNumber());
    
    final String defaultMessage = exception.getMessage();
    
    final String errorCode = "csvError";
    final String objectName = beanMapping.getType().getSimpleName();
    final String[] errorCodes = codeGenerator.generateCodes(errorCode, objectName);
    
    final CsvError error = new CsvError.Builder(objectName, errorCodes)
            .variables(variables)
            .defaultMessage(defaultMessage)
            .build();
    
    return Arrays.asList(error);
    
}
 
Example #6
Source File: CsvDozerBeanReaderTest.java    From super-csv with Apache License 2.0 6 votes vote down vote up
private void testReadForBrokenCSV(final CsvDozerBeanReader brokenCsvBeanReader, final List<SurveyResponse> responses,
		final String expectedMessage, final int expectedRowsRead) throws IOException {
	brokenCsvBeanReader.getHeader(true);
	brokenCsvBeanReader.configureBeanMapping(SurveyResponse.class, FIELD_MAPPING);
	Exception expected = null;
	for(int i = 0; i < 4; i++) {
		try{
			final SurveyResponse response = readSurveyResponse(brokenCsvBeanReader, USE_PROCESSORS, EXISTING_BEAN);
			if(response != null) {
				responses.add(response);
			}
		} catch (final SuperCsvException e) {
			expected = e;
		}
	}
	assertNotNull(expected);
	assertEquals(expectedMessage, expected.getLocalizedMessage());
	assertEquals(expectedRowsRead, responses.size());
}
 
Example #7
Source File: AbstractCsvReaderTest.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * Tests the getCsvHeader(true) can't be called when a line has already been read.
 */
@Test
public void testIllegalGetHeader() throws IOException {
	
	abstractReader.getHeader(true);
	
	try {
		abstractReader.getHeader(true);
		fail("should have thrown SuperCsvException");
	}
	catch(SuperCsvException e) {
		assertEquals("CSV header must be fetched as the first read operation, but 1 lines have already been read",
			e.getMessage());
	}
	
}
 
Example #8
Source File: TokenizerEscapingTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the readColumns() method with an odd number of escape characters
 * char.
 */
@Test(expected = SuperCsvException.class)
public void testOddSeriesOfEscapeChars() throws Exception {

	final CsvPreference csvPref = new CsvPreference.Builder(CsvPreference.STANDARD_PREFERENCE)
			.setQuoteEscapeChar('#')
			.build();

	final String input = "\"#####\"";
	tokenizer = createTokenizer(input, csvPref);
	tokenizer.readColumns(columns);
}
 
Example #9
Source File: SuperCsvRowException.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
@Override
public void printStackTrace(final PrintStream s) {
    
    super.printStackTrace(s);
    
    int count = 1;
    for(SuperCsvException e : columnErrors) {
        s.printf("[ColumnError-%d] : ", count);
        e.printStackTrace(s);
        count++;
    }
    
}
 
Example #10
Source File: SuperCsvRowException.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
@Override
public void printStackTrace(final PrintWriter s) {
    
    super.printStackTrace(s);
    
    int count = 1;
    for(SuperCsvException e : columnErrors) {
        s.printf("[ColumnError-%d] : ", count);
        e.printStackTrace(s);
        count++;
    }
    
}
 
Example #11
Source File: SuperCsvBindingException.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
@Override
public void printStackTrace(final PrintWriter s) {
    
    super.printStackTrace(s);
    
    int count = 1;
    for(SuperCsvException e : processingErrors) {
        s.printf("[ProcessingError-%d] : ", count);
        e.printStackTrace(s);
        count++;
    }
}
 
Example #12
Source File: SuperCsvBindingException.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
@Override
public void printStackTrace(final PrintStream s) {
    
    super.printStackTrace(s);
    
    int count = 1;
    for(SuperCsvException e : processingErrors) {
        s.printf("[ProcessingError-%d] : ", count);
        e.printStackTrace(s);
        count++;
    }
    
}
 
Example #13
Source File: LazyCsvAnnotationBeanReaderTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
/**
 * 初期化に失敗する場合 - Beanに定義してある情報とCSVのヘッダーが一致しない場合
 * @since 2.2
 */
@Test
public void testInit_nonDeterminedColumnNumbers() throws Exception {
    
    File file = new File("src/test/data/test_read_lazy.csv");
    
    LazyCsvAnnotationBeanReader<SampleLazyBean> csvReader = new LazyCsvAnnotationBeanReader<>(
            SampleLazyBean.class,
            new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")),
            CsvPreference.STANDARD_PREFERENCE);
    csvReader.setExceptionConverter(exceptionConverter);
    
    final String[] headers = new String[]{
            "no",
            "name1",
            "生年月日2",
            "備考",
            "あああ"
        };
    
    assertThatThrownBy(() -> csvReader.init(headers))
        .isInstanceOf(SuperCsvException.class)
        .hasMessage("'%s' のヘッダー([no, name1, 生年月日2, 備考, あああ])において、定義しているが一致しないラベル([生年月日])があります。",
                SampleLazyBean.class.getName());
    
    assertThat(csvReader.getErrorMessages())
        .hasSize(0);
    
}
 
Example #14
Source File: CsvAnnotationBeanReaderTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
/**
     * ヘッダーの列数が一致しない
     */
    @Test
    public void testRead_error_header_size() throws IOException {
        
        File file = new File("src/test/data/test_read_error_header_size.csv");
        
        CsvAnnotationBeanReader<SampleNormalBean> csvReader = new CsvAnnotationBeanReader<>(
                SampleNormalBean.class,
                new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")),
                CsvPreference.STANDARD_PREFERENCE,
                DefaultGroup.class, SampleNormalBean.ReadGroup.class);
        csvReader.setExceptionConverter(exceptionConverter);
        
        List<SampleNormalBean> list = new ArrayList<>();
        
        try {
            final String[] headers = csvReader.getHeader(true);
            fail();
            
        } catch(SuperCsvException e) {
            assertThat(e).isInstanceOf(SuperCsvNoMatchColumnSizeException.class);
            
//            e.printStackTrace();
        }
        
        // convert error messages.
        List<String> messages = csvReader.getErrorMessages();
        assertThat(messages).hasSize(1)
            .contains("[1行] : 列数が不正です。 11列で設定すべきですが、実際には10列になっています。");
        messages.forEach(System.out::println);
        
        csvReader.close();
    }
 
Example #15
Source File: TokenizerEscapingTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * Test double-quote char when in backslash-escape mode should throw exception
 */
@Test(expected = SuperCsvException.class)
public void testDoubleQuoteBackslashEscapeChar() throws Exception {

	// quote char is ' and escape char is $
	final CsvPreference csvPref = new CsvPreference.Builder('\'', ',', "\n")
			.setQuoteEscapeChar('$')
			.build();

	final String input = "'field with an escaped quote #' and a '' double quote'";
	tokenizer = createTokenizer(input, csvPref);
	tokenizer.readColumns(columns);

	Assert.fail();
}
 
Example #16
Source File: TokenizerTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the readColumns() method when a newline is reached in quote
 * scoped when two lines are only supposed to be read
 */
@Test
public void testQuotedFieldWithTwoMaxLinesNoMoreLinesRead() throws Exception {

	// Row 2 has a missing trailing quote
	final String input = "col1,col2\n" +
			"\"foo,bar\n" +
			"baz,zoo\n" +
			"aaa,bbb";
	CsvPreference pref = new CsvPreference.Builder(EXCEL_PREFERENCE)
			.maxLinesPerRow(2).build();

	tokenizer = createTokenizer(input, pref);
	try {
		boolean first = tokenizer.readColumns(columns);
		assertEquals(true , first);
		assertEquals("[col1, col2]" , columns.toString());
		

		boolean second = tokenizer.readColumns(columns);
		assertEquals(true , second);
		assertEquals("[\"foo,bar]" , columns.toString());


		tokenizer.readColumns(columns);
		fail("should have thrown SuperCsvException");
	}
	catch(SuperCsvException e) {
		assertEquals("max number of lines to read exceeded while reading quoted column beginning on line 2 and ending on line 3",
				e.getMessage());
	}
	boolean fourth = tokenizer.readColumns(columns);
	assertEquals(true , fourth);
	assertEquals("[aaa, bbb]" , columns.toString());
	
}
 
Example #17
Source File: TokenizerTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testQuotedFieldWithUnexpectedNewlineNoNextLineRead() throws Exception {

	// Row 2 has a missing trailing quote
	final String input = "col1,col2\n" +
			"\"foo\",\"bar\n" +
			"\"baz\",\"zoo\"\n" +
			"\"aaa\",\"bbb\"";
	CsvPreference pref = new CsvPreference.Builder(EXCEL_PREFERENCE)
			.maxLinesPerRow(1).build();

	tokenizer = createTokenizer(input, pref);
	try {
		final boolean first = tokenizer.readColumns(columns);
		assertEquals(true , first);
		assertEquals("[col1, col2]" , columns.toString());

		tokenizer.readColumns(columns);
		fail("should have thrown SuperCsvException");
	}
	catch(SuperCsvException e) {
		assertEquals("unexpected end of line while reading quoted column on line 2",
				e.getMessage());
	}
	final boolean third = tokenizer.readColumns(columns);
	assertEquals(true , third);
	assertEquals("[baz, zoo]" , columns.toString());

	final boolean fourth = tokenizer.readColumns(columns);
	assertEquals(true , fourth);
	assertEquals("[aaa, bbb]" , columns.toString());

	//line 4 was the last 
	final boolean fifth = tokenizer.readColumns(columns);
	assertEquals(false , fifth);
}
 
Example #18
Source File: TokenizerTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the readColumns() method when a newline is reached in quote
 * scoped when two lines are only supposed to be read
 */
@Test
public void testQuotedFieldWithTwoMaxLines() throws Exception {

	// Row 2 has a missing trailing quote
	final String input = "col1,col2\n" +
			"\"foo\",\"bar\n" +
			"baz,zoo\n" +
			"aaa,bbb";
	CsvPreference pref = new CsvPreference.Builder(EXCEL_PREFERENCE)
			.maxLinesPerRow(2).build();

	tokenizer = createTokenizer(input, pref);
	try {
		boolean first = tokenizer.readColumns(columns);
		assertEquals(true , first);

		boolean second = tokenizer.readColumns(columns);
		assertEquals(true , second);

		tokenizer.readColumns(columns);
		fail("should have thrown SuperCsvException");
	}
	catch(SuperCsvException e) {
		assertEquals("max number of lines to read exceeded while reading quoted column beginning on line 2 and ending on line 3",
				e.getMessage());
	}
}
 
Example #19
Source File: CsvAnnotationBeanReaderTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
/**
 * ヘッダの値が一致しない
 */
@Test
public void testRead_error_header_value() throws IOException {
    
    File file = new File("src/test/data/test_read_error_header_value.csv");
    
    CsvAnnotationBeanReader<SampleNormalBean> csvReader = new CsvAnnotationBeanReader<>(
            SampleNormalBean.class,
            new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")),
            CsvPreference.STANDARD_PREFERENCE,
            DefaultGroup.class, SampleNormalBean.ReadGroup.class);
    csvReader.setExceptionConverter(exceptionConverter);
    
    List<SampleNormalBean> list = new ArrayList<>();
    
    try {
        // read header
        final String headers[] = csvReader.getHeader(true);
        
        fail();
        
    } catch(SuperCsvException e) {
        assertThat(e).isInstanceOf(SuperCsvNoMatchHeaderException.class);
        
    }
    
    // convert error messages.
    List<String> messages = csvReader.getErrorMessages();
    assertThat(messages).hasSize(1)
        .contains("[1行]  : ヘッダーの値「id, 間違い, number2, string1, string2, date1, date2, enum1, 列挙型2, boolean1, boolean2」は、「id, 数字1, number2, string1, string2, date1, date2, enum1, 列挙型2, boolean1, boolean2」と一致しません。");
    messages.forEach(System.out::println);
    
    csvReader.close();
}
 
Example #20
Source File: AbstractCsvReader.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public String[] getHeader(final boolean firstLineCheck) throws IOException {
	
	if( firstLineCheck && tokenizer.getLineNumber() != 0 ) {
		throw new SuperCsvException(String.format(
			"CSV header must be fetched as the first read operation, but %d lines have already been read",
			tokenizer.getLineNumber()));
	}
	
	if( readRow() ) {
		return columns.toArray(new String[columns.size()]);
	}
	
	return null;
}
 
Example #21
Source File: Util.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a List to a Map using the elements of the nameMapping array as the keys of the Map.
 * 
 * @param destinationMap
 *            the destination Map (which is cleared before it's populated)
 * @param nameMapping
 *            the keys of the Map (corresponding with the elements in the sourceList). Cannot contain duplicates.
 * @param sourceList
 *            the List to convert
 * @param <T>
 *            the type of the values in the map
 * @throws NullPointerException
 *             if destinationMap, nameMapping or sourceList is null
 * @throws SuperCsvException
 *             if nameMapping and sourceList are not the same size
 */
public static <T> void filterListToMap(final Map<String, T> destinationMap, final String[] nameMapping,
	final List<? extends T> sourceList) {
	if( destinationMap == null ) {
		throw new NullPointerException("destinationMap should not be null");
	} else if( nameMapping == null ) {
		throw new NullPointerException("nameMapping should not be null");
	} else if( sourceList == null ) {
		throw new NullPointerException("sourceList should not be null");
	} else if( nameMapping.length != sourceList.size() ) {
		throw new SuperCsvException(
			String
				.format(
					"the nameMapping array and the sourceList should be the same size (nameMapping length = %d, sourceList size = %d)",
					nameMapping.length, sourceList.size()));
	}
	
	destinationMap.clear();
	
	for( int i = 0; i < nameMapping.length; i++ ) {
		final String key = nameMapping[i];
		
		if( key == null ) {
			continue; // null's in the name mapping means skip column
		}
		
		// no duplicates allowed
		if( destinationMap.containsKey(key) ) {
			throw new SuperCsvException(String.format("duplicate nameMapping '%s' at index %d", key, i));
		}
		
		destinationMap.put(key, sourceList.get(i));
	}
}
 
Example #22
Source File: Util.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * Processes each element in the source List (using the corresponding processor chain in the processors array) and
 * adds it to the destination List. A <tt>null</tt> CellProcessor in the array indicates that no processing is
 * required and the element should be added as-is.
 * 
 * @param destination
 *            the List to add the processed elements to (which is cleared before it's populated)
 * @param source
 *            the List of source elements to be processed
 * @param processors
 *            the array of CellProcessors used to process each element. The number of elements in this array must
 *            match the size of the source List. A <tt>null</tt> CellProcessor in this array indicates that no
 *            processing is required and the element should be added as-is.
 * @param lineNo
 *            the current line number
 * @param rowNo
 *            the current row number
 * @throws NullPointerException
 *             if destination, source or processors are null
 * @throws SuperCsvConstraintViolationException
 *             if a CellProcessor constraint failed
 * @throws SuperCsvException
 *             if source.size() != processors.length, or CellProcessor execution failed
 */
public static void executeCellProcessors(final List<Object> destination, final List<?> source,
	final CellProcessor[] processors, final int lineNo, final int rowNo) {
	
	if( destination == null ) {
		throw new NullPointerException("destination should not be null");
	} else if( source == null ) {
		throw new NullPointerException("source should not be null");
	} else if( processors == null ) {
		throw new NullPointerException("processors should not be null");
	}
	
	// the context used when cell processors report exceptions
	final CsvContext context = new CsvContext(lineNo, rowNo, 1);
	context.setRowSource(new ArrayList<Object>(source));
	
	if( source.size() != processors.length ) {
		throw new SuperCsvException(String.format(
			"The number of columns to be processed (%d) must match the number of CellProcessors (%d): check that the number"
				+ " of CellProcessors you have defined matches the expected number of columns being read/written",
			source.size(), processors.length), context);
	}
	
	destination.clear();
	
	for( int i = 0; i < source.size(); i++ ) {
		
		context.setColumnNumber(i + 1); // update context (columns start at 1)
		
		if( processors[i] == null ) {
			destination.add(source.get(i)); // no processing required
		} else {
			destination.add(processors[i].execute(source.get(i), context)); // execute the processor chain
		}
	}
}
 
Example #23
Source File: CsvAnnotationBeanReaderTest.java    From super-csv-annotation with Apache License 2.0 4 votes vote down vote up
/**
     * 列のパターンが不正な場合
     */
    @Test
    public void testRead_error_wrong_pattern() throws IOException {
        
        File file = new File("src/test/data/test_read_error_wrong_pattern.csv");
        
        CsvAnnotationBeanReader<SampleNormalBean> csvReader = new CsvAnnotationBeanReader<>(
                SampleNormalBean.class,
                new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")),
                CsvPreference.STANDARD_PREFERENCE,
                DefaultGroup.class, SampleNormalBean.ReadGroup.class);
        csvReader.setExceptionConverter(exceptionConverter);
        
        List<SampleNormalBean> list = new ArrayList<>();
        
        // read header
        final String headers[] = csvReader.getHeader(true);
        
        try {
            SampleNormalBean bean;
            while((bean = csvReader.read()) != null) {
                list.add(bean);
                
                assertBean(bean);
            }
            
            fail();
            
        } catch(SuperCsvException e) {
            assertThat(e).isInstanceOf(SuperCsvBindingException.class);
            
//            e.printStackTrace();
            
            
        }
        
        // convert error messages.
        List<String> messages = csvReader.getErrorMessages();
        assertThat(messages).hasSize(1)
            .contains("[2行, 6列] : 項目「date1」の値(2000/01/01 00:01:02)の書式は不正です。");
        messages.forEach(System.out::println);
        
        csvReader.close();
        
    }
 
Example #24
Source File: CsvAnnotationBeanReaderTest.java    From super-csv-annotation with Apache License 2.0 4 votes vote down vote up
/**
     * 列のサイズが一致しない
     */
    @Test
    public void testRead_error_column_size() throws IOException {
        
        File file = new File("src/test/data/test_read_error_column_size.csv");
        
        CsvAnnotationBeanReader<SampleNormalBean> csvReader = new CsvAnnotationBeanReader<>(
                SampleNormalBean.class,
                new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")),
                CsvPreference.STANDARD_PREFERENCE,
                DefaultGroup.class, SampleNormalBean.ReadGroup.class);
        csvReader.setExceptionConverter(exceptionConverter);
        
        List<SampleNormalBean> list = new ArrayList<>();
        
        // read header
        final String headers[] = csvReader.getHeader(true);
        
        try {
            SampleNormalBean bean;
            while((bean = csvReader.read()) != null) {
                list.add(bean);
                
                assertBean(bean);
            }
            
            fail();
            
        } catch(SuperCsvException e) {
            
            assertThat(e).isInstanceOf(SuperCsvNoMatchColumnSizeException.class);
            
//            e.printStackTrace();
            
        }
        
        // convert error messages.
        List<String> messages = csvReader.getErrorMessages();
        assertThat(messages).hasSize(1)
            .contains("[3行] : 列数が不正です。 11列で設定すべきですが、実際には13列になっています。");
        messages.forEach(System.out::println);
        
        csvReader.close();
        
    }
 
Example #25
Source File: CsvParser.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
@Override
public void processTuple(byte[] tuple)
{
  if (tuple == null) {
    if (err.isConnected()) {
      err.emit(new KeyValPair<String, String>(null, "Blank/null tuple"));
    }
    errorTupleCount++;
    return;
  }
  String incomingString = new String(tuple);
  if (StringUtils.isBlank(incomingString) || StringUtils.equals(incomingString, header)) {
    if (err.isConnected()) {
      err.emit(new KeyValPair<String, String>(incomingString, "Blank/header tuple"));
    }
    errorTupleCount++;
    return;
  }
  try {
    if (parsedOutput.isConnected()) {
      csvStringReader.open(incomingString);
      Map<String, Object> map = csvMapReader.read(nameMapping, processors);
      parsedOutput.emit(map);
      parsedOutputCount++;
    }

    if (out.isConnected() && clazz != null) {
      csvStringReader.open(incomingString);
      Object obj = csvBeanReader.read(clazz, nameMapping, processors);
      out.emit(obj);
      emittedObjectCount++;
    }

  } catch (SuperCsvException | IOException | IllegalArgumentException e) {
    if (err.isConnected()) {
      err.emit(new KeyValPair<String, String>(incomingString, e.getMessage()));
    }
    errorTupleCount++;
    logger.error("Tuple could not be parsed. Reason {}", e.getMessage());
  }
}
 
Example #26
Source File: SuperCsvRowException.java    From super-csv-annotation with Apache License 2.0 4 votes vote down vote up
public List<SuperCsvException> getColumnErrors() {
    return columnErrors;
}
 
Example #27
Source File: SuperCsvRowException.java    From super-csv-annotation with Apache License 2.0 4 votes vote down vote up
public void addAllErrors(final List<SuperCsvException> errors) {
    for(SuperCsvException error : errors) {
        addError(error);
    }
}
 
Example #28
Source File: SuperCsvRowException.java    From super-csv-annotation with Apache License 2.0 4 votes vote down vote up
public void addError(final SuperCsvException error) {
    this.columnErrors.add(error);
}
 
Example #29
Source File: UtilTest.java    From super-csv with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the executeCellProcessors() method with a source List whose size doesn't match the number of CellProcessors
 * (should throw an Exception).
 */
@Test(expected = SuperCsvException.class)
public void testExecuteCellProcessorsWithSizeMismatch() {
	final List<Object> invalidSizeList = new ArrayList<Object>();
	Util.executeCellProcessors(new ArrayList<Object>(), invalidSizeList, PROCESSORS, LINE_NO, ROW_NO);
}
 
Example #30
Source File: UtilTest.java    From super-csv with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the filterListToMap() method with a name mapping array with duplicate elements (should throw an exception).
 */
@Test(expected = SuperCsvException.class)
public void testFilterListToMapWithDuplicateNameMapping() {
	Util.filterListToMap(new HashMap<String, String>(), new String[] { "name", "name", "city" }, LIST);
}