Skip to content

Commit 340bd18

Browse files
committed
Add failing test for #285
1 parent e00bab9 commit 340bd18

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

csv/src/test/java/com/fasterxml/jackson/dataformat/csv/deser/MissingColumnsTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ static class ABC {
2020
public String b = "b";
2121
public String c = "c";
2222
}
23-
23+
2424
/*
2525
/**********************************************************************
2626
/* Test methods
2727
/**********************************************************************
2828
*/
2929

30-
final CsvMapper MAPPER = mapperForCsv();
30+
private final CsvMapper MAPPER = mapperForCsv();
3131

32-
final CsvSchema SCHEMA = MAPPER.schemaFor(ABC.class);
32+
private final CsvSchema SCHEMA_ABC = MAPPER.schemaFor(ABC.class);
3333

3434
// by default, just... ignore
3535
public void testDefaultMissingHandling() throws Exception
3636
{
37-
ObjectReader r = MAPPER.readerFor(ABC.class).with(SCHEMA);
37+
ObjectReader r = MAPPER.readerFor(ABC.class).with(SCHEMA_ABC);
3838
final ABC DEFAULT = new ABC();
3939

4040
ABC result = r.readValue("first,second,third\n");
@@ -65,7 +65,7 @@ public void testDefaultMissingHandling() throws Exception
6565
public void testInjectMissingAsNulls() throws Exception
6666
{
6767
ObjectReader r = MAPPER.readerFor(ABC.class)
68-
.with(SCHEMA)
68+
.with(SCHEMA_ABC)
6969
.with(CsvParser.Feature.INSERT_NULLS_FOR_MISSING_COLUMNS);
7070

7171
// check with various number of missing; but first with no missing
@@ -95,7 +95,7 @@ public void testInjectMissingAsNulls() throws Exception
9595
public void testFailOnMissingColumns() throws Exception
9696
{
9797
ObjectReader r = MAPPER.readerFor(ABC.class)
98-
.with(SCHEMA)
98+
.with(SCHEMA_ABC)
9999
.with(CsvParser.Feature.FAIL_ON_MISSING_COLUMNS);
100100

101101
// check with various number of missing, as well as recovery
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fasterxml.jackson.dataformat.csv.failing;
2+
3+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
4+
5+
import com.fasterxml.jackson.databind.MappingIterator;
6+
7+
import com.fasterxml.jackson.dataformat.csv.*;
8+
9+
/**
10+
* Tests for cases where one more of schema-declared columns is
11+
* missing; various handling choices include "null-injection"
12+
* as well as failure (throw exception) and just skipping (default).
13+
*/
14+
public class MissingColumns285Test extends ModuleTestBase
15+
{
16+
@JsonPropertyOrder({ "name", "age" })
17+
static class Person {
18+
public String name;
19+
public int age;
20+
}
21+
22+
/*
23+
/**********************************************************************
24+
/* Test methods
25+
/**********************************************************************
26+
*/
27+
28+
private final CsvMapper MAPPER = mapperForCsv();
29+
30+
// [dataformats-text#285]
31+
public void testMissingWithReorder() throws Exception
32+
{
33+
CsvSchema csvSchema = CsvSchema.builder().setUseHeader(true).setReorderColumns(true)
34+
.addColumn("name").addColumn("age").build();
35+
final String CSV = "name\nRoger\n";
36+
MappingIterator<Person> it = MAPPER
37+
.readerFor(Person.class)
38+
.with(csvSchema)
39+
.readValues(CSV);
40+
try {
41+
it.nextValue();
42+
fail("Should not pass with missing columns");
43+
} catch (CsvReadException e) {
44+
verifyException(e, "Not enough column values");
45+
verifyException(e, "expected 2, found 1");
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)