Debian Codegraph @

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

raw
1 package com.fasterxml.jackson.dataformat.smile;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.util.*;
6
7 import org.junit.Assert;
8
9 import com.fasterxml.jackson.core.*;
10 import com.fasterxml.jackson.databind.JsonNode;
11 import com.fasterxml.jackson.databind.ObjectMapper;
12
13 public class TestMapper extends SmileTestBase
14 {
15 static class BytesBean {
16 public byte[] bytes;
17
18 public BytesBean() { }
19 public BytesBean(byte[] b) { bytes = b; }
20 }
21
22 /*
23 /**********************************************************
24 /* Test methods
25 /**********************************************************
26 */
27
28 private final ObjectMapper MAPPER = smileMapper();
29
30 // [JACKSON-733]
31 public void testBinary() throws IOException
32 {
33 byte[] input = new byte[] { 1, 2, 3, -1, 8, 0, 42 };
34 byte[] smile = MAPPER.writeValueAsBytes(new BytesBean(input));
35 BytesBean result = MAPPER.readValue(smile, BytesBean.class);
36
37 assertNotNull(result.bytes);
38 Assert.assertArrayEquals(input, result.bytes);
39 }
40
41 // @since 2.1
42 public void testCopy() throws IOException
43 {
44 ObjectMapper mapper1 = smileMapper();
45 ObjectMapper mapper2 = mapper1.copy();
46
47 assertNotSame(mapper1, mapper2);
48 assertNotSame(mapper1.getFactory(), mapper2.getFactory());
49 assertEquals(SmileFactory.class, mapper2.getFactory().getClass());
50 }
51
52 // UUIDs should be written as binary (starting with 2.3)
53 public void testUUIDs() throws IOException
54 {
55 UUID uuid = UUID.randomUUID();
56 byte[] bytes = MAPPER.writeValueAsBytes(uuid);
57
58 // first, just verify we can decode it
59 UUID out = MAPPER.readValue(bytes, UUID.class);
60 assertEquals(uuid, out);
61
62 // then verify it comes back as binary
63 JsonParser p = MAPPER.getFactory().createParser(bytes);
64 assertEquals(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
65 byte[] b = p.getBinaryValue();
66 assertNotNull(b);
67 assertEquals(16, b.length);
68 p.close();
69 }
70
71 public void testWithNestedMaps() throws IOException
72 {
73 Map<Object,Object> map = new HashMap<Object,Object>();
74 map.put("foo", Collections.singletonMap("", "bar"));
75 byte[] bytes = MAPPER.writeValueAsBytes(map);
76 JsonNode n = MAPPER.readTree(new ByteArrayInputStream(bytes));
77 assertTrue(n.isObject());
78 assertEquals(1, n.size());
79 JsonNode n2 = n.get("foo");
80 assertNotNull(n2);
81 assertEquals(1, n2.size());
82 JsonNode n3 = n2.get("");
83 assertNotNull(n3);
84 assertTrue(n3.isTextual());
85 }
86 }
87