Package org.apache.axiom.testutils.activation

Examples of org.apache.axiom.testutils.activation.RandomDataSource


        super(metaFactory);
    }

    protected void runTest() throws Throwable {
        OMFactory factory = metaFactory.getOMFactory();
        DataSource ds = new RandomDataSource(654321, 64, 128, 20000000);
        Vector/*<InputStream>*/ v = new Vector/*<InputStream>*/();
        v.add(new ByteArrayInputStream("<a>".getBytes("ascii")));
        v.add(ds.getInputStream());
        v.add(new ByteArrayInputStream("</a>".getBytes("ascii")));
        OMElement element = OMXMLBuilderFactory.createOMBuilder(factory,
                StAXParserConfiguration.NON_COALESCING,
                new SequenceInputStream(v.elements()), "ascii").getDocumentElement();
        Reader in = element.getTextAsStream(false);
        IOTestUtils.compareStreams(new InputStreamReader(ds.getInputStream(), "ascii"), in);
    }
View Full Code Here


        Assert.assertEquals(usesAttachments, customBuilder.isAttachmentsAccessed());
    }
   
    @Test
    public void testPlain() throws Exception {
        DataHandler dh = new DataHandler(new RandomDataSource(10000));
        MemoryBlob blob = new MemoryBlob();
        OutputStream out = blob.getOutputStream();
        createTestDocument(dh).serialize(out);
        out.close();
        test(dh, new StAXOMBuilder(blob.getInputStream()), false, false, true);
View Full Code Here

        test(dh, new StAXOMBuilder(document.getXMLStreamReader()), true, true, false);
    }
   
    @Test
    public void testWithXOP() throws Exception {
        DataHandler dh = new DataHandler(new RandomDataSource(10000));
        MemoryBlob blob = new MemoryBlob();
        OutputStream out = blob.getOutputStream();
        OMOutputFormat format = new OMOutputFormat();
        format.setDoOptimize(true);
        createTestDocument(dh).serialize(out, format);
View Full Code Here

        super(metaFactory);
    }

    protected void runTest() throws Throwable {
        OMFactory factory = metaFactory.getOMFactory();
        DataSource ds = new RandomDataSource(445566, 32, 128, 20000000);
        QName qname = new QName("a");
        Charset cs = Charset.forName("ascii");
        OMSourcedElement element = factory.createOMElement(
                new WrappedTextNodeOMDataSourceFromDataSource(qname, ds, cs), qname);
        Reader in = element.getTextAsStream(true);
        assertFalse(in instanceof StringReader);
        IOTestUtils.compareStreams(new InputStreamReader(ds.getInputStream(), cs), in);
        assertFalse(element.isExpanded());
    }
View Full Code Here

        assertEquals(element.getText(), IOUtils.toString(in));
    }
   
    public void testGetTextAsStreamWithOMSourcedElement() throws Exception {
        OMFactory factory = OMAbstractFactory.getOMFactory();
        DataSource ds = new RandomDataSource(445566, 32, 128, 20000000);
        QName qname = new QName("a");
        Charset cs = Charset.forName("ascii");
        OMSourcedElement element = new OMSourcedElementImpl(qname, factory,
                new WrappedTextNodeOMDataSourceFromDataSource(qname, ds, cs));
        Reader in = ElementHelper.getTextAsStream(element, true);
        assertFalse(in instanceof StringReader);
        IOTestUtils.compareStreams(new InputStreamReader(ds.getInputStream(), cs), in);
    }
View Full Code Here

        if (factory.getClass().getName().equals("com.bea.xml.stream.MXParserFactory")) {
            // Skip the test on the StAX reference implementation because it
            // causes an out of memory error
            return;
        }
        DataSource ds = new RandomDataSource(654321, 64, 128, 20000000);
        Vector/*<InputStream>*/ v = new Vector/*<InputStream>*/();
        v.add(new ByteArrayInputStream("<a>".getBytes("ascii")));
        v.add(ds.getInputStream());
        v.add(new ByteArrayInputStream("</a>".getBytes("ascii")));
        factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
        XMLStreamReader reader = factory.createXMLStreamReader(
                new SequenceInputStream(v.elements()), "ascii");
        OMElement element = OMXMLBuilderFactory.createStAXOMBuilder(reader).getDocumentElement();
        Reader in = ElementHelper.getTextAsStream(element, false);
        IOTestUtils.compareStreams(new InputStreamReader(ds.getInputStream(), "ascii"), in);
    }
