|
15 | 15 | //
|
16 | 16 | package org.msgpack.jackson.dataformat;
|
17 | 17 |
|
| 18 | +import com.fasterxml.jackson.core.JsonProcessingException; |
18 | 19 | import org.junit.Test;
|
19 | 20 |
|
20 | 21 | import java.io.IOException;
|
21 | 22 | import java.math.BigDecimal;
|
| 23 | +import java.math.BigInteger; |
22 | 24 |
|
23 | 25 | import static org.junit.Assert.assertEquals;
|
| 26 | +import static org.junit.Assert.fail; |
24 | 27 |
|
25 | 28 | public class MessagePackMapperTest
|
26 | 29 | {
|
27 |
| - static class Pojo |
| 30 | + static class PojoWithBigInteger |
| 31 | + { |
| 32 | + public BigInteger value; |
| 33 | + } |
| 34 | + |
| 35 | + static class PojoWithBigDecimal |
28 | 36 | {
|
29 | 37 | public BigDecimal value;
|
30 | 38 | }
|
31 | 39 |
|
32 |
| - @Test |
33 |
| - public void handleBigDecimalAsString() throws IOException |
| 40 | + private void shouldFailToHandleBigInteger(MessagePackMapper messagePackMapper) throws JsonProcessingException |
| 41 | + { |
| 42 | + PojoWithBigInteger obj = new PojoWithBigInteger(); |
| 43 | + obj.value = BigInteger.valueOf(Long.MAX_VALUE).multiply(BigInteger.valueOf(10)); |
| 44 | + |
| 45 | + try { |
| 46 | + messagePackMapper.writeValueAsBytes(obj); |
| 47 | + fail(); |
| 48 | + } |
| 49 | + catch (IllegalArgumentException e) { |
| 50 | + // Expected |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private void shouldSuccessToHandleBigInteger(MessagePackMapper messagePackMapper) throws IOException |
34 | 55 | {
|
35 |
| - MessagePackMapper mapper = new MessagePackMapper().handleBigDecimalAsString(); |
36 |
| - Pojo obj = new Pojo(); |
| 56 | + PojoWithBigInteger obj = new PojoWithBigInteger(); |
| 57 | + obj.value = BigInteger.valueOf(Long.MAX_VALUE).multiply(BigInteger.valueOf(10)); |
| 58 | + |
| 59 | + byte[] converted = messagePackMapper.writeValueAsBytes(obj); |
| 60 | + |
| 61 | + PojoWithBigInteger deserialized = messagePackMapper.readValue(converted, PojoWithBigInteger.class); |
| 62 | + assertEquals(obj.value, deserialized.value); |
| 63 | + } |
| 64 | + |
| 65 | + private void shouldFailToHandleBigDecimal(MessagePackMapper messagePackMapper) throws JsonProcessingException |
| 66 | + { |
| 67 | + PojoWithBigDecimal obj = new PojoWithBigDecimal(); |
37 | 68 | obj.value = new BigDecimal("1234567890.98765432100");
|
38 | 69 |
|
39 |
| - byte[] converted = mapper.writeValueAsBytes(obj); |
| 70 | + try { |
| 71 | + messagePackMapper.writeValueAsBytes(obj); |
| 72 | + fail(); |
| 73 | + } |
| 74 | + catch (IllegalArgumentException e) { |
| 75 | + // Expected |
| 76 | + } |
| 77 | + } |
40 | 78 |
|
41 |
| - Pojo deserialized = mapper.readValue(converted, Pojo.class); |
| 79 | + private void shouldSuccessToHandleBigDecimal(MessagePackMapper messagePackMapper) throws IOException |
| 80 | + { |
| 81 | + PojoWithBigDecimal obj = new PojoWithBigDecimal(); |
| 82 | + obj.value = new BigDecimal("1234567890.98765432100"); |
| 83 | + |
| 84 | + byte[] converted = messagePackMapper.writeValueAsBytes(obj); |
| 85 | + |
| 86 | + PojoWithBigDecimal deserialized = messagePackMapper.readValue(converted, PojoWithBigDecimal.class); |
42 | 87 | assertEquals(obj.value, deserialized.value);
|
43 | 88 | }
|
| 89 | + |
| 90 | + @Test |
| 91 | + public void handleBigIntegerAsString() throws IOException |
| 92 | + { |
| 93 | + shouldFailToHandleBigInteger(new MessagePackMapper()); |
| 94 | + shouldSuccessToHandleBigInteger(new MessagePackMapper().handleBigIntegerAsString()); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void handleBigDecimalAsString() throws IOException |
| 99 | + { |
| 100 | + shouldFailToHandleBigDecimal(new MessagePackMapper()); |
| 101 | + shouldSuccessToHandleBigDecimal(new MessagePackMapper().handleBigDecimalAsString()); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void handleBigIntegerAndBigDecimalAsString() throws IOException |
| 106 | + { |
| 107 | + MessagePackMapper messagePackMapper = new MessagePackMapper().handleBigIntegerAndBigDecimalAsString(); |
| 108 | + shouldSuccessToHandleBigInteger(messagePackMapper); |
| 109 | + shouldSuccessToHandleBigDecimal(messagePackMapper); |
| 110 | + } |
44 | 111 | }
|
0 commit comments