~ubuntu-branches/ubuntu/wily/jackson-dataformat-cbor/wily

« back to all changes in this revision

Viewing changes to src/test/java/com/fasterxml/jackson/dataformat/cbor/GeneratorSimpleTest.java

  • Committer: Package Import Robot
  • Author(s): Hilko Bengen
  • Date: 2014-10-08 21:32:08 UTC
  • Revision ID: package-import@ubuntu.com-20141008213208-701046755m525zh8
Tags: upstream-2.4.3
ImportĀ upstreamĀ versionĀ 2.4.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.fasterxml.jackson.dataformat.cbor;
 
2
 
 
3
import java.io.*;
 
4
import java.util.*;
 
5
 
 
6
import com.fasterxml.jackson.databind.ObjectMapper;
 
7
 
 
8
public class GeneratorSimpleTest extends CBORTestBase
 
9
{
 
10
    private final ObjectMapper MAPPER = cborMapper();
 
11
    
 
12
    /**
 
13
     * Test for verifying handling of 'true', 'false' and 'null' literals
 
14
     */
 
15
    public void testSimpleLiterals() throws Exception
 
16
    {
 
17
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
18
        CBORGenerator gen = cborGenerator(out);
 
19
        gen.writeBoolean(true);
 
20
        gen.close();
 
21
        _verifyBytes(out.toByteArray(), CBORConstants.BYTE_TRUE);
 
22
 
 
23
        out = new ByteArrayOutputStream();
 
24
        gen = cborGenerator(out);
 
25
        gen.writeBoolean(false);
 
26
        gen.close();
 
27
        _verifyBytes(out.toByteArray(), CBORConstants.BYTE_FALSE);
 
28
 
 
29
        out = new ByteArrayOutputStream();
 
30
        gen = cborGenerator(out);
 
31
        gen.writeNull();
 
32
        gen.close();
 
33
        _verifyBytes(out.toByteArray(), CBORConstants.BYTE_NULL);
 
34
    }
 
35
 
 
36
    public void testMinimalIntValues() throws Exception
 
37
    {
 
38
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
39
        CBORGenerator gen = cborGenerator(out);
 
40
        assertTrue(gen.isEnabled(CBORGenerator.Feature.WRITE_MINIMAL_INTS));
 
41
        gen.writeNumber(17);
 
42
        gen.close();
 
43
        _verifyBytes(out.toByteArray(),
 
44
                (byte) (CBORConstants.PREFIX_TYPE_INT_POS + 17));
 
45
 
 
46
        // then without minimal
 
47
        out = new ByteArrayOutputStream();
 
48
        gen = cborGenerator(out);
 
49
        gen.disable(CBORGenerator.Feature.WRITE_MINIMAL_INTS);
 
50
        gen.writeNumber(17);
 
51
        gen.close();
 
52
        _verifyBytes(out.toByteArray(),
 
53
                (byte) (CBORConstants.PREFIX_TYPE_INT_POS + 26),
 
54
                (byte) 0, (byte) 0, (byte) 0, (byte) 17);
 
55
 
 
56
        out = new ByteArrayOutputStream();
 
57
        gen = cborGenerator(out);
 
58
        gen.writeNumber(Integer.MAX_VALUE);
 
59
        gen.close();
 
60
        _verifyBytes(out.toByteArray(),
 
61
                (byte) (CBORConstants.PREFIX_TYPE_INT_POS + 26),
 
62
                (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF);
 
63
 
 
64
        out = new ByteArrayOutputStream();
 
65
        gen = cborGenerator(out);
 
66
        gen.writeNumber(Integer.MIN_VALUE);
 
67
        gen.close();
 
68
        _verifyBytes(out.toByteArray(),
 
69
                (byte) (CBORConstants.PREFIX_TYPE_INT_NEG + 26),
 
70
                (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF);
 
71
    }
 
72
 
 
73
    public void testIntValues() throws Exception
 
74
    {
 
75
        // first, single-byte
 
76
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
77
        CBORGenerator gen = cborGenerator(out);
 
78
        gen.writeNumber(13);
 
79
        gen.close();
 
80
        _verifyBytes(out.toByteArray(),
 
81
                (byte) (CBORConstants.PREFIX_TYPE_INT_POS + 13));
 
82
 
 
83
        out = new ByteArrayOutputStream();
 
84
        gen = cborGenerator(out);
 
85
        gen.writeNumber(-13);
 
86
        gen.close();
 
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));
 
90
 
 
91
        // then two byte
 
92
        out = new ByteArrayOutputStream();
 
93
        gen = cborGenerator(out);
 
94
        gen.writeNumber(0xFF);
 
95
        gen.close();
 
96
        _verifyBytes(out.toByteArray(),
 
97
                (byte) (CBORConstants.PREFIX_TYPE_INT_POS + 24), (byte) 0xFF);
 
98
 
 
99
        out = new ByteArrayOutputStream();
 
100
        gen = cborGenerator(out);
 
101
        gen.writeNumber(-256);
 
102
        gen.close();
 
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);
 
106
 
 
107
        // and three byte
 
108
        out = new ByteArrayOutputStream();
 
109
        gen = cborGenerator(out);
 
110
        gen.writeNumber(0xFEDC);
 
111
        gen.close();
 
112
        _verifyBytes(out.toByteArray(),
 
113
                (byte) (CBORConstants.PREFIX_TYPE_INT_POS + 25), (byte) 0xFE, (byte) 0xDC);
 
114
 
 
115
        out = new ByteArrayOutputStream();
 
116
        gen = cborGenerator(out);
 
117
        gen.writeNumber(-0xFFFE);
 
118
        gen.close();
 
119
        _verifyBytes(out.toByteArray(),
 
120
                (byte) (CBORConstants.PREFIX_TYPE_INT_NEG + 25), (byte) 0xFF, (byte) 0xFD);
 
121
 
 
122
        out = new ByteArrayOutputStream();
 
123
        gen = cborGenerator(out);
 
124
        gen.writeNumber(Integer.MAX_VALUE);
 
125
        gen.close();
 
126
        _verifyBytes(out.toByteArray(),
 
127
                (byte) (CBORConstants.PREFIX_TYPE_INT_POS + 26),
 
128
                (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF);
 
129
 
 
130
        out = new ByteArrayOutputStream();
 
131
        gen = cborGenerator(out);
 
132
        gen.writeNumber(Integer.MIN_VALUE);
 
133
        gen.close();
 
134
        _verifyBytes(out.toByteArray(),
 
135
                (byte) (CBORConstants.PREFIX_TYPE_INT_NEG + 26),
 
136
                (byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF);
 
137
    }
 
138
 
 
139
    public void testLongValues() throws Exception
 
140
    {
 
141
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
142
        CBORGenerator gen = cborGenerator(out);
 
143
        long l = -1L + Integer.MIN_VALUE;
 
144
        gen.writeNumber(l);
 
145
        gen.close();
 
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]);
 
153
    }
 