View Full Code Here

        assertEquals(element.getText(), out.toString());
    }
   
    public void testWriteTextToWithOMSourcedElement() throws Exception {
        OMFactory factory = OMAbstractFactory.getOMFactory();
        DataSource ds = new RandomDataSource(665544, 32, 128, 20000000);
        QName qname = new QName("a");
        OMSourcedElement element = new OMSourcedElementImpl(qname, factory,
                new WrappedTextNodeOMDataSourceFromDataSource(qname, ds, Charset.forName("ascii")));
        Reader in = new InputStreamReader(ds.getInputStream(), "ascii");
        Writer out = new CharacterStreamComparator(in);
        ElementHelper.writeTextTo(element, out, true); // cache doesn't matter here
        out.close();
    }
View Full Code Here

        OMFactory factory = metaFactory.getOMFactory();
        OMElement elem = factory.createOMElement("test", null);
        // Create a data source that would eat up all memory when loaded. If the test
        // doesn't fail with an OutOfMemoryError, we know that the OMText implementation
        // supports streaming.
        DataSource ds = new RandomDataSource(654321L, Runtime.getRuntime().maxMemory());
        OMText text = factory.createOMText(new DataHandler(ds), false);
        elem.addChild(text);
        SAXSource saxSource = elem.getSAXSource(true);
        XMLReader xmlReader = saxSource.getXMLReader();
        xmlReader.setContentHandler(new Base64Comparator(ds.getInputStream()));
        xmlReader.parse(saxSource.getInputSource());
    }
View Full Code Here

        OMFactory factory = metaFactory.getOMFactory();
       
        // Programmatically create the message
        OMElement orgRoot = factory.createOMElement("root", null);
        OMElement orgChild1 = factory.createOMElement("child1", null, orgRoot);
        DataSource ds = new RandomDataSource(54321, 4096);
        orgChild1.addChild(factory.createOMText(new DataHandler(ds), true));
        // Create a child with a large text content and insert it after the binary node.
        // If we don't do this, then the root part may be buffered entirely by the parser,
        // and the test would not be effective.
        OMElement orgChild2 = factory.createOMElement("child2", null, orgRoot);
        String s = RandomUtils.randomString(128*1024);
        orgChild2.setText(s);
       
        // Serialize the message
        OMOutputFormat format = new OMOutputFormat();
        format.setDoOptimize(true);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        orgRoot.serialize(baos, format);
       
        // Parse the message
        OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(factory,
                StAXParserConfiguration.NON_COALESCING,
                new Attachments(new ByteArrayInputStream(baos.toByteArray()),
                        format.getContentType()));
        OMElement root = builder.getDocumentElement();
        OMElement child1 = (OMElement)root.getFirstOMChild();
        OMText text = (OMText)child1.getFirstOMChild();
        assertTrue(text.isBinary());
        // Access the DataHandler
        DataHandler dh = (DataHandler)text.getDataHandler();
        IOTestUtils.compareStreams(ds.getInputStream(), dh.getInputStream());
        OMElement child2 = (OMElement)child1.getNextOMSibling();
        assertFalse(child2.isComplete());
        assertEquals(s, child2.getText());
    }
View Full Code Here

        bp1.addHeader("Content-Transfer-Encoding", "binary");
        bp1.addHeader("Content-ID", "part1@apache.org");
        mp.addBodyPart(bp1);
       
        // Create an attachment that is larger than the maximum heap
        DataSource dataSource = new RandomDataSource((int)Math.min(Runtime.getRuntime().maxMemory(), Integer.MAX_VALUE));
        MimeBodyPart bp2 = new MimeBodyPart();
        bp2.setDataHandler(new DataHandler(dataSource));
        bp2.addHeader("Content-Transfer-Encoding", "binary");
        bp2.addHeader("Content-ID", "part2@apache.org");
        mp.addBodyPart(bp2);
       
        message.setContent(mp);
        // Compute the correct content type
        message.saveChanges();
       
        // We use a pipe (with a producer running in a separate thread) because obviously we can't
        // store the multipart in memory.
        final PipedOutputStream pipeOut = new PipedOutputStream();
        PipedInputStream pipeIn = new PipedInputStream(pipeOut);
       
        Thread producerThread = new Thread(new Runnable() {
            public void run() {
                try {
                    try {
                        mp.writeTo(pipeOut);
                    } finally {
                        pipeOut.close();
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        producerThread.start();
       
        try {
            // We configure Attachments to buffer MIME parts in memory. If the part content is not
            // streamed, then this will result in an OOM error.
            Attachments attachments = new Attachments(pipeIn, message.getContentType());
            DataHandlerExt dh = (DataHandlerExt)attachments.getDataHandler("part2@apache.org");
            IOTestUtils.compareStreams(dataSource.getInputStream(), dh.readOnce());
        } finally {
            pipeIn.close();
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.axiom.testutils.activation.RandomDataSource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.