示例#1
0
 public CSVFormat getCSVFormat() {
   return CSVFormat.DEFAULT
       .withDelimiter(delimiter)
       .withRecordSeparator(recordSeparator)
       .withNullString(nullString)
       .withEscape(escapeCharacter)
       .withQuote(quoteCharacter)
       .withQuoteMode(quoteMode);
 }
 private List<Measurement> readMeasurements(String measurementCsvLocation) {
   List<Measurement> measurements = new ArrayList<>();
   try (Reader in = new FileReader(measurementCsvLocation)) {
     Iterable<CSVRecord> records = CSVFormat.DEFAULT.withDelimiter(';').parse(in);
     for (CSVRecord record : records) {
       Long timeStamp = Long.parseLong(record.get(0));
       Double temperature = Double.parseDouble(record.get(1));
       measurements.add(new Measurement(timeStamp, temperature));
     }
   } catch (IOException e) {
     e.printStackTrace();
   }
   return measurements;
 }
  @Test
  public void testApacheCommonCsvDelimiter() throws Exception {
    StringReader stringReader = new StringReader("foo;bar;15;true");
    CSVFormat csvFormat =
        CSVFormat.DEFAULT.withDelimiter(';').withHeader("firstName", "lastName", "age", "married");
    ApacheCommonCsvRecord record = getApacheCommonCsvRecord(stringReader, csvFormat);

    Foo foo = mapper.mapRecord(record);

    assertThat(foo).isNotNull();
    assertThat(foo.getFirstName()).isEqualTo("foo");
    assertThat(foo.getLastName()).isEqualTo("bar");
    assertThat(foo.getAge()).isEqualTo(15);
    assertThat(foo.isMarried()).isTrue();
  }
示例#4
0
 @Test(expected = IllegalArgumentException.class)
 public void testDelimiterSameAsEscapeThrowsException() {
   CSVFormat.DEFAULT.withDelimiter('!').withEscape('!');
 }
示例#5
0
 @Test(expected = IllegalArgumentException.class)
 public void testDelimiterSameAsCommentStartThrowsException() {
   CSVFormat.DEFAULT.withDelimiter('!').withCommentMarker('!');
 }
示例#6
0
 @Test(expected = IllegalArgumentException.class)
 public void testWithDelimiterLFThrowsException() {
   CSVFormat.DEFAULT.withDelimiter(LF);
 }
示例#7
0
 @Test
 public void testWithDelimiter() throws Exception {
   final CSVFormat formatWithDelimiter = CSVFormat.DEFAULT.withDelimiter('!');
   assertEquals('!', formatWithDelimiter.getDelimiter());
 }
示例#8
0
 protected CSVFormat getFormat() {
   return CSVFormat.DEFAULT.withDelimiter(';').withRecordSeparator('\n');
 }