use of org.eclipse.persistence.oxm.XMLRoot in project eclipselink by eclipse-ee4j.
the class JAXBTestCases method testObjectToContentHandler.
@Override
public void testObjectToContentHandler() throws Exception {
SAXDocumentBuilder builder = new SAXDocumentBuilder();
Object objectToWrite = getWriteControlObject();
XMLDescriptor desc = null;
if (objectToWrite instanceof XMLRoot) {
desc = (XMLDescriptor) xmlContext.getSession(0).getProject().getDescriptor(((XMLRoot) objectToWrite).getObject().getClass());
} else {
desc = (XMLDescriptor) xmlContext.getSession(0).getProject().getDescriptor(objectToWrite.getClass());
}
int sizeBefore = getNamespaceResolverSize(desc);
jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
try {
jaxbMarshaller.marshal(objectToWrite, builder);
} catch (Exception e) {
assertMarshalException(e);
return;
}
if (expectsMarshalException) {
fail("An exception should have occurred but didn't.");
return;
}
int sizeAfter = getNamespaceResolverSize(desc);
assertEquals(sizeBefore, sizeAfter);
Document controlDocument = getWriteControlDocument();
Document testDocument = builder.getDocument();
log("**testObjectToContentHandler**");
log("Expected:");
log(controlDocument);
log("\nActual:");
log(testDocument);
// Diff diff = new Diff(controlDocument, testDocument);
// this.assertXMLEqual(diff, true);
assertXMLIdentical(controlDocument, testDocument);
}
use of org.eclipse.persistence.oxm.XMLRoot in project eclipselink by eclipse-ee4j.
the class ExternalMetadataTestCases method testObjectToOutputStream.
@Override
public void testObjectToOutputStream() throws Exception {
Object objectToWrite = getWriteControlObject();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
XMLDescriptor desc = null;
if (objectToWrite instanceof XMLRoot) {
desc = (XMLDescriptor) xmlContext.getSession(0).getProject().getDescriptor(((XMLRoot) objectToWrite).getObject().getClass());
} else {
desc = (XMLDescriptor) xmlContext.getSession(0).getProject().getDescriptor(objectToWrite.getClass());
}
int sizeBefore = getNamespaceResolverSize(desc);
jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
jaxbMarshaller.marshal(objectToWrite, stream);
int sizeAfter = getNamespaceResolverSize(desc);
assertEquals(sizeBefore, sizeAfter);
InputStream is = new ByteArrayInputStream(stream.toByteArray());
Document testDocument = parser.parse(is);
stream.close();
is.close();
objectToXMLDocumentTest(testDocument);
if (getProperties() != null) {
log("************test with JSON bindings*********");
ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
JAXBContext jaxbContextFromJSONBindings = createJaxbContextFromJSONBindings();
Marshaller jaxbMarshallerFromJSONBindings = jaxbContextFromJSONBindings.createMarshaller();
jaxbMarshallerFromJSONBindings.setAttachmentMarshaller(jaxbMarshaller.getAttachmentMarshaller());
jaxbMarshallerFromJSONBindings.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, jaxbMarshaller.getProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER));
// before marshalling object again, need to reset it's triggered events
((Employee) objectToWrite).triggeredEvents = new ArrayList();
jaxbMarshallerFromJSONBindings.marshal(objectToWrite, stream2);
InputStream is2 = new ByteArrayInputStream(stream2.toByteArray());
Document testDocument2 = parser.parse(is2);
stream2.close();
is2.close();
objectToXMLDocumentTest(testDocument2);
}
}
use of org.eclipse.persistence.oxm.XMLRoot in project eclipselink by eclipse-ee4j.
the class JAXBInheritanceNSSeparatorTestCases method testObjectToXMLStreamWriterRecordJSON.
public void testObjectToXMLStreamWriterRecordJSON() throws Exception {
if (XML_OUTPUT_FACTORY != null) {
StringWriter writer = new StringWriter();
XMLOutputFactory factory = XMLOutputFactory.newInstance();
factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE);
XMLStreamWriter streamWriter = factory.createXMLStreamWriter(writer);
Object objectToWrite = getWriteControlObject();
XMLDescriptor desc = null;
if (objectToWrite instanceof XMLRoot) {
desc = (XMLDescriptor) xmlContext.getSession(0).getProject().getDescriptor(((XMLRoot) objectToWrite).getObject().getClass());
} else {
desc = (XMLDescriptor) xmlContext.getSession(0).getProject().getDescriptor(objectToWrite.getClass());
}
jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
int sizeBefore = getNamespaceResolverSize(desc);
XMLStreamWriterRecord record = new XMLStreamWriterRecord(streamWriter);
((org.eclipse.persistence.jaxb.JAXBMarshaller) jaxbMarshaller).marshal(objectToWrite, record);
streamWriter.flush();
int sizeAfter = getNamespaceResolverSize(desc);
assertEquals(sizeBefore, sizeAfter);
StringReader reader = new StringReader(writer.toString());
InputSource inputSource = new InputSource(reader);
Document testDocument = parser.parse(inputSource);
writer.close();
reader.close();
objectToXMLDocumentTest(testDocument);
}
}
use of org.eclipse.persistence.oxm.XMLRoot in project eclipselink by eclipse-ee4j.
the class TypeMappingInfoTestCases method testObjectToXMLStreamWriterWithTypeMappingInfo.
public void testObjectToXMLStreamWriterWithTypeMappingInfo() throws Exception {
if (XML_OUTPUT_FACTORY != null) {
StringWriter writer = new StringWriter();
XMLOutputFactory factory = XMLOutputFactory.newInstance();
factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE);
XMLStreamWriter streamWriter = factory.createXMLStreamWriter(writer);
Object objectToWrite = getWriteControlObject();
XMLDescriptor desc = null;
if (objectToWrite instanceof XMLRoot) {
desc = (XMLDescriptor) ((org.eclipse.persistence.jaxb.JAXBContext) jaxbContext).getXMLContext().getSession(0).getProject().getDescriptor(((XMLRoot) objectToWrite).getObject().getClass());
} else {
desc = (XMLDescriptor) ((org.eclipse.persistence.jaxb.JAXBContext) jaxbContext).getXMLContext().getSession(0).getProject().getDescriptor(objectToWrite.getClass());
}
int sizeBefore = getNamespaceResolverSize(desc);
((JAXBMarshaller) jaxbMarshaller).marshal(objectToWrite, streamWriter, getTypeMappingInfo());
streamWriter.flush();
int sizeAfter = getNamespaceResolverSize(desc);
assertEquals(sizeBefore, sizeAfter);
StringReader reader = new StringReader(writer.toString());
InputSource inputSource = new InputSource(reader);
Document testDocument = parser.parse(inputSource);
writer.close();
reader.close();
objectToXMLDocumentTest(testDocument);
}
}
use of org.eclipse.persistence.oxm.XMLRoot in project eclipselink by eclipse-ee4j.
the class TypeMappingInfoTestCases method testObjectToResultWithTypeMappingInfoXML.
public void testObjectToResultWithTypeMappingInfoXML() throws Exception {
Object objectToWrite = getWriteControlObject();
XMLDescriptor desc = null;
if (objectToWrite instanceof XMLRoot) {
desc = (XMLDescriptor) ((org.eclipse.persistence.jaxb.JAXBContext) jaxbContext).getXMLContext().getSession(0).getProject().getDescriptor(((XMLRoot) objectToWrite).getObject().getClass());
} else {
desc = (XMLDescriptor) ((org.eclipse.persistence.jaxb.JAXBContext) jaxbContext).getXMLContext().getSession(0).getProject().getDescriptor(objectToWrite.getClass());
}
int sizeBefore = getNamespaceResolverSize(desc);
StringWriter stringWriter = new StringWriter();
StreamResult result = new StreamResult(stringWriter);
jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
((JAXBMarshaller) jaxbMarshaller).marshal(objectToWrite, result, getTypeMappingInfo());
int sizeAfter = getNamespaceResolverSize(desc);
assertEquals(sizeBefore, sizeAfter);
StringReader reader = new StringReader(stringWriter.toString());
InputSource inputSource = new InputSource(reader);
Document testDocument = parser.parse(inputSource);
stringWriter.close();
reader.close();
objectToXMLDocumentTest(testDocument);
}
Aggregations