You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2012/07/28 21:53:41 UTC

svn commit: r1366754 - in /webservices/axiom/branches/JAXB2_DS/modules/axiom-jaxb/src: main/java/org/apache/axiom/om/ds/jaxb/MTOMXMLStreamWriterAttachmentMarshaller.java test/java/org/apache/axiom/om/ds/jaxb/JAXBOMDataSourceTest.java

Author: veithen
Date: Sat Jul 28 19:53:40 2012
New Revision: 1366754

URL: http://svn.apache.org/viewvc?rev=1366754&view=rev
Log:
Correctly support MTOMXMLStreamWriter.

Modified:
    webservices/axiom/branches/JAXB2_DS/modules/axiom-jaxb/src/main/java/org/apache/axiom/om/ds/jaxb/MTOMXMLStreamWriterAttachmentMarshaller.java
    webservices/axiom/branches/JAXB2_DS/modules/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/jaxb/JAXBOMDataSourceTest.java

Modified: webservices/axiom/branches/JAXB2_DS/modules/axiom-jaxb/src/main/java/org/apache/axiom/om/ds/jaxb/MTOMXMLStreamWriterAttachmentMarshaller.java
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/JAXB2_DS/modules/axiom-jaxb/src/main/java/org/apache/axiom/om/ds/jaxb/MTOMXMLStreamWriterAttachmentMarshaller.java?rev=1366754&r1=1366753&r2=1366754&view=diff
==============================================================================
--- webservices/axiom/branches/JAXB2_DS/modules/axiom-jaxb/src/main/java/org/apache/axiom/om/ds/jaxb/MTOMXMLStreamWriterAttachmentMarshaller.java (original)
+++ webservices/axiom/branches/JAXB2_DS/modules/axiom-jaxb/src/main/java/org/apache/axiom/om/ds/jaxb/MTOMXMLStreamWriterAttachmentMarshaller.java Sat Jul 28 19:53:40 2012
@@ -32,7 +32,6 @@ final class MTOMXMLStreamWriterAttachmen
     @Override
     public String addMtomAttachment(DataHandler data, String elementNamespace,
             String elementLocalName) {
-        // TODO: add "cid:" + test!
-        return out.prepareDataHandler(data);
+        return "cid:" + out.prepareDataHandler(data);
     }
 }

Modified: webservices/axiom/branches/JAXB2_DS/modules/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/jaxb/JAXBOMDataSourceTest.java
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/JAXB2_DS/modules/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/jaxb/JAXBOMDataSourceTest.java?rev=1366754&r1=1366753&r2=1366754&view=diff
==============================================================================
--- webservices/axiom/branches/JAXB2_DS/modules/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/jaxb/JAXBOMDataSourceTest.java (original)
+++ webservices/axiom/branches/JAXB2_DS/modules/axiom-jaxb/src/test/java/org/apache/axiom/om/ds/jaxb/JAXBOMDataSourceTest.java Sat Jul 28 19:53:40 2012
@@ -19,18 +19,28 @@
 package org.apache.axiom.om.ds.jaxb;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+
 import javax.activation.DataHandler;
 import javax.xml.bind.JAXBContext;
+import javax.xml.namespace.QName;
 
+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.om.OMSourcedElement;
 import org.apache.axiom.om.OMText;
+import org.apache.axiom.om.OMXMLBuilderFactory;
 import org.apache.axiom.om.ds.jaxb.beans.DocumentBean;
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axiom.soap.SOAPFactory;
 import org.junit.Test;
 
 public class JAXBOMDataSourceTest {
@@ -58,4 +68,41 @@ public class JAXBOMDataSourceTest {
         assertTrue(content.isOptimized());
         assertSame(dh, content.getDataHandler());
     }
+    
+    /**
+     * Tests that an {@link OMSourcedElement} backed by a {@link JAXBOMDataSource} is correctly
+     * serialized to MTOM. The test uses a bean containing a {@link DataHandler}.
+     */
+    @Test
+    public void testMTOM() throws Exception {
+        SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
+        JAXBContext context = JAXBContext.newInstance(DocumentBean.class);
+        
+        // Construct the original message
+        DocumentBean object = new DocumentBean();
+        object.setId("123456");
+        object.setContent(new DataHandler("some content", "text/plain; charset=utf-8"));
+        SOAPEnvelope orgEnvelope = factory.getDefaultEnvelope();
+        OMSourcedElement element = factory.createOMElement(new JAXBOMDataSource(context, object));
+        orgEnvelope.getBody().addChild(element);
+        
+        // Serialize the message
+        OMOutputFormat format = new OMOutputFormat();
+        format.setDoOptimize(true);
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        orgEnvelope.serialize(out, format);
+        assertFalse(element.isExpanded());
+        
+        // Parse the serialized message
+        Attachments att = new Attachments(new ByteArrayInputStream(out.toByteArray()), format.getContentType());
+        assertEquals(2, att.getAllContentIDs().length);
+        SOAPEnvelope envelope = OMXMLBuilderFactory.createSOAPModelBuilder(att).getSOAPEnvelope();
+        OMElement contentElement = envelope.getBody().getFirstElement().getFirstChildWithName(
+                new QName("http://ws.apache.org/axiom/test/jaxb", "content"));
+        OMText content = (OMText)contentElement.getFirstOMChild();
+        assertTrue(content.isBinary());
+        assertTrue(content.isOptimized());
+        DataHandler dh = (DataHandler)content.getDataHandler();
+        assertEquals("some content", dh.getContent());
+    }
 }