Debian Codegraph @

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

raw
1 package com.fasterxml.jackson.dataformat.smile;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7 import java.util.Arrays;
8
9 import org.junit.Assert;
10
11 import com.fasterxml.jackson.core.*;
12 import com.fasterxml.jackson.databind.ObjectMapper;
13 import com.fasterxml.jackson.dataformat.smile.SmileFactory;
14 import com.fasterxml.jackson.dataformat.smile.SmileGenerator;
15 import com.fasterxml.jackson.dataformat.smile.SmileParser;
16
17 public abstract class SmileTestBase
18 extends junit.framework.TestCase
19 {
20 // From JSON specification, sample doc...
21 protected final static int SAMPLE_SPEC_VALUE_WIDTH = 800;
22 protected final static int SAMPLE_SPEC_VALUE_HEIGHT = 600;
23 protected final static String SAMPLE_SPEC_VALUE_TITLE = "View from 15th Floor";
24 protected final static String SAMPLE_SPEC_VALUE_TN_URL = "http://www.example.com/image/481989943";
25 protected final static int SAMPLE_SPEC_VALUE_TN_HEIGHT = 125;
26 protected final static String SAMPLE_SPEC_VALUE_TN_WIDTH = "100";
27 protected final static int SAMPLE_SPEC_VALUE_TN_ID1 = 116;
28 protected final static int SAMPLE_SPEC_VALUE_TN_ID2 = 943;
29 protected final static int SAMPLE_SPEC_VALUE_TN_ID3 = 234;
30 protected final static int SAMPLE_SPEC_VALUE_TN_ID4 = 38793;
31
32 protected final static String SAMPLE_DOC_JSON_SPEC =
33 "{\n"
34 +" \"Image\" : {\n"
35 +" \"Width\" : "+SAMPLE_SPEC_VALUE_WIDTH+",\n"
36 +" \"Height\" : "+SAMPLE_SPEC_VALUE_HEIGHT+","
37 +"\"Title\" : \""+SAMPLE_SPEC_VALUE_TITLE+"\",\n"
38 +" \"Thumbnail\" : {\n"
39 +" \"Url\" : \""+SAMPLE_SPEC_VALUE_TN_URL+"\",\n"
40 +"\"Height\" : "+SAMPLE_SPEC_VALUE_TN_HEIGHT+",\n"
41 +" \"Width\" : \""+SAMPLE_SPEC_VALUE_TN_WIDTH+"\"\n"
42 +" },\n"
43 +" \"IDs\" : ["+SAMPLE_SPEC_VALUE_TN_ID1+","+SAMPLE_SPEC_VALUE_TN_ID2+","+SAMPLE_SPEC_VALUE_TN_ID3+","+SAMPLE_SPEC_VALUE_TN_ID4+"]\n"
44 +" }"
45 +"}"
46 ;
47
48 /*
49 /**********************************************************
50 /* Factory methods
51 /**********************************************************
52 */
53
54 protected SmileParser _smileParser(byte[] input) throws IOException {
55 return _smileParser(input, false);
56 }
57
58 protected SmileParser _smileParser(InputStream in) throws IOException {
59 return _smileParser(in, false);
60 }
61
62 protected SmileParser _smileParser(byte[] input, boolean requireHeader) throws IOException
63 {
64 SmileFactory f = smileFactory(requireHeader, false, false);
65 return _smileParser(f, input);
66 }
67
68 protected SmileParser _smileParser(InputStream in, boolean requireHeader) throws IOException
69 {
70 SmileFactory f = smileFactory(requireHeader, false, false);
71 return _smileParser(f, in);
72 }
73
74 protected SmileParser _smileParser(SmileFactory f, byte[] input) throws IOException {
75 return f.createParser(input);
76 }
77
78 protected SmileParser _smileParser(SmileFactory f, InputStream in) throws IOException {
79 return f.createParser(in);
80 }
81
82 protected ObjectMapper smileMapper() {
83 return smileMapper(false);
84 }
85
86 protected ObjectMapper smileMapper(boolean requireHeader) {
87 return smileMapper(requireHeader, requireHeader, false);
88 }
89
90 protected ObjectMapper smileMapper(boolean requireHeader,
91 boolean writeHeader, boolean writeEndMarker)
92 {
93 return new ObjectMapper(smileFactory(requireHeader, writeHeader, writeEndMarker));
94 }
95
96 protected SmileFactory smileFactory(boolean requireHeader,
97 boolean writeHeader, boolean writeEndMarker)
98 {
99 SmileFactory f = new SmileFactory();
100 f.configure(SmileParser.Feature.REQUIRE_HEADER, requireHeader);
101 f.configure(SmileGenerator.Feature.WRITE_HEADER, writeHeader);
102 f.configure(SmileGenerator.Feature.WRITE_END_MARKER, writeEndMarker);
103 return f;
104 }
105
106 protected byte[] _smileDoc(String json) throws IOException
107 {
108 return _smileDoc(json, true);
109 }
110
111 protected byte[] _smileDoc(String json, boolean writeHeader) throws IOException
112 {
113 return _smileDoc(new SmileFactory(), json, writeHeader);
114 }
115
116 protected byte[] _smileDoc(SmileFactory smileFactory, String json, boolean writeHeader) throws IOException
117 {
118 JsonFactory jf = new JsonFactory();
119 JsonParser jp = jf.createParser(json);
120 ByteArrayOutputStream out = new ByteArrayOutputStream();
121 JsonGenerator jg = smileGenerator(out, writeHeader);
122
123 while (jp.nextToken() != null) {
124 jg.copyCurrentEvent(jp);
125 }
126 jp.close();
127 jg.close();
128 return out.toByteArray();
129 }
130
131 protected SmileGenerator smileGenerator(OutputStream result, boolean addHeader)
132 throws IOException
133 {
134 return smileGenerator(new SmileFactory(), result, addHeader);
135 }
136
137 protected SmileGenerator smileGenerator(SmileFactory f,
138 OutputStream result, boolean addHeader)
139 throws IOException
140 {
141 f.configure(SmileGenerator.Feature.WRITE_HEADER, addHeader);
142 return f.createGenerator(result, null);
143 }
144
145 /*
146 /**********************************************************
147 /* Additional assertion methods
148 /**********************************************************
149 */
150
151 protected void assertToken(JsonToken expToken, JsonToken actToken)
152 {
153 if (actToken != expToken) {
154 fail("Expected token "+expToken+", current token "+actToken);
155 }
156 }
157
158 protected void assertToken(JsonToken expToken, JsonParser jp)
159 {
160 assertToken(expToken, jp.getCurrentToken());
161 }
162
163 protected void assertType(Object ob, Class<?> expType)
164 {
165 if (ob == null) {
166 fail("Expected an object of type "+expType.getName()+", got null");
167 }
168 Class<?> cls = ob.getClass();
169 if (!expType.isAssignableFrom(cls)) {
170 fail("Expected type "+expType.getName()+", got "+cls.getName());
171 }
172 }
173
174 protected void verifyException(Throwable e, String... matches)
175 {
176 String msg = e.getMessage();
177 String lmsg = (msg == null) ? "" : msg.toLowerCase();
178 for (String match : matches) {
179 String lmatch = match.toLowerCase();
180 if (lmsg.indexOf(lmatch) >= 0) {
181 return;
182 }
183 }
184 fail("Expected an exception with one of substrings ("+Arrays.asList(matches)+"): got one with message \""+msg+"\"");
185 }
186
187 protected void _verifyBytes(byte[] actBytes, byte... expBytes)
188 {
189 Assert.assertArrayEquals(expBytes, actBytes);
190 }
191
192 /**
193 * Method that gets textual contents of the current token using
194 * available methods, and ensures results are consistent, before
195 * returning them
196 */
197 protected String getAndVerifyText(JsonParser jp)
198 throws IOException, JsonParseException
199 {
200 // Ok, let's verify other accessors
201 int actLen = jp.getTextLength();
202 char[] ch = jp.getTextCharacters();
203 String str2 = new String(ch, jp.getTextOffset(), actLen);
204 String str = jp.getText();
205
206 if (str.length() != actLen) {
207 fail("Internal problem (jp.token == "+jp.getCurrentToken()+"): jp.getText().length() ['"+str+"'] == "+str.length()+"; jp.getTextLength() == "+actLen);
208 }
209 assertEquals("String access via getText(), getTextXxx() must be the same", str, str2);
210
211 return str;
212 }
213
214 /*
215 /**********************************************************
216 /* Other helper methods
217 /**********************************************************
218 */
219
220 public String quote(String str) {
221 return '"'+str+'"';
222 }
223 }