Skip to content

Commit 77fa454

Browse files
committed
start work on #333
1 parent f5cbda5 commit 77fa454

11 files changed

+236
-16
lines changed

src/test/java/com/fasterxml/jackson/dataformat/xml/MapperCopyTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static class Pojo282
1919

2020
public void testMapperCopy()
2121
{
22-
XmlMapper mapper1 = objectMapperBuilder()
22+
XmlMapper mapper1 = mapperBuilder()
2323
.nameForTextElement("foo")
2424
.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true)
2525
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
@@ -55,7 +55,7 @@ public void testSerializerProviderCopy() {
5555

5656
public void testMapperSerialization() throws Exception
5757
{
58-
XmlMapper mapper1 = objectMapperBuilder()
58+
XmlMapper mapper1 = mapperBuilder()
5959
.nameForTextElement("foo")
6060
.build();
6161
assertEquals("foo", mapper1.getFactory().getXMLTextElementName());

src/test/java/com/fasterxml/jackson/dataformat/xml/XmlTestBase.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fasterxml.jackson.dataformat.xml;
22

33
import java.io.*;
4+
import java.nio.charset.StandardCharsets;
45
import java.util.Arrays;
56

67
import junit.framework.TestCase;
@@ -208,14 +209,22 @@ protected XmlTestBase() {
208209
super();
209210
}
210211

212+
protected XmlFactoryBuilder streamFactoryBuilder() {
213+
return XmlFactory.builder();
214+
}
215+
211216
protected static XmlMapper newMapper() {
212217
return new XmlMapper();
213218
}
214219

215-
protected static XmlMapper.Builder objectMapperBuilder() {
220+
protected static XmlMapper.Builder mapperBuilder() {
216221
return XmlMapper.builder();
217222
}
218-
223+
224+
protected static XmlMapper.Builder mapperBuilder(XmlFactory f) {
225+
return XmlMapper.builder(f);
226+
}
227+
219228
protected XmlMapper xmlMapper(boolean useListWrapping)
220229
{
221230
JacksonXmlModule module = new JacksonXmlModule();
@@ -295,11 +304,7 @@ protected static String aposToQuotes(String json) {
295304
}
296305

297306
protected byte[] utf8Bytes(String str) {
298-
try {
299-
return str.getBytes("UTF-8");
300-
} catch (IOException e) {
301-
throw new IllegalArgumentException(e);
302-
}
307+
return str.getBytes(StandardCharsets.UTF_8);
303308
}
304309

305310
/**

src/test/java/com/fasterxml/jackson/dataformat/xml/deser/CaseInsensitiveDeserTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public InsensitiveCreator(@JsonProperty("value") int v0) {
3232

3333
private final ObjectMapper MAPPER = newMapper();
3434

35-
private final ObjectMapper INSENSITIVE_MAPPER = objectMapperBuilder()
35+
private final ObjectMapper INSENSITIVE_MAPPER = mapperBuilder()
3636
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
3737
.build();
3838

src/test/java/com/fasterxml/jackson/dataformat/xml/deser/Issue274PropertyNameTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static class RootObject {
4848
// [dataformat-xml#274]
4949
public void testIssue274() throws Exception
5050
{
51-
final ObjectMapper xm = objectMapperBuilder()
51+
final ObjectMapper xm = mapperBuilder()
5252

5353
// serialization features
5454
// xm.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

src/test/java/com/fasterxml/jackson/dataformat/xml/failing/CaseInsensitiveDeser273Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void setName(String n) {
5151
/********************************************************
5252
*/
5353

54-
private final ObjectMapper INSENSITIVE_MAPPER = objectMapperBuilder()
54+
private final ObjectMapper INSENSITIVE_MAPPER = mapperBuilder()
5555
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
5656
.build();
5757

src/test/java/com/fasterxml/jackson/dataformat/xml/failing/DefaultTyping325Test.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static class Simple {
1919

2020
public void testCanSerialize() throws IOException
2121
{
22-
ObjectMapper mapper = objectMapperBuilder()
22+
ObjectMapper mapper = mapperBuilder()
2323
.build();
2424
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT);
2525

@@ -28,7 +28,6 @@ public void testCanSerialize() throws IOException
2828
s.setList(Arrays.asList("foo", "bar"));
2929

3030
String doc = mapper.writeValueAsString(s);
31-
System.err.println("DOC: "+doc);
3231
Simple result = mapper.readValue(doc, Simple.class);
3332
assertNotNull(result.list);
3433
assertEquals(2, result.list.size());

src/test/java/com/fasterxml/jackson/dataformat/xml/jaxb/BuilderWithJAXB291Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void testBuilder291() throws Exception
121121
XmlJaxbAnnotationIntrospector xmlIntr = new XmlJaxbAnnotationIntrospector(TypeFactory.defaultInstance());
122122
AnnotationIntrospector intr = XmlAnnotationIntrospector.Pair.instance
123123
(xmlIntr, new JacksonAnnotationIntrospector());
124-
XmlMapper mapper = objectMapperBuilder()
124+
XmlMapper mapper = mapperBuilder()
125125
.annotationIntrospector(intr)
126126
.build();
127127
Address value = mapper.readValue(DOC, Address.class);

src/test/java/com/fasterxml/jackson/dataformat/xml/lists/NestedUnwrappedListsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static class VehicleActivity {
3232
/**********************************************************************
3333
*/
3434

35-
protected XmlMapper _xmlMapper = objectMapperBuilder()
35+
protected XmlMapper _xmlMapper = mapperBuilder()
3636
.propertyNamingStrategy(new PropertyNamingStrategy.UpperCamelCaseStrategy())
3737
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
3838
.defaultUseWrapper(false)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.fasterxml.jackson.dataformat.xml.misc;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
import com.fasterxml.jackson.annotation.JsonRootName;
7+
import com.fasterxml.jackson.dataformat.xml.*;
8+
import com.fasterxml.jackson.dataformat.xml.testutil.PrefixInputDecorator;
9+
import com.fasterxml.jackson.dataformat.xml.testutil.PrefixOutputDecorator;
10+
11+
public class StreamingDecoratorsTest extends XmlTestBase
12+
{
13+
@JsonRootName("wrapper")
14+
static class Value {
15+
public String value = "all";
16+
}
17+
18+
@SuppressWarnings("unchecked")
19+
public void testInputDecorators() throws IOException
20+
{
21+
final byte[] DOC = utf8Bytes("<secret: mum\n");
22+
final XmlMapper mapper = mapperBuilder(
23+
streamFactoryBuilder().inputDecorator(new PrefixInputDecorator(DOC))
24+
.build())
25+
.build();
26+
Map<String,Object> value = mapper.readValue(utf8Bytes("value: foo\n"), Map.class);
27+
assertEquals(2, value.size());
28+
assertEquals("foo", value.get("value"));
29+
assertEquals("mum", value.get("secret"));
30+
31+
// and then via Reader as well
32+
value = mapper.readValue(new StringReader("value: xyz\n"), Map.class);
33+
assertEquals(2, value.size());
34+
assertEquals("xyz", value.get("value"));
35+
assertEquals("mum", value.get("secret"));
36+
}
37+
38+
public void testOutputDecorators() throws IOException
39+
{
40+
final String PREFIX = "///////";
41+
final byte[] DOC = utf8Bytes(PREFIX);
42+
final XmlMapper mapper = mapperBuilder(
43+
streamFactoryBuilder().outputDecorator(new PrefixOutputDecorator(DOC))
44+
.build())
45+
.build();
46+
final Value input = new Value();
47+
48+
// Gets bit tricky because writer will add doc prefix. So let's do simpler check here
49+
50+
try (ByteArrayOutputStream bytes = new ByteArrayOutputStream()) {
51+
mapper.writeValue(bytes, input);
52+
53+
String raw = bytes.toString("UTF-8");
54+
if (!raw.startsWith(PREFIX)) {
55+
fail("Should start with prefix, did not: ["+raw+"]");
56+
}
57+
}
58+
59+
// and same with char-backed too
60+
try (StringWriter sw = new StringWriter()) {
61+
mapper.writeValue(sw, input);
62+
String raw = sw.toString();
63+
if (!raw.startsWith(PREFIX)) {
64+
fail("Should start with prefix, did not: ["+raw+"]");
65+
}
66+
}
67+
}
68+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.fasterxml.jackson.dataformat.xml.testutil;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.Reader;
7+
import java.io.SequenceInputStream;
8+
import java.io.StringReader;
9+
import java.nio.charset.StandardCharsets;
10+
11+
import com.fasterxml.jackson.core.io.IOContext;
12+
import com.fasterxml.jackson.core.io.InputDecorator;
13+
14+
@SuppressWarnings("serial")
15+
public class PrefixInputDecorator extends InputDecorator
16+
{
17+
protected final byte[] _prefix;
18+
19+
public PrefixInputDecorator(byte[] p) {
20+
_prefix = p;
21+
}
22+
23+
@Override
24+
public InputStream decorate(IOContext ctxt, InputStream in) {
25+
if (in instanceof MySequenceInputStream) {
26+
throw new IllegalStateException("Trying to decorate MySequenceInputStream (double-decoration!)");
27+
}
28+
return new MySequenceInputStream(new ByteArrayInputStream(_prefix), in);
29+
}
30+
31+
@Override
32+
public InputStream decorate(IOContext ctxt, byte[] src, int offset, int length) {
33+
return decorate(ctxt, new ByteArrayInputStream(src, offset, length));
34+
}
35+
36+
@Override
37+
public Reader decorate(IOContext ctxt, Reader r) throws IOException {
38+
if (r instanceof SequenceReader) {
39+
throw new IllegalStateException("Trying to decorate SequenceReader (double-decoration!)");
40+
}
41+
String str = new String(_prefix, StandardCharsets.UTF_8);
42+
return new SequenceReader(new StringReader(str), r);
43+
}
44+
45+
// sub-class only so we can check for "double decoration"
46+
static class MySequenceInputStream extends SequenceInputStream {
47+
public MySequenceInputStream(InputStream in1, InputStream in2) {
48+
super(in1, in2);
49+
}
50+
}
51+
52+
static class SequenceReader extends Reader {
53+
protected Reader _reader1, _reader2;
54+
55+
public SequenceReader(Reader r1, Reader r2) {
56+
_reader1 = r1;
57+
_reader2 = r2;
58+
}
59+
60+
@Override
61+
public int read(char[] cbuf, int off, int len) throws IOException {
62+
if (_reader1 != null) {
63+
int count = _reader1.read(cbuf, off, len);
64+
if (count > 0) {
65+
return count;
66+
}
67+
_reader1 = null;
68+
}
69+
if (_reader2 != null) {
70+
int count = _reader2.read(cbuf, off, len);
71+
if (count > 0) {
72+
return count;
73+
}
74+
_reader2 = null;
75+
}
76+
return -1;
77+
}
78+
79+
@Override
80+
public void close() throws IOException {
81+
_reader1 = _reader2 = null;
82+
}
83+
}
84+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.fasterxml.jackson.dataformat.xml.testutil;
2+
3+
import java.io.FilterOutputStream;
4+
import java.io.IOException;
5+
import java.io.OutputStream;
6+
import java.io.Writer;
7+
8+
import com.fasterxml.jackson.core.io.IOContext;
9+
import com.fasterxml.jackson.core.io.OutputDecorator;
10+
11+
@SuppressWarnings("serial")
12+
public class PrefixOutputDecorator extends OutputDecorator
13+
{
14+
protected final byte[] _prefix;
15+
16+
public PrefixOutputDecorator(byte[] p) {
17+
_prefix = p;
18+
}
19+
20+
@Override
21+
public OutputStream decorate(IOContext ctxt, OutputStream out)
22+
throws IOException
23+
{
24+
if (out instanceof BufferedOut) {
25+
throw new IllegalStateException("Trying to decorate `Buffered` (double-decoration!)");
26+
}
27+
return new BufferedOut(out, _prefix);
28+
}
29+
30+
@Override
31+
public Writer decorate(IOContext ctxt, Writer w) throws IOException {
32+
for (byte b : _prefix) {
33+
w.write((char) (b & 0xFF));
34+
}
35+
return w;
36+
}
37+
38+
static class BufferedOut extends FilterOutputStream {
39+
protected byte[] _prefix;
40+
41+
public BufferedOut(OutputStream b, byte[] prefix) {
42+
super(b);
43+
_prefix = prefix;
44+
}
45+
46+
@Override
47+
public void write(int b) throws IOException {
48+
if (_prefix != null) {
49+
out.write(_prefix);
50+
_prefix = null;
51+
}
52+
super.write(b);
53+
}
54+
55+
@Override
56+
public void write(byte[] b, int offset, int len) throws IOException {
57+
if (_prefix != null) {
58+
out.write(_prefix);
59+
_prefix = null;
60+
}
61+
super.write(b, offset, len);
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)