154
    
 
155
    public void testFloatValues() throws Exception
 
156
    {
 
157
        // first, 32-bit float
 
158
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
159
        CBORGenerator gen = cborGenerator(out);
 
160
        float f = 1.25f;
 
161
        gen.writeNumber(f);
 
162
        gen.close();
 
163
        int raw = Float.floatToIntBits(f);
 
164
        _verifyBytes(out.toByteArray(),
 
165
                (byte) (CBORConstants.BYTE_FLOAT32),
 
166
                (byte) (raw >> 24),
 
167
                (byte) (raw >> 16),
 
168
                (byte) (raw >> 8),
 
169
                (byte) raw);
 
170
 
 
171
        // then 64-bit double
 
172
        out = new ByteArrayOutputStream();
 
173
        gen = cborGenerator(out);
 
174
        double d = 0.75f;
 
175
        gen.writeNumber(d);
 
176
        gen.close();
 
177
        long rawL = Double.doubleToLongBits(d);
 
178
        _verifyBytes(out.toByteArray(),
 
179
                (byte) (CBORConstants.BYTE_FLOAT64),
 
180
                (byte) (rawL >> 56),
 
181
                (byte) (rawL >> 48),
 
182
                (byte) (rawL >> 40),
 
183
                (byte) (rawL >> 32),
 
184
                (byte) (rawL >> 24),
 
185
                (byte) (rawL >> 16),
 
186
                (byte) (rawL >> 8),
 
187
                (byte) rawL);
 
188
    }
 
189
 
 
190
    public void testEmptyArray() throws Exception
 
191
    {
 
192
        // First: empty array (2 bytes)
 
193
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
194
        CBORGenerator gen = cborGenerator(out);
 
195
        gen.writeStartArray();
 
196
        gen.writeEndArray();
 
197
        gen.close();
 
198
        _verifyBytes(out.toByteArray(), CBORConstants.BYTE_ARRAY_INDEFINITE,
 
199
               CBORConstants.BYTE_BREAK);
 
200
    }
 
