-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5cbda5
commit 77fa454
Showing
11 changed files
with
236 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/test/java/com/fasterxml/jackson/dataformat/xml/misc/StreamingDecoratorsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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+"]"); | ||
} | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/test/java/com/fasterxml/jackson/dataformat/xml/testutil/PrefixInputDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/test/java/com/fasterxml/jackson/dataformat/xml/testutil/PrefixOutputDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |