Skip to content

Commit

Permalink
start work on #333
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 26, 2019
1 parent f5cbda5 commit 77fa454
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static class Pojo282

public void testMapperCopy()
{
XmlMapper mapper1 = objectMapperBuilder()
XmlMapper mapper1 = mapperBuilder()
.nameForTextElement("foo")
.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true)
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
Expand Down Expand Up @@ -55,7 +55,7 @@ public void testSerializerProviderCopy() {

public void testMapperSerialization() throws Exception
{
XmlMapper mapper1 = objectMapperBuilder()
XmlMapper mapper1 = mapperBuilder()
.nameForTextElement("foo")
.build();
assertEquals("foo", mapper1.getFactory().getXMLTextElementName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fasterxml.jackson.dataformat.xml;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import junit.framework.TestCase;
Expand Down Expand Up @@ -208,14 +209,22 @@ protected XmlTestBase() {
super();
}

protected XmlFactoryBuilder streamFactoryBuilder() {
return XmlFactory.builder();
}

protected static XmlMapper newMapper() {
return new XmlMapper();
}

protected static XmlMapper.Builder objectMapperBuilder() {
protected static XmlMapper.Builder mapperBuilder() {
return XmlMapper.builder();
}


protected static XmlMapper.Builder mapperBuilder(XmlFactory f) {
return XmlMapper.builder(f);
}

protected XmlMapper xmlMapper(boolean useListWrapping)
{
JacksonXmlModule module = new JacksonXmlModule();
Expand Down Expand Up @@ -295,11 +304,7 @@ protected static String aposToQuotes(String json) {
}

protected byte[] utf8Bytes(String str) {
try {
return str.getBytes("UTF-8");
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
return str.getBytes(StandardCharsets.UTF_8);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public InsensitiveCreator(@JsonProperty("value") int v0) {

private final ObjectMapper MAPPER = newMapper();

private final ObjectMapper INSENSITIVE_MAPPER = objectMapperBuilder()
private final ObjectMapper INSENSITIVE_MAPPER = mapperBuilder()
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static class RootObject {
// [dataformat-xml#274]
public void testIssue274() throws Exception
{
final ObjectMapper xm = objectMapperBuilder()
final ObjectMapper xm = mapperBuilder()

// serialization features
// xm.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void setName(String n) {
/********************************************************
*/

private final ObjectMapper INSENSITIVE_MAPPER = objectMapperBuilder()
private final ObjectMapper INSENSITIVE_MAPPER = mapperBuilder()
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static class Simple {

public void testCanSerialize() throws IOException
{
ObjectMapper mapper = objectMapperBuilder()
ObjectMapper mapper = mapperBuilder()
.build();
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT);

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

String doc = mapper.writeValueAsString(s);
System.err.println("DOC: "+doc);
Simple result = mapper.readValue(doc, Simple.class);
assertNotNull(result.list);
assertEquals(2, result.list.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void testBuilder291() throws Exception
XmlJaxbAnnotationIntrospector xmlIntr = new XmlJaxbAnnotationIntrospector(TypeFactory.defaultInstance());
AnnotationIntrospector intr = XmlAnnotationIntrospector.Pair.instance
(xmlIntr, new JacksonAnnotationIntrospector());
XmlMapper mapper = objectMapperBuilder()
XmlMapper mapper = mapperBuilder()
.annotationIntrospector(intr)
.build();
Address value = mapper.readValue(DOC, Address.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static class VehicleActivity {
/**********************************************************************
*/

protected XmlMapper _xmlMapper = objectMapperBuilder()
protected XmlMapper _xmlMapper = mapperBuilder()
.propertyNamingStrategy(new PropertyNamingStrategy.UpperCamelCaseStrategy())
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.defaultUseWrapper(false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.fasterxml.jackson.dataformat.xml.misc;

import java.io.*;
import java.util.*;

import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.dataformat.xml.*;
import com.fasterxml.jackson.dataformat.xml.testutil.PrefixInputDecorator;
import com.fasterxml.jackson.dataformat.xml.testutil.PrefixOutputDecorator;

public class StreamingDecoratorsTest extends XmlTestBase
{
@JsonRootName("wrapper")
static class Value {
public String value = "all";
}

@SuppressWarnings("unchecked")
public void testInputDecorators() throws IOException
{
final byte[] DOC = utf8Bytes("<secret: mum\n");
final XmlMapper mapper = mapperBuilder(
streamFactoryBuilder().inputDecorator(new PrefixInputDecorator(DOC))
.build())
.build();
Map<String,Object> value = mapper.readValue(utf8Bytes("value: foo\n"), Map.class);
assertEquals(2, value.size());
assertEquals("foo", value.get("value"));
assertEquals("mum", value.get("secret"));

// and then via Reader as well
value = mapper.readValue(new StringReader("value: xyz\n"), Map.class);
assertEquals(2, value.size());
assertEquals("xyz", value.get("value"));
assertEquals("mum", value.get("secret"));
}

public void testOutputDecorators() throws IOException
{
final String PREFIX = "///////";
final byte[] DOC = utf8Bytes(PREFIX);
final XmlMapper mapper = mapperBuilder(
streamFactoryBuilder().outputDecorator(new PrefixOutputDecorator(DOC))
.build())
.build();
final Value input = new Value();

// Gets bit tricky because writer will add doc prefix. So let's do simpler check here

try (ByteArrayOutputStream bytes = new ByteArrayOutputStream()) {
mapper.writeValue(bytes, input);

String raw = bytes.toString("UTF-8");
if (!raw.startsWith(PREFIX)) {
fail("Should start with prefix, did not: ["+raw+"]");
}
}

// and same with char-backed too
try (StringWriter sw = new StringWriter()) {
mapper.writeValue(sw, input);
String raw = sw.toString();
if (!raw.startsWith(PREFIX)) {
fail("Should start with prefix, did not: ["+raw+"]");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.fasterxml.jackson.dataformat.xml.testutil;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.SequenceInputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;

import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.core.io.InputDecorator;

@SuppressWarnings("serial")
public class PrefixInputDecorator extends InputDecorator
{
protected final byte[] _prefix;

public PrefixInputDecorator(byte[] p) {
_prefix = p;
}

@Override
public InputStream decorate(IOContext ctxt, InputStream in) {
if (in instanceof MySequenceInputStream) {
throw new IllegalStateException("Trying to decorate MySequenceInputStream (double-decoration!)");
}
return new MySequenceInputStream(new ByteArrayInputStream(_prefix), in);
}

@Override
public InputStream decorate(IOContext ctxt, byte[] src, int offset, int length) {
return decorate(ctxt, new ByteArrayInputStream(src, offset, length));
}

@Override
public Reader decorate(IOContext ctxt, Reader r) throws IOException {
if (r instanceof SequenceReader) {
throw new IllegalStateException("Trying to decorate SequenceReader (double-decoration!)");
}
String str = new String(_prefix, StandardCharsets.UTF_8);
return new SequenceReader(new StringReader(str), r);
}

// sub-class only so we can check for "double decoration"
static class MySequenceInputStream extends SequenceInputStream {
public MySequenceInputStream(InputStream in1, InputStream in2) {
super(in1, in2);
}
}

static class SequenceReader extends Reader {
protected Reader _reader1, _reader2;

public SequenceReader(Reader r1, Reader r2) {
_reader1 = r1;
_reader2 = r2;
}

@Override
public int read(char[] cbuf, int off, int len) throws IOException {
if (_reader1 != null) {
int count = _reader1.read(cbuf, off, len);
if (count > 0) {
return count;
}
_reader1 = null;
}
if (_reader2 != null) {
int count = _reader2.read(cbuf, off, len);
if (count > 0) {
return count;
}
_reader2 = null;
}
return -1;
}

@Override
public void close() throws IOException {
_reader1 = _reader2 = null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.fasterxml.jackson.dataformat.xml.testutil;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;

import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.core.io.OutputDecorator;

@SuppressWarnings("serial")
public class PrefixOutputDecorator extends OutputDecorator
{
protected final byte[] _prefix;

public PrefixOutputDecorator(byte[] p) {
_prefix = p;
}

@Override
public OutputStream decorate(IOContext ctxt, OutputStream out)
throws IOException
{
if (out instanceof BufferedOut) {
throw new IllegalStateException("Trying to decorate `Buffered` (double-decoration!)");
}
return new BufferedOut(out, _prefix);
}

@Override
public Writer decorate(IOContext ctxt, Writer w) throws IOException {
for (byte b : _prefix) {
w.write((char) (b & 0xFF));
}
return w;
}

static class BufferedOut extends FilterOutputStream {
protected byte[] _prefix;

public BufferedOut(OutputStream b, byte[] prefix) {
super(b);
_prefix = prefix;
}

@Override
public void write(int b) throws IOException {
if (_prefix != null) {
out.write(_prefix);
_prefix = null;
}
super.write(b);
}

@Override
public void write(byte[] b, int offset, int len) throws IOException {
if (_prefix != null) {
out.write(_prefix);
_prefix = null;
}
super.write(b, offset, len);
}
}
}

0 comments on commit 77fa454

Please sign in to comment.