|
1 | 1 | package com.fasterxml.jackson.databind.introspect;
|
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
3 | 4 | import java.util.List;
|
| 5 | +import java.util.Objects; |
4 | 6 |
|
5 | 7 | import org.junit.jupiter.api.Test;
|
6 | 8 |
|
| 9 | +import com.fasterxml.jackson.annotation.JsonCreator; |
7 | 10 | import com.fasterxml.jackson.databind.*;
|
| 11 | +import com.fasterxml.jackson.databind.cfg.MapperConfig; |
| 12 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
8 | 13 | import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
|
9 | 14 |
|
| 15 | +import static org.junit.Assert.assertEquals; |
| 16 | + |
10 | 17 | // Tests for [databind#4584]: extension point for discovering "Canonical"
|
11 | 18 | // Creator (primary Creator, usually constructor, used in case no creator
|
12 | 19 | // explicitly annotated)
|
|
15 | 22 | public class CanonicalCreator4584Test extends DatabindTestUtil
|
16 | 23 | {
|
17 | 24 | static class POJO4584 {
|
18 |
| - String value; |
| 25 | + final String value; |
19 | 26 |
|
20 |
| - // actually fundamentally canonical constructor... |
21 |
| - private POJO4584(String v, int bogus) { |
| 27 | + POJO4584(@ImplicitName("v") String v, @ImplicitName("bogus") int bogus) { |
22 | 28 | value = v;
|
23 | 29 | }
|
24 |
| - |
25 |
| - public POJO4584(List<Object> bogus) { |
26 |
| - value = "List["+((bogus == null) ? -1 : bogus.size())+"]"; |
| 30 | + |
| 31 | + public POJO4584(@ImplicitName("list") List<Object> list) { |
| 32 | + value = "List["+((list == null) ? -1 : list.size())+"]"; |
27 | 33 | }
|
28 | 34 |
|
29 |
| - public POJO4584(Object[] bogus) { |
30 |
| - value = "Array["+((bogus == null) ? -1 : bogus.length)+"]"; |
| 35 | + public POJO4584(@ImplicitName("array") Object[] array) { |
| 36 | + value = "Array["+((array == null) ? -1 : array.length)+"]"; |
31 | 37 | }
|
32 | 38 |
|
33 |
| - public static POJO4584 factoryInt(int i) { |
| 39 | + public static POJO4584 factoryInt(@ImplicitName("i") int i) { |
34 | 40 | return new POJO4584("int["+i+"]", 0);
|
35 | 41 | }
|
36 | 42 |
|
37 |
| - public static POJO4584 factoryLong(long l) { |
38 |
| - return new POJO4584("long["+l+"]", 0); |
| 43 | + public static POJO4584 factoryString(@ImplicitName("v") String v) { |
| 44 | + return new POJO4584(v, 0); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public boolean equals(Object o) { |
| 49 | + return (o instanceof POJO4584) && Objects.equals(((POJO4584) o).value, value); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public String toString() { |
| 54 | + return "'"+value+"'"; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + static class DelegatingCanonicalFindingIntrospector extends ImplicitNameIntrospector |
| 59 | + { |
| 60 | + private static final long serialVersionUID = 1L; |
| 61 | + |
| 62 | + private final Class<?> _argType; |
| 63 | + |
| 64 | + public DelegatingCanonicalFindingIntrospector(Class<?> argType) { |
| 65 | + _argType = argType; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public PotentialCreator findPrimaryCreator(MapperConfig<?> config, |
| 70 | + AnnotatedClass valueClass, |
| 71 | + List<PotentialCreator> declaredConstructors, |
| 72 | + List<PotentialCreator> declaredFactories) |
| 73 | + { |
| 74 | + if (valueClass.getRawType() != POJO4584.class) { |
| 75 | + System.err.println("findCanonicalCreator SKIPPED for: "+valueClass.getRawType()); |
| 76 | + return null; |
| 77 | + } |
| 78 | + System.err.println("findCanonicalCreator DONE for: "+valueClass.getRawType()); |
| 79 | + List<PotentialCreator> combo = new ArrayList<>(declaredConstructors); |
| 80 | + combo.addAll(declaredFactories); |
| 81 | + for (PotentialCreator ctor : combo) { |
| 82 | + if (ctor.paramCount() == 1 |
| 83 | + && _argType == ctor.param(0).getRawType()) { |
| 84 | +System.err.println("MATCH! "+ctor); |
| 85 | + ctor.overrideMode(JsonCreator.Mode.DELEGATING); |
| 86 | + return ctor; |
| 87 | + } |
| 88 | + } |
| 89 | + System.err.println("No MATCH! "); |
| 90 | + return null; |
39 | 91 | }
|
40 | 92 | }
|
41 | 93 |
|
42 |
| - private final ObjectMapper MAPPER = newJsonMapper(); |
| 94 | + /* |
| 95 | + /********************************************************************** |
| 96 | + /* Test methods; properties-based Creators |
| 97 | + /********************************************************************** |
| 98 | + */ |
| 99 | + |
| 100 | + /* |
| 101 | + @Test |
| 102 | + public void testCanonicalConstructorPropertiesCreator() throws Exception |
| 103 | + { |
| 104 | + assertEquals(POJO4584.factoryString("List[0]"), |
| 105 | + readerWith(new DelegatingCanonicalFindingIntrospector(List.class)) |
| 106 | + .readValue(a2q("{'x':[ ]}"))); |
| 107 | + } |
| 108 | + */ |
43 | 109 |
|
44 | 110 | /*
|
45 | 111 | /**********************************************************************
|
46 |
| - /* Test methods |
| 112 | + /* Test methods; delegation-based Creators |
47 | 113 | /**********************************************************************
|
48 | 114 | */
|
| 115 | + |
| 116 | + @Test |
| 117 | + public void testCanonicalConstructorDelegatingIntCreator() throws Exception |
| 118 | + { |
| 119 | + assertEquals(POJO4584.factoryString("List[3]"), |
| 120 | + readerWith(new DelegatingCanonicalFindingIntrospector(List.class)) |
| 121 | + .readValue(a2q("[1, 2, 3]"))); |
| 122 | + } |
49 | 123 |
|
50 | 124 | @Test
|
51 |
| - public void testCanonicalConstructorPropertiesCreator() throws Exception { |
52 |
| - // TODO |
| 125 | + public void testCanonicalConstructorDelegatingListCreator() throws Exception |
| 126 | + { |
| 127 | + assertEquals(POJO4584.factoryString("List[3]"), |
| 128 | + readerWith(new DelegatingCanonicalFindingIntrospector(List.class)) |
| 129 | + .readValue(a2q("[1, 2, 3]"))); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testCanonicalConstructorDelegatingArrayCreator() throws Exception |
| 134 | + { |
| 135 | + assertEquals(POJO4584.factoryString("Array[1]"), |
| 136 | + readerWith(new DelegatingCanonicalFindingIntrospector(Object[].class)) |
| 137 | + .readValue(a2q("[true]"))); |
| 138 | + } |
| 139 | + |
| 140 | + /* |
| 141 | + /********************************************************************** |
| 142 | + /* Helper methods |
| 143 | + /********************************************************************** |
| 144 | + */ |
| 145 | + |
| 146 | + private ObjectReader readerWith(AnnotationIntrospector intr) { |
| 147 | + return mapperWith(intr).readerFor(POJO4584.class); |
| 148 | + } |
| 149 | + |
| 150 | + private ObjectMapper mapperWith(AnnotationIntrospector intr) { |
| 151 | + return JsonMapper.builder() |
| 152 | + .annotationIntrospector(intr) |
| 153 | + .build(); |
53 | 154 | }
|
54 | 155 | }
|
0 commit comments