Search in sources :

Example 16 with XmlBindings

use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.

the class XMLBindingsPopulatedTestCases method testXmlToObjectToJsonToObjectToXML.

public void testXmlToObjectToJsonToObjectToXML() throws Exception {
    StringWriter writer = new StringWriter();
    // unmarshal control XML
    InputStream instream = ClassLoader.getSystemResourceAsStream(resourceName);
    jaxbUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/xml");
    Object testObject = jaxbUnmarshaller.unmarshal(instream);
    // marshal to JSON
    jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
    StringWriter sw = new StringWriter();
    jaxbMarshaller.marshal(testObject, sw);
    // unmarshal from JSON
    StringReader sr = new StringReader(sw.toString());
    jaxbUnmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
    JAXBElement<XmlBindings> jbe = jaxbUnmarshaller.unmarshal(new StreamSource(sr), XmlBindings.class);
    Object unmarshalledJSON = jbe.getValue();
    // marshal to XML again
    jaxbMarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
    StringWriter xmlStringWriter = new StringWriter();
    jaxbMarshaller.marshal(testObject, xmlStringWriter);
    InputSource inputSource = new InputSource(new StringReader(xmlStringWriter.toString()));
    Document testDocument = parser.parse(inputSource);
    objectToXMLDocumentTest(testDocument);
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) InputSource(org.xml.sax.InputSource) StringWriter(java.io.StringWriter) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Example 17 with XmlBindings

use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.

the class XMLBindingsPopulatedTestCases method getControlObject.

