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