From 1f1cbbf8b95d7e42e5bf3d17195a85c08e3cd997 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Sat, 4 Jan 2025 20:02:43 -0800 Subject: [PATCH] Prep for #31 --- .../jackson/dataformat/csv/CsvParser.java | 1 + .../csv/deser/TestParserWithHeader.java | 37 +++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java b/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java index 11717d492..28daa1a73 100644 --- a/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java +++ b/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/CsvParser.java @@ -900,6 +900,7 @@ protected void _readHeaderLine() throws IOException { while ((name = _reader.nextString()) != null) { // one more thing: always trim names, regardless of config settings + // TODO!!! [dataformats-text#31]: Allow disabling of trimming name = name.trim(); // See if "old" schema defined type; if so, use that type... CsvSchema.Column prev = _schema.column(name); diff --git a/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/TestParserWithHeader.java b/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/TestParserWithHeader.java index c3183c425..a0cd75d5f 100644 --- a/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/TestParserWithHeader.java +++ b/csv/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/TestParserWithHeader.java @@ -22,9 +22,11 @@ protected static class Entry { /********************************************************************** */ + private final CsvMapper MAPPER = mapperForCsv(); + public void testSimpleHeader() throws Exception { - CsvParser parser = new CsvFactory().createParser( + CsvParser parser = (CsvParser) MAPPER.createParser( "name, age, other\nfoo,2,xyz\n"); // need to enable first-line-as-schema handling: parser.setSchema(CsvSchema.emptySchema().withHeader()); @@ -41,10 +43,8 @@ public void testSimpleHeader() throws Exception public void testSimpleQuotes() throws Exception { - CsvMapper mapper = mapperForCsv(); - mapper.disable(CsvParser.Feature.WRAP_AS_ARRAY); CsvSchema schema = CsvSchema.emptySchema().withHeader(); - Entry entry = mapper.readerFor(Entry.class).with(schema).readValue( + Entry entry = MAPPER.readerFor(Entry.class).with(schema).readValue( "name,age,\"cute\" \nLeo,4,true\n"); assertEquals("Leo", entry.name); assertEquals(4, entry.age); @@ -53,10 +53,8 @@ public void testSimpleQuotes() throws Exception public void testSkipFirstDataLine() throws Exception { - CsvMapper mapper = mapperForCsv(); - mapper.disable(CsvParser.Feature.WRAP_AS_ARRAY); - CsvSchema schema = mapper.schemaFor(Entry.class).withSkipFirstDataRow(true); - MappingIterator it = mapper.readerFor(Entry.class).with(schema).readValues( + CsvSchema schema = MAPPER.schemaFor(Entry.class).withSkipFirstDataRow(true); + MappingIterator it = MAPPER.readerFor(Entry.class).with(schema).readValues( "12354\n6,Lila,true"); Entry entry; @@ -86,11 +84,9 @@ public void testLongHeader() throws Exception // Ok, then, first let's try reading columns: - - CsvMapper mapper = mapperForCsv(); - mapper.disable(CsvParser.Feature.WRAP_AS_ARRAY); + CsvSchema schema = CsvSchema.emptySchema().withHeader(); - CsvParser p = (CsvParser) mapper.getFactory().createParser(CSV); + CsvParser p = (CsvParser) MAPPER.getFactory().createParser(CSV); p.setSchema(schema); // need to read something to ensure header line is processed assertEquals(JsonToken.START_OBJECT, p.nextToken()); @@ -118,11 +114,9 @@ public void testLongColumnName() throws Exception final String CSV = sb.toString(); // Ok, then, first let's try reading columns: - - CsvMapper mapper = mapperForCsv(); - mapper.disable(CsvParser.Feature.WRAP_AS_ARRAY); + CsvSchema schema = CsvSchema.emptySchema().withHeader(); - CsvParser p = (CsvParser) mapper.getFactory().createParser(CSV); + CsvParser p = (CsvParser) MAPPER.getFactory().createParser(CSV); p.setSchema(schema); // need to read something to ensure header line is processed assertEquals(JsonToken.START_OBJECT, p.nextToken()); @@ -132,7 +126,13 @@ public void testLongColumnName() throws Exception assertEquals(COLUMN, actual.columnName(0)); p.close(); } - + + // [dataformats-text#31]: Allow disabling header name trimming + public void testHeaderNamePadding() throws Exception + { + // TODO + } + /* /********************************************************************** /* Test methods, fail @@ -141,9 +141,8 @@ public void testLongColumnName() throws Exception public void testInvalidMissingHeader() throws Exception { - CsvMapper mapper = mapperForCsv(); try { - mapper.readerFor(Entry.class).with(CsvSchema.emptySchema().withHeader()).readValue(" \nJoseph,57,false"); + MAPPER.readerFor(Entry.class).with(CsvSchema.emptySchema().withHeader()).readValue(" \nJoseph,57,false"); fail("Should have failed with exception"); } catch (Exception e) { verifyException(e, "Empty header line");