use of com.helger.commons.io.stream.StringInputStream in project ph-commons by phax.
the class ByteArrayOutputStreamProviderTest method testSimple.
@Test
public void testSimple() {
final ByteArrayOutputStreamProvider aOSP = new ByteArrayOutputStreamProvider();
final OutputStream aOS = aOSP.getOutputStream(EAppend.DEFAULT);
assertNotNull(aOS);
StreamHelper.copyInputStreamToOutputStreamAndCloseOS(new StringInputStream("Hiho", StandardCharsets.ISO_8859_1), aOS);
assertEquals("Hiho", aOSP.getAsString(StandardCharsets.ISO_8859_1));
assertArrayEquals("Hiho".getBytes(StandardCharsets.ISO_8859_1), aOSP.getBytes());
// Close the underlying OS
StreamHelper.close(aOSP.getWriter(StandardCharsets.UTF_8, EAppend.DEFAULT));
// Reader/Writer
StreamHelper.copyReaderToWriterAndCloseWriter(new NonBlockingStringReader("Hiho"), aOSP.getWriter(StandardCharsets.UTF_8, EAppend.DEFAULT));
assertEquals("Hiho", aOSP.getAsString(StandardCharsets.UTF_8));
}
use of com.helger.commons.io.stream.StringInputStream in project ph-commons by phax.
the class DOMReaderTest method testReadWithSchema.
@Test
public void testReadWithSchema() {
final Schema aSchema = XMLSchemaCache.getInstance().getSchema(new ClassPathResource("xml/schema1.xsd"));
assertNotNull(aSchema);
// read valid
final String sValid = "<?xml version='1.0'?><root xmlns='http://www.example.org/schema1'><a>1</a><b>2</b></root>";
Document doc = DOMReader.readXMLDOM(sValid, new DOMReaderSettings().setSchema(aSchema));
assertNotNull(doc);
doc = DOMReader.readXMLDOM(sValid, new DOMReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
assertNotNull(doc);
doc = DOMReader.readXMLDOM(sValid, new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
assertNotNull(doc);
DOMReader.readXMLDOM(new NonBlockingStringReader(sValid), new DOMReaderSettings().setSchema(aSchema));
assertNotNull(doc);
doc = DOMReader.readXMLDOM(new NonBlockingStringReader(sValid), new DOMReaderSettings().setErrorHandler(new CollectingSAXErrorHandler()));
assertNotNull(doc);
doc = DOMReader.readXMLDOM(new NonBlockingStringReader(sValid), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
assertNotNull(doc);
doc = DOMReader.readXMLDOM(new StringInputStream(sValid, StandardCharsets.ISO_8859_1), new DOMReaderSettings().setSchema(aSchema));
assertNotNull(doc);
doc = DOMReader.readXMLDOM(new StringInputStream(sValid, StandardCharsets.ISO_8859_1), new DOMReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
assertNotNull(doc);
doc = DOMReader.readXMLDOM(new StringInputStream(sValid, StandardCharsets.ISO_8859_1), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
assertNotNull(doc);
doc = DOMReader.readXMLDOM(new StringSAXInputSource(sValid), new DOMReaderSettings().setSchema(aSchema));
assertNotNull(doc);
doc = DOMReader.readXMLDOM(new StringSAXInputSource(sValid), new DOMReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
assertNotNull(doc);
doc = DOMReader.readXMLDOM(new StringSAXInputSource(sValid), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
assertNotNull(doc);
doc = DOMReader.readXMLDOM(new ClassPathResource("xml/schema1-valid.xml"), new DOMReaderSettings().setSchema(aSchema));
assertNotNull(doc);
doc = DOMReader.readXMLDOM(new ClassPathResource("xml/schema1-valid.xml"), new DOMReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
assertNotNull(doc);
doc = DOMReader.readXMLDOM(new ClassPathResource("xml/schema1-valid.xml"), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
assertNotNull(doc);
// Read invalid (<c> tag is unknown)
final String sInvalid = "<?xml version='1.0'?><root xmlns='http://www.example.org/schema1'><a>1</a><b>2</b><c>3</c></root>";
doc = DOMReader.readXMLDOM(sInvalid, new DOMReaderSettings().setSchema(aSchema));
assertNull(doc);
doc = DOMReader.readXMLDOM(sInvalid, new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
assertNull(doc);
doc = DOMReader.readXMLDOM(new NonBlockingStringReader(sInvalid), new DOMReaderSettings().setSchema(aSchema));
assertNull(doc);
doc = DOMReader.readXMLDOM(new NonBlockingStringReader(sInvalid), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
assertNull(doc);
doc = DOMReader.readXMLDOM(new StringInputStream(sInvalid, StandardCharsets.ISO_8859_1), new DOMReaderSettings().setSchema(aSchema));
assertNull(doc);
doc = DOMReader.readXMLDOM(new StringInputStream(sInvalid, StandardCharsets.ISO_8859_1), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
assertNull(doc);
doc = DOMReader.readXMLDOM(new StringSAXInputSource(sInvalid), new DOMReaderSettings().setSchema(aSchema));
assertNull(doc);
doc = DOMReader.readXMLDOM(new StringSAXInputSource(sInvalid), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
assertNull(doc);
doc = DOMReader.readXMLDOM(new ClassPathResource("xml/schema1-invalid.xml"), new DOMReaderSettings().setSchema(aSchema));
assertNull(doc);
doc = DOMReader.readXMLDOM(new ClassPathResource("xml/schema1-invalid.xml"), new DOMReaderSettings().setSchema(aSchema).setErrorHandler(new LoggingSAXErrorHandler()));
assertNull(doc);
try {
DOMReader.readXMLDOM((Reader) null, new DOMReaderSettings());
fail();
} catch (final NullPointerException ex) {
}
try {
DOMReader.readXMLDOM((InputStream) null, new DOMReaderSettings());
fail();
} catch (final NullPointerException ex) {
}
try {
DOMReader.readXMLDOM((InputSource) null, new DOMReaderSettings());
fail();
} catch (final NullPointerException ex) {
}
try {
DOMReader.readXMLDOM((IReadableResource) null, new DOMReaderSettings());
fail();
} catch (final NullPointerException ex) {
}
}
use of com.helger.commons.io.stream.StringInputStream in project ph-commons by phax.
the class DOMReaderTest method testReadXMLDOMInputSource.
/**
* Test method readXMLDOM @ never
*/
@Test
public void testReadXMLDOMInputSource() {
Document doc = DOMReader.readXMLDOM("<root/>");
assertNotNull(doc);
doc = DOMReader.readXMLDOM(new NonBlockingStringReader("<root/>"));
assertNotNull(doc);
doc = DOMReader.readXMLDOM(new StringSAXInputSource("<root/>"));
assertNotNull(doc);
doc = DOMReader.readXMLDOM(new StringSAXInputSource("<?xml version=\"1.0\"?>\n" + "<root/>"));
assertNotNull(doc);
doc = DOMReader.readXMLDOM(new StringInputStream("<?xml version=\"1.0\"?>\n<root/>", StandardCharsets.ISO_8859_1));
assertNotNull(doc);
try {
// null reader not allowed
DOMReader.readXMLDOM((InputSource) null);
fail();
} catch (final NullPointerException ex) {
}
// non-XML
assertNull(DOMReader.readXMLDOM(new StringSAXInputSource("")));
// non-XML
assertNull(DOMReader.readXMLDOM(new StringSAXInputSource("<bla>")));
}
use of com.helger.commons.io.stream.StringInputStream in project ph-commons by phax.
the class JsonParserTest method testReadMultipleObjectsFromStream.
/**
* Does not work with InputStream, because the used StreamDecoder reads to
* eagerly:
*
* <pre>
* StringInputStream(NonBlockingByteArrayInputStream).read(byte[], int, int) line: 216
* LoggingInputStream(FilterInputStream).read(byte[], int, int) line: 133
* LoggingInputStream.read(byte[], int, int) line: 109
* NonBlockingPushbackInputStream(FilterInputStream).read(byte[], int, int) line: 133
* NonBlockingPushbackInputStream.read(byte[], int, int) line: 197
* StreamDecoder.readBytes() line: 284 [local variables unavailable]
* StreamDecoder.implRead(char[], int, int) line: 326
* StreamDecoder.read(char[], int, int) line: 178
* StreamDecoder.read0() line: 127 [local variables unavailable]
* StreamDecoder.read() line: 112 [local variables unavailable]
* InputStreamReader.read() line: 168 [local variables unavailable]
* NonClosingReader(FilterReader).read() line: 65
* NonBlockingPushbackReader(FilterReader).read() line: 65
* NonBlockingPushbackReader.read() line: 111
* JsonParser._readChar() line: 240
* JsonParser._skipSpaces() line: 349
* JsonParser._readValue() line: 910
* JsonParser.parse() line: 985
* JsonReader.parseJson(Reader, IJsonParserHandler, IJsonParserCustomizeCallback, IJsonParseExceptionCallback) line: 156
* JsonReader.readJson(Reader, IJsonParserCustomizeCallback, IJsonParseExceptionCallback) line: 445
* JsonReader$Builder.read() line: 1138
* JsonParserTest.testReadMultipleObjectsFromStream() line: 137
* </pre>
*
* @throws IOException
* hopefully never
*/
@Test
@Ignore
public void testReadMultipleObjectsFromStream() throws IOException {
final String sJson = "{ 'a': 1}{ 'b':true}";
try (final InputStream aIS = new LoggingInputStream(new StringInputStream(sJson, StandardCharsets.UTF_8))) {
IJson aJson;
int nCount = 0;
do {
LOGGER.info("Run " + nCount);
aJson = JsonReader.builderMultiObject().source(aIS).read();
if (aJson != null)
++nCount;
} while (aJson != null);
assertEquals(2, nCount);
}
}
Aggregations