|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0, |
| 7 | + * or the Eclipse Distribution License v. 1.0 which is available at |
| 8 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 9 | + * |
| 10 | + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
| 11 | + */ |
| 12 | + |
| 13 | +// Contributors: |
| 14 | +// Oracle - initial API and implementation |
| 15 | +package org.eclipse.persistence.testing.jaxb.xmlanyelement.ns3; |
| 16 | + |
| 17 | +import org.eclipse.persistence.jaxb.MarshallerProperties; |
| 18 | +import org.eclipse.persistence.oxm.XMLDescriptor; |
| 19 | +import org.eclipse.persistence.oxm.XMLRoot; |
| 20 | +import org.eclipse.persistence.oxm.record.XMLStreamWriterRecord; |
| 21 | +import org.eclipse.persistence.platform.xml.XMLPlatform; |
| 22 | +import org.eclipse.persistence.platform.xml.XMLPlatformFactory; |
| 23 | +import org.eclipse.persistence.testing.jaxb.JAXBTestCases; |
| 24 | +import org.w3c.dom.Document; |
| 25 | +import org.w3c.dom.Element; |
| 26 | + |
| 27 | +import javax.xml.stream.XMLOutputFactory; |
| 28 | +import javax.xml.stream.XMLStreamWriter; |
| 29 | +import java.io.StringWriter; |
| 30 | + |
| 31 | +public class DefaultNamespace3TestCases extends JAXBTestCases { |
| 32 | + |
| 33 | + private final static String XML_RESOURCE = "org/eclipse/persistence/testing/jaxb/xmlanyelement/ns/root3.xml"; |
| 34 | + |
| 35 | + public DefaultNamespace3TestCases(String name) throws Exception { |
| 36 | + super(name); |
| 37 | + setControlDocument(XML_RESOURCE); |
| 38 | + Class<?>[] classes = new Class<?>[1]; |
| 39 | + classes[0] = Root.class; |
| 40 | + setClasses(classes); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected Root getControlObject() { |
| 45 | + Root root = new Root(); |
| 46 | + |
| 47 | + XMLPlatform xmlPlatform = XMLPlatformFactory.getInstance().getXMLPlatform(); |
| 48 | + Document document = xmlPlatform.createDocument(); |
| 49 | + Element element = document.createElementNS("urn:namespace1", "childelem"); |
| 50 | + element.setAttributeNS(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns", "urn:namespace1"); |
| 51 | + element.setAttributeNS(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:ns1", "urn:attributeNs0"); |
| 52 | + element.setAttributeNS("urn:attributeNs0", "ns1:attr1", "attr1Value"); |
| 53 | + root.setChild(element); |
| 54 | + |
| 55 | + return root; |
| 56 | + } |
| 57 | + |
| 58 | +/* |
| 59 | + @Override |
| 60 | + public Root getReadControlObject() { |
| 61 | + Root root = new Root(); |
| 62 | +
|
| 63 | + XMLPlatform xmlPlatform = XMLPlatformFactory.getInstance().getXMLPlatform(); |
| 64 | + Document document = xmlPlatform.createDocument(); |
| 65 | + Element element = document.createElementNS("urn:namespace1", "childelem"); |
| 66 | + element.setAttributeNS(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns", "urn:namespace1"); |
| 67 | + element.setAttributeNS(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:ns1", "urn:attributeNs0"); |
| 68 | + element.setAttributeNS("urn:attributeNs0", "ns1:attr1", "attr1Value"); |
| 69 | + root.setChild(element); |
| 70 | +
|
| 71 | + return root; |
| 72 | + } |
| 73 | +*/ |
| 74 | + |
| 75 | + public void testObjectToXMLStreamWriterRepairing() throws Exception { |
| 76 | + if(XML_OUTPUT_FACTORY != null) { |
| 77 | + StringWriter writer = new StringWriter(); |
| 78 | + |
| 79 | + XMLOutputFactory factory = XMLOutputFactory.newInstance(); |
| 80 | + factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); |
| 81 | + XMLStreamWriter streamWriter= factory.createXMLStreamWriter(writer); |
| 82 | + |
| 83 | + Object objectToWrite = getWriteControlObject(); |
| 84 | + XMLDescriptor desc = null; |
| 85 | + if (objectToWrite instanceof XMLRoot) { |
| 86 | + desc = (XMLDescriptor)xmlContext.getSession(0).getProject().getDescriptor(((XMLRoot)objectToWrite).getObject().getClass()); |
| 87 | + } else { |
| 88 | + desc = (XMLDescriptor)xmlContext.getSession(0).getProject().getDescriptor(objectToWrite.getClass()); |
| 89 | + } |
| 90 | + jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml"); |
| 91 | + |
| 92 | + int sizeBefore = getNamespaceResolverSize(desc); |
| 93 | + try { |
| 94 | + jaxbMarshaller.marshal(objectToWrite, streamWriter); |
| 95 | + } catch(Exception e) { |
| 96 | + assertMarshalException(e); |
| 97 | + return; |
| 98 | + } |
| 99 | + if(expectsMarshalException){ |
| 100 | + fail("An exception should have occurred but didn't."); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + streamWriter.flush(); |
| 105 | + int sizeAfter = getNamespaceResolverSize(desc); |
| 106 | + |
| 107 | + assertEquals(sizeBefore, sizeAfter); |
| 108 | + Document testDocument = getTestDocument(writer.toString()); |
| 109 | + |
| 110 | + writer.close(); |
| 111 | + objectToXMLDocumentTest(testDocument); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public void testObjectToXMLStreamWriterRepairingRecord() throws Exception { |
| 116 | + if(XML_OUTPUT_FACTORY != null) { |
| 117 | + StringWriter writer = new StringWriter(); |
| 118 | + |
| 119 | + XMLOutputFactory factory = XMLOutputFactory.newInstance(); |
| 120 | + factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); |
| 121 | + XMLStreamWriter streamWriter= factory.createXMLStreamWriter(writer); |
| 122 | + |
| 123 | + Object objectToWrite = getWriteControlObject(); |
| 124 | + XMLDescriptor desc = null; |
| 125 | + if (objectToWrite instanceof XMLRoot) { |
| 126 | + desc = (XMLDescriptor)xmlContext.getSession(0).getProject().getDescriptor(((XMLRoot)objectToWrite).getObject().getClass()); |
| 127 | + } else { |
| 128 | + desc = (XMLDescriptor)xmlContext.getSession(0).getProject().getDescriptor(objectToWrite.getClass()); |
| 129 | + } |
| 130 | + jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml"); |
| 131 | + |
| 132 | + int sizeBefore = getNamespaceResolverSize(desc); |
| 133 | + XMLStreamWriterRecord record = new XMLStreamWriterRecord(streamWriter); |
| 134 | + try { |
| 135 | + ((org.eclipse.persistence.jaxb.JAXBMarshaller)jaxbMarshaller).marshal(objectToWrite, record); |
| 136 | + } catch(Exception e) { |
| 137 | + assertMarshalException(e); |
| 138 | + return; |
| 139 | + } |
| 140 | + if(expectsMarshalException){ |
| 141 | + fail("An exception should have occurred but didn't."); |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + streamWriter.flush(); |
| 146 | + int sizeAfter = getNamespaceResolverSize(desc); |
| 147 | + |
| 148 | + assertEquals(sizeBefore, sizeAfter); |
| 149 | + |
| 150 | + Document testDocument = getTestDocument(writer.toString()); |
| 151 | + writer.close(); |
| 152 | + objectToXMLDocumentTest(testDocument); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments