[595] | 1 | package geniusweb.issuevalue;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.assertEquals;
|
---|
| 4 |
|
---|
| 5 | import java.io.IOException;
|
---|
| 6 | import java.math.BigDecimal;
|
---|
| 7 | import java.util.ArrayList;
|
---|
| 8 | import java.util.Arrays;
|
---|
| 9 | import java.util.List;
|
---|
| 10 |
|
---|
[786] | 11 | import javax.annotation.Nonnull;
|
---|
| 12 |
|
---|
[595] | 13 | import org.junit.Test;
|
---|
| 14 |
|
---|
| 15 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
| 16 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
| 17 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
| 18 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 19 |
|
---|
| 20 | import tudelft.utilities.junit.GeneralTests;
|
---|
| 21 |
|
---|
| 22 | public class NumberValueTest extends GeneralTests<NumberValue> {
|
---|
[786] | 23 | private final @Nonnull NumberValue value = new NumberValue("12.31");
|
---|
| 24 | private final @Nonnull NumberValue value1 = new NumberValue("12.31");
|
---|
| 25 | private final @Nonnull NumberValue valueb = new NumberValue(
|
---|
| 26 | "12.3100000000001");
|
---|
| 27 | private final @Nonnull NumberValue valuec = new NumberValue("1000");
|
---|
| 28 | private final @Nonnull NumberValue valuec2 = new NumberValue("1e+3");
|
---|
| 29 | private final @Nonnull String serialized = "12.31";
|
---|
[595] | 30 |
|
---|
[786] | 31 | private final @Nonnull String bigserialized = "8748703924870439218876954320948372058";
|
---|
| 32 | private final @Nonnull NumberValue bigvalue = new NumberValue(
|
---|
| 33 | bigserialized);
|
---|
[595] | 34 | private static final ObjectMapper jackson = new ObjectMapper();
|
---|
| 35 |
|
---|
| 36 | @Override
|
---|
| 37 | public List<List<NumberValue>> getGeneralTestData() {
|
---|
| 38 | return Arrays.asList(Arrays.asList(value, value1),
|
---|
| 39 | Arrays.asList(valueb), Arrays.asList(valuec, valuec2));
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | @Override
|
---|
| 43 | public List<String> getGeneralTestStrings() {
|
---|
| 44 | // 1000 and 1e+3 are bot ok
|
---|
| 45 | return Arrays.asList("12.31", "12.3100000000001", "1...");
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | @Test
|
---|
| 49 | public void testSerialize() throws JsonProcessingException {
|
---|
| 50 | assertEquals(serialized, jackson.writeValueAsString(value));
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | @Test
|
---|
| 54 | public void testDeserialize() throws IOException {
|
---|
| 55 | assertEquals(value, jackson.readValue(serialized, NumberValue.class));
|
---|
| 56 |
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | @Test
|
---|
| 60 | public void testDeserializeFromValue() throws IOException {
|
---|
| 61 | assertEquals(value, jackson.readValue(serialized, Value.class));
|
---|
| 62 |
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | @Test
|
---|
| 66 | public void testBigSerialize() throws JsonProcessingException {
|
---|
| 67 | assertEquals(bigserialized, jackson.writeValueAsString(bigvalue));
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | @Test
|
---|
| 71 | public void testBigDeserialize() throws IOException {
|
---|
| 72 | assertEquals(bigvalue,
|
---|
| 73 | jackson.readValue(bigserialized, NumberValue.class));
|
---|
| 74 |
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | @Test
|
---|
| 78 | public void testSerializeShorts() throws IOException {
|
---|
[732] | 79 | //System.out.println(
|
---|
| 80 | // jackson.writeValueAsString(new short[] { 1, 2, 3, 4, 5 }));
|
---|
[595] | 81 |
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | @Test
|
---|
| 85 | public void testDeserializeShorts() throws IOException {
|
---|
| 86 | @SuppressWarnings("unchecked")
|
---|
| 87 | ArrayList<Integer> list = jackson.readValue("[1,2,3,4,5]",
|
---|
| 88 | ArrayList.class);
|
---|
[732] | 89 | //System.out.println(list);
|
---|
| 90 | //System.out.println(list.get(0).getClass());
|
---|
[595] | 91 |
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | @Test
|
---|
| 95 | public void testDeserializeMix() throws IOException {
|
---|
| 96 | @SuppressWarnings("unchecked")
|
---|
| 97 | ArrayList<Object> list = jackson.readValue("[\"short\",1,2,3,4,5]",
|
---|
| 98 | ArrayList.class);
|
---|
[732] | 99 | //System.out.println(list);
|
---|
| 100 | //System.out.println(list.get(0).getClass());
|
---|
[595] | 101 |
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /**
|
---|
| 105 | * Showing that we CAN deserialize big numbers without double quotes
|
---|
| 106 | * correctly, if we tell jackson upfront that it's a BigDecimal.
|
---|
| 107 | */
|
---|
| 108 | @Test
|
---|
| 109 | public void testDeserializePlainNumber()
|
---|
| 110 | throws JsonParseException, JsonMappingException, IOException {
|
---|
[786] | 111 | @Nonnull
|
---|
[595] | 112 | String valstr = "483958743698732691487326987569213874694328974329874328947320984372498327493827432987231874681273648127";
|
---|
[786] | 113 | @Nonnull
|
---|
[595] | 114 | BigDecimal val = jackson.readValue(valstr, BigDecimal.class);
|
---|
| 115 | assertEquals(valstr, val.toString());
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | @Test
|
---|
| 119 | public void testNumberBlab() throws IOException {
|
---|
| 120 | // jackson.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
|
---|
| 121 | assertEquals(value, jackson.readValue("12.31", Value.class));
|
---|
| 122 |
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | @Test
|
---|
| 126 | public void testDeserializeScientificNotation()
|
---|
| 127 | throws JsonParseException, JsonMappingException, IOException {
|
---|
[786] | 128 | @Nonnull
|
---|
[595] | 129 | String valstr = "9E+2";
|
---|
[786] | 130 | @Nonnull
|
---|
[595] | 131 | Value val = jackson.readValue(valstr, Value.class);
|
---|
| 132 | assertEquals(valstr, val.toString());
|
---|
| 133 | assertEquals(val, new NumberValue(new BigDecimal("900")));
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | } |
---|