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