/** * header is declared but file is empty. * @throws Exception if failed */ @Test public void empty_with_header() throws Exception { headers = Arrays.asList("key", "value"); CsvParser parser = create(""); assertThat(parser.next(), is(false)); }
/** * only header. * @throws Exception if failed */ @Test public void only_header() throws Exception { headers = Arrays.asList("key", "value"); CsvParser parser = create("key,value\r\n"); assertThat(parser.next(), is(false)); }
/** * many separators in a cell. * @throws Exception if failed */ @Test public void many_separators() throws Exception { StringBuilder buf = new StringBuilder(); final int separators = 10000; for (int i = 0; i < separators; i++) { buf.append('a'); buf.append(','); } CsvParser parser = create(buf.toString()); assertThat(parser.next(), is(true)); for (int i = 0; i < separators; i++) { assertFill(parser, "a"); } assertFill(parser, null); parser.endRecord(); assertThat(parser.next(), is(false)); }
/** * many characters in a cell. * @throws Exception if failed */ @Test public void many_characters() throws Exception { StringBuilder buf = new StringBuilder(); final int characters = 100000; for (int i = 0; i < characters; i++) { buf.append('a'); } CsvParser parser = create(buf.toString()); assertThat(parser.next(), is(true)); StringOption option = new StringOption(); parser.fill(option); assertThat(option.getAsString().length(), is(characters)); parser.endRecord(); assertThat(parser.next(), is(false)); }
/** * test for invalid short values. * @throws Exception if failed */ @Test public void invalid_short() throws Exception { CsvParser parser = create(String.valueOf(Short.MAX_VALUE + 1)); assertThat(parser.next(), is(true)); try { parser.fill(new ShortOption()); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.INVALID_CELL_FORMAT)); } }
/** * test for invalid float values. * @throws Exception if failed */ @Test public void invalid_float() throws Exception { CsvParser parser = create(String.valueOf("?")); assertThat(parser.next(), is(true)); try { parser.fill(new FloatOption()); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.INVALID_CELL_FORMAT)); } }
/** * test for invalid bytes. * @throws Exception if failed */ @Test public void invalid_byte() throws Exception { CsvParser parser = create(String.valueOf(Byte.MAX_VALUE + 1)); assertThat(parser.next(), is(true)); try { parser.fill(new ByteOption()); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.INVALID_CELL_FORMAT)); } }
/** * test for invalid double values. * @throws Exception if failed */ @Test public void invalid_double() throws Exception { CsvParser parser = create(String.valueOf("?")); assertThat(parser.next(), is(true)); try { parser.fill(new DoubleOption()); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.INVALID_CELL_FORMAT)); } }
/** * test for invalid date values. * @throws Exception if failed */ @Test public void invalid_date() throws Exception { CsvParser parser = create(String.valueOf("?")); assertThat(parser.next(), is(true)); try { parser.fill(new DateOption()); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.INVALID_CELL_FORMAT)); } }
/** * test for invalid int values. * @throws Exception if failed */ @Test public void invalid_int() throws Exception { CsvParser parser = create(String.valueOf((long) Integer.MAX_VALUE + 1)); assertThat(parser.next(), is(true)); try { parser.fill(new IntOption()); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.INVALID_CELL_FORMAT)); } }
/** * test for invalid decimal values. * @throws Exception if failed */ @Test public void invalid_decimal() throws Exception { CsvParser parser = create(String.valueOf("?")); assertThat(parser.next(), is(true)); try { parser.fill(new DecimalOption()); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.INVALID_CELL_FORMAT)); } }
/** * test for invalid long values. * @throws Exception if failed */ @Test public void invalid_long() throws Exception { CsvParser parser = create(String.valueOf(Long.MAX_VALUE + "0")); assertThat(parser.next(), is(true)); try { parser.fill(new LongOption()); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.INVALID_CELL_FORMAT)); } }
/** * test for invalid datetime values. * @throws Exception if failed */ @Test public void invalid_datetime() throws Exception { CsvParser parser = create(String.valueOf("?")); assertThat(parser.next(), is(true)); try { parser.fill(new DateTimeOption()); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.INVALID_CELL_FORMAT)); } }
/** * has header. * @throws Exception if failed */ @Test public void with_header() throws Exception { headers = Arrays.asList("key", "value"); CsvParser parser = create( "key,value\r\n" + "hello,world\r\n"); StringOption key = new StringOption(); StringOption value = new StringOption(); assertThat(parser.next(), is(true)); assertThat(parser.getCurrentLineNumber(), is(2L)); assertThat(parser.getCurrentRecordNumber(), is(1L)); parser.fill(key); parser.fill(value); parser.endRecord(); assertThat(parser.next(), is(false)); assertThat(key.getAsString(), is("hello")); assertThat(value.getAsString(), is("world")); }
/** * record is too long. * @throws Exception if failed */ @Test public void too_long_record() throws Exception { CsvParser parser = create("a,b,c"); assertThat(parser.next(), is(true)); try { assertFill(parser, "a"); assertFill(parser, "b"); parser.endRecord(); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.TOO_LONG_RECORD)); } assertThat(parser.next(), is(false)); }
/** * test for booleans. * @throws Exception if failed */ @Test public void boolean_values() throws Exception { trueFormat = "true"; falseFormat = "false"; CsvParser parser = create("true,false,"); BooleanOption option = new BooleanOption(); assertThat(parser.next(), is(true)); parser.fill(option); assertThat(option.get(), is(true)); parser.fill(option); assertThat(option.get(), is(false)); parser.fill(option); assertThat(option.isNull(), is(true)); parser.endRecord(); assertThat(parser.next(), is(false)); }
/** * test for date values. * @throws Exception if failed */ @Test public void date_values_direct() throws Exception { dateFormat = "yyyyMMdd"; CsvParser parser = create( "20110331," + "19710401,"); DateOption option = new DateOption(); assertThat(parser.next(), is(true)); parser.fill(option); assertThat(option.get(), is(new Date(2011, 3, 31))); parser.fill(option); assertThat(option.get(), is(new Date(1971, 4, 1))); parser.fill(option); assertThat(option.isNull(), is(true)); parser.endRecord(); assertThat(parser.next(), is(false)); }
/** * record is too short. * @throws Exception if failed */ @Test public void too_short_record() throws Exception { CsvParser parser = create("a,b,c"); assertThat(parser.next(), is(true)); try { assertFill(parser, "a"); assertFill(parser, "b"); assertFill(parser, "c"); parser.fill(new StringOption()); parser.endRecord(); fail(); } catch (CsvFormatException e) { assertThat(e.getStatus().getReason(), is(Reason.TOO_SHORT_RECORD)); } assertThat(parser.next(), is(false)); }
/** * test for date values. * @throws Exception if failed */ @Test public void date_values() throws Exception { dateFormat = "yyyy/MM/dd"; CsvParser parser = create( "2011/03/31," + "1971/4/1," + " 1971/4/1 ,"); DateOption option = new DateOption(); assertThat(parser.next(), is(true)); parser.fill(option); assertThat(option.get(), is(new Date(2011, 3, 31))); parser.fill(option); assertThat(option.get(), is(new Date(1971, 4, 1))); parser.fill(option); assertThat(option.get(), is(new Date(1971, 4, 1))); parser.fill(option); assertThat(option.isNull(), is(true)); parser.endRecord(); assertThat(parser.next(), is(false)); }
/** * test for date-time values. * @throws Exception if failed */ @Test public void datetime_values_direct() throws Exception { dateTimeFormat = "yyyyMMddHHmmss"; CsvParser parser = create( "20110331235959," + "19710401010203," + " 19710401010203 ,"); DateTimeOption option = new DateTimeOption(); assertThat(parser.next(), is(true)); parser.fill(option); assertThat(option.get(), is(new DateTime(2011, 3, 31, 23, 59, 59))); parser.fill(option); assertThat(option.get(), is(new DateTime(1971, 4, 1, 1, 2, 3))); parser.fill(option); assertThat(option.get(), is(new DateTime(1971, 4, 1, 1, 2, 3))); parser.fill(option); assertThat(option.isNull(), is(true)); parser.endRecord(); assertThat(parser.next(), is(false)); }