You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by ve...@apache.org on 2010/05/23 13:59:26 UTC

svn commit: r947401 - in /webservices/commons/trunk/modules/axiom/modules: axiom-api/src/main/java/org/apache/axiom/om/impl/builder/ axiom-api/src/main/java/org/apache/axiom/util/stax/xop/ axiom-integration/ axiom-integration/src/test/java/org/apache/a...

Author: veithen
Date: Sun May 23 11:59:26 2010
New Revision: 947401

URL: http://svn.apache.org/viewvc?rev=947401&view=rev
Log:
WSCOMMONS-540:
* Completed the contract of CustomBuilder with a precise specification about how XOP (or other forms of optimized binary content) is handled.
* Added code that allows CustomBuilder (and other code) to get an XOP encoded reader in a well defined way. In particular, it will unwrap XOPDecodingStreamReader if necessary.
* Added an integration test that demonstrates this with JAXB2.

Added:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPEncodedStream.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPUtils.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/AttachmentUnmarshallerImpl.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/JAXBCustomBuilder.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/JAXBCustomBuilderTest.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/MyDocument.java   (with props)
Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/CustomBuilder.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPDecodingStreamReader.java
    webservices/commons/trunk/modules/axiom/modules/axiom-integration/pom.xml
    webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/activation/RandomDataSource.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/CustomBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/CustomBuilder.java?rev=947401&r1=947400&r2=947401&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/CustomBuilder.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/CustomBuilder.java Sun May 23 11:59:26 2010
