|
3 | 3 | import com.fasterxml.jackson.annotation.JsonProperty;
|
4 | 4 | import com.fasterxml.jackson.databind.JsonMappingException;
|
5 | 5 | import com.fasterxml.jackson.databind.ObjectMapper;
|
6 |
| -import org.junit.jupiter.api.Test; |
| 6 | +import org.junit.Test; |
7 | 7 |
|
8 | 8 | import javax.annotation.Nullable;
|
9 | 9 | import java.time.Duration;
|
10 | 10 |
|
11 | 11 | import static org.assertj.core.api.Assertions.assertThat;
|
12 | 12 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
13 |
| -import static org.junit.jupiter.api.Assertions.assertTimeout; |
14 |
| - |
15 | 13 |
|
16 | 14 | public class JacksonDeserializationOfBigNumbersToDurationTest {
|
17 | 15 |
|
18 | 16 | private final ObjectMapper objectMapper = Jackson.newObjectMapper();
|
19 | 17 |
|
20 |
| - @Test |
21 |
| - void testDoesNotAttemptToDeserializeExtremelyBigNumbers() throws Exception { |
22 |
| - Task task = objectMapper.readValue("{\"id\": 42, \"duration\": 1e1000000000}", Task.class); |
23 |
| - assertTimeout(Duration.ofSeconds(5L), () -> assertThat(task.getDuration()).isEqualTo(Duration.ofSeconds(0))); |
| 18 | + @Test(timeout = 5000) |
| 19 | + public void testDoesNotAttemptToDeserializeExtremelyBigNumbers() { |
| 20 | + assertThatExceptionOfType(JsonMappingException.class).isThrownBy( |
| 21 | + () -> objectMapper.readValue("{\"id\": 42, \"duration\": 1e1000000000}", Task.class)) |
| 22 | + .withMessageStartingWith("Value is out of range of Duration"); |
24 | 23 | }
|
25 | 24 |
|
26 | 25 | @Test
|
27 |
| - void testCanDeserializeZero() throws Exception { |
| 26 | + public void testCanDeserializeZero() throws Exception { |
28 | 27 | Task task = objectMapper.readValue("{\"id\": 42, \"duration\": 0}", Task.class);
|
29 | 28 | assertThat(task.getDuration()).isEqualTo(Duration.ofSeconds(0));
|
30 | 29 | }
|
31 | 30 |
|
32 | 31 | @Test
|
33 |
| - void testCanDeserializeNormalTimestamp() throws Exception { |
| 32 | + public void testCanDeserializeNormalTimestamp() throws Exception { |
34 | 33 | Task task = objectMapper.readValue("{\"id\": 42, \"duration\": 30}", Task.class);
|
35 | 34 | assertThat(task.getDuration()).isEqualTo(Duration.ofSeconds(30));
|
36 | 35 | }
|
37 | 36 |
|
38 | 37 | @Test
|
39 |
| - void testCanDeserializeNormalTimestampWithNanoseconds() throws Exception { |
| 38 | + public void testCanDeserializeNormalTimestampWithNanoseconds() throws Exception { |
40 | 39 | Task task = objectMapper.readValue("{\"id\": 42, \"duration\": 30.314400507}", Task.class);
|
41 | 40 | assertThat(task.getDuration()).isEqualTo(Duration.ofSeconds(30, 314400507L));
|
42 | 41 | }
|
43 | 42 |
|
44 | 43 | @Test
|
45 |
| - void testCanDeserializeFromString() throws Exception { |
| 44 | + public void testCanDeserializeFromString() throws Exception { |
46 | 45 | Task task = objectMapper.readValue("{\"id\": 42, \"duration\": \"PT30S\"}", Task.class);
|
47 | 46 | assertThat(task.getDuration()).isEqualTo(Duration.ofSeconds(30));
|
48 | 47 | }
|
49 | 48 |
|
50 | 49 | @Test
|
51 |
| - void testCanDeserializeMinDuration() throws Exception { |
| 50 | + public void testCanDeserializeMinDuration() throws Exception { |
52 | 51 | Task task = objectMapper.readValue("{\"id\": 42, \"duration\": -9223372036854775808}", Task.class);
|
53 | 52 | assertThat(task.getDuration()).isEqualTo(Duration.ofSeconds(Long.MIN_VALUE));
|
54 | 53 | }
|
55 | 54 |
|
56 | 55 | @Test
|
57 |
| - void testCanDeserializeMaxDuration() throws Exception { |
| 56 | + public void testCanDeserializeMaxDuration() throws Exception { |
58 | 57 | Task task = objectMapper.readValue("{\"id\": 42, \"duration\": 9223372036854775807}", Task.class);
|
59 | 58 | assertThat(task.getDuration()).isEqualTo(Duration.ofSeconds(Long.MAX_VALUE));
|
60 | 59 | }
|
61 | 60 |
|
62 | 61 | @Test
|
63 |
| - void testCanNotDeserializeValueMoreThanMaxDuration() { |
| 62 | + public void testCanNotDeserializeValueMoreThanMaxDuration() { |
64 | 63 | assertThatExceptionOfType(JsonMappingException.class).isThrownBy(
|
65 | 64 | () -> objectMapper.readValue("{\"id\": 42, \"duration\": 9223372036854775808}", Task.class));
|
66 | 65 | }
|
67 | 66 |
|
68 | 67 | @Test
|
69 |
| - void testCanNotDeserializeValueLessThanMinDuration() { |
| 68 | + public void testCanNotDeserializeValueLessThanMinDuration() { |
70 | 69 | assertThatExceptionOfType(JsonMappingException.class).isThrownBy(
|
71 | 70 | () -> objectMapper.readValue("{\"id\": 42, \"duration\": -9223372036854775809}", Task.class));
|
72 | 71 | }
|
|
0 commit comments