Search in sources :

Example 1 with StringInputStream

use of com.helger.commons.io.stream.StringInputStream in project as2-lib by phax.

the class HTTPHelperTest method testReadChunkLenEOS.

@Test(expected = EOFException.class)
public void testReadChunkLenEOS() throws Exception {
    final InputStream noNewLine = new StringInputStream("1", StandardCharsets.UTF_8);
    HTTPHelper.readChunkLen(noNewLine);
    fail("An EOFException should have been thrown");
}
Also used : StringInputStream(com.helger.commons.io.stream.StringInputStream) NonBlockingByteArrayInputStream(com.helger.commons.io.stream.NonBlockingByteArrayInputStream) StringInputStream(com.helger.commons.io.stream.StringInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 2 with StringInputStream

use of com.helger.commons.io.stream.StringInputStream in project ph-commons by phax.

the class MicroHelperTest method testConvertToMicroNode.

@Test
public void testConvertToMicroNode() throws SAXException, IOException, ParserConfigurationException {
    final String sXML = "<?xml version='1.0'?>" + "<!DOCTYPE root [ <!ENTITY sc \"sc.exe\"> <!ELEMENT root (child, child2)> <!ELEMENT child (#PCDATA)> <!ELEMENT child2 (#PCDATA)> ]>" + "<root attr='value'>" + "<![CDATA[hihi]]>" + "text" + "&sc;" + "<child xmlns='http://myns' a='b' />" + "<child2 />" + "<!-- comment -->" + "<?stylesheet x y z?>" + "</root>";
    final DocumentBuilderFactory aDBF = XMLFactory.createDefaultDocumentBuilderFactory();
    aDBF.setCoalescing(false);
    aDBF.setIgnoringComments(false);
    final Document doc = aDBF.newDocumentBuilder().parse(new StringInputStream(sXML, StandardCharsets.ISO_8859_1));
    assertNotNull(doc);
    final IMicroNode aNode = MicroHelper.convertToMicroNode(doc);
    assertNotNull(aNode);
    try {
        MicroHelper.convertToMicroNode(null);
        fail();
    } catch (final NullPointerException ex) {
    }
}
Also used : StringInputStream(com.helger.commons.io.stream.StringInputStream) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) IMicroNode(com.helger.xml.microdom.IMicroNode) IMicroDocument(com.helger.xml.microdom.IMicroDocument) MicroDocument(com.helger.xml.microdom.MicroDocument) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 3 with StringInputStream

use of com.helger.commons.io.stream.StringInputStream in project ph-commons by phax.

the class DOMReaderTest method testReadXMLDOMInputStream.

/**
 * Test method readXMLDOM @ never
 */
@Test
public void testReadXMLDOMInputStream() {
    Document doc = DOMReader.readXMLDOM(new StringInputStream("<root/>", StandardCharsets.ISO_8859_1));
    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((InputStream) null);
        fail();
    } catch (final NullPointerException ex) {
    }
    // non-XML
    assertNull(DOMReader.readXMLDOM(new NonBlockingByteArrayInputStream(new byte[0])));
    doc = DOMReader.readXMLDOM(new StringInputStream("<?xml version=\"1.0\"?>\n<root/>", StandardCharsets.ISO_8859_1));
    assertNotNull(doc);
}
Also used : NonBlockingByteArrayInputStream(com.helger.commons.io.stream.NonBlockingByteArrayInputStream) StringInputStream(com.helger.commons.io.stream.StringInputStream) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 4 with StringInputStream

use of com.helger.commons.io.stream.StringInputStream in project ph-schematron by phax.

the class SchematronResourcePureTest method testFromByteArray.

@Test
public void testFromByteArray() {
    final String sTest = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" + "<iso:schema xmlns=\"http://purl.oclc.org/dsdl/schematron\" \n" + "         xmlns:iso=\"http://purl.oclc.org/dsdl/schematron\" \n" + "         xmlns:sch=\"http://www.ascc.net/xml/schematron\"\n" + "         queryBinding='xpath2'\n" + "         schemaVersion=\"ISO19757-3\">\n" + "  <iso:title>Test ISO schematron file. Introduction mode</iso:title>\n" + "  <iso:ns prefix=\"dp\" uri=\"http://www.dpawson.co.uk/ns#\" />\n" + " <iso:pattern >\n" + "    <iso:title>A very simple pattern with a title</iso:title>\n" + "    <iso:rule context=\"chapter\">\n" + "      <iso:assert test=\"title\">Chapter should have a title</iso:assert>\n" + "      <iso:report test=\"count(para)\">\n" + "      <iso:value-of select=\"count(para)\"/> paragraphs</iso:report>\n" + "    </iso:rule>\n" + "  </iso:pattern>\n" + "\n" + "</iso:schema>";
    assertTrue(SchematronResourcePure.fromByteArray(sTest.getBytes(StandardCharsets.UTF_8)).isValidSchematron());
    assertTrue(SchematronResourcePure.fromInputStream("ba-from-string", new StringInputStream(sTest, StandardCharsets.UTF_8)).isValidSchematron());
}
Also used : StringInputStream(com.helger.commons.io.stream.StringInputStream) Test(org.junit.Test)

