|
| 1 | +package com.fasterxml.jackson.databind.deser.creators; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 8 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 12 | + |
| 13 | +/** |
| 14 | + * Tests to help cover simpler cases wrt [databind#4515] |
| 15 | + */ |
| 16 | +public class Creators4515Test extends DatabindTestUtil |
| 17 | +{ |
| 18 | + static class ConstructorBeanPropsExplicit { |
| 19 | + int x; |
| 20 | + |
| 21 | + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) |
| 22 | + protected ConstructorBeanPropsExplicit(@JsonProperty("x") int x) { |
| 23 | + this.x = x; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + static class ConstructorBeanPropsWithName { |
| 28 | + int x; |
| 29 | + |
| 30 | + @JsonCreator |
| 31 | + protected ConstructorBeanPropsWithName(@JsonProperty("x") int x) { |
| 32 | + this.x = x; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + static class FactoryBeanPropsExplicit { |
| 37 | + double d; |
| 38 | + |
| 39 | + private FactoryBeanPropsExplicit(double value, boolean dummy) { d = value; } |
| 40 | + |
| 41 | + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) |
| 42 | + protected static FactoryBeanPropsExplicit createIt(@JsonProperty("f") double value) { |
| 43 | + return new FactoryBeanPropsExplicit(value, true); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /* |
| 48 | + /********************************************************************** |
| 49 | + /* Test methods, simple Properties-based (constructor) explicitly annotated |
| 50 | + /********************************************************************** |
| 51 | + */ |
| 52 | + |
| 53 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testPropsBasedConstructorExplicit() throws Exception |
| 57 | + { |
| 58 | + ConstructorBeanPropsExplicit bean = MAPPER.readValue("{ \"x\" : 42 }", |
| 59 | + ConstructorBeanPropsExplicit.class); |
| 60 | + assertEquals(42, bean.x); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testPropsBasedConstructorWithName() throws Exception |
| 65 | + { |
| 66 | + ConstructorBeanPropsWithName bean = MAPPER.readValue("{ \"x\" : 28 }", |
| 67 | + ConstructorBeanPropsWithName.class); |
| 68 | + assertEquals(28, bean.x); |
| 69 | + } |
| 70 | + |
| 71 | + /* |
| 72 | + /********************************************************************** |
| 73 | + /* Test methods, simple Properties-based (constructor) explicitly annotated |
| 74 | + /********************************************************************** |
| 75 | + */ |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testPropsBasedFactoryExplicit() throws Exception |
| 79 | + { |
| 80 | + FactoryBeanPropsExplicit bean = MAPPER.readValue("{ \"f\" : 0.5 }", |
| 81 | + FactoryBeanPropsExplicit.class); |
| 82 | + assertEquals(0.5, bean.d); |
| 83 | + } |
| 84 | + |
| 85 | + /* |
| 86 | + /********************************************************************** |
| 87 | + /* Test methods, simple Delegating, explicitly annotated |
| 88 | + /********************************************************************** |
| 89 | + */ |
| 90 | + |
| 91 | +} |
0 commit comments