Skip to content

Commit

Permalink
Fixed #58
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 10, 2017
1 parent c6c1f97 commit e0bf9eb
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,13 @@ public static boolean isStringable(AnnotatedClass type) {
protected static String getNamespace(JavaType type) {
Class<?> cls = type.getRawClass();
// 16-Feb-2017, tatu: Fixed as suggested by `baharclerode@github`
// 09-Mar-2016, tatu: Alas, need to undo for 2.8.8; will be in 2.9
/*
Class<?> enclosing = cls.getEnclosingClass();
if (enclosing != null) {
return enclosing.getName() + "$";
}
*/
Package pkg = cls.getPackage();
return (pkg == null) ? "" : pkg.getName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.reflect.ReflectData;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.dataformat.avro.AvroMapper;
Expand All @@ -38,7 +37,7 @@ public byte[] apply(Schema schema, Object originalObject) {
/**
* Functor of {@link #getJacksonSchema(Type)}
*/
public static final Function<Type, Schema> getJacksonSchema = new Function<Type, Schema>() {
public static final Function<Type, Schema> getJacksonSchema = new Function<Type, Schema>() {
@Override
public Schema apply(Type input) {
return getJacksonSchema(input);
Expand All @@ -48,7 +47,7 @@ public Schema apply(Type input) {
* Functor of {@link #jacksonDeserialize(Schema, Type, byte[])} which uses {@link Object} as the target type,
* requiring the use of native type IDs
*/
public static final BiFunction<Schema, byte[], Object> jacksonDeserializer = new BiFunction<Schema, byte[], Object>() {
public static final BiFunction<Schema, byte[], Object> jacksonDeserializer = new BiFunction<Schema, byte[], Object>() {
@Override
public Object apply(Schema schema, byte[] originalObject) {
return jacksonDeserialize(schema, Object.class, originalObject);
Expand All @@ -57,7 +56,7 @@ public Object apply(Schema schema, byte[] originalObject) {
/**
* Functor of {@link #getApacheSchema(Type)}
*/
public static final Function<Type, Schema> getApacheSchema = new Function<Type, Schema>() {
public static final Function<Type, Schema> getApacheSchema = new Function<Type, Schema>() {
@Override
public Schema apply(Type input) {
return getApacheSchema(input);
Expand All @@ -66,7 +65,7 @@ public Schema apply(Type input) {
/**
* Functor of {@link #apacheDeserialize(Schema, byte[])}
*/
public static final BiFunction<Schema, byte[], Object> apacheDeserializer = new BiFunction<Schema, byte[], Object>() {
public static final BiFunction<Schema, byte[], Object> apacheDeserializer = new BiFunction<Schema, byte[], Object>() {
@Override
public Object apply(Schema first, byte[] second) {
return apacheDeserialize(first, second);
Expand All @@ -75,7 +74,7 @@ public Object apply(Schema first, byte[] second) {
/**
* Functor of {@link #apacheSerialize(Schema, Object)}
*/
public static final BiFunction<Schema, Object, byte[]> apacheSerializer = new BiFunction<Schema, Object, byte[]>() {
public static final BiFunction<Schema, Object, byte[]> apacheSerializer = new BiFunction<Schema, Object, byte[]>() {
@Override
public byte[] apply(Schema schema, Object originalObject) {
return apacheSerialize(schema, originalObject);
Expand All @@ -93,8 +92,7 @@ public byte[] apply(Schema schema, Object originalObject) {
@SuppressWarnings({"unchecked", "SuspiciousMethodCalls", "rawtypes"})
@Override
protected Schema createSchema(Type type, Map<String, Schema> names) {
/*
* Note, we abuse the fact that we can stick whatever we want into "names" and it won't interfere as long as we don't use String
/* Note, we abuse the fact that we can stick whatever we want into "names" and it won't interfere as long as we don't use String
* keys. To persist and look up type variable information, we watch for ParameterizedTypes, TypeVariables, and Classes with
* generic superclasses to extract type variable information and store it in the map. This allows full type variable resolution
* when building a schema from reflection data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@

import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.avro.Schema;
import org.junit.Assume;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

Expand All @@ -20,7 +18,22 @@
* interoperability between the implementations.
*/
@RunWith(Parameterized.class)
public abstract class InteropTestBase {
public abstract class InteropTestBase
{
// To work around 2.8/2.9 difference wrt namespaces for enums
// See [dataformats-binary#58] for details
protected void assumeCompatibleNsForDeser() {
Assume.assumeTrue(deserializeFunctor != ApacheAvroInteropUtil.apacheDeserializer
|| schemaFunctor != ApacheAvroInteropUtil.getJacksonSchema);
}

// To work around 2.8/2.9 difference wrt namespaces for enums
// See [dataformats-binary#58] for details
protected void assumeCompatibleNsForSer() {
Assume.assumeTrue(serializeFunctor != ApacheAvroInteropUtil.apacheSerializer
|| schemaFunctor != ApacheAvroInteropUtil.getJacksonSchema);
}

/**
* Helper method for building a {@link ParameterizedType} for use with {@link #roundTrip(Type, Object)}
*
Expand Down Expand Up @@ -155,13 +168,32 @@ public enum DummyEnum {
NORTH, SOUTH, EAST, WEST
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public static class DummyRecord {
@JsonProperty(required = true)
private String firstValue;
@JsonProperty(required = true)
private int secondValue;

public DummyRecord(String f, int s) {
firstValue = f;
secondValue = s;
}
protected DummyRecord() { }

public String getFirstValue() { return firstValue; }
public int getSecondValue() { return secondValue; }

@Override
public boolean equals(Object o) {
if (o == null || !(o instanceof DummyRecord)) return false;
DummyRecord other = (DummyRecord) o;
if (other.secondValue == secondValue) {
if (firstValue == null) {
return other.firstValue == null;
}
return firstValue.equals(other.firstValue);
}
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.fasterxml.jackson.dataformat.avro.interop.annotations;

import com.fasterxml.jackson.dataformat.avro.interop.InteropTestBase;
import org.apache.avro.reflect.AvroIgnore;
import org.junit.Before;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.*;

import static org.junit.Assert.assertThat;

import com.fasterxml.jackson.dataformat.avro.interop.InteropTestBase;

public class AvroIgnoreTest extends InteropTestBase
{
static class RecordWithIgnoredField {
Expand All @@ -18,6 +21,12 @@ public RecordWithIgnoredField() {}

}

@Before
public void setup() {
// 2.8 doesn't generate schemas with compatible namespaces for Apache deserializer
assumeCompatibleNsForDeser();
}

@Test
public void testFieldIgnored() {
RecordWithIgnoredField r = new RecordWithIgnoredField();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.fasterxml.jackson.dataformat.avro.interop.InteropTestBase;

import org.apache.avro.reflect.AvroName;

import org.junit.Before;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -32,6 +32,12 @@ public static class RecordWithNameCollision {
public String otherField;
}

@Before
public void setup() {
// 2.8 doesn't generate schemas with compatible namespaces for Apache deserializer
assumeCompatibleNsForDeser();
}

@Test
public void testRecordWithRenamedField() {
RecordWithRenamed original = new RecordWithRenamed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

import com.fasterxml.jackson.dataformat.avro.interop.InteropTestBase;
Expand All @@ -15,7 +16,14 @@
/**
* Tests lists involving complex element types (Lists, Records, Maps, Enums)
*/
public class ListWithComplexTest extends InteropTestBase {
public class ListWithComplexTest extends InteropTestBase
{
@Before
public void setup() {
// 2.8 doesn't generate schemas with compatible namespaces for Apache deserializer
assumeCompatibleNsForDeser();
}

@Test
public void testEmptyListWithRecordElements() {
List<DummyRecord> original = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.*;

import org.junit.Before;
import org.junit.Test;

import com.fasterxml.jackson.dataformat.avro.interop.InteropTestBase;
Expand All @@ -12,7 +13,13 @@
/**
* Tests Maps involving complex value types (Lists, Records, Maps, Enums)
*/
public class MapWithComplexTest extends InteropTestBase {
public class MapWithComplexTest extends InteropTestBase
{
@Before
public void setup() {
// 2.8 doesn't generate schemas with compatible namespaces for Apache deserializer
assumeCompatibleNsForDeser();
}

@Test
public void testMapWithRecordValues() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.apache.avro.reflect.Nullable;
import org.junit.Before;
import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -21,7 +22,8 @@
/**
* Tests records involving complex value types (Lists, Records, Maps, Enums)
*/
public class RecordWithComplexTest extends InteropTestBase {
public class RecordWithComplexTest extends InteropTestBase
{
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
Expand All @@ -43,6 +45,13 @@ public RecursiveDummyRecord(String firstValue, Integer secondValue, DummyRecord
}
}

@Before
public void setup() {
// 2.8 doesn't generate schemas with compatible namespaces for Apache deserializer
assumeCompatibleNsForDeser();
assumeCompatibleNsForSer();
}

@Test
public void testEmptyRecordWithRecordValues() {
Map<String, DummyRecord> original = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.fasterxml.jackson.dataformat.avro.interop.records;

import lombok.Data;

import org.junit.Before;
import org.junit.Test;

import com.fasterxml.jackson.dataformat.avro.interop.InteropTestBase;
Expand All @@ -10,7 +12,14 @@
/**
* Tests serializing primitive array fields on records
*/
public class RecordWithPrimitiveArrayTest extends InteropTestBase {
public class RecordWithPrimitiveArrayTest extends InteropTestBase
{
@Before
public void setup() {
// 2.8 doesn't generate schemas with compatible namespaces for Apache deserializer
assumeCompatibleNsForDeser();
}

@Data
public static class TestRecord {
private byte[] byteArrayField = new byte[0];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.fasterxml.jackson.dataformat.avro.interop.records;

import lombok.Data;

import org.junit.Before;
import org.junit.Test;

import com.fasterxml.jackson.dataformat.avro.interop.InteropTestBase;
Expand All @@ -10,7 +12,8 @@
/**
* Tests serializing primitive fields on records
*/
public class RecordWithPrimitiveTest extends InteropTestBase {
public class RecordWithPrimitiveTest extends InteropTestBase
{
@Data
public static class TestRecord {
private byte byteField;
Expand All @@ -22,6 +25,12 @@ public static class TestRecord {
private double doubleField;
}

@Before
public void setup() {
// 2.8 doesn't generate schemas with compatible namespaces for Apache deserializer
assumeCompatibleNsForDeser();
}

@Test
public void testByteField() {
TestRecord record = new TestRecord();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.fasterxml.jackson.dataformat.avro.interop.records;

import lombok.Data;

import org.junit.Before;
import org.junit.Test;

import com.fasterxml.jackson.dataformat.avro.interop.InteropTestBase;
Expand All @@ -10,7 +12,8 @@
/**
* Tests serializing primitive array fields on records
*/
public class RecordWithPrimitiveWrapperArrayTest extends InteropTestBase {
public class RecordWithPrimitiveWrapperArrayTest extends InteropTestBase
{
@Data
public static class TestRecord {
private Byte[] byteArrayField = new Byte[0];
Expand All @@ -23,6 +26,12 @@ public static class TestRecord {
private String[] stringArrayField = new String[0];
}

@Before
public void setup() {
// 2.8 doesn't generate schemas with compatible namespaces for Apache deserializer
assumeCompatibleNsForDeser();
}

@Test
public void testByteField() {
TestRecord record = new TestRecord();
Expand Down
Loading

0 comments on commit e0bf9eb

Please sign in to comment.