|
19 | 19 | import static org.assertj.core.api.Assertions.assertThat;
|
20 | 20 | import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
21 | 21 |
|
| 22 | +import com.fasterxml.jackson.annotation.JsonProperty; |
22 | 23 | import com.fasterxml.jackson.core.JsonParseException;
|
23 | 24 | import com.fasterxml.jackson.core.JsonProcessingException;
|
24 | 25 | import com.fasterxml.jackson.core.exc.InputCoercionException;
|
25 | 26 | import com.fasterxml.jackson.core.type.TypeReference;
|
26 | 27 | import com.fasterxml.jackson.databind.ObjectMapper;
|
27 | 28 | import com.fasterxml.jackson.databind.exc.InvalidFormatException;
|
| 29 | +import com.palantir.logsafe.Preconditions; |
28 | 30 | import java.io.File;
|
29 | 31 | import java.io.IOException;
|
30 | 32 | import java.math.BigInteger;
|
|
38 | 40 | import java.time.ZonedDateTime;
|
39 | 41 | import java.util.Collections;
|
40 | 42 | import java.util.Map;
|
| 43 | +import java.util.Objects; |
41 | 44 | import java.util.Optional;
|
42 | 45 | import java.util.OptionalInt;
|
43 | 46 | import java.util.OptionalLong;
|
@@ -128,11 +131,115 @@ public void testLongTypeDeserializationFromString() throws IOException {
|
128 | 131 | assertThat(MAPPER.readValue("\"1\"", Long.TYPE)).isEqualTo(1L);
|
129 | 132 | }
|
130 | 133 |
|
| 134 | + @Test |
| 135 | + public void testLongBeanTypeDeserializationFromString() throws IOException { |
| 136 | + assertThat(MAPPER.readValue("{\"value\":\"1\"}", LongBean.class)).isEqualTo(new LongBean(1L)); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testLongBeanTypeDeserializationFromNumber() throws IOException { |
| 141 | + assertThat(MAPPER.readValue("{\"value\":\"1\"}", LongBean.class)).isEqualTo(new LongBean(1L)); |
| 142 | + } |
| 143 | + |
| 144 | + static final class LongBean { |
| 145 | + @JsonProperty |
| 146 | + private long value; |
| 147 | + |
| 148 | + LongBean() {} |
| 149 | + |
| 150 | + LongBean(long value) { |
| 151 | + setValue(value); |
| 152 | + } |
| 153 | + |
| 154 | + public long getValue() { |
| 155 | + return value; |
| 156 | + } |
| 157 | + |
| 158 | + public void setValue(long value) { |
| 159 | + this.value = value; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public boolean equals(Object other) { |
| 164 | + if (this == other) { |
| 165 | + return true; |
| 166 | + } |
| 167 | + if (other == null || getClass() != other.getClass()) { |
| 168 | + return false; |
| 169 | + } |
| 170 | + LongBean that = (LongBean) other; |
| 171 | + return value == that.value; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public int hashCode() { |
| 176 | + return Objects.hashCode(value); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public String toString() { |
| 181 | + return "LongBean{value=" + value + '}'; |
| 182 | + } |
| 183 | + } |
| 184 | + |
131 | 185 | @Test
|
132 | 186 | public void testOptionalLongTypeDeserializationFromString() throws IOException {
|
133 | 187 | assertThat(MAPPER.readValue("\"1\"", OptionalLong.class)).hasValue(1L);
|
134 | 188 | }
|
135 | 189 |
|
| 190 | + @Test |
| 191 | + public void testOptionalLongBeanTypeDeserializationFromString() throws IOException { |
| 192 | + assertThat(MAPPER.readValue("{\"value\":\"1\"}", OptionalLongBean.class)) |
| 193 | + .isEqualTo(new OptionalLongBean(OptionalLong.of(1L))); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + public void testOptionalLongBeanTypeDeserializationFromNumber() throws IOException { |
| 198 | + assertThat(MAPPER.readValue("{\"value\":1}", OptionalLongBean.class)) |
| 199 | + .isEqualTo(new OptionalLongBean(OptionalLong.of(1L))); |
| 200 | + } |
| 201 | + |
| 202 | + static final class OptionalLongBean { |
| 203 | + @JsonProperty |
| 204 | + private OptionalLong value = OptionalLong.empty(); |
| 205 | + |
| 206 | + OptionalLongBean() {} |
| 207 | + |
| 208 | + OptionalLongBean(OptionalLong value) { |
| 209 | + setValue(value); |
| 210 | + } |
| 211 | + |
| 212 | + public OptionalLong getValue() { |
| 213 | + return value; |
| 214 | + } |
| 215 | + |
| 216 | + public void setValue(OptionalLong value) { |
| 217 | + this.value = Preconditions.checkNotNull(value, "value"); |
| 218 | + } |
| 219 | + |
| 220 | + @Override |
| 221 | + public boolean equals(Object other) { |
| 222 | + if (this == other) { |
| 223 | + return true; |
| 224 | + } |
| 225 | + if (other == null || getClass() != other.getClass()) { |
| 226 | + return false; |
| 227 | + } |
| 228 | + OptionalLongBean that = (OptionalLongBean) other; |
| 229 | + return value.equals(that.value); |
| 230 | + } |
| 231 | + |
| 232 | + @Override |
| 233 | + public int hashCode() { |
| 234 | + return Objects.hashCode(value); |
| 235 | + } |
| 236 | + |
| 237 | + @Override |
| 238 | + public String toString() { |
| 239 | + return "OptionalLongBean{value=" + value + '}'; |
| 240 | + } |
| 241 | + } |
| 242 | + |
136 | 243 | @Test
|
137 | 244 | public void testLongDeserializationFromJsonNumber() throws IOException {
|
138 | 245 | assertThat(MAPPER.readValue("1", Long.class)).isEqualTo(1L);
|
|
0 commit comments