Skip to content

Commit

Permalink
Add more test cases for #1604
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 18, 2017
1 parent 2c4b2dd commit cab130e
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -293,15 +293,9 @@ public void testCollectionTypesRefined()
assertEquals(Long.class, subtype.getContentType().getRawClass());

// but with refinement, should have non-null super class
// 20-Oct-2015, tatu: For now refinement does not faithfully replicate the
// structure, it only retains most important information. Here it means
// that actually existing super-classes are skipped, and only original
// type is linked as expected
/*
JavaType superType = subtype.getSuperClass();
assertNotNull(superType);
assertEquals(AbstractList.class, superType.getRawClass());
*/
}

/*
Expand Down Expand Up @@ -372,15 +366,13 @@ public void testMapTypesRefined()
// that actually existing super-classes are skipped, and only original
// type is linked as expected

/*
JavaType superType = subtype.getSuperClass();
assertNotNull(superType);
assertEquals(HashMap.class, superType.getRawClass());
// which also should have proper typing
assertEquals(String.class, superType.getKeyType().getRawClass());
assertEquals(List.class, superType.getContentType().getRawClass());
assertEquals(Integer.class, superType.getContentType().getContentType().getRawClass());
*/
}

public void testMapTypesRaw()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public T getData() {
public static <T> Data<List<T>> of(List<T> data) {
return new DataList<>(data);
}

public static <T> Data<List<T>> ofRefined(List<T> data) {
return new RefinedDataList<>(data);
}

public static <T> Data<List<T>> ofSneaky(List<T> data) {
return new SneakyDataList<String,T>(data);
}
}

public static class DataList<T> extends Data<List<T>> {
Expand All @@ -30,6 +38,18 @@ public DataList(List<T> data) {
}
}

public static class RefinedDataList<T> extends Data<List<T>> {
public RefinedDataList(List<T> data) {
super(data);
}
}

public static class SneakyDataList<BOGUS,T> extends Data<List<T>> {
public SneakyDataList(List<T> data) {
super(data);
}
}

public static class Inner {
private int index;

Expand Down Expand Up @@ -66,9 +86,10 @@ public DataList<Inner> getInner() {
}
}

public void testIssue1604() throws Exception
private final ObjectMapper objectMapper = new ObjectMapper();

public void testIssue1604Simple() throws Exception
{
final ObjectMapper objectMapper = new ObjectMapper();
List<Inner> inners = new ArrayList<>();
for (int i = 0; i < 2; i++) {
inners.add(new Inner(i));
Expand All @@ -81,4 +102,26 @@ public void testIssue1604() throws Exception
String json = objectMapper.writeValueAsString(badOuter);
assertNotNull(json);
}

public void testIssue1604Subtype() throws Exception
{
List<Inner> inners = new ArrayList<>();
for (int i = 0; i < 2; i++) {
inners.add(new Inner(i));
}
BadOuter badOuter = new BadOuter(Data.ofRefined(inners));
String json = objectMapper.writeValueAsString(badOuter);
assertNotNull(json);
}

public void testIssue1604Sneaky() throws Exception
{
List<Inner> inners = new ArrayList<>();
for (int i = 0; i < 2; i++) {
inners.add(new Inner(i));
}
BadOuter badOuter = new BadOuter(Data.ofSneaky(inners));
String json = objectMapper.writeValueAsString(badOuter);
assertNotNull(json);
}
}
102 changes: 102 additions & 0 deletions src/test/java/com/fasterxml/jackson/failing/TestTypeFactory1604.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.fasterxml.jackson.failing;

import java.util.*;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;

// for [databind#1604]
public class TestTypeFactory1604 extends BaseMapTest
{
static class Data1604<T> { }

static class DataList1604<T> extends Data1604<List<T>> {
}

static class RefinedDataList1604<T> extends DataList1604<List<T>> {
}

public static class SneakyDataList1604<BOGUS,T> extends Data1604<List<T>> {

}

static class TwoParam1604<KEY,VALUE> { }

static class SneakyTwoParam1604<V,K> extends TwoParam1604<K,List<V>> { }

public void testCustomTypesRefinedSimple()
{
TypeFactory tf = newTypeFactory();
JavaType type = tf.constructType(new TypeReference<Data1604<Long>>() { });
assertEquals(Data1604.class, type.getRawClass());

JavaType subtype = tf.constructSpecializedType(type, DataList1604.class);
assertEquals(DataList1604.class, subtype.getRawClass());
assertEquals(1, subtype.containedTypeCount());

// should properly resolve type parameter:
JavaType paramType = subtype.containedType(0);
assertEquals(Long.class, paramType.getRawClass());
}

public void testCustomTypesRefinedNested()
{
TypeFactory tf = newTypeFactory();
JavaType type = tf.constructType(new TypeReference<Data1604<Long>>() { });
assertEquals(Data1604.class, type.getRawClass());

JavaType subtype = tf.constructSpecializedType(type, RefinedDataList1604.class);
assertEquals(RefinedDataList1604.class, subtype.getRawClass());
assertEquals(1, subtype.containedTypeCount());

// should properly resolve type parameter:
JavaType paramType = subtype.containedType(0);
assertEquals(Long.class, paramType.getRawClass());

// and have correct parent too
assertEquals(DataList1604.class, subtype.getSuperClass().getRawClass());
}

public void testCustomTypesRefinedSneaky()
{
TypeFactory tf = newTypeFactory();
JavaType type = tf.constructType(new TypeReference<Data1604<Long>>() { });
assertEquals(Data1604.class, type.getRawClass());

JavaType subtype = tf.constructSpecializedType(type, SneakyDataList1604.class);
assertEquals(SneakyDataList1604.class, subtype.getRawClass());
assertEquals(1, subtype.containedTypeCount());

// should properly resolve type parameter:
JavaType paramType = subtype.containedType(0);
assertEquals(Long.class, paramType.getRawClass());

// and have correct parent too
assertEquals(Data1604.class, subtype.getSuperClass().getRawClass());
}

public void testTwoParamSneakyCustom()
{
TypeFactory tf = newTypeFactory();
JavaType type = tf.constructType(new TypeReference<TwoParam1604<String,Long>>() { });
assertEquals(TwoParam1604.class, type.getRawClass());
assertEquals(String.class, type.containedType(0).getRawClass());
assertEquals(Long.class, type.containedType(1).getRawClass());

JavaType subtype = tf.constructSpecializedType(type, SneakyTwoParam1604.class);
assertEquals(SneakyTwoParam1604.class, subtype.getRawClass());
assertEquals(TwoParam1604.class, subtype.getSuperClass().getRawClass());
assertEquals(2, subtype.containedTypeCount());

// should properly resolve type parameters despite sneaky switching
JavaType first = subtype.containedType(0);
assertEquals(List.class, first.getRawClass());
assertEquals(1, first.containedTypeCount());
assertEquals(Long.class, first.containedType(0).getRawClass());

JavaType second = subtype.containedType(1);
assertEquals(String.class, second.getRawClass());
}
}

0 comments on commit cab130e

Please sign in to comment.