Debian Codegraph @

src/test/java/com/fasterxml/jackson/dataformat/smile/ParserBiggerDataTest.java

raw
1 package com.fasterxml.jackson.dataformat.smile;
2
3 import java.io.*;
4 import java.util.*;
5
6 import com.fasterxml.jackson.databind.*;
7
8 /**
9 * Bigger test to try to do smoke-testing of overall functionality,
10 * using more sizable (500k of JSON, 200k of Smile) dataset.
11 * Should tease out at least some of boundary conditions.
12 */
13 public class ParserBiggerDataTest extends SmileTestBase
14 {
15 static class Citm
16 {
17 public Map<Integer,String> areaNames;
18 public Map<Integer,String> audienceSubCategoryNames;
19 public Map<Integer,String> blockNames;
20 public Map<Integer,String> seatCategoryNames;
21 public Map<Integer,String> subTopicNames;
22 public Map<Integer,String> subjectNames;
23 public Map<Integer,String> topicNames;
24 public Map<Integer,int[]> topicSubTopics;
25 public Map<String,String> venueNames;
26
27 public Map<Integer,Event> events;
28 public List<Performance> performances;
29 }
30
31 static class Event
32 {
33 public int id;
34 public String name;
35 public String description;
36 public String subtitle;
37 public String logo;
38 public int subjectCode;
39 public int[] topicIds;
40 public LinkedHashSet<Integer> subTopicIds;
41 }
42
43 static class Performance
44 {
45 public int id;
46 public int eventId;
47 public String name;
48 public String description;
49 public String logo;
50
51 public List<Price> prices;
52 public List<SeatCategory> seatCategories;
53
54 public long start;
55 public String seatMapImage;
56 public String venueCode;
57 }
58
59 static class Price {
60 public int amount;
61 public int audienceSubCategoryId;
62 public int seatCategoryId;
63 }
64
65 static class SeatCategory {
66 public int seatCategoryId;
67 public List<Area> areas;
68 }
69
70 static class Area {
71 public int areaId;
72 public int[] blockIds;
73 }
74
75 /*
76 /**********************************************************
77 /* Test methods
78 /**********************************************************
79 */
80
81 final ObjectMapper JSON_MAPPER = new ObjectMapper();
82
83 public void testReading() throws Exception
84 {
85 Citm citm0 = JSON_MAPPER.readValue(getClass().getResourceAsStream("/data/citm_catalog.json"),
86 Citm.class);
87
88 ObjectMapper smiler = smileMapper(false);
89 byte[] smile = smiler.writeValueAsBytes(citm0);
90
91 Citm citm = smiler.readValue(new ByteArrayInputStream(smile), Citm.class);
92
93 assertNotNull(citm);
94 assertNotNull(citm.areaNames);
95 assertEquals(17, citm.areaNames.size());
96 assertNotNull(citm.events);
97 assertEquals(184, citm.events.size());
98
99 assertNotNull(citm.seatCategoryNames);
100 assertEquals(64, citm.seatCategoryNames.size());
101 assertNotNull(citm.subTopicNames);
102 assertEquals(19, citm.subTopicNames.size());
103 assertNotNull(citm.subjectNames);
104 assertEquals(0, citm.subjectNames.size());
105 assertNotNull(citm.topicNames);
106 assertEquals(4, citm.topicNames.size());
107 assertNotNull(citm.topicSubTopics);
108 assertEquals(4, citm.topicSubTopics.size());
109 assertNotNull(citm.venueNames);
110 assertEquals(1, citm.venueNames.size());
111 }
112
113 public void testRoundTrip() throws Exception
114 {
115 Citm citm0 = JSON_MAPPER.readValue(getClass().getResourceAsStream("/data/citm_catalog.json"),
116 Citm.class);
117 ObjectMapper smiler = smileMapper(false);
118 byte[] smile = smiler.writeValueAsBytes(citm0);
119
120 Citm citm = smiler.readValue(new ByteArrayInputStream(smile), Citm.class);
121
122 ObjectMapper mapper = smileMapper(false);
123
124 byte[] smile1 = mapper.writeValueAsBytes(citm);
125 // IMPORTANT: use InputStream so boundary conditions are actually checked
126 Citm citm2 = mapper.readValue(new ByteArrayInputStream(smile1), Citm.class);
127 byte[] smile2 = mapper.writeValueAsBytes(citm2);
128
129 assertEquals(smile1.length, smile2.length);
130
131 assertNotNull(citm.areaNames);
132 assertEquals(17, citm.areaNames.size());
133 assertNotNull(citm.events);
134 assertEquals(184, citm.events.size());
135
136 assertEquals(citm.seatCategoryNames.size(), citm2.seatCategoryNames.size());
137 assertEquals(citm.subTopicNames.size(), citm2.subTopicNames.size());
138 assertEquals(citm.subjectNames.size(), citm2.subjectNames.size());
139 assertEquals(citm.topicNames.size(), citm2.topicNames.size());
140 assertEquals(citm.topicSubTopics.size(), citm2.topicSubTopics.size());
141 assertEquals(citm.venueNames.size(), citm2.venueNames.size());
142 }
143
144 public void testIssue17BoundaryWithFloat() throws Exception
145 {
146 _testWithFloats(false);
147 _testWithFloats(true);
148 }
149
150 private void _testWithFloats(boolean useHeader) throws Exception
151 {
152 double[] data = new double[4096];
153 for (int i = 0; i < data.length; ++i) {
154 data[i] = (double) i;
155 }
156 ObjectMapper mapper = smileMapper(useHeader);
157 byte[] encoded = mapper.writeValueAsBytes(data);
158
159 // first, read from byte array; no boundary
160 double[] decoded = mapper.readValue(encoded, double[].class);
161 assertEquals(data.length, decoded.length);
162
163 // and then via InputStream
164 decoded = mapper.readValue(new ByteArrayInputStream(encoded), double[].class); // This fails on 2.4.0
165 assertEquals(data.length, decoded.length);
166
167 for (int i = 0; i < data.length; ++i) {
168 if (data[i] != decoded[i]) {
169 assertEquals("Different value at #"+i, data[i], decoded[i]);
170 }
171 }
172 }
173 }