Skip to content

Commit

Permalink
Add a failing test for #39 (not skipped by maven for some reason, nee…
Browse files Browse the repository at this point in the history
…d to troubleshoot, but not now)
  • Loading branch information
cowtowncoder committed Jan 24, 2017
1 parent dda7a6d commit 08468c2
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.fasterxml.jackson.dataformat.avro;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

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

public class Issue39Test extends AvroTestBase
{
final static String SCHEMA_MAP_OF_MAPS_JSON = aposToQuotes("{\n"
+" 'namespace': 'com.salesforce.conduit.avro',\n"
+" 'type': 'record',\n"
+" 'name': 'MapContainer',\n"
+" 'fields': [\n"
+" {'name':'props', \n"
+" 'type' : {\n"
+" 'type' : 'map', \n"
+" 'values': ['null','int','long','float','double','string','boolean',{'type':'map','values':['null','int','long','float','double','string','boolean']}]\n"
+" }\n"
+" }\n"
+" ]\n"
+"}");
static class MapContainer {
public Map<String, Object> props;

public MapContainer() {}
public MapContainer(Map<String, Object> p) {
props = p;
}
}

/*
/**********************************************************
/* Test methods
/**********************************************************
*/

private final AvroMapper MAPPER = getMapper();

public void testMapOfMaps() throws IOException
{
Map<String,Object> map = new LinkedHashMap<String,Object>();
map.put("hello", "world");
map.put("goodbye", "charlie");
Map<String,String> otherMap = new LinkedHashMap<String,String>();
otherMap.put("foo", "bar");
map.put("otherMap", otherMap);
MapContainer event = new MapContainer(map);

AvroSchema avroSchema = MAPPER.schemaFrom(SCHEMA_MAP_OF_MAPS_JSON);
byte[] serialized = MAPPER.writer(avroSchema).writeValueAsBytes(event);

/*
MapContainer deserialized = MAPPER.readerFor(MapContainer.class)
.with(avroSchema)
.readValue(serialized);
assertEquals(3, deserialized.props.size());
*/

JsonParser p = MAPPER.getFactory().createParser(serialized);
p.setSchema(avroSchema);
JsonToken t;
while ((t = p.nextToken()) != null) {
System.err.println("GOT: "+t);
}
System.err.println("DONE!");
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.fasterxml.jackson.dataformat.avro;

import java.io.IOException;
import java.util.Map;

import org.apache.avro.Schema;
import java.util.*;

import com.fasterxml.jackson.annotation.JsonProperty;

Expand All @@ -15,33 +13,39 @@

public class NestedMapTest extends AvroTestBase
{
public static class Nester {
@JsonProperty
public Map<String,Map<String,Integer>> nested;
}

public void testSerialization() throws IOException
{
Nester fromJson = new ObjectMapper().readValue(
"{\"nested\": {\"map\":{\"value\":1}}}"
, Nester.class);

AvroMapper mapper = new AvroMapper();
//Generate schema from class
AvroSchemaGenerator gen = new AvroSchemaGenerator();
mapper.acceptJsonFormatVisitor(Nester.class, gen);
Schema schema = gen.getGeneratedSchema().getAvroSchema();

//Serialize
byte[] avroData = mapper.writer(new AvroSchema(schema))
.writeValueAsBytes(fromJson);

//Deserialize
Nester nester = mapper.readerFor(Nester.class)
.with(new AvroSchema(schema))
.readValue(avroData);
int val = nester.nested.get("map").get("value");
assertEquals(1, val);

}
public static class Nester {
@JsonProperty
public Map<String,Map<String,Integer>> nested;
}

/*
/**********************************************************
/* Test methods
/**********************************************************
*/

private final AvroMapper MAPPER = getMapper();

public void testSerialization() throws IOException
{
Nester fromJson = new ObjectMapper().readValue(
"{\"nested\": {\"map\":{\"value\":1}}}"
, Nester.class);

//Generate schema from class
AvroSchemaGenerator gen = new AvroSchemaGenerator();
MAPPER.acceptJsonFormatVisitor(Nester.class, gen);
AvroSchema schema = gen.getGeneratedSchema();

//Serialize
byte[] avroData = MAPPER.writer(schema)
.writeValueAsBytes(fromJson);

//Deserialize
Nester nester = MAPPER.readerFor(Nester.class)
.with(schema)
.readValue(avroData);
int val = nester.nested.get("map").get("value");
assertEquals(1, val);
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.jackson.core>2.8.6</version.jackson.core>
<version.jackson.core>2.8.7-SNAPSHOT</version.jackson.core>
<version.asm>5.1</version.asm>
</properties>

Expand Down

0 comments on commit 08468c2

Please sign in to comment.