@Override
protected Object getControlObject() {
    XmlBindings xmlBindings = new XmlBindings();
    xmlBindings.setPackageName("test.model");
    xmlBindings.setXmlAccessorType(XmlAccessType.PUBLIC_MEMBER);
    xmlBindings.setXmlAccessorOrder(org.eclipse.persistence.jaxb.xmlmodel.XmlAccessOrder.ALPHABETICAL);
    XmlEnums enums = new XmlEnums();
    XmlEnum e1 = new XmlEnum();
    e1.setJavaEnum("A");
    e1.setValue("A_Value");
    XmlEnum e2 = new XmlEnum();
    e2.setJavaEnum("B");
    e2.setValue("B_Value");
    enums.getXmlEnum().add(e1);
    enums.getXmlEnum().add(e2);
    xmlBindings.setXmlEnums(enums);
    xmlBindings.setXmlNameTransformer("test.someNameTransformer");
    XmlSchema xmlSchema = new XmlSchema();
    xmlSchema.setAttributeFormDefault(XmlNsForm.UNQUALIFIED);
    xmlSchema.setElementFormDefault(XmlNsForm.UNQUALIFIED);
    xmlSchema.setLocation("someLocation");
    xmlSchema.setNamespace("testnamespace");
    xmlBindings.setXmlSchema(xmlSchema);
    XmlSchemaTypes xmlSchemaTypes = new XmlSchemaTypes();
    XmlSchemaType xmlSchemaType = new XmlSchemaType();
    xmlSchemaType.setName("someSchemaType");
    xmlSchemaType.setNamespace("someSchemaTypeNamespace");
    xmlSchemaType.setType("someSchemaType");
    XmlSchemaType xmlSchemaType2 = new XmlSchemaType();
    xmlSchemaType2.setName("someSchemaType2");
    xmlSchemaType2.setNamespace("someSchemaTypeNamespace2");
    xmlSchemaType2.setType("someSchemaType2");
    xmlSchemaTypes.getXmlSchemaType().add(xmlSchemaType);
    xmlSchemaTypes.getXmlSchemaType().add(xmlSchemaType2);
    xmlBindings.setXmlSchemaTypes(xmlSchemaTypes);
    xmlBindings.setXmlMappingMetadataComplete(Boolean.FALSE);
    XmlRegistries xmlRegistries = new XmlRegistries();
    XmlRegistry r1 = new XmlRegistry();
    r1.setName("someRegistry");
    xmlRegistries.getXmlRegistry().add(r1);
    xmlBindings.setXmlRegistries(xmlRegistries);
    XmlJavaTypeAdapters xmlJavaTypeAdapters = new XmlJavaTypeAdapters();
    XmlJavaTypeAdapter a1 = new XmlJavaTypeAdapter();
    a1.setType("someAdapterType");
    a1.setValue("someAdapterValue");
    a1.setValueType("somValueType");
    a1.setJavaAttribute("someJavaAttribute");
    xmlJavaTypeAdapters.getXmlJavaTypeAdapter().add(a1);
    xmlBindings.setXmlJavaTypeAdapters(xmlJavaTypeAdapters);
    JavaTypes types = new JavaTypes();
    JavaType javaType = new JavaType();
    javaType.setName("myType");
    javaType.setXmlTransient(Boolean.FALSE);
    javaType.setXmlAccessorOrder(org.eclipse.persistence.jaxb.xmlmodel.XmlAccessOrder.ALPHABETICAL);
    XmlProperties xmlProperties = new XmlProperties();
    XmlProperty p1 = new XmlProperty();
    p1.setName("prop1");
    p1.setValue("propValue");
    p1.setValueType("propValueType");
    xmlProperties.getXmlProperty().add(p1);
    javaType.setXmlProperties(xmlProperties);
    javaType.setXmlDiscriminatorNode("descriminator node");
    javaType.setXmlDiscriminatorValue("discriminator value");
    XmlClassExtractor extractor = new XmlClassExtractor();
    extractor.setClazz("a.b.c.someClass");
    javaType.setXmlClassExtractor(extractor);
    javaType.setXmlInlineBinaryData(Boolean.FALSE);
    XmlRootElement root = new XmlRootElement();
    root.setName("someRoot");
    root.setNamespace("someNamespace");
    javaType.setXmlRootElement(root);
    XmlType xmlType = new XmlType();
    xmlType.setFactoryClass("somepackage.someFactoryclass");
    xmlType.setName("someName");
    xmlType.setFactoryMethod("someMethod");
    xmlType.getPropOrder().add("p2");
    xmlType.getPropOrder().add("p1");
    javaType.setXmlType(xmlType);
    JavaAttributes javaAttributes = new JavaAttributes();
    XmlElement javaAttribute = new XmlElement();
    javaAttribute.setName("elementName");
    javaAttribute.setJavaAttribute("theJavaAttributeValue");
    JAXBElement<XmlElement> jbe = new JAXBElement<XmlElement>(new QName("http://www.eclipse.org/eclipselink/xsds/persistence/oxm", "xml-element"), XmlElement.class, javaAttribute);
    XmlElement javaAttribute2 = new XmlElement();
    javaAttribute2.setName("elementName2");
    javaAttribute2.setJavaAttribute("theJavaAttributeValue2");
    JAXBElement<XmlElement> jbe2 = new JAXBElement<XmlElement>(new QName("http://www.eclipse.org/eclipselink/xsds/persistence/oxm", "xml-element"), XmlElement.class, javaAttribute2);
    XmlAttribute javaAttribute3 = new XmlAttribute();
    javaAttribute3.setContainerType("someContainerType");
    javaAttribute3.setJavaAttribute("javaAttribute");
    javaAttribute3.setNamespace("somenamespace");
    javaAttribute3.setName("attributename1");
    javaAttribute3.setReadOnly(Boolean.TRUE);
    javaAttribute3.setRequired(Boolean.TRUE);
    JAXBElement<XmlAttribute> jbe3 = new JAXBElement<XmlAttribute>(new QName("http://www.eclipse.org/eclipselink/xsds/persistence/oxm", "xml-attribute"), XmlAttribute.class, javaAttribute3);
    javaAttributes.getJavaAttribute().add(jbe);
    javaAttributes.getJavaAttribute().add(jbe3);
    javaAttributes.getJavaAttribute().add(jbe2);
    javaType.setJavaAttributes(javaAttributes);
    types.getJavaType().add(javaType);
    xmlBindings.setJavaTypes(types);
    return xmlBindings;
}
Also used : JavaTypes(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.JavaTypes) XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) XmlEnums(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.XmlEnums) XmlRootElement(org.eclipse.persistence.jaxb.xmlmodel.XmlRootElement) XmlProperties(org.eclipse.persistence.jaxb.xmlmodel.XmlProperties) XmlAttribute(org.eclipse.persistence.jaxb.xmlmodel.XmlAttribute) XmlProperty(org.eclipse.persistence.jaxb.xmlmodel.XmlProperties.XmlProperty) QName(javax.xml.namespace.QName) XmlJavaTypeAdapter(org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter) JAXBElement(jakarta.xml.bind.JAXBElement) XmlSchemaType(org.eclipse.persistence.jaxb.xmlmodel.XmlSchemaType) XmlRegistry(org.eclipse.persistence.jaxb.xmlmodel.XmlRegistry) XmlSchemaTypes(org.eclipse.persistence.jaxb.xmlmodel.XmlSchemaTypes) XmlEnum(org.eclipse.persistence.jaxb.xmlmodel.XmlEnum) XmlClassExtractor(org.eclipse.persistence.jaxb.xmlmodel.XmlClassExtractor) XmlType(org.eclipse.persistence.jaxb.xmlmodel.XmlType) JavaType(org.eclipse.persistence.jaxb.xmlmodel.JavaType) JavaAttributes(org.eclipse.persistence.jaxb.xmlmodel.JavaType.JavaAttributes) XmlSchema(org.eclipse.persistence.jaxb.xmlmodel.XmlSchema) XmlRegistries(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.XmlRegistries) XmlElement(org.eclipse.persistence.jaxb.xmlmodel.XmlElement) XmlJavaTypeAdapters(org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapters)

