Author: veithen Date: Wed Dec 17 15:43:12 2008 New Revision: 727579 URL: http://svn.apache.org/viewvc?rev=727579&view=rev Log: WSCOMMONS-334: Fixed a problem in OMSourcedElementImpl where the element is not correctly serialized if the data source is non destructive and the element has been modified. Also added a test case for this.
Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/llom/OMSourcedElementTest.java Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java?rev=727579&r1=727578&r2=727579&view=diff ============================================================================== --- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java (original) +++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java Wed Dec 17 15:43:12 2008 @@ -768,7 +768,9 @@ * serialize(java.io.OutputStream, org.apache.axiom.om.OMOutputFormat) */ public void serialize(OutputStream output, OMOutputFormat format) throws XMLStreamException { - if (isDestructiveWrite()) { + if (isExpanded) { + super.serialize(output, format); + } else if (isDestructiveWrite()) { forceExpand(); super.serialize(output, format); } else { @@ -781,7 +783,9 @@ * serialize(java.io.Writer, org.apache.axiom.om.OMOutputFormat) */ public void serialize(Writer writer, OMOutputFormat format) throws XMLStreamException { - if (isDestructiveWrite()) { + if (isExpanded) { + super.serialize(writer, format); + } else if (isDestructiveWrite()) { forceExpand(); super.serialize(writer, format); } else { @@ -972,7 +976,9 @@ * @see org.apache.axiom.om.impl.llom.OMElementImpl#toString() */ public String toString() { - if (isDestructiveWrite()) { + if (isExpanded) { + return super.toString(); + } else if (isDestructiveWrite()) { forceExpand(); return super.toString(); } else { Modified: webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/llom/OMSourcedElementTest.java URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/llom/OMSourcedElementTest.java?rev=727579&r1=727578&r2=727579&view=diff ============================================================================== --- webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/llom/OMSourcedElementTest.java (original) +++ webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/llom/OMSourcedElementTest.java Wed Dec 17 15:43:12 2008 @@ -21,12 +21,14 @@ import org.apache.axiom.om.AbstractTestCase; import org.apache.axiom.om.OMDataSource; +import org.apache.axiom.om.OMDataSourceExt; import org.apache.axiom.om.OMDocument; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axiom.om.OMNode; import org.apache.axiom.om.OMOutputFormat; +import org.apache.axiom.om.ds.CharArrayDataSource; import org.apache.axiom.om.impl.OMNamespaceImpl; import org.apache.axiom.om.impl.builder.StAXOMBuilder; import org.apache.axiom.om.impl.llom.factory.OMLinkedListImplFactory; @@ -1049,6 +1051,26 @@ assertFalse(element.isExpanded()); } + public void testSerializeModifiedOMSEWithNonDestructiveDataSource() throws Exception { + OMDataSourceExt ds = new CharArrayDataSource("<element><child/></element>".toCharArray()); + assertFalse(ds.isDestructiveWrite()); + + OMFactory f = new OMLinkedListImplFactory(); + OMElement element = new OMSourcedElementImpl("element", null, f, ds); + + element.getFirstElement().setText("TEST"); + + StringWriter sw = new StringWriter(); + element.serialize(sw); + assertTrue(sw.toString().indexOf("TEST") != -1); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + element.serialize(baos); + assertTrue(new String(baos.toByteArray(), "UTF-8").indexOf("TEST") != -1); + + assertTrue(element.toString().indexOf("TEST") != -1); + } + private static class TestDataSource implements OMDataSource { // The data source is a ByteArrayInputStream so that we can verify that the datasource // is only accessed once. Currently there is no way to identify a destructive vs. non-destructive OMDataSource.