1
package com.fasterxml.jackson.dataformat.cbor;
6
import com.fasterxml.jackson.databind.ObjectMapper;
8
public class GeneratorSimpleTest extends CBORTestBase
10
private final ObjectMapper MAPPER = cborMapper();
13
* Test for verifying handling of 'true', 'false' and 'null' literals
15
public void testSimpleLiterals() throws Exception
17
ByteArrayOutputStream out = new ByteArrayOutputStream();
18
CBORGenerator gen = cborGenerator(out);
19
gen.writeBoolean(true);
21
_verifyBytes(out.toByteArray(), CBORConstants.BYTE_TRUE);
23
out = new ByteArrayOutputStream();
24
gen = cborGenerator(out);
25
gen.writeBoolean(false);
27
_verifyBytes(out.toByteArray(), CBORConstants.BYTE_FALSE);
29
out = new ByteArrayOutputStream();
30
gen = cborGenerator(out);
33
_verifyBytes(out.toByteArray(), CBORConstants.BYTE_NULL);
36
public void testMinimalIntValues() throws Exception
38
ByteArrayOutputStream out = new ByteArrayOutputStream();
39
CBORGenerator gen = cborGenerator(out);
40
assertTrue(gen.isEnabled(CBORGenerator.Feature.WRITE_MINIMAL_INTS));
43
_verifyBytes(out.toByteArray(),
44
(byte) (CBORConstants.PREFIX_TYPE_INT_POS + 17));
46
// then without minimal
47
out = new ByteArrayOutputStream();
48
gen = cborGenerator(out);
49
gen.disable(CBORGenerator.Feature.WRITE_MINIMAL_INTS);
52
_verifyBytes(out.toByteArray(),
53
(byte) (CBORConstants.PREFIX_TYPE_INT_POS + 26),
54
(byte) 0, (byte) 0, (byte) 0, (byte) 17);
56
out = new ByteArrayOutputStream();
57
gen = cborGenerator(out);
58
gen.writeNumber(Integer.MAX_VALUE);
60
_verifyBytes(out.toByteArray(),
61
(byte) (CBORConstants.PREFIX_TYPE_INT_POS + 26),
62
(byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF);
64
out = new ByteArrayOutputStream();
65
gen = cborGenerator(out);
66
gen.writeNumber(Integer.MIN_VALUE);
68
_verifyBytes(out.toByteArray(),
69
(byte) (CBORConstants.PREFIX_TYPE_INT_NEG + 26),
70
(byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF);
73
public void testIntValues() throws Exception
76
ByteArrayOutputStream out = new ByteArrayOutputStream();
77
CBORGenerator gen = cborGenerator(out);
80
_verifyBytes(out.toByteArray(),
81
(byte) (CBORConstants.PREFIX_TYPE_INT_POS + 13));
83
out = new ByteArrayOutputStream();
84
gen = cborGenerator(out);
87
_verifyBytes(out.toByteArray(),
88
// note: since there is no "-0", number one less than it'd appear
89
(byte) (CBORConstants.PREFIX_TYPE_INT_NEG + 12));
92
out = new ByteArrayOutputStream();
93
gen = cborGenerator(out);
94
gen.writeNumber(0xFF);
96
_verifyBytes(out.toByteArray(),
97
(byte) (CBORConstants.PREFIX_TYPE_INT_POS + 24), (byte) 0xFF);
99
out = new ByteArrayOutputStream();
100
gen = cborGenerator(out);
101
gen.writeNumber(-256);
103
_verifyBytes(out.toByteArray(),
104
// note: since there is no "-0", number one less than it'd appear
105
(byte) (CBORConstants.PREFIX_TYPE_INT_NEG + 24), (byte) 0xFF);
108
out = new ByteArrayOutputStream();
109
gen = cborGenerator(out);
110
gen.writeNumber(0xFEDC);
112
_verifyBytes(out.toByteArray(),
113
(byte) (CBORConstants.PREFIX_TYPE_INT_POS + 25), (byte) 0xFE, (byte) 0xDC);
115
out = new ByteArrayOutputStream();
116
gen = cborGenerator(out);
117
gen.writeNumber(-0xFFFE);
119
_verifyBytes(out.toByteArray(),
120
(byte) (CBORConstants.PREFIX_TYPE_INT_NEG + 25), (byte) 0xFF, (byte) 0xFD);
122
out = new ByteArrayOutputStream();
123
gen = cborGenerator(out);
124
gen.writeNumber(Integer.MAX_VALUE);
126
_verifyBytes(out.toByteArray(),
127
(byte) (CBORConstants.PREFIX_TYPE_INT_POS + 26),
128
(byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF);
130
out = new ByteArrayOutputStream();
131
gen = cborGenerator(out);
132
gen.writeNumber(Integer.MIN_VALUE);
134
_verifyBytes(out.toByteArray(),
135
(byte) (CBORConstants.PREFIX_TYPE_INT_NEG + 26),
136
(byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF);
139
public void testLongValues() throws Exception
141
ByteArrayOutputStream out = new ByteArrayOutputStream();
142
CBORGenerator gen = cborGenerator(out);
143
long l = -1L + Integer.MIN_VALUE;
146
byte[] b = out.toByteArray();
147
assertEquals((byte) (CBORConstants.PREFIX_TYPE_INT_NEG + 27), b[0]);
148
assertEquals(9, b.length);
149
// could test full contents, but for now this shall suffice
150
assertEquals(0, b[1]);
151
assertEquals(0, b[2]);
152
assertEquals(0, b[3]);
155
public void testFloatValues() throws Exception
157
// first, 32-bit float
158
ByteArrayOutputStream out = new ByteArrayOutputStream();
159
CBORGenerator gen = cborGenerator(out);
163
int raw = Float.floatToIntBits(f);
164
_verifyBytes(out.toByteArray(),
165
(byte) (CBORConstants.BYTE_FLOAT32),
171
// then 64-bit double
172
out = new ByteArrayOutputStream();
173
gen = cborGenerator(out);
177
long rawL = Double.doubleToLongBits(d);
178
_verifyBytes(out.toByteArray(),
179
(byte) (CBORConstants.BYTE_FLOAT64),
190
public void testEmptyArray() throws Exception
192
// First: empty array (2 bytes)
193
ByteArrayOutputStream out = new ByteArrayOutputStream();
194
CBORGenerator gen = cborGenerator(out);
195
gen.writeStartArray();
198
_verifyBytes(out.toByteArray(), CBORConstants.BYTE_ARRAY_INDEFINITE,
199
CBORConstants.BYTE_BREAK);
202
public void testEmptyObject() throws Exception
204
// First: empty array (2 bytes)
205
ByteArrayOutputStream out = new ByteArrayOutputStream();
206
CBORGenerator gen = cborGenerator(out);
207
gen.writeStartObject();
208
gen.writeEndObject();
210
_verifyBytes(out.toByteArray(), CBORConstants.BYTE_OBJECT_INDEFINITE,
211
CBORConstants.BYTE_BREAK);
214
public void testIntArray() throws Exception
216
ByteArrayOutputStream out = new ByteArrayOutputStream();
217
CBORGenerator gen = cborGenerator(out);
219
// currently will produce indefinite-length array
220
gen.writeStartArray();
227
final byte[] EXP = new byte[] {
228
CBORConstants.BYTE_ARRAY_INDEFINITE,
229
(byte) (CBORConstants.PREFIX_TYPE_INT_POS + 1),
230
(byte) (CBORConstants.PREFIX_TYPE_INT_POS + 2),
231
(byte) (CBORConstants.PREFIX_TYPE_INT_POS + 3),
232
CBORConstants.BYTE_BREAK
235
_verifyBytes(out.toByteArray(), EXP);
237
// Also, data-binding should produce identical
238
byte[] b = MAPPER.writeValueAsBytes(new int[] { 1, 2, 3 });
239
_verifyBytes(b, EXP);
242
public void testTrivialObject() throws Exception
244
ByteArrayOutputStream out = new ByteArrayOutputStream();
245
CBORGenerator gen = cborGenerator(out);
247
// currently will produce indefinite-length Object
248
gen.writeStartObject();
249
gen.writeNumberField("a", 1);
250
gen.writeNumberField("b", 2);
251
gen.writeEndObject();
254
final byte[] EXP = new byte[] {
255
CBORConstants.BYTE_OBJECT_INDEFINITE,
256
(byte) (CBORConstants.PREFIX_TYPE_TEXT + 1),
258
(byte) (CBORConstants.PREFIX_TYPE_INT_POS + 1),
259
(byte) (CBORConstants.PREFIX_TYPE_TEXT + 1),
261
(byte) (CBORConstants.PREFIX_TYPE_INT_POS + 2),
262
CBORConstants.BYTE_BREAK
265
_verifyBytes(out.toByteArray(), EXP);
266
Map<String,Integer> map = new LinkedHashMap<String,Integer>();
269
byte[] b = MAPPER.writeValueAsBytes(map);
270
_verifyBytes(b, EXP);
273
public void testShortText() throws Exception
275
ByteArrayOutputStream out = new ByteArrayOutputStream();
276
CBORGenerator gen = cborGenerator(out);
279
_verifyBytes(out.toByteArray(), CBORConstants.BYTE_EMPTY_STRING);
281
out = new ByteArrayOutputStream();
282
gen = cborGenerator(out);
283
gen.writeString("abc");
285
_verifyBytes(out.toByteArray(), (byte) (CBORConstants.PREFIX_TYPE_TEXT + 3),
286
(byte) 'a', (byte) 'b', (byte) 'c');
289
public void testLongerText() throws Exception
291
// First, something with 8-bit length
292
ByteArrayOutputStream out = new ByteArrayOutputStream();
293
CBORGenerator gen = cborGenerator(out);
294
final String SHORT_ASCII = generateAsciiString(240);
295
gen.writeString(SHORT_ASCII);
297
byte[] b = SHORT_ASCII.getBytes("UTF-8");
299
_verifyBytes(out.toByteArray(),
300
(byte) (CBORConstants.PREFIX_TYPE_TEXT + 24), (byte) len, b);
302
// and ditto with fuller Unicode
303
out = new ByteArrayOutputStream();
304
gen = cborGenerator(out);
305
final String SHORT_UNICODE = generateUnicodeString(160);
306
gen.writeString(SHORT_UNICODE);
308
b = SHORT_UNICODE.getBytes("UTF-8");
310
// just a sanity check; will break if generation changes
311
assertEquals(196, len);
312
_verifyBytes(out.toByteArray(),
313
(byte) (CBORConstants.PREFIX_TYPE_TEXT + 24), (byte) len, b);
315
// and then something bit more sizable
316
out = new ByteArrayOutputStream();
317
gen = cborGenerator(out);
318
final String MEDIUM_UNICODE = generateUnicodeString(800);
319
gen.writeString(MEDIUM_UNICODE);
321
b = MEDIUM_UNICODE.getBytes("UTF-8");
323
// just a sanity check; will break if generation changes
324
assertEquals(926, len);
325
_verifyBytes(out.toByteArray(),
326
(byte) (CBORConstants.PREFIX_TYPE_TEXT + 25),
327
(byte) (len>>8), (byte) len,