Search in sources :

Example 31 with DynamicJAXBContext

use of org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext in project eclipselink by eclipse-ee4j.

the class DynamicJAXBRefreshTestCases method testRefreshSchemaInputStream.

public void testRefreshSchemaInputStream() throws Exception {
    ClassLoader classLoader = this.getClass().getClassLoader();
    InputStream metadataStream = classLoader.getResourceAsStream(XSD_METADATA);
    DynamicJAXBContext jc = DynamicJAXBContextFactory.createContextFromXSD(metadataStream, null, classLoader, null);
    Exception caughtException = null;
    try {
        JAXBHelper.getJAXBContext(jc).refreshMetadata();
    } catch (Exception e) {
        caughtException = e;
    }
    assertNotNull("Refresh Exception was not thrown as expected.", caughtException);
    assertEquals("Incorrect exception thrown.", JAXBException.class, caughtException.getClass());
    assertEquals("Incorrect exception thrown.", JAXBException.CANNOT_REFRESH_METADATA, ((JAXBException) caughtException).getErrorCode());
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) InputStream(java.io.InputStream) JAXBException(org.eclipse.persistence.exceptions.JAXBException)

Example 32 with DynamicJAXBContext

use of org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext in project eclipselink by eclipse-ee4j.

the class DynamicJAXBUsingXMLNamesTestCases method testCreateEntityByXPathNameCollision1.

public void testCreateEntityByXPathNameCollision1() throws Exception {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream iStream = classLoader.getResourceAsStream(EXAMPLE_XSD_2);
    if (iStream == null) {
        fail("Couldn't load metadata file [" + EXAMPLE_XSD_2 + "]");
    }
    Map<String, InputStream> properties = new HashMap<String, InputStream>();
    properties.put(DynamicJAXBContextFactory.XML_SCHEMA_KEY, iStream);
    DynamicJAXBContext jaxbContext = (DynamicJAXBContext) JAXBContext.newInstance("org.eclipse.persistence.testing.jaxb.dynamic", classLoader, properties);
    DynamicEntity person = (DynamicEntity) jaxbContext.createByQualifiedName("mynamespace", "person", true);
    assertNotNull("Could not create Dynamic Entity.", person);
    NamespaceResolver nsResolver = new NamespaceResolver();
    nsResolver.put("ns0", "mynamespace");
    jaxbContext.setValueByXPath(person, "ns0:full_name/text()", nsResolver, "Larry King");
    Document marshalDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    JAXBElement jbe = new JAXBElement(new QName("person"), DynamicEntity.class, person);
    jaxbContext.createMarshaller().marshal(jbe, marshalDoc);
    DynamicEntity person2 = ((JAXBElement<DynamicEntity>) jaxbContext.createUnmarshaller().unmarshal(marshalDoc)).getValue();
    String newName = jaxbContext.getValueByXPath(person2, "ns0:full_name/text()", nsResolver, String.class);
    assertEquals("Larry King", newName);
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) HashMap(java.util.HashMap) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) JAXBElement(jakarta.xml.bind.JAXBElement) Document(org.w3c.dom.Document)

Example 33 with DynamicJAXBContext

use of org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext in project eclipselink by eclipse-ee4j.

the class DynamicJAXBUsingXMLNamesTestCases method testCreateEntityByXMLNameJSON.