Example 5 with StringInputStream

use of com.helger.commons.io.stream.StringInputStream in project as2-lib by phax.

the class MimeBodyPartFuncTest method testReadContentTransferEncodingBase64.

@Test
public void testReadContentTransferEncodingBase64() throws MessagingException, IOException {
    final String sHTTP = "Content-Type: text/plain" + CHttp.EOL + "Content-Transfer-Encoding: base64" + CHttp.EOL + "x-custom: junit" + CHttp.EOL + "Content-Length: 44" + CHttp.EOL + CHttp.EOL + "VGVzdCBtZXNzYWdlCkxpbmUgMgoKTGluZSA0CkVPRg==" + CHttp.EOL;
    InputStream aIS = new StringInputStream(sHTTP, StandardCharsets.ISO_8859_1);
    // Parse all HTTP headers from stream
    final InternetHeaders aHeaders = new InternetHeaders(aIS);
    final String sCTE = aHeaders.getHeader(CHttpHeader.CONTENT_TRANSFER_ENCODING)[0];
    if (StringHelper.hasText(sCTE))
        aIS = AS2IOHelper.getContentTransferEncodingAwareInputStream(aIS, sCTE);
    // Read the payload
    final byte[] aData = StreamHelper.getAllBytes(aIS);
    // Extract content type
    final String sReceivedContentType = AS2HttpHelper.getCleanContentType(aHeaders.getHeader(CHttpHeader.CONTENT_TYPE)[0]);
    final MimeBodyPart aReceivedPart = new MimeBodyPart();
    aReceivedPart.setDataHandler(new ByteArrayDataSource(aData, sReceivedContentType, null).getAsDataHandler());
    aReceivedPart.setHeader("x-received", "true");
    final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream();
    aReceivedPart.writeTo(aBAOS);
    StreamHelper.close(aBAOS);
    final String sMsgPart = aBAOS.getAsString(StandardCharsets.ISO_8859_1);
    if (true)
        LOGGER.info(sMsgPart);
}
Also used : StringInputStream(com.helger.commons.io.stream.StringInputStream) InternetHeaders(javax.mail.internet.InternetHeaders) StringInputStream(com.helger.commons.io.stream.StringInputStream) InputStream(java.io.InputStream) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(com.helger.mail.datasource.ByteArrayDataSource) Test(org.junit.Test)

Aggregations

StringInputStream (com.helger.commons.io.stream.StringInputStream)9 Test (org.junit.Test)9 Document (org.w3c.dom.Document)4 NonBlockingStringReader (com.helger.commons.io.stream.NonBlockingStringReader)3 InputStream (java.io.InputStream)3 NonBlockingByteArrayInputStream (com.helger.commons.io.stream.NonBlockingByteArrayInputStream)2 StringSAXInputSource (com.helger.xml.sax.StringSAXInputSource)2 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)1 LoggingInputStream (com.helger.commons.io.stream.LoggingInputStream)1 NonBlockingByteArrayOutputStream (com.helger.commons.io.stream.NonBlockingByteArrayOutputStream)1 IJson (com.helger.json.IJson)1 ByteArrayDataSource (com.helger.mail.datasource.ByteArrayDataSource)1 IMicroDocument (com.helger.xml.microdom.IMicroDocument)1 IMicroNode (com.helger.xml.microdom.IMicroNode)1 MicroDocument (com.helger.xml.microdom.MicroDocument)1 CollectingSAXErrorHandler (com.helger.xml.sax.CollectingSAXErrorHandler)1 LoggingSAXErrorHandler (com.helger.xml.sax.LoggingSAXErrorHandler)1 OutputStream (java.io.OutputStream)1 InternetHeaders (javax.mail.internet.InternetHeaders)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1