201
 
 
202
    public void testEmptyObject() throws Exception
 
203
    {
 
204
        // First: empty array (2 bytes)
 
205
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
206
        CBORGenerator gen = cborGenerator(out);
 
207
        gen.writeStartObject();
 
208
        gen.writeEndObject();
 
209
        gen.close();
 
210
        _verifyBytes(out.toByteArray(), CBORConstants.BYTE_OBJECT_INDEFINITE,
 
211
               CBORConstants.BYTE_BREAK);
 
212
    }
 
213
    
 
214
    public void testIntArray() throws Exception
 
215
    {
 
216
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
217
        CBORGenerator gen = cborGenerator(out);
 
218
        
 
219
        // currently will produce indefinite-length array
 
220
        gen.writeStartArray();
 
221
        gen.writeNumber(1);
 
222
        gen.writeNumber(2);
 
223
        gen.writeNumber(3);
 
224
        gen.writeEndArray();
 
225
        gen.close();
 
226
        
 
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
 
233
        };
 
234
        
 
235
        _verifyBytes(out.toByteArray(), EXP);
 
236
 
 
237
        // Also, data-binding should produce identical
 
238
        byte[] b = MAPPER.writeValueAsBytes(new int[] { 1, 2, 3 });
 
239
        _verifyBytes(b, EXP);
 
240
    }
 
241
 
 
242
    public void testTrivialObject() throws Exception
 
243
    {
 
244
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
245
        CBORGenerator gen = cborGenerator(out);
 
246
        
 
247
        // currently will produce indefinite-length Object
 
248
        gen.writeStartObject();
 
249
        gen.writeNumberField("a", 1);
 
250
        gen.writeNumberField("b", 2);
 
251
        gen.writeEndObject();
 
252
        gen.close();
 
253
 
 
254
        final byte[] EXP = new byte[] {
 
255
                CBORConstants.BYTE_OBJECT_INDEFINITE,
 
256
                (byte) (CBORConstants.PREFIX_TYPE_TEXT + 1),
 
257
                (byte) 'a',
 
258
                (byte) (CBORConstants.PREFIX_TYPE_INT_POS + 1),
 
259
                (byte) (CBORConstants.PREFIX_TYPE_TEXT + 1),
 
260
                (byte) 'b',
 
261
                (byte) (CBORConstants.PREFIX_TYPE_INT_POS + 2),
 
262
                CBORConstants.BYTE_BREAK
 
263
        };
 
264
 
 
265
        _verifyBytes(out.toByteArray(), EXP);
 
266
        Map<String,Integer> map = new LinkedHashMap<String,Integer>();
 
267
        map.put("a", 1);
 
268
        map.put("b", 2);
 
269
        byte[] b = MAPPER.writeValueAsBytes(map);
 
270
        _verifyBytes(b, EXP);
 
271
    }
 
272
    
 
273
    public void testShortText() throws Exception
 
274
    {
 
275
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
276
        CBORGenerator gen = cborGenerator(out);
 
277
        gen.writeString("");
 
278
        gen.close();
 
279
        _verifyBytes(out.toByteArray(), CBORConstants.BYTE_EMPTY_STRING);
 
280
 
 
281
        out = new ByteArrayOutputStream();
 
282
        gen = cborGenerator(out);
 
283
        gen.writeString("abc");
 
284
        gen.close();
 
285
        _verifyBytes(out.toByteArray(), (byte) (CBORConstants.PREFIX_TYPE_TEXT + 3),
 
286
                (byte) 'a', (byte) 'b', (byte) 'c');
 
287
    }
 
288
    
 
289
    public void testLongerText() throws Exception
 
290
    {
 
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);
 
296
        gen.close();
 
297
        byte[] b = SHORT_ASCII.getBytes("UTF-8");
 
298
        int len = b.length;
 
299
        _verifyBytes(out.toByteArray(),
 
300
                (byte) (CBORConstants.PREFIX_TYPE_TEXT + 24), (byte) len, b);
 
301
 
 
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);
 
307
        gen.close();
 
308
        b = SHORT_UNICODE.getBytes("UTF-8");
 
309
        len = b.length;
 
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);
 
314
 
 
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);
 
320
        gen.close();
 
321
        b = MEDIUM_UNICODE.getBytes("UTF-8");
 
322
        len = b.length;
 
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,
 
328
                b);
 
329
    }
 
330
 
 
331
}