Example 18 with XmlBindings

use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.

the class XMLBindingsTestCases method getControlObject.

@Override
protected Object getControlObject() {
    XmlBindings xmlBindings = new XmlBindings();
    xmlBindings.setPackageName("myPackage");
    JavaTypes types = new JavaTypes();
    JavaType javaType = new JavaType();
    javaType.setName("myType");
    JavaAttributes javaAttributes = new JavaAttributes();
    XmlElement javaAttribute = new XmlElement();
    javaAttribute.setName("elementName");
    javaAttribute.setJavaAttribute("theJavaAttributeValue");
    JAXBElement<XmlElement> jbe = new JAXBElement<XmlElement>(new QName("http://www.eclipse.org/eclipselink/xsds/persistence/oxm", "xml-element"), XmlElement.class, javaAttribute);
    javaAttributes.getJavaAttribute().add(jbe);
    javaType.setJavaAttributes(javaAttributes);
    types.getJavaType().add(javaType);
    xmlBindings.setJavaTypes(types);
    return xmlBindings;
}
Also used : JavaTypes(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings.JavaTypes) XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) JavaType(org.eclipse.persistence.jaxb.xmlmodel.JavaType) JavaAttributes(org.eclipse.persistence.jaxb.xmlmodel.JavaType.JavaAttributes) QName(javax.xml.namespace.QName) XmlElement(org.eclipse.persistence.jaxb.xmlmodel.XmlElement) JAXBElement(jakarta.xml.bind.JAXBElement)

Example 19 with XmlBindings

use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.

the class XMLBindingsTestCases method jsonToObjectTest.

@Override
public void jsonToObjectTest(Object testObject) throws Exception {
    log("\n**xmlToObjectTest**");
    log("Expected:");
    log(getReadControlObject().toString());
    log("Actual:");
    log(testObject.toString());
    XmlBindings expectedBindings = (XmlBindings) getJSONReadControlObject();
    XmlBindings actualBindings = (XmlBindings) testObject;
    assertEquals(expectedBindings.getPackageName(), actualBindings.getPackageName());
    assertEquals(1, actualBindings.getJavaTypes().getJavaType().size());
    assertEquals(expectedBindings.getJavaTypes().getJavaType().get(0).getClass(), actualBindings.getJavaTypes().getJavaType().get(0).getClass());
    assertEquals(expectedBindings.getJavaTypes().getJavaType().get(0).getName(), actualBindings.getJavaTypes().getJavaType().get(0).getName());
    assertEquals(expectedBindings.getJavaTypes().getJavaType().get(0).getJavaAttributes().getJavaAttribute().size(), actualBindings.getJavaTypes().getJavaType().get(0).getJavaAttributes().getJavaAttribute().size());
    assertEquals(expectedBindings.getJavaTypes().getJavaType().get(0).getJavaAttributes().getJavaAttribute().get(0).getName(), actualBindings.getJavaTypes().getJavaType().get(0).getJavaAttributes().getJavaAttribute().get(0).getName());
    assertEquals(expectedBindings.getJavaTypes().getJavaType().get(0).getJavaAttributes().getJavaAttribute().get(0).getDeclaredType(), actualBindings.getJavaTypes().getJavaType().get(0).getJavaAttributes().getJavaAttribute().get(0).getDeclaredType());
    assertEquals(expectedBindings.getJavaTypes().getJavaType().get(0).getJavaAttributes().getJavaAttribute().get(0).getValue().getJavaAttribute(), actualBindings.getJavaTypes().getJavaType().get(0).getJavaAttributes().getJavaAttribute().get(0).getValue().getJavaAttribute());
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings)

