This is an automated email from the ASF dual-hosted git repository. veithen pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/ws-axiom.git
The following commit(s) were added to refs/heads/master by this push: new bb26db991 AXIOM-506: Replace TestDataSource with a Blob implementation bb26db991 is described below commit bb26db9919cb31882540bd881a9be5c2be9abd9b Author: Andreas Veithen <andreas.veit...@gmail.com> AuthorDate: Tue Jun 6 21:25:51 2023 +0000 AXIOM-506: Replace TestDataSource with a Blob implementation --- .../ds/custombuilder/CustomBuilderSupportTest.java | 41 +++++++++++++--------- .../om/text/TestBase64StreamingWithSerialize.java | 15 +++----- .../apache/axiom/ts/om/xop/XOPRoundtripTest.java | 11 +++--- .../soap12/envelope/TestMTOMForwardStreaming.java | 20 +++++------ .../org/apache/axiom/testutils/blob/TestBlob.java} | 38 ++++++++++---------- 5 files changed, 62 insertions(+), 63 deletions(-) diff --git a/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/custombuilder/CustomBuilderSupportTest.java b/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/custombuilder/CustomBuilderSupportTest.java index 4f373e165..197d77ba4 100644 --- a/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/custombuilder/CustomBuilderSupportTest.java +++ b/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/custombuilder/CustomBuilderSupportTest.java @@ -22,10 +22,10 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.OutputStream; -import javax.activation.DataHandler; import javax.xml.bind.JAXBContext; import javax.xml.namespace.QName; +import org.apache.axiom.blob.Blob; import org.apache.axiom.blob.Blobs; import org.apache.axiom.blob.MemoryBlob; import org.apache.axiom.mime.MultipartBody; @@ -36,38 +36,39 @@ import org.apache.axiom.om.OMOutputFormat; import org.apache.axiom.om.OMXMLBuilderFactory; import org.apache.axiom.om.OMXMLParserWrapper; import org.apache.axiom.om.util.StAXParserConfiguration; -import org.apache.axiom.testutils.activation.TestDataSource; import org.apache.axiom.testutils.blob.RandomBlob; +import org.apache.axiom.testutils.blob.TestBlob; import org.apache.axiom.testutils.io.IOTestUtils; import org.apache.axiom.util.activation.DataHandlerUtils; import org.junit.jupiter.api.Test; public class CustomBuilderSupportTest { - private OMElement createTestDocument(DataHandler dh) { + private OMElement createTestDocument(Blob blob) { OMFactory factory = OMAbstractFactory.getOMFactory(); OMElement document = factory.createOMElement(new QName("urn:test", "document")); OMElement name = factory.createOMElement(new QName("name")); name.setText("some name"); document.addChild(name); OMElement content = factory.createOMElement(new QName("content")); - content.addChild(factory.createOMText(DataHandlerUtils.toBlob(dh), true)); + content.addChild(factory.createOMText(blob, true)); document.addChild(content); return document; } - private void test(DataHandler dh, OMXMLParserWrapper builder, boolean same) throws Exception { + private void test(Blob blob, OMXMLParserWrapper builder, boolean same) throws Exception { JAXBCustomBuilder customBuilder = new JAXBCustomBuilder(JAXBContext.newInstance(MyDocument.class)); ((CustomBuilderSupport) builder) .registerCustomBuilder(CustomBuilder.Selector.PAYLOAD, customBuilder); builder.getDocumentElement().build(); MyDocument myDocument = (MyDocument) customBuilder.getJaxbObject(); + Blob actualBlob = DataHandlerUtils.toBlob(myDocument.getContent()); if (same) { - assertThat(myDocument.getContent()).isSameAs(dh); + assertThat(actualBlob).isSameAs(blob); } else { - assertThat(myDocument.getContent()).isNotSameAs(dh); + assertThat(actualBlob).isNotSameAs(blob); IOTestUtils.compareStreams( - dh.getInputStream(), + blob.getInputStream(), "expected", myDocument.getContent().getInputStream(), "actual"); @@ -76,36 +77,42 @@ public class CustomBuilderSupportTest { @Test public void testRegisterCustomBuilderForPayloadJAXBPlain() throws Exception { - DataHandler dh = DataHandlerUtils.toDataHandler(new RandomBlob(10000)); + Blob contentBlob = new RandomBlob(10000); MemoryBlob blob = Blobs.createMemoryBlob(); OutputStream out = blob.getOutputStream(); - createTestDocument(dh).serialize(out); + createTestDocument(contentBlob).serialize(out); out.close(); - test(dh, OMXMLBuilderFactory.createOMBuilder(blob.getInputStream()), false); + test(contentBlob, OMXMLBuilderFactory.createOMBuilder(blob.getInputStream()), false); } @Test public void testRegisterCustomBuilderForPayloadJAXBWithDataHandlerReaderExtension() throws Exception { - DataHandler dh = new DataHandler(new TestDataSource('X', Integer.MAX_VALUE)); - OMElement document = createTestDocument(dh); - test(dh, OMXMLBuilderFactory.createStAXOMBuilder(document.getXMLStreamReader()), true); + Blob contentBlob = new TestBlob('X', Integer.MAX_VALUE); + OMElement document = createTestDocument(contentBlob); + test( + contentBlob, + OMXMLBuilderFactory.createStAXOMBuilder(document.getXMLStreamReader()), + true); } @Test public void testRegisterCustomBuilderForPayloadJAXBWithXOP() throws Exception { - DataHandler dh = DataHandlerUtils.toDataHandler(new RandomBlob(10000)); + Blob contentBlob = new RandomBlob(10000); MemoryBlob blob = Blobs.createMemoryBlob(); OutputStream out = blob.getOutputStream(); OMOutputFormat format = new OMOutputFormat(); format.setDoOptimize(true); - createTestDocument(dh).serialize(out, format); + createTestDocument(contentBlob).serialize(out, format); out.close(); MultipartBody mb = MultipartBody.builder() .setInputStream(blob.getInputStream()) .setContentType(format.getContentType()) .build(); - test(dh, OMXMLBuilderFactory.createOMBuilder(StAXParserConfiguration.DEFAULT, mb), false); + test( + contentBlob, + OMXMLBuilderFactory.createOMBuilder(StAXParserConfiguration.DEFAULT, mb), + false); } } diff --git a/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64StreamingWithSerialize.java b/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64StreamingWithSerialize.java index f8475b633..cbb700d1d 100644 --- a/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64StreamingWithSerialize.java +++ b/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestBase64StreamingWithSerialize.java @@ -18,16 +18,12 @@ */ package org.apache.axiom.ts.om.text; -import javax.activation.DataHandler; -import javax.activation.DataSource; - import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMMetaFactory; import org.apache.axiom.om.OMText; -import org.apache.axiom.testutils.activation.TestDataSource; +import org.apache.axiom.testutils.blob.TestBlob; import org.apache.axiom.ts.AxiomTestCase; -import org.apache.axiom.util.activation.DataHandlerUtils; import org.apache.commons.io.output.NullOutputStream; /** @@ -46,11 +42,10 @@ public class TestBase64StreamingWithSerialize extends AxiomTestCase { protected void runTest() throws Throwable { 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 TestDataSource('A', Runtime.getRuntime().maxMemory()); - OMText text = factory.createOMText(DataHandlerUtils.toBlob(new DataHandler(ds)), false); + // Create a blob 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. + OMText text = + factory.createOMText(new TestBlob('A', Runtime.getRuntime().maxMemory()), false); elem.addChild(text); elem.serialize(NullOutputStream.NULL_OUTPUT_STREAM); } diff --git a/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/xop/XOPRoundtripTest.java b/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/xop/XOPRoundtripTest.java index 1467d75aa..b31ef73c8 100644 --- a/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/xop/XOPRoundtripTest.java +++ b/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/xop/XOPRoundtripTest.java @@ -18,20 +18,19 @@ */ package org.apache.axiom.ts.om.xop; -import javax.activation.DataHandler; import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamReader; import javax.xml.transform.stax.StAXSource; +import org.apache.axiom.blob.Blob; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMMetaFactory; import org.apache.axiom.om.OMText; import org.apache.axiom.om.OMXMLBuilderFactory; import org.apache.axiom.om.XOPEncoded; -import org.apache.axiom.testutils.activation.TestDataSource; +import org.apache.axiom.testutils.blob.TestBlob; import org.apache.axiom.ts.AxiomTestCase; -import org.apache.axiom.util.activation.DataHandlerUtils; public class XOPRoundtripTest extends AxiomTestCase { public XOPRoundtripTest(OMMetaFactory metaFactory) { @@ -41,9 +40,9 @@ public class XOPRoundtripTest extends AxiomTestCase { @Override protected void runTest() throws Throwable { OMFactory factory = metaFactory.getOMFactory(); - DataHandler dh = new DataHandler(new TestDataSource('x', Runtime.getRuntime().maxMemory())); + Blob blob = new TestBlob('x', Runtime.getRuntime().maxMemory()); OMElement element1 = factory.createOMElement(new QName("test")); - element1.addChild(factory.createOMText(DataHandlerUtils.toBlob(dh), true)); + element1.addChild(factory.createOMText(blob, true)); XOPEncoded<XMLStreamReader> xopEncodedStream = element1.getXOPEncodedStreamReader(true); OMElement element2 = OMXMLBuilderFactory.createOMBuilder( @@ -55,6 +54,6 @@ public class XOPRoundtripTest extends AxiomTestCase { assertNotNull(child); assertTrue(child.isBinary()); assertTrue(child.isOptimized()); - assertSame(dh, DataHandlerUtils.toDataHandler(child.getBlob())); + assertSame(blob, child.getBlob()); } } diff --git a/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap12/envelope/TestMTOMForwardStreaming.java b/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap12/envelope/TestMTOMForwardStreaming.java index ea65fde9c..ac750896b 100644 --- a/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap12/envelope/TestMTOMForwardStreaming.java +++ b/testing/axiom-testsuite/src/main/java/org/apache/axiom/ts/soap12/envelope/TestMTOMForwardStreaming.java @@ -22,9 +22,7 @@ import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.util.Iterator; -import javax.activation.DataHandler; -import javax.activation.DataSource; - +import org.apache.axiom.blob.Blob; import org.apache.axiom.mime.MultipartBody; import org.apache.axiom.mime.activation.PartDataHandlerBlobFactory; import org.apache.axiom.mime.activation.PartDataHandler; @@ -36,7 +34,7 @@ import org.apache.axiom.om.OMXMLBuilderFactory; import org.apache.axiom.soap.SOAPBody; import org.apache.axiom.soap.SOAPEnvelope; import org.apache.axiom.soap.SOAPFactory; -import org.apache.axiom.testutils.activation.TestDataSource; +import org.apache.axiom.testutils.blob.TestBlob; import org.apache.axiom.testutils.io.IOTestUtils; import org.apache.axiom.ts.AxiomTestCase; import org.apache.axiom.util.activation.DataHandlerUtils; @@ -58,8 +56,8 @@ public class TestMTOMForwardStreaming extends AxiomTestCase { @Override protected void runTest() throws Throwable { - DataSource ds1 = new TestDataSource('A', Runtime.getRuntime().maxMemory()); - DataSource ds2 = new TestDataSource('B', Runtime.getRuntime().maxMemory()); + Blob blob1 = new TestBlob('A', Runtime.getRuntime().maxMemory()); + Blob blob2 = new TestBlob('B', Runtime.getRuntime().maxMemory()); // Programmatically create the original message SOAPFactory factory = metaFactory.getSOAP12Factory(); @@ -69,11 +67,9 @@ public class TestMTOMForwardStreaming extends AxiomTestCase { factory.createOMElement( "test", factory.createOMNamespace("urn:test", "p"), orgBody); OMElement orgData1 = factory.createOMElement("data", null, orgBodyElement); - orgData1.addChild( - factory.createOMText(DataHandlerUtils.toBlob(new DataHandler(ds1)), true)); + orgData1.addChild(factory.createOMText(blob1, true)); OMElement orgData2 = factory.createOMElement("data", null, orgBodyElement); - orgData2.addChild( - factory.createOMText(DataHandlerUtils.toBlob(new DataHandler(ds2)), true)); + orgData2.addChild(factory.createOMText(blob2, true)); OMOutputFormat format = new OMOutputFormat(); format.setDoOptimize(true); @@ -152,14 +148,14 @@ public class TestMTOMForwardStreaming extends AxiomTestCase { OMElement data2 = it.next(); IOTestUtils.compareStreams( - ds1.getInputStream(), + blob1.getInputStream(), ((PartDataHandler) DataHandlerUtils.toDataHandler( ((OMText) data1.getFirstOMChild()).getBlob())) .getPart() .getInputStream(false)); IOTestUtils.compareStreams( - ds2.getInputStream(), + blob2.getInputStream(), ((PartDataHandler) DataHandlerUtils.toDataHandler( ((OMText) data2.getFirstOMChild()).getBlob())) diff --git a/testing/testutils/src/main/java/org/apache/axiom/testutils/activation/TestDataSource.java b/testing/blob-testutils/src/main/java/org/apache/axiom/testutils/blob/TestBlob.java similarity index 68% rename from testing/testutils/src/main/java/org/apache/axiom/testutils/activation/TestDataSource.java rename to testing/blob-testutils/src/main/java/org/apache/axiom/testutils/blob/TestBlob.java index 04a33aae2..5231aee2f 100644 --- a/testing/testutils/src/main/java/org/apache/axiom/testutils/activation/TestDataSource.java +++ b/testing/blob-testutils/src/main/java/org/apache/axiom/testutils/blob/TestBlob.java @@ -17,37 +17,28 @@ * under the License. */ -package org.apache.axiom.testutils.activation; +package org.apache.axiom.testutils.blob; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import javax.activation.DataSource; +import org.apache.axiom.blob.Blob; +import org.apache.axiom.ext.io.StreamCopyException; /** - * Test data source that produces a byte sequence with specified length and with all bytes equal to - * a specified value. + * Test blob that produces a byte sequence with specified length and with all bytes equal to a + * specified value. */ -public class TestDataSource implements DataSource { +public class TestBlob implements Blob { final int value; final long length; - public TestDataSource(int value, long length) { + public TestBlob(int value, long length) { this.value = value; this.length = length; } - @Override - public String getName() { - return null; - } - - @Override - public String getContentType() { - return null; - } - @Override public InputStream getInputStream() throws IOException { return new InputStream() { @@ -66,7 +57,18 @@ public class TestDataSource implements DataSource { } @Override - public OutputStream getOutputStream() throws IOException { - throw new UnsupportedOperationException(); + public void writeTo(OutputStream out) throws StreamCopyException { + for (long i = 0; i < length; i++) { + try { + out.write(value); + } catch (IOException ex) { + throw new StreamCopyException(StreamCopyException.WRITE, ex); + } + } + } + + @Override + public long getSize() { + return length; } }