public void testCreateEntityByXMLNameJSON() throws Exception {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream iStream = classLoader.getResourceAsStream(EXAMPLE_OXM);
    if (iStream == null) {
        fail("Couldn't load metadata file [" + EXAMPLE_OXM + "]");
    }
    HashMap<String, Source> metadataSourceMap = new HashMap<String, Source>();
    metadataSourceMap.put("org.eclipse.persistence.testing.jaxb.dynamic", new StreamSource(iStream));
    Map<String, Map<String, Source>> properties = new HashMap<String, Map<String, Source>>();
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);
    DynamicJAXBContext jaxbContext = (DynamicJAXBContext) JAXBContext.newInstance("org.eclipse.persistence.testing.jaxb.dynamic", classLoader, properties);
    DynamicEntity employee = (DynamicEntity) jaxbContext.createByQualifiedName(EMPLOYEE_NAMESPACE, EMPLOYEE_TYPE_NAME, false);
    assertNotNull("Could not create Dynamic Entity.", employee);
    NamespaceResolver nsResolver = new NamespaceResolver();
    nsResolver.put("ns0", "mynamespace");
    jaxbContext.setValueByXPath(employee, "name/text()", nsResolver, "Larry King");
    jaxbContext.setValueByXPath(employee, "employee-id/text()", nsResolver, "CA34287");
    JAXBMarshaller m = jaxbContext.createMarshaller();
    m.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
    Document marshalDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    m.marshal(employee, marshalDoc);
    JAXBUnmarshaller u = jaxbContext.createUnmarshaller();
    u.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
    DynamicEntity employee2 = (DynamicEntity) u.unmarshal(marshalDoc);
    String newName = jaxbContext.getValueByXPath(employee2, "name/text()", nsResolver, String.class);
    String newId = jaxbContext.getValueByXPath(employee2, "employee-id/text()", nsResolver, String.class);
    assertEquals("Larry King", newName);
    assertEquals("CA34287", newId);
}
Also used : HashMap(java.util.HashMap) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Document(org.w3c.dom.Document) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) HashMap(java.util.HashMap) Map(java.util.Map)

Example 34 with DynamicJAXBContext

use of org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext in project eclipselink by eclipse-ee4j.

the class DynamicJAXBContextCreationTestCases method testCreateContextFromOXM.

public void testCreateContextFromOXM() throws JAXBException {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream iStream = classLoader.getResourceAsStream(EXAMPLE_OXM);
    if (iStream == null) {
        fail("Couldn't load metadata file [" + EXAMPLE_OXM + "]");
    }
    HashMap<String, Source> metadataSourceMap = new HashMap<String, Source>();
    metadataSourceMap.put("org.eclipse.persistence.testing.jaxb.dynamic", new StreamSource(iStream));
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);
    DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromOXM(classLoader, properties);
    DynamicEntity person = jaxbContext.newDynamicEntity(PERSON_CLASS_NAME);
    assertNotNull(person);
// DynamicEntity person2 = jaxbContext.getObjectFactory().createInstance(PERSON_CLASS_NAME);
// assertNotNull(person2);
// JAXBElement<DynamicEntity> personElem = jaxbContext.getObjectFactory().createElementInstance(new QName("mynamespace", "human"), person2);
// assertNotNull(personElem);
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) HashMap(java.util.HashMap) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Example 35 with DynamicJAXBContext

use of org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext in project eclipselink by eclipse-ee4j.

the class DynamicJAXBContextCreationTestCases method testCreateContextFromSessionsXMLString.

public void testCreateContextFromSessionsXMLString() throws JAXBException {
    DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContext(SESSION_NAMES, null, null);
    DynamicEntity docWrapper = jaxbContext.newDynamicEntity(DOCWRAPPER_CLASS_NAME);
    assertNotNull(docWrapper);
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity)

Aggregations

DynamicJAXBContext (org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext)44 DynamicEntity (org.eclipse.persistence.dynamic.DynamicEntity)32 InputStream (java.io.InputStream)21 HashMap (java.util.HashMap)17 Document (org.w3c.dom.Document)12 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)10 StreamSource (javax.xml.transform.stream.StreamSource)10 Source (javax.xml.transform.Source)8 JAXBException (jakarta.xml.bind.JAXBException)7 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 DocumentBuilder (javax.xml.parsers.DocumentBuilder)6 DOMSource (javax.xml.transform.dom.DOMSource)6 SchemaVersion (org.onap.aai.setup.SchemaVersion)6 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 Element (org.w3c.dom.Element)5 JAXBElement (jakarta.xml.bind.JAXBElement)4 Map (java.util.Map)4 DynamicException (org.eclipse.persistence.exceptions.DynamicException)4