Search in sources :

Example 11 with DynamicJAXBContext

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

the class DynamicJAXBContextCreationTestCases method testNewInstanceXSDExternalBindingError.

public void testNewInstanceXSDExternalBindingError() throws Exception {
    // To use external bindings files, both schema and .xjb must be given as Sources
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream iStream = classLoader.getResourceAsStream(EXAMPLE_XSD);
    if (iStream == null) {
        fail("Couldn't load metadata file [" + EXAMPLE_XSD + "]");
    }
    InputStream xjbStream = classLoader.getResourceAsStream(EXTERNAL_BINDINGS);
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(DynamicJAXBContextFactory.XML_SCHEMA_KEY, iStream);
    ArrayList<InputStream> xjbs = new ArrayList<InputStream>();
    xjbs.add(xjbStream);
    xjbs.add(xjbStream);
    properties.put(DynamicJAXBContextFactory.EXTERNAL_BINDINGS_KEY, xjbs);
    JAXBException caughtEx = null;
    try {
        DynamicJAXBContext jaxbContext = (DynamicJAXBContext) JAXBContext.newInstance("org.eclipse.persistence.testing.jaxb.dynamic", classLoader, properties);
    } catch (JAXBException e) {
        caughtEx = e;
    } catch (Exception e) {
        fail("Unexpected exception thrown. " + e);
    }
    org.eclipse.persistence.exceptions.JAXBException jEx = null;
    Exception currentException = caughtEx;
    // Walk the exception looking for an EclipseLink JAXBException
    while (true) {
        if (currentException instanceof JAXBException) {
            Exception linkedEx = (Exception) ((JAXBException) currentException).getLinkedException();
            if (linkedEx instanceof org.eclipse.persistence.exceptions.JAXBException) {
                jEx = (org.eclipse.persistence.exceptions.JAXBException) linkedEx;
                break;
            } else {
                currentException = linkedEx;
            }
        } else {
            break;
        }
    }
    if (jEx == null) {
        fail("Unexpected exception thrown. " + caughtEx);
    }
    assertEquals("Unexpected EclipseLink exception thrown.", org.eclipse.persistence.exceptions.JAXBException.XJB_NOT_SOURCE, jEx.getErrorCode());
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) JAXBException(jakarta.xml.bind.JAXBException) ArrayList(java.util.ArrayList) DynamicException(org.eclipse.persistence.exceptions.DynamicException) JAXBException(jakarta.xml.bind.JAXBException) DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext)

Example 12 with DynamicJAXBContext

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

the class DynamicJAXBContextCreationTestCases method testNewInstanceStringLoader.

public void testNewInstanceStringLoader() throws JAXBException {
    DynamicJAXBContext jaxbContext = (DynamicJAXBContext) JAXBContext.newInstance(SESSION_NAMES, Thread.currentThread().getContextClassLoader());
    DynamicEntity docWrapper = jaxbContext.newDynamicEntity(DOCWRAPPER_CLASS_NAME);
    assertNotNull(docWrapper);
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity)

Example 13 with DynamicJAXBContext

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

the class DynamicJAXBContextCreationTestCases method testCreateContextFromXSDNodeError.

public void testCreateContextFromXSDNodeError() throws Exception {
    InputStream inputStream = ClassLoader.getSystemResourceAsStream(EXAMPLE_XSD);
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document xsdDocument = docBuilder.parse(inputStream);
    Node textNode = xsdDocument.createTextNode("TEXT NODE");
    JAXBException caughtException = null;
    try {
        DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(textNode, null, null, null);
    } catch (JAXBException e) {
        caughtException = e;
    }
    assertNotNull("Did not catch exception as expected.", caughtException);
    assertEquals("Incorrect exception thrown.", 50039, ((org.eclipse.persistence.exceptions.JAXBException) caughtException.getLinkedException()).getErrorCode());
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) JAXBException(jakarta.xml.bind.JAXBException) Document(org.w3c.dom.Document)

Example 14 with DynamicJAXBContext

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

the class DynamicJAXBContextCreationTestCases method testNewInstanceClassesPropsError.

public void testNewInstanceClassesPropsError() throws JAXBException {
    Class<?>[] classes = new Class<?>[] { this.getClass() };
    JAXBException ex = null;
    try {
        DynamicJAXBContext jaxbContext = (DynamicJAXBContext) JAXBContext.newInstance(classes, new HashMap());
    } catch (JAXBException e) {
        ex = e;
    }
    assertNotNull("Did not catch exception as expected.", ex);
    assertEquals("Incorrect exception thrown.", 50038, ((org.eclipse.persistence.exceptions.JAXBException) ex.getLinkedException()).getErrorCode());
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) HashMap(java.util.HashMap) JAXBException(jakarta.xml.bind.JAXBException)

Example 15 with DynamicJAXBContext

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

the class DynamicJAXBContextCreationTestCases method testNewInstanceXSDExternalBinding.

public void testNewInstanceXSDExternalBinding() throws Exception {
    System.clearProperty(JAXBContext.JAXB_CONTEXT_FACTORY);
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);
    InputStream xsdStream = ClassLoader.getSystemResourceAsStream(EXAMPLE_XSD);
    Source xsdSource = new StreamSource(xsdStream);
    // Set SYSTEM_ID to the filename part of the fully qualified EXAMPLE_XSD
    xsdSource.setSystemId(EXAMPLE_XSD.substring(EXAMPLE_XSD.lastIndexOf('/') + 1));
    InputStream xjbStream = classLoader.getResourceAsStream(EXTERNAL_BINDINGS);
    Source xjbSource = new StreamSource(xjbStream);
    // Set SYSTEM_ID to be the same as the XSD
    xjbSource.setSystemId(xsdSource.getSystemId());
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(DynamicJAXBContextFactory.XML_SCHEMA_KEY, xsdSource);
    properties.put(DynamicJAXBContextFactory.EXTERNAL_BINDINGS_KEY, xjbSource);
    properties.put(JAXBContextProperties.MOXY_FACTORY, JAXBContextProperties.Factory.DYNAMIC);
    // Have to include a path to a jaxb.properties, so just reusing a context path that does contain one.
    DynamicJAXBContext jaxbContext = (DynamicJAXBContext) JAXBContext.newInstance("org.eclipse.persistence.testing.jaxb.dynamic", classLoader, properties);
    DynamicEntity emp = jaxbContext.newDynamicEntity(ALT_EMPLOYEE_CLASS_NAME);
    assertNotNull(emp);
    try {
        // These sets will fail if the external bindings file was not read properly
        emp.set("empId", "747");
    } catch (DynamicException e) {
        fail("External bindings file not applied.");
    }
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) 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) DynamicException(org.eclipse.persistence.exceptions.DynamicException)

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