Example 20 with XmlBindings

use of org.eclipse.persistence.jaxb.xmlmodel.XmlBindings in project eclipselink by eclipse-ee4j.

the class JAXBContextTransformationMappingTestCases method testInvalidTransformerClass.

/**
 * Test exception handling:  in this case an invalid transformer class
 * has been set.  An exception should be thrown.
 *
 * Negative test.
 */
public void testInvalidTransformerClass() {
    InputStream inputStream = ClassLoader.getSystemResourceAsStream(METADATA_FILE_BAD_CLASS);
    HashMap<String, Source> metadataSourceMap = new HashMap<>();
    metadataSourceMap.put("org.eclipse.persistence.testing.jaxb.externalizedmetadata.mappings.xmltransformation", new StreamSource(inputStream));
    Map<String, Object> properties = new HashMap<>();
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);
    int exceptionCount = 0;
    // test exception from MappingsGenerator
    try {
        JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class<?>[] { Employee.class }, properties, Thread.currentThread().getContextClassLoader());
    } catch (JAXBException e) {
        exceptionCount++;
    }
    assertTrue("The expected exception was never thrown.", exceptionCount > 0);
    exceptionCount--;
    // test exception from SchemaGenerator
    try {
        Map<String, XmlBindings> bindings = JAXBContextFactory.getXmlBindingsFromProperties(properties, Thread.currentThread().getContextClassLoader());
        JavaModelInputImpl jModelInput = new JavaModelInputImpl(new Class<?>[] { Employee.class }, new JavaModelImpl(new JaxbClassLoader(Thread.currentThread().getContextClassLoader(), new Class<?>[] { Employee.class })));
        Generator generator = new Generator(jModelInput, bindings, Thread.currentThread().getContextClassLoader(), "", false);
        generator.generateSchema();
    } catch (Exception e) {
        exceptionCount++;
    }
    assertTrue("The expected exception was never thrown.", exceptionCount > 0);
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) HashMap(java.util.HashMap) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(jakarta.xml.bind.JAXBException) JAXBContext(jakarta.xml.bind.JAXBContext) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JAXBException(jakarta.xml.bind.JAXBException) JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) JaxbClassLoader(org.eclipse.persistence.internal.jaxb.JaxbClassLoader) Generator(org.eclipse.persistence.jaxb.compiler.Generator)

Aggregations

XmlBindings (org.eclipse.persistence.jaxb.xmlmodel.XmlBindings)30 HashMap (java.util.HashMap)18 JAXBException (jakarta.xml.bind.JAXBException)16 StreamSource (javax.xml.transform.stream.StreamSource)16 JAXBContext (jakarta.xml.bind.JAXBContext)13 Unmarshaller (jakarta.xml.bind.Unmarshaller)12 Map (java.util.Map)11 StringReader (java.io.StringReader)10 XMLContext (org.eclipse.persistence.oxm.XMLContext)10 Project (org.eclipse.persistence.sessions.Project)10 XmlBindingsModel (org.eclipse.persistence.internal.xr.XmlBindingsModel)9 InputStream (java.io.InputStream)8 Platform (org.eclipse.persistence.internal.databaseaccess.Platform)8 ConversionManager (org.eclipse.persistence.internal.helper.ConversionManager)8 MetadataProcessor (org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor)8 DatabaseSessionImpl (org.eclipse.persistence.internal.sessions.DatabaseSessionImpl)8 XRDynamicClassLoader (org.eclipse.persistence.internal.xr.XRDynamicClassLoader)8 XMLPlatform (org.eclipse.persistence.platform.xml.XMLPlatform)8 DatabaseLogin (org.eclipse.persistence.sessions.DatabaseLogin)8 DatabaseSession (org.eclipse.persistence.sessions.DatabaseSession)8