|
3 | 3 | import com.fasterxml.jackson.annotation.JsonProperty;
|
4 | 4 | import com.fasterxml.jackson.databind.ObjectMapper;
|
5 | 5 | import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
| 6 | +import com.fasterxml.jackson.databind.cfg.MapperConfig; |
| 7 | +import com.fasterxml.jackson.datatype.guava.deser.util.RangeHelper; |
6 | 8 | import org.junit.jupiter.api.BeforeEach;
|
7 | 9 | import org.junit.jupiter.api.Test;
|
8 | 10 |
|
9 | 11 | import javax.annotation.Nullable;
|
10 | 12 |
|
11 | 13 | import static org.assertj.core.api.Assertions.assertThat;
|
| 14 | +import static org.mockito.Mockito.mock; |
12 | 15 |
|
13 |
| -public class AnnotationSensitivePropertyNamingStrategyTest { |
| 16 | +class AnnotationSensitivePropertyNamingStrategyTest { |
14 | 17 | public static class RegularExample {
|
15 | 18 | @JsonProperty
|
16 | 19 | @Nullable
|
@@ -42,31 +45,69 @@ public SnakeCaseExample(String firstName) {
|
42 | 45 | private final ObjectMapper mapper = new ObjectMapper();
|
43 | 46 |
|
44 | 47 | @BeforeEach
|
45 |
| - public void setUp() throws Exception { |
| 48 | + void setUp() { |
46 | 49 | mapper.setPropertyNamingStrategy(strategy);
|
47 | 50 | }
|
48 | 51 |
|
49 | 52 | @Test
|
50 |
| - public void serializesRegularProperties() throws Exception { |
| 53 | + void serializesRegularProperties() throws Exception { |
51 | 54 | assertThat(mapper.writeValueAsString(new RegularExample("woo")))
|
52 | 55 | .isEqualTo("{\"firstName\":\"woo\"}");
|
53 | 56 | }
|
54 | 57 |
|
55 | 58 | @Test
|
56 |
| - public void serializesSnakeCaseProperties() throws Exception { |
| 59 | + void serializesSnakeCaseProperties() throws Exception { |
57 | 60 | assertThat(mapper.writeValueAsString(new SnakeCaseExample("woo")))
|
58 | 61 | .isEqualTo("{\"first_name\":\"woo\"}");
|
59 | 62 | }
|
60 | 63 |
|
61 | 64 | @Test
|
62 |
| - public void deserializesRegularProperties() throws Exception { |
| 65 | + void deserializesRegularProperties() throws Exception { |
63 | 66 | assertThat(mapper.readValue("{\"firstName\":\"woo\"}", RegularExample.class).firstName)
|
64 | 67 | .isEqualTo("woo");
|
65 | 68 | }
|
66 | 69 |
|
67 | 70 | @Test
|
68 |
| - public void deserializesSnakeCaseProperties() throws Exception { |
| 71 | + void deserializesSnakeCaseProperties() throws Exception { |
69 | 72 | assertThat(mapper.readValue("{\"first_name\":\"woo\"}", SnakeCaseExample.class).firstName)
|
70 | 73 | .isEqualTo("woo");
|
71 | 74 | }
|
| 75 | + |
| 76 | + @Test |
| 77 | + void nameForConstructorParameterWorksWithNullField() { |
| 78 | + final MapperConfig<?> mapperConfig = mock(MapperConfig.class); |
| 79 | + final String name = strategy.nameForConstructorParameter(mapperConfig, null, "defaultName"); |
| 80 | + assertThat(name).isEqualTo("defaultName"); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void nameForFieldWorksWithNullField() { |
| 85 | + final MapperConfig<?> mapperConfig = mock(MapperConfig.class); |
| 86 | + final String name = strategy.nameForField(mapperConfig, null, "defaultName"); |
| 87 | + assertThat(name).isEqualTo("defaultName"); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void nameForGetterMethodWorksWithNullField() { |
| 92 | + final MapperConfig<?> mapperConfig = mock(MapperConfig.class); |
| 93 | + final String name = strategy.nameForGetterMethod(mapperConfig, null, "defaultName"); |
| 94 | + assertThat(name).isEqualTo("defaultName"); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void nameForSetterMethodWorksWithNullField() { |
| 99 | + final MapperConfig<?> mapperConfig = mock(MapperConfig.class); |
| 100 | + final String name = strategy.nameForSetterMethod(mapperConfig, null, "defaultName"); |
| 101 | + assertThat(name).isEqualTo("defaultName"); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + // https://github.com/dropwizard/dropwizard/issues/3514 |
| 106 | + void usingRangeHelperDoesNotThrowNullPointerException() { |
| 107 | + final RangeHelper.RangeProperties standardNames = RangeHelper.standardNames(); |
| 108 | + assertThat(standardNames.lowerBoundType).isNotBlank(); |
| 109 | + assertThat(standardNames.lowerEndpoint).isNotBlank(); |
| 110 | + assertThat(standardNames.upperBoundType).isNotBlank(); |
| 111 | + assertThat(standardNames.upperEndpoint).isNotBlank(); |
| 112 | + } |
72 | 113 | }
|
0 commit comments