Skip to content

Commit

Permalink
Fix #88 (remove Guava test dep)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 31, 2021
1 parent 14fe722 commit 18a754d
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 78 deletions.
7 changes: 0 additions & 7 deletions csv/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ abstractions.
<artifactId>jackson-annotations</artifactId>
</dependency>

<!-- and for testing -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.1-jre</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -230,6 +231,15 @@ protected String aposToQuotes(String json) {
return json.replace("'", "\"");
}

protected static Map<String, Object> mapOf(Object...strings)
{
final Map<String, Object> map = new LinkedHashMap<>();
for (int i = 0, end = strings.length; i < end; i += 2) {
map.put(strings[i].toString(), strings[i+1]);
}
return map;
}

protected void assertToken(JsonToken expToken, JsonToken actToken) {
if (actToken != expToken) {
fail("Expected token "+expToken+", current token "+actToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import java.util.*;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.dataformat.csv.*;
import com.google.common.collect.Lists;

import static org.junit.Assert.assertArrayEquals;

Expand Down Expand Up @@ -37,13 +38,15 @@ public static class Point {

final CsvMapper MAPPER = mapperForCsv();

public void testSimpleExplicit() throws Exception {
public void testSimpleExplicit() throws Exception
{
ObjectReader r = MAPPER.reader(SIMPLE_SCHEMA);
_testSimpleExplicit(r, false);
_testSimpleExplicit(r, true);
}

private void _testSimpleExplicit(ObjectReader r, boolean useBytes) throws Exception {
private void _testSimpleExplicit(ObjectReader r, boolean useBytes) throws Exception
{
r = r.forType(FiveMinuteUser.class);
FiveMinuteUser user;
final String INPUT = "Bob,Robertson,MALE,AQIDBAU=,false\n";
Expand Down Expand Up @@ -420,7 +423,7 @@ public void testStrictColumnReturnsExpectedData() throws IOException {
String CSV = "x,y,z\n1,2,3\n4,5,6\n7,8,9";

final MappingIterator<Point> iter = MAPPER.readerFor(Point.class).with(schema).readValues(CSV);
final ArrayList<Point> values = Lists.newArrayList(iter);
final List<Point> values = iter.readAll(new ArrayList<Point>());
assertEquals(3, values.size());
assertEquals(1, values.get(0).x);
assertEquals(2, values.get(0).y.intValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import com.fasterxml.jackson.dataformat.csv.*;

import com.google.common.collect.ImmutableMap;

import org.junit.Test;

import java.lang.String;
Expand All @@ -24,7 +22,7 @@ public class TestWriterWithMissingValues extends ModuleTestBase
@Test
public void testWrite_NoNulls() throws JsonProcessingException {
final String csv = WRITER.writeValueAsString(
ImmutableMap.of("timestamp", "2014-03-10T23:32:47+00:00",
mapOf("timestamp", "2014-03-10T23:32:47+00:00",
"value", 42, "id", "hello"));

assertEquals("\"2014-03-10T23:32:47+00:00\",42,hello\n", csv);
Expand All @@ -33,14 +31,14 @@ public void testWrite_NoNulls() throws JsonProcessingException {
@Test
public void testWrite_NullFirstColumn() throws JsonProcessingException {
final String csv = WRITER.writeValueAsString(
ImmutableMap.of("value", 42, "id", "hello"));
mapOf("value", 42, "id", "hello"));
assertEquals(",42,hello\n", csv);
}

@Test
public void testWrite_NullSecondColumn() throws JsonProcessingException {
final String csv = WRITER.writeValueAsString(
ImmutableMap.of("timestamp", "2014-03-10T23:32:47+00:00",
mapOf("timestamp", "2014-03-10T23:32:47+00:00",
"id", "hello"));

assertEquals("\"2014-03-10T23:32:47+00:00\",,hello\n", csv);
Expand All @@ -52,13 +50,13 @@ public void testWrite_NullThirdColumn() throws JsonProcessingException
CsvMapper mapper = new CsvMapper();
assertFalse(mapper.getFactory().isEnabled(CsvGenerator.Feature.OMIT_MISSING_TAIL_COLUMNS));
String csv = mapper.writer(SCHEMA).writeValueAsString(
ImmutableMap.of("timestamp", "2014-03-10T23:32:47+00:00",
mapOf("timestamp", "2014-03-10T23:32:47+00:00",
"value", 42));

assertEquals("\"2014-03-10T23:32:47+00:00\",42,\n", csv);
mapper.getFactory().enable(CsvGenerator.Feature.OMIT_MISSING_TAIL_COLUMNS);
csv = mapper.writer(SCHEMA).writeValueAsString(
ImmutableMap.of("timestamp", "2014-03-10T23:32:47+00:00",
mapOf("timestamp", "2014-03-10T23:32:47+00:00",
"value", 42));
assertEquals("\"2014-03-10T23:32:47+00:00\",42\n", csv);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
package com.fasterxml.jackson.dataformat.csv.ser;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.LinkedHashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectWriter;

import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.fasterxml.jackson.dataformat.csv.ModuleTestBase;

import com.google.common.collect.ImmutableMap;

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

public void testWithAStringAndAUuid() throws Exception {
public void testWithAStringAndAUuid() throws Exception
{
final CsvSchema schema = new CsvSchema.Builder()
.addColumn("string1", CsvSchema.ColumnType.STRING)
.addColumn("string2", CsvSchema.ColumnType.STRING)
.build();
final ObjectWriter writer = MAPPER.writer().with(schema);

final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
builder.put("string1", "hello");
builder.put("string2", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3");
final Map<String, Object> map = mapOf(
"string1", "hello",
"string2", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3"
);

final String csv = writer.writeValueAsString(builder.build());
final String csv = writer.writeValueAsString(map);

assertEquals("hello,\"2a36b911-9699-45d2-abd5-b9f2d2c9c4a3\"\n", csv);
}

public void testWithTwoStringsAndAUuid() throws JsonProcessingException {
public void testWithTwoStringsAndAUuid() throws Exception
{

final CsvSchema schema = new CsvSchema.Builder()
.addColumn("string1", CsvSchema.ColumnType.STRING)
Expand All @@ -38,17 +41,19 @@ public void testWithTwoStringsAndAUuid() throws JsonProcessingException {
.build();
final ObjectWriter writer = MAPPER.writer().with(schema);

final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
builder.put("string1", "hello");
builder.put("string2", "world");
builder.put("string3", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3");
final Map<String, Object> map = mapOf(
"string1", "hello",
"string2", "world",
"string3", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3"
);

final String csv = writer.writeValueAsString(builder.build());
final String csv = writer.writeValueAsString(map);

assertEquals("hello,world,\"2a36b911-9699-45d2-abd5-b9f2d2c9c4a3\"\n", csv);
}

public void testWithANullAStringAndAUuid() throws JsonProcessingException {
public void testWithANullAStringAndAUuid() throws Exception
{

final CsvSchema schema = new CsvSchema.Builder()
.addColumn("string1", CsvSchema.ColumnType.STRING)
Expand All @@ -57,16 +62,18 @@ public void testWithANullAStringAndAUuid() throws JsonProcessingException {
.build();
final ObjectWriter writer = MAPPER.writer().with(schema);

final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
builder.put("string2", "world");
builder.put("string3", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3");
final Map<String, Object> map = mapOf(
"string2", "world",
"string3", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3"
);

final String csv = writer.writeValueAsString(builder.build());
final String csv = writer.writeValueAsString(map);

assertEquals(",world,\"2a36b911-9699-45d2-abd5-b9f2d2c9c4a3\"\n", csv);
}

public void testWithAStringANullAndAUuid() throws JsonProcessingException {
public void testWithAStringANullAndAUuid() throws Exception
{

final CsvSchema schema = new CsvSchema.Builder()
.addColumn("string1", CsvSchema.ColumnType.STRING)
Expand All @@ -75,16 +82,18 @@ public void testWithAStringANullAndAUuid() throws JsonProcessingException {
.build();
final ObjectWriter writer = MAPPER.writer().with(schema);

final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
builder.put("string1", "hello");
builder.put("string3", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3");
final Map<String, Object> map = mapOf(
"string1", "hello",
"string3", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3"
);

final String csv = writer.writeValueAsString(builder.build());
final String csv = writer.writeValueAsString(map);

assertEquals("hello,,\"2a36b911-9699-45d2-abd5-b9f2d2c9c4a3\"\n", csv);
}

public void testWithThreeStringsAndAUuid() throws JsonProcessingException {
public void testWithThreeStringsAndAUuid() throws Exception
{

final CsvSchema schema = new CsvSchema.Builder()
.addColumn("string1", CsvSchema.ColumnType.STRING)
Expand All @@ -94,19 +103,20 @@ public void testWithThreeStringsAndAUuid() throws JsonProcessingException {
.build();
final ObjectWriter writer = MAPPER.writer().with(schema);

final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
builder.put("string1", "hello");
builder.put("string2", "dear");
builder.put("string3", "world");
builder.put("string4", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3");
final Map<String, Object> map = mapOf(
"string1", "hello",
"string2", "dear",
"string3", "world",
"string4", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3"
);

final String csv = writer.writeValueAsString(builder.build());
final String csv = writer.writeValueAsString(map);

assertEquals("hello,dear,world,\"2a36b911-9699-45d2-abd5-b9f2d2c9c4a3\"\n", csv);
}

public void testWithANullAStringAStringAndAUuid() throws JsonProcessingException {

public void testWithANullAStringAStringAndAUuid() throws Exception
{
final CsvSchema schema = new CsvSchema.Builder()
.addColumn("string1", CsvSchema.ColumnType.STRING)
.addColumn("string2", CsvSchema.ColumnType.STRING)
Expand All @@ -115,17 +125,18 @@ public void testWithANullAStringAStringAndAUuid() throws JsonProcessingException
.build();
final ObjectWriter writer = MAPPER.writer().with(schema);

final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
builder.put("string2", "hello");
builder.put("string3", "world");
builder.put("string4", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3");

final String csv = writer.writeValueAsString(builder.build());
final Map<String, Object> map = mapOf(
"string2", "hello",
"string3", "world",
"string4", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3"
);
final String csv = writer.writeValueAsString(map);

assertEquals(",hello,world,\"2a36b911-9699-45d2-abd5-b9f2d2c9c4a3\"\n", csv);
}

public void testWithAStringANullAStringAndAUuid() throws JsonProcessingException {
public void testWithAStringANullAStringAndAUuid() throws Exception
{

final CsvSchema schema = new CsvSchema.Builder()
.addColumn("string1", CsvSchema.ColumnType.STRING)
Expand All @@ -135,18 +146,19 @@ public void testWithAStringANullAStringAndAUuid() throws JsonProcessingException
.build();
final ObjectWriter writer = MAPPER.writer().with(schema);

final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
builder.put("string1", "hello");
builder.put("string3", "world");
builder.put("string4", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3");
final Map<String, Object> map = mapOf(
"string1", "hello",
"string3", "world",
"string4", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3"
);

final String csv = writer.writeValueAsString(builder.build());
final String csv = writer.writeValueAsString(map);

assertEquals("hello,,world,\"2a36b911-9699-45d2-abd5-b9f2d2c9c4a3\"\n", csv);
}

public void testWithTwoStringsANullAndAUuid() throws JsonProcessingException {

public void testWithTwoStringsANullAndAUuid() throws Exception
{
final CsvSchema schema = new CsvSchema.Builder()
.addColumn("string1", CsvSchema.ColumnType.STRING)
.addColumn("string2", CsvSchema.ColumnType.STRING)
Expand All @@ -155,17 +167,19 @@ public void testWithTwoStringsANullAndAUuid() throws JsonProcessingException {
.build();
final ObjectWriter writer = MAPPER.writer().with(schema);

final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
builder.put("string1", "hello");
builder.put("string2", "world");
builder.put("string4", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3");
final Map<String, Object> map = mapOf(
"string1", "hello",
"string2", "world",
"string4", "2a36b911-9699-45d2-abd5-b9f2d2c9c4a3"
);

final String csv = writer.writeValueAsString(builder.build());
final String csv = writer.writeValueAsString(map);

assertEquals("hello,world,,\"2a36b911-9699-45d2-abd5-b9f2d2c9c4a3\"\n", csv);
}

public void testWithTwoStringsANullAndAString() throws JsonProcessingException {
public void testWithTwoStringsANullAndAString() throws Exception
{

final CsvSchema schema = new CsvSchema.Builder()
.addColumn("string1", CsvSchema.ColumnType.STRING)
Expand All @@ -175,18 +189,19 @@ public void testWithTwoStringsANullAndAString() throws JsonProcessingException {
.build();
final ObjectWriter writer = MAPPER.writer().with(schema);

final ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
builder.put("string1", "hello");
builder.put("string2", "world");
builder.put("string4", "again");
final Map<String, String> map = new LinkedHashMap<>();
map.put("string1", "hello");
map.put("string2", "world");
map.put("string4", "again");

final String csv = writer.writeValueAsString(builder.build());
final String csv = writer.writeValueAsString(map);

assertEquals("hello,world,,again\n", csv);
}

// [Issue#45]
public void testWriteNullThirdColumn() throws JsonProcessingException {
public void testWriteNullThirdColumn() throws Exception
{
final CsvSchema.Builder csvSchemaBuilder = new CsvSchema.Builder();
csvSchemaBuilder.addColumn("timestamp", CsvSchema.ColumnType.STRING);
csvSchemaBuilder.addColumn("value", CsvSchema.ColumnType.NUMBER);
Expand All @@ -195,7 +210,7 @@ public void testWriteNullThirdColumn() throws JsonProcessingException {
final ObjectWriter writer = MAPPER.writer().with(schema);

final String string = writer.writeValueAsString(
ImmutableMap.of("timestamp", 0L, "value", 42));
mapOf("timestamp", 0L, "value", 42));
assertEquals("0,42,\n", string);
}
}

0 comments on commit 18a754d

Please sign in to comment.