Skip to content

Commit a9dbcc3

Browse files
committed
Add tests to verify that #238 works
1 parent ef0343b commit a9dbcc3

File tree

6 files changed

+295
-4
lines changed

6 files changed

+295
-4
lines changed

cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/CBORTestBase.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,35 @@ protected static String generateLongAsciiString(int length, Random rnd)
247247
*/
248248

249249
protected static String aposToQuotes(String str) {
250+
return a2q(str);
251+
}
252+
253+
protected static String a2q(String str) {
250254
return str.replace("'", "\"");
251255
}
252-
253-
protected static String quote(String str) {
256+
257+
public String quote(String str) {
258+
return q(str);
259+
}
260+
261+
public String q(String str) {
254262
return '"'+str+'"';
255263
}
264+
265+
protected static byte[] concat(byte[] ... chunks)
266+
{
267+
int len = 0;
268+
for (byte[] chunk : chunks) {
269+
len += chunk.length;
270+
}
271+
ByteArrayOutputStream bout = new ByteArrayOutputStream(len);
272+
for (byte[] chunk : chunks) {
273+
try {
274+
bout.write(chunk);
275+
} catch (IOException e) {
276+
throw new RuntimeException(e);
277+
}
278+
}
279+
return bout.toByteArray();
280+
}
256281
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.fasterxml.jackson.dataformat.cbor.seq;
2+
3+
import java.util.List;
4+
5+
import com.fasterxml.jackson.databind.*;
6+
7+
import com.fasterxml.jackson.dataformat.cbor.CBORTestBase;
8+
9+
public class ReadTreesTest extends CBORTestBase
10+
{
11+
private final ObjectMapper MAPPER = cborMapper();
12+
13+
static class IdValue {
14+
public int id, value;
15+
}
16+
17+
/*
18+
/**********************************************************
19+
/* Unit tests; happy case
20+
/**********************************************************
21+
*/
22+
23+
public void testReadTreeSequence() throws Exception
24+
{
25+
final byte[] INPUT = concat(
26+
cborDoc(a2q("{\"id\":1, \"value\":137 }")),
27+
cborDoc(a2q("{\"id\":2, \"value\":256 }\n")),
28+
cborDoc(a2q("{\"id\":3, \"value\":-89 }"))
29+
);
30+
//System.err.println("DEBUG/cbor: byte length = "+INPUT.length);
31+
try (MappingIterator<JsonNode> it = MAPPER.readerFor(JsonNode.class)
32+
.readValues(INPUT)) {
33+
assertTrue(it.hasNextValue());
34+
JsonNode node = it.nextValue();
35+
assertEquals("{\"id\":1,\"value\":137}", node.toString());
36+
assertEquals(1, node.path("id").intValue());
37+
assertEquals(1, node.path("id").asInt());
38+
39+
assertTrue(it.hasNextValue());
40+
node = it.nextValue();
41+
assertEquals("{\"id\":2,\"value\":256}", node.toString());
42+
43+
assertTrue(it.hasNextValue());
44+
node = it.nextValue();
45+
assertEquals("{\"id\":3,\"value\":-89}", node.toString());
46+
47+
assertFalse(it.hasNextValue());
48+
}
49+
50+
// Or with "readAll()":
51+
try (MappingIterator<JsonNode> it = MAPPER.readerFor(JsonNode.class)
52+
.readValues(INPUT)) {
53+
List<JsonNode> all = it.readAll();
54+
assertEquals(3, all.size());
55+
assertEquals("{\"id\":3,\"value\":-89}", all.get(2).toString());
56+
}
57+
}
58+
59+
/*
60+
/**********************************************************
61+
/* (note: no error recovery unlike in JSON tests, should
62+
/* not differ)
63+
/**********************************************************
64+
*/
65+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fasterxml.jackson.dataformat.cbor.seq;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.StringWriter;
5+
6+
import com.fasterxml.jackson.databind.JsonNode;
7+
import com.fasterxml.jackson.databind.MappingIterator;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import com.fasterxml.jackson.databind.SequenceWriter;
10+
11+
import com.fasterxml.jackson.dataformat.cbor.CBORTestBase;
12+
13+
public class SequenceWriterTest extends CBORTestBase
14+
{
15+
private final ObjectMapper MAPPER = cborMapper();
16+
17+
static class IdValue {
18+
public int id, value;
19+
20+
public IdValue(int id, int value) {
21+
this.id = id;
22+
this.value = value;
23+
}
24+
}
25+
26+
/*
27+
/**********************************************************
28+
/* Unit tests; happy case
29+
/**********************************************************
30+
*/
31+
32+
public void testSimpleSeqWrite() throws Exception
33+
{
34+
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
35+
try (SequenceWriter seq = MAPPER.writer()
36+
.writeValues(bytes)) {
37+
seq.write(new IdValue(1, 15))
38+
.write(new IdValue(2, 16))
39+
.write(new IdValue(3, -999));
40+
}
41+
42+
try (MappingIterator<JsonNode> it = MAPPER.readerFor(JsonNode.class)
43+
.readValues(bytes.toByteArray())) {
44+
assertTrue(it.hasNext());
45+
assertEquals(a2q("{'id':1,'value':15}"), it.nextValue().toString());
46+
assertTrue(it.hasNext());
47+
assertEquals(a2q("{'id':2,'value':16}"), it.nextValue().toString());
48+
assertTrue(it.hasNext());
49+
assertEquals(a2q("{'id':3,'value':-999}"), it.nextValue().toString());
50+
assertFalse(it.hasNext());
51+
}
52+
}
53+
}

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,36 @@ protected String getAndVerifyText(JsonParser p) throws IOException
223223
/**********************************************************
224224
*/
225225

226+
protected static String aposToQuotes(String str) {
227+
return a2q(str);
228+
}
229+
230+
protected static String a2q(String str) {
231+
return str.replace("'", "\"");
232+
}
233+
226234
public String quote(String str) {
235+
return q(str);
236+
}
237+
238+
public String q(String str) {
227239
return '"'+str+'"';
228240
}
229241

230-
protected static String aposToQuotes(String str) {
231-
return str.replace("'", "\"");
242+
protected static byte[] concat(byte[] ... chunks)
243+
{
244+
int len = 0;
245+
for (byte[] chunk : chunks) {
246+
len += chunk.length;
247+
}
248+
ByteArrayOutputStream bout = new ByteArrayOutputStream(len);
249+
for (byte[] chunk : chunks) {
250+
try {
251+
bout.write(chunk);
252+
} catch (IOException e) {
253+
throw new RuntimeException(e);
254+
}
255+
}
256+
return bout.toByteArray();
232257
}
233258
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.fasterxml.jackson.dataformat.smile.seq;
2+
3+
import java.util.List;
4+
5+
import com.fasterxml.jackson.databind.*;
6+
7+
import com.fasterxml.jackson.dataformat.smile.BaseTestForSmile;
8+
9+
public class ReadTreesTest extends BaseTestForSmile
10+
{
11+
private final ObjectMapper MAPPER = smileMapper();
12+
13+
static class IdValue {
14+
public int id, value;
15+
}
16+
17+
/*
18+
/**********************************************************
19+
/* Unit tests; happy case
20+
/**********************************************************
21+
*/
22+
23+
public void testReadTreeSequence() throws Exception {
24+
_testReadTreeSequence(true);
25+
_testReadTreeSequence(false);
26+
}
27+
28+
public void _testReadTreeSequence(boolean writeHeader) throws Exception
29+
{
30+
final byte[] INPUT = concat(
31+
_smileDoc(a2q("{\"id\":1, \"value\":137 }"), writeHeader),
32+
_smileDoc(a2q("{\"id\":2, \"value\":256 }"), writeHeader),
33+
_smileDoc(a2q("{\"id\":3, \"value\":-89 }"), writeHeader)
34+
);
35+
//System.err.println("DEBUG/Smile: byte length = "+INPUT.length);
36+
37+
try (MappingIterator<JsonNode> it = MAPPER.readerFor(JsonNode.class)
38+
.readValues(INPUT)) {
39+
assertTrue(it.hasNextValue());
40+
JsonNode node = it.nextValue();
41+
assertEquals("{\"id\":1,\"value\":137}", node.toString());
42+
assertEquals(1, node.path("id").intValue());
43+
assertEquals(1, node.path("id").asInt());
44+
45+
assertTrue(it.hasNextValue());
46+
node = it.nextValue();
47+
assertEquals("{\"id\":2,\"value\":256}", node.toString());
48+
49+
assertTrue(it.hasNextValue());
50+
node = it.nextValue();
51+
assertEquals("{\"id\":3,\"value\":-89}", node.toString());
52+
53+
assertFalse(it.hasNextValue());
54+
}
55+
56+
// Or with "readAll()":
57+
try (MappingIterator<JsonNode> it = MAPPER.readerFor(JsonNode.class)
58+
.readValues(INPUT)) {
59+
List<JsonNode> all = it.readAll();
60+
assertEquals(3, all.size());
61+
assertEquals("{\"id\":3,\"value\":-89}", all.get(2).toString());
62+
}
63+
}
64+
65+
/*
66+
/**********************************************************
67+
/* (note: no error recovery unlike in JSON tests, should
68+
/* not differ)
69+
/**********************************************************
70+
*/
71+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fasterxml.jackson.dataformat.smile.seq;
2+
3+
import java.io.ByteArrayOutputStream;
4+
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.MappingIterator;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.databind.SequenceWriter;
9+
10+
import com.fasterxml.jackson.dataformat.smile.BaseTestForSmile;
11+
12+
public class SequenceWriterTest extends BaseTestForSmile
13+
{
14+
private final ObjectMapper MAPPER = smileMapper();
15+
16+
static class IdValue {
17+
public int id, value;
18+
19+
public IdValue(int id, int value) {
20+
this.id = id;
21+
this.value = value;
22+
}
23+
}
24+
25+
/*
26+
/**********************************************************
27+
/* Unit tests; happy case
28+
/**********************************************************
29+
*/
30+
31+
public void testSimpleSeqWrite() throws Exception
32+
{
33+
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
34+
try (SequenceWriter seq = MAPPER.writer()
35+
.writeValues(bytes)) {
36+
seq.write(new IdValue(1, 15))
37+
.write(new IdValue(2, 16))
38+
.write(new IdValue(3, -999));
39+
}
40+
41+
try (MappingIterator<JsonNode> it = MAPPER.readerFor(JsonNode.class)
42+
.readValues(bytes.toByteArray())) {
43+
assertTrue(it.hasNext());
44+
assertEquals(a2q("{'id':1,'value':15}"), it.nextValue().toString());
45+
assertTrue(it.hasNext());
46+
assertEquals(a2q("{'id':2,'value':16}"), it.nextValue().toString());
47+
assertTrue(it.hasNext());
48+
assertEquals(a2q("{'id':3,'value':-999}"), it.nextValue().toString());
49+
assertFalse(it.hasNext());
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)