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 |
|
---|
11 | import org.eclipse.jdt.annotation.NonNull;
|
---|
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> {
|
---|
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(
|
---|
25 | "12.3100000000001");
|
---|
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";
|
---|
29 |
|
---|
30 | private final @NonNull String bigserialized = "8748703924870439218876954320948372058";
|
---|
31 | private final @NonNull NumberValue bigvalue = new NumberValue(
|
---|
32 | bigserialized);
|
---|
33 | private static final ObjectMapper jackson = new ObjectMapper();
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | @NonNull
|
---|
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 | @NonNull
|
---|
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 {
|
---|
80 | //System.out.println(
|
---|
81 | // jackson.writeValueAsString(new short[] { 1, 2, 3, 4, 5 }));
|
---|
82 |
|
---|
83 | }
|
---|
84 |
|
---|
85 | @Test
|
---|
86 | public void testDeserializeShorts() throws IOException {
|
---|
87 | @SuppressWarnings("unchecked") @NonNull
|
---|
88 | ArrayList<Integer> list = jackson.readValue("[1,2,3,4,5]",
|
---|
89 | ArrayList.class);
|
---|
90 | //System.out.println(list);
|
---|
91 | //System.out.println(list.get(0).getClass());
|
---|
92 |
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Test
|
---|
96 | public void testDeserializeMix() throws IOException {
|
---|
97 | @NonNull @SuppressWarnings("unchecked")
|
---|
98 | ArrayList<Object> list = jackson.readValue("[\"short\",1,2,3,4,5]",
|
---|
99 | ArrayList.class);
|
---|
100 | //System.out.println(list);
|
---|
101 | //System.out.println(list.get(0).getClass());
|
---|
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 {
|
---|
112 | @NonNull
|
---|
113 | String valstr = "483958743698732691487326987569213874694328974329874328947320984372498327493827432987231874681273648127";
|
---|
114 | @NonNull
|
---|
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 {
|
---|
129 | @NonNull
|
---|
130 | String valstr = "9E+2";
|
---|
131 | @NonNull
|
---|
132 | Value val = jackson.readValue(valstr, Value.class);
|
---|
133 | assertEquals(900, ((BigDecimal) val.getValue()).intValue());
|
---|
134 | assertEquals(val, new NumberValue(new BigDecimal("900")));
|
---|
135 | }
|
---|
136 |
|
---|
137 | } |
---|