@@ -44,8 +44,32 @@ public interface CustomBuilder {
      * @param localPart
      * @param parent
      * @param reader
-     *            The stream reader to read the StAX events from. The
-     *            implementation MUST NOT assume that this is the original
+     *            The stream reader to read the StAX events from. The data read
+     *            from this reader always represents plain XML, even if the
+     *            original document was XOP encoded. The reader optionally
+     *            implements the {@link org.apache.axiom.ext.stax.datahandler.DataHandlerReader}
+     *            extension to give the custom builder access to optimized
+     *            binary data. This is appropriate for custom builders that
+     *            support {@link org.apache.axiom.ext.stax.datahandler.DataHandlerReader}
+     *            or in cases where there is no other option than to transfer
+     *            binary data as base64 encoded character data.
+     *            <p>
+     *            However, if the custom builder interacts with a third party
+     *            library that supports XOP, it may want to use that encoding
+     *            to optimize the transfer of binary data. To do so, the
+     *            custom builder MUST use {@link org.apache.axiom.util.stax.xop.XOPUtils#getXOPEncodedStream(XMLStreamReader)}
+     *            to get an XOP encoded stream. This guarantees that the original
+     *            reader is wrapped or unwrapped properly and also that
+     *            the custom builder correctly gets access to the attachments,
+     *            regardless of the type of the original reader. In particular,
+     *            the custom builder MUST NOT attempt to retrieve attachments
+     *            through the {@link org.apache.axiom.om.OMAttachmentAccessor}
+     *            that may be implemented by the builder (because this wouldn't
+     *            work if the builder was created from an {@link XMLStreamReader}
+     *            implementing the {@link org.apache.axiom.ext.stax.datahandler.DataHandlerReader}
+     *            extension).
+     *            <p>
+     *            The implementation MUST NOT assume that <code>reader</code> is the original
      *            reader returned by the StAX implementation. In general, it
      *            will be a wrapper around the original reader, e.g. one added
      *            by the {@link org.apache.axiom.util.stax.dialect.StAXDialect}
@@ -57,6 +81,11 @@ public interface CustomBuilder {
      *            to unwrap the reader. If the method solely relies on the
      *            conformance of the reader to the StAX specification, it SHOULD
      *            NOT attempt to unwrap it.
+     *            <p>
+     *            If the implementation requires both an
+     *            XOP encoded stream and wants to get access to the original reader, it should invoke
+     *            {@link org.apache.axiom.util.stax.XMLStreamReaderUtils#getOriginalXMLStreamReader(XMLStreamReader)}
+     *            after {@link org.apache.axiom.util.stax.xop.XOPUtils#getXOPEncodedStream(XMLStreamReader)}.
      * @return null or OMElement
      */
     public OMElement create(String namespace, 

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPDecodingStreamReader.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPDecodingStreamReader.java?rev=947401&r1=947400&r2=947401&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPDecodingStreamReader.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPDecodingStreamReader.java Sun May 23 11:59:26 2010
@@ -478,4 +478,8 @@ public class XOPDecodingStreamReader ext
     public DataHandlerProvider getDataHandlerProvider() {
         return dh;
     }
+
+    XOPEncodedStream getXOPEncodedStream() {
+        return new XOPEncodedStream(getParent(), mimePartProvider);
+    }
 }

Added: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPEncodedStream.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPEncodedStream.java?rev=947401&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPEncodedStream.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPEncodedStream.java Sun May 23 11:59:26 2010
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.axiom.util.stax.xop;
+
+import javax.xml.stream.XMLStreamReader;
+
+/**
+ * Represents an XOP encoded stream. Since an XOP message is a MIME package with
+ * an main part in XML and a set of additional (binary) parts referenced from
+ * the main part, this class encapsulates an {@link XMLStreamReader}
+ * representing the main part and a {@link MimePartProvider} giving access to
+ * the attachments.
+ */
+public class XOPEncodedStream {
+    private final XMLStreamReader reader;
+    private final MimePartProvider mimePartProvider;
+    
+    XOPEncodedStream(XMLStreamReader reader, MimePartProvider mimePartProvider) {
+        this.reader = reader;
+        this.mimePartProvider = mimePartProvider;
+    }
+
+    /**
+     * Get the stream reader for the main part of the XOP message.
+     * 
+     * @return the stream reader for the main part
+     */
+    public XMLStreamReader getReader() {
+        return reader;
+    }
+
+    /**
+     * Get the provider object for the additional MIME parts referenced by the
+     * main part.
+     * 
+     * @return the MIME part provider
+     */
+    public MimePartProvider getMimePartProvider() {
+        return mimePartProvider;
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPEncodedStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPUtils.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPUtils.java?rev=947401&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPUtils.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPUtils.java Sun May 23 11:59:26 2010
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.axiom.util.stax.xop;
+
+import java.io.IOException;
+
+import javax.activation.DataHandler;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.axiom.om.impl.builder.DataHandlerReaderUtils;
+
+public class XOPUtils {
+    private static final MimePartProvider nullMimePartProvider = new MimePartProvider() {
+        public boolean isLoaded(String contentID) {
+            return false;
+        }
+        
+        public DataHandler getDataHandler(String contentID) throws IOException {
+            throw new IOException("There are no MIME parts!");
+        }
+    };
+    
+    private XOPUtils() {}
+    
+    /**
+     * Get an XOP encoded stream for a given stream reader. Depending on its
+     * type and characteristics, this method may wrap or unwrap the stream
+     * reader:
+     * <ol>
+     * <li>If the original reader is an {@link XOPEncodingStreamReader} it will
+     * be preserved, since it is already XOP encoded.
+     * <li>If the original reader is an {@link XOPDecodingStreamReader}, it will
+     * be unwrapped to give access to the underlying XOP encoded reader.
+     * <li>If the original reader is a plain XML stream reader implementing the
+     * {@link org.apache.axiom.ext.stax.datahandler.DataHandlerReader}
+     * extension, it will be wrapped in an {@link XOPEncodingStreamReader} so
+     * that optimized binary data can be transferred using XOP.
+     * <li>In all other cases, the original reader is simply preserved.
+     * </ol>
+     * 
+     * @param reader
+     *            the original reader
+     * @return the XOP encoded stream
+     */
+    public static XOPEncodedStream getXOPEncodedStream(XMLStreamReader reader) {
+        if (reader instanceof XOPEncodingStreamReader) {
+            return new XOPEncodedStream(reader, (MimePartProvider)reader);
+        } else if (reader instanceof XOPDecodingStreamReader) {
+            return ((XOPDecodingStreamReader)reader).getXOPEncodedStream();
+        } else if (DataHandlerReaderUtils.getDataHandlerReader(reader) != null) {
+            XOPEncodingStreamReader wrapper = new XOPEncodingStreamReader(reader,
+                    ContentIDGenerator.DEFAULT, OptimizationPolicy.ALL);
+            return new XOPEncodedStream(wrapper, wrapper);
+        } else {
+            return new XOPEncodedStream(reader, nullMimePartProvider);
+        }
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/xop/XOPUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-integration/pom.xml
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-integration/pom.xml?rev=947401&r1=947400&r2=947401&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-integration/pom.xml (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-integration/pom.xml Sun May 23 11:59:26 2010
@@ -93,6 +93,11 @@
             <classifier>tests</classifier>
             <version>${version}</version>
         </dependency>
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>axiom-testutils</artifactId>
+            <version>${project.version}</version>
+        </dependency>
     </dependencies>
     <build>
         <plugins>

Added: webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/AttachmentUnmarshallerImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/AttachmentUnmarshallerImpl.java?rev=947401&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/AttachmentUnmarshallerImpl.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/AttachmentUnmarshallerImpl.java Sun May 23 11:59:26 2010
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.axiom.om.impl.builder;
+
+import java.io.IOException;
+
+import javax.activation.DataHandler;
+import javax.xml.bind.attachment.AttachmentUnmarshaller;
+
+import org.apache.axiom.om.util.ElementHelper;
+import org.apache.axiom.util.stax.xop.MimePartProvider;
+
+public class AttachmentUnmarshallerImpl extends AttachmentUnmarshaller {
+    private final MimePartProvider mimePartProvider;
+    private boolean accessed;
+    
+    public AttachmentUnmarshallerImpl(MimePartProvider mimePartProvider) {
+        this.mimePartProvider = mimePartProvider;
+    }
+
+    @Override
+    public byte[] getAttachmentAsByteArray(String cid) {
+        // TODO
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public DataHandler getAttachmentAsDataHandler(String cid) {
+        try {
+            accessed = true;
+            return mimePartProvider.getDataHandler(ElementHelper.getContentIDFromHref(cid));
+        } catch (IOException ex) {
+            ex.printStackTrace();
+            // TODO: we should distinguish I/O errors from non existing attachments
+            throw new UnsupportedOperationException();
+        }
+    }
+
+    @Override
+    public boolean isXOPPackage() {
+        return true;
+    }
+
+    public boolean isAccessed() {
+        return accessed;
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/AttachmentUnmarshallerImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/JAXBCustomBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/JAXBCustomBuilder.java?rev=947401&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/JAXBCustomBuilder.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/JAXBCustomBuilder.java Sun May 23 11:59:26 2010
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.axiom.om.impl.builder;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamReader;
+
+import junit.framework.Assert;
+
+import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.util.stax.xop.XOPEncodedStream;
+import org.apache.axiom.util.stax.xop.XOPUtils;
+
+public class JAXBCustomBuilder implements CustomBuilder {
+    private final JAXBContext jaxbContext;
+    private final boolean expectBareReader;
+    private Object jaxbObject;
+    private boolean attachmentsAccessed;
+    
+    public JAXBCustomBuilder(JAXBContext jaxbContext, boolean expectBareReader) {
+        this.jaxbContext = jaxbContext;
+        this.expectBareReader = expectBareReader;
+    }
+
+    public OMElement create(String namespaceURI, String localPart,
+            OMContainer parent, XMLStreamReader reader, OMFactory factory)
+            throws OMException {
+        try {
+            XOPEncodedStream xopStream = XOPUtils.getXOPEncodedStream(reader);
+            XMLStreamReader encodedReader = xopStream.getReader();
+            if (expectBareReader) {
+                String className = encodedReader.getClass().getName();
+                Assert.assertTrue(!className.startsWith("org.apache.axiom.")
+                        || className.startsWith("org.apache.axiom.util.stax.dialect."));
+            }
+            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
+            AttachmentUnmarshallerImpl attachmentUnmarshaller = new AttachmentUnmarshallerImpl(xopStream.getMimePartProvider());
+            unmarshaller.setAttachmentUnmarshaller(attachmentUnmarshaller);
+            // For the purpose of the test we just store the JAXB object and return
+            // a dummy element. Normally, one would create an OMSourcedElement here.
+            jaxbObject = unmarshaller.unmarshal(encodedReader);
+            attachmentsAccessed = attachmentUnmarshaller.isAccessed();
+            return factory.createOMElement(new QName(namespaceURI, localPart), parent);
+        } catch (JAXBException ex) {
+            throw new OMException(ex);
+        }
+    }
+
+    public Object getJaxbObject() {
+        return jaxbObject;
+    }
+
+    public boolean isAttachmentsAccessed() {
+        return attachmentsAccessed;
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/JAXBCustomBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/JAXBCustomBuilderTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/JAXBCustomBuilderTest.java?rev=947401&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/JAXBCustomBuilderTest.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/JAXBCustomBuilderTest.java Sun May 23 11:59:26 2010
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.axiom.om.impl.builder;
+
+import static junit.framework.Assert.assertNotSame;
+import static junit.framework.Assert.assertSame;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.activation.DataHandler;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.namespace.QName;
+
+import junit.framework.Assert;
+
+import org.apache.axiom.attachments.Attachments;
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMOutputFormat;
+import org.apache.axiom.testutils.activation.RandomDataSource;
+import org.apache.axiom.testutils.activation.TestDataSource;
+import org.apache.axiom.testutils.io.IOTestUtils;
+import org.apache.axiom.util.blob.MemoryBlob;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class JAXBCustomBuilderTest {
+    private static JAXBContext jaxbContext;
+    
+    @BeforeClass
+    public static void initJAXBContext() throws JAXBException {
+        jaxbContext = JAXBContext.newInstance(MyDocument.class);
+    }
+    
+    private OMElement createTestDocument(DataHandler dh) {
+        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(dh, true));
+        document.addChild(content);
+        return document;
+    }
+    
+    private void test(DataHandler dh, StAXOMBuilder builder, boolean same, boolean usesAttachments, boolean expectBareReader) throws IOException {
+        JAXBCustomBuilder customBuilder = new JAXBCustomBuilder(jaxbContext, expectBareReader);
+        builder.registerCustomBuilderForPayload(customBuilder);
+        builder.getDocumentElement().build();
+        MyDocument myDocument = (MyDocument)customBuilder.getJaxbObject();
+        if (same) {
+            assertSame(dh, myDocument.getContent());
+        } else {
+            assertNotSame(dh, myDocument.getContent());
+            IOTestUtils.compareStreams(dh.getInputStream(), myDocument.getContent().getInputStream());
+        }
+        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);
+    }
+    
+    @Test
+    public void testWithDataHandlerReaderExtension() throws Exception {
+        DataHandler dh = new DataHandler(new TestDataSource('X', Integer.MAX_VALUE));
+        OMElement document = createTestDocument(dh);
+        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);
+        out.close();
+        Attachments attachments = new Attachments(blob.getInputStream(), format.getContentType());
+        test(dh, new XOPAwareStAXOMBuilder(attachments.getSOAPPartInputStream(), attachments), false, true, true);
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/JAXBCustomBuilderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/MyDocument.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/MyDocument.java?rev=947401&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/MyDocument.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/MyDocument.java Sun May 23 11:59:26 2010
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.axiom.om.impl.builder;
+
+import javax.activation.DataHandler;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlType;
+
+@XmlRootElement(namespace = "urn:test", name = "document")
+@XmlType(propOrder = { "name", "content" })
+public class MyDocument {
+    private String name;
+    private DataHandler content;
+    
+    public String getName() {
+        return name;
+    }
+    
+    public void setName(String name) {
+        this.name = name;
+    }
+    
+    public DataHandler getContent() {
+        return content;
+    }
+    
+    public void setContent(DataHandler content) {
+        this.content = content;
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-integration/src/test/java/org/apache/axiom/om/impl/builder/MyDocument.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/activation/RandomDataSource.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/activation/RandomDataSource.java?rev=947401&r1=947400&r2=947401&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/activation/RandomDataSource.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testutils/src/main/java/org/apache/axiom/testutils/activation/RandomDataSource.java Sun May 23 11:59:26 2010
@@ -38,6 +38,14 @@ public class RandomDataSource implements
         this.rangeEnd = rangeEnd;
         this.length = length;
     }
+    
+    public RandomDataSource(long seed, int length) {
+        this(seed, 0, 256, length);
+    }
+    
+    public RandomDataSource(int length) {
+        this(System.currentTimeMillis(), length);
+    }
 
     public String getName() {
         return null;