|
| 1 | +package com.fasterxml.jackson.dataformat.csv.failing; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.Random; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.annotation.JsonPropertyOrder; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.databind.*; |
| 9 | +import com.fasterxml.jackson.dataformat.csv.*; |
| 10 | + |
| 11 | +public class SkipEmptyLines174Test extends ModuleTestBase |
| 12 | +{ |
| 13 | + private final static CsvMapper MAPPER = new CsvMapper(); |
| 14 | + |
| 15 | + @JsonPropertyOrder({ "timestamp", "random" }) |
| 16 | + static class Row { |
| 17 | + private int timestamp; |
| 18 | + private String random; |
| 19 | + |
| 20 | + public int getTimestamp() { |
| 21 | + return timestamp; |
| 22 | + } |
| 23 | + |
| 24 | + public void setTimestamp(int timestamp) { |
| 25 | + this.timestamp = timestamp; |
| 26 | + } |
| 27 | + |
| 28 | + public String getRandom() { |
| 29 | + return random; |
| 30 | + } |
| 31 | + |
| 32 | + public void setRandom(String random) { |
| 33 | + this.random = random; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public String toString() { |
| 38 | + return "Row{timestamp=" + timestamp + ", random='" + random + "'}"; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public void testEmptyLines174() throws Exception |
| 43 | + { |
| 44 | + String doc = generateCsvFile(); |
| 45 | + ObjectReader objectReader = MAPPER |
| 46 | + .enable(CsvParser.Feature.SKIP_EMPTY_LINES) |
| 47 | + .readerFor(Row.class) |
| 48 | + .with(MAPPER.schemaFor(Row.class)); |
| 49 | + int lineCount = 0; |
| 50 | + MappingIterator<Row> iterator = objectReader.readValues(doc); |
| 51 | + Row data = null; |
| 52 | + while (iterator.hasNext()) { |
| 53 | + ++lineCount; |
| 54 | + try { |
| 55 | + data = iterator.next(); |
| 56 | + } catch (Exception e) { |
| 57 | +// System.out.println(lineCount + " : " + data); |
| 58 | + fail("Failed on row #"+lineCount+", previous row: "+data); |
| 59 | + } |
| 60 | + } |
| 61 | + iterator.close(); |
| 62 | + } |
| 63 | + |
| 64 | + private String generateCsvFile() throws Exception { |
| 65 | + final StringWriter sw = new StringWriter(50000); |
| 66 | + int lineCount = 0; |
| 67 | + final Random rnd = new Random(); |
| 68 | + |
| 69 | + while (lineCount < 4000) { |
| 70 | + sw.append("\"" + System.currentTimeMillis()/1000 + "\",\"" + randomString(rnd) + "\"\n"); |
| 71 | + ++lineCount; |
| 72 | + } |
| 73 | + return sw.toString(); |
| 74 | + } |
| 75 | + |
| 76 | + private String randomString(Random rnd) { |
| 77 | + StringBuilder sb = new StringBuilder(); |
| 78 | + for (int i = 0; i < 10; ++i) { |
| 79 | + sb.append((char) ('A' + (rnd.nextInt() & 0xF))); |
| 80 | + } |
| 81 | + return sb.toString(); |
| 82 | + } |
| 83 | +} |
0 commit comments