Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Commit

Permalink
Add a (failing for now) unit test for #72, hoping to implement for 2.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 18, 2015
1 parent 18dd546 commit cd86b3f
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ public int hashCode() {
return firstName.hashCode();
}
}

@JsonPropertyOrder({"id", "desc"})
protected static class IdDesc {
public String id, desc;

protected IdDesc() { }
public IdDesc(String id, String desc) {
this.id = id;
this.desc = desc;
}
}

protected ModuleTestBase() { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import static org.junit.Assert.assertArrayEquals;

public class TestParser extends ModuleTestBase
public class BasicParserTest extends ModuleTestBase
{
@JsonPropertyOrder({ "x", "y", "z" })
public static class Point {
Expand All @@ -32,10 +32,11 @@ public static class Point {
/**********************************************************
*/

final CsvMapper MAPPER = mapperForCsv();

public void testSimpleExplicit() throws Exception
{
ObjectMapper mapper = mapperForCsv();
ObjectReader r = mapper.reader(SIMPLE_SCHEMA);
ObjectReader r = MAPPER.reader(SIMPLE_SCHEMA);
_testSimpleExplicit(r, false);
_testSimpleExplicit(r, true);
}
Expand All @@ -59,8 +60,7 @@ private void _testSimpleExplicit(ObjectReader r, boolean useBytes) throws Except

public void testSimpleExplicitWithBOM() throws Exception
{
ObjectMapper mapper = mapperForCsv();
ObjectReader r = mapper.reader(SIMPLE_SCHEMA);
ObjectReader r = MAPPER.reader(SIMPLE_SCHEMA);
r = r.forType(FiveMinuteUser.class);
FiveMinuteUser user;

Expand All @@ -85,10 +85,9 @@ public void testSimpleExplicitWithBOM() throws Exception

public void testSimpleWithAutoSchema() throws Exception
{
CsvMapper mapper = mapperForCsv();
CsvSchema schema = mapper.schemaFor(FiveMinuteUser.class);
CsvSchema schema = MAPPER.schemaFor(FiveMinuteUser.class);
// NOTE: order different from above test (as per POJO def!)
FiveMinuteUser user = mapper.reader(schema).forType(FiveMinuteUser.class).readValue("Joe,Josephson,MALE,true,AwE=\n");
FiveMinuteUser user = MAPPER.reader(schema).forType(FiveMinuteUser.class).readValue("Joe,Josephson,MALE,true,AwE=\n");
assertEquals("Joe", user.firstName);
assertEquals("Josephson", user.lastName);
assertEquals(Gender.MALE, user.getGender());
Expand All @@ -102,9 +101,8 @@ public void testSimpleWithAutoSchema() throws Exception
*/
public void testSimpleAsMaps() throws Exception
{
CsvMapper mapper = mapperForCsv();
CsvSchema schema = mapper.schemaFor(FiveMinuteUser.class);
MappingIterator<Map<?,?>> it = mapper.reader(schema).forType(Map.class).readValues(
CsvSchema schema = MAPPER.schemaFor(FiveMinuteUser.class);
MappingIterator<Map<?,?>> it = MAPPER.reader(schema).forType(Map.class).readValues(
"Joe,Smith,MALE,false,"
);
assertTrue(it.hasNext());
Expand All @@ -128,15 +126,14 @@ public void testMapsWithLinefeeds() throws Exception {

private void _testMapsWithLinefeeds(boolean useBytes) throws Exception
{
CsvMapper mapper = mapperForCsv();
String CSV = "A,B,C\n"
+"data11,data12\n"
+"data21,data22,data23\r\n"
+"data31,\"data32 data32\ndata32 data32\",data33\n"
+"data41,\"data42 data42\r\ndata42\",data43\n";

CsvSchema cs = CsvSchema.emptySchema().withHeader();
ObjectReader or = mapper.readerFor(HashMap.class).with(cs);
ObjectReader or = MAPPER.readerFor(HashMap.class).with(cs);

MappingIterator<Map<String,String>> mi;

Expand Down Expand Up @@ -182,23 +179,21 @@ private void _testMapsWithLinefeeds(boolean useBytes) throws Exception
// [Issue#12]
public void testEmptyHandlingForInteger() throws Exception
{
CsvMapper mapper = mapperForCsv();
CsvSchema schema = mapper.typedSchemaFor(Point.class).withoutHeader();
CsvSchema schema = MAPPER.typedSchemaFor(Point.class).withoutHeader();

// First: empty value, to be considered as null
Point result = mapper.readerFor(Point.class).with(schema).readValue(",,\n");
Point result = MAPPER.readerFor(Point.class).with(schema).readValue(",,\n");
assertEquals(0, result.x);
assertNull(result.y);
assertNull(result.z);
}

public void testStringNullHandlingForInteger() throws Exception
{
CsvMapper mapper = mapperForCsv();
CsvSchema schema = mapper.typedSchemaFor(Point.class).withoutHeader();
CsvSchema schema = MAPPER.typedSchemaFor(Point.class).withoutHeader();

// First: empty value, to be considered as null
Point result = mapper.readerFor(Point.class).with(schema).readValue("null,null,null\n");
Point result = MAPPER.readerFor(Point.class).with(schema).readValue("null,null,null\n");
assertEquals(0, result.x);
assertNull(result.y);
assertNull(result.z);
Expand All @@ -211,7 +206,7 @@ public void testIncorrectDups41() throws Exception
CsvSchema schema = CsvSchema.builder().addColumn("Col1").addColumn("Col2")
.addColumn("Col3").build();

MappingIterator<Object> iter = new CsvMapper().readerFor(Object.class)
MappingIterator<Object> iter = MAPPER.readerFor(Object.class)
.with(schema).readValues(INPUT);

Map<?,?> m = (Map<?,?>) iter.next();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.fasterxml.jackson.dataformat.csv.failing;

import java.io.ByteArrayOutputStream;
import java.util.*;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.dataformat.csv.*;

import static org.junit.Assert.assertArrayEquals;

public class NullRead72Test extends ModuleTestBase
{
final CsvMapper MAPPER = mapperForCsv();

// For [dataformat-csv#72]: recognize "null value" for reading too
public void testReadNullValue() throws Exception
{
CsvSchema schema = CsvSchema.builder()
.setNullValue("n/a")
.addColumn("id")
.addColumn("desc")
.build();

// start by writing, first
String csv = MAPPER.writer(schema).writeValueAsString(new IdDesc("id", null));
// MUST use doubling for quotes!
assertEquals("id,n/a\n", csv);

// but read back

ObjectReader r = MAPPER.readerFor(IdDesc.class)
.with(schema);

IdDesc result = r.readValue(csv);
assertNotNull(result);
assertEquals("id", result.id);
assertNull(result.desc);

// also try the other combination
result = r.readValue("n/a,Whatevs\n");
assertNotNull(result);
assertNull(result.id);
assertEquals("Whatevs", result.desc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public void testMultipleListWrites() throws Exception

csvWriter.writeValue(gen, line2);

gen.close();

String csv = sw.toString().trim();
// may get different linefeed on different OSes?
csv = csv.replaceAll("[\\r\\n]", "/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.dataformat.csv.*;

// for [dataformat-csv#69]
// for [dataformat-csv#69], other null value serialization
public class NullWritingTest extends ModuleTestBase
{
private final CsvMapper csv = new CsvMapper();
Expand Down Expand Up @@ -41,6 +41,7 @@ public void testObjectWithNullMembersToStream() throws Exception {
writeValues.flush();
String nullMembers = stream.toString("UTF-8");
assertEquals("a,b,c,d\n,,,\n,,,\n", nullMembers);
writeValues.close();
}

public void testNullToStream() throws Exception {
Expand All @@ -60,5 +61,21 @@ public void testNullToStream() throws Exception {

assertEquals("a,b,c,d\n", nullObject);
// assertEquals("a,b,c,d\n\n\n", nullObject);
}
writeValues.close();
}

// [dataformat-csv#53]
public void testCustomNullValue() throws Exception
{
ObjectMapper mapper = mapperForCsv();
CsvSchema schema = CsvSchema.builder()
.setNullValue("n/a")
.addColumn("id")
.addColumn("desc")
.build();

String result = mapper.writer(schema).writeValueAsString(new IdDesc("id", null));
// MUST use doubling for quotes!
assertEquals("id,n/a\n", result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ public Entry2(String id, float amount) {
}
}

@JsonPropertyOrder({"id", "desc"})
static class IdDesc {
public String id, desc;

public IdDesc(String id, String desc) {
this.id = id;
this.desc = desc;
}
}

/*
/**********************************************************************
/* Test methods
Expand Down Expand Up @@ -196,21 +186,6 @@ public void testWriteInFile() throws Exception
}
}

// [dataformat-csv#53]
public void testCustomNullValue() throws Exception
{
ObjectMapper mapper = mapperForCsv();
CsvSchema schema = CsvSchema.builder()
.setNullValue("n/a")
.addColumn("id")
.addColumn("desc")
.build();

String result = mapper.writer(schema).writeValueAsString(new IdDesc("id", null));
// MUST use doubling for quotes!
assertEquals("id,n/a\n", result);
}

public void testForcedQuoting60() throws Exception
{
CsvMapper mapper = mapperForCsv();
Expand Down

0 comments on commit cd86b3f

Please sign in to comment.