Search in sources :

Example 36 with DynamicJAXBContext

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

the class DynamicJAXBContextCreationTestCases method testNewInstanceOXM.

public void testNewInstanceOXM() 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, Map<String, Source>> properties = new HashMap<String, Map<String, Source>>();
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);
    // 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 person = jaxbContext.newDynamicEntity(PERSON_CLASS_NAME);
    assertNotNull(person);
}
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) HashMap(java.util.HashMap) Map(java.util.Map) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Example 37 with DynamicJAXBContext

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

the class DynamicJAXBContextCreationTestCases method testNewInstanceXSDImportError.

public void testNewInstanceXSDImportError() throws Exception {
    // To use schema imports, schemas must be given as Sources
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream inputStream = ClassLoader.getSystemResourceAsStream(EXAMPLE_XSD);
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document xsdDocument = docBuilder.parse(inputStream);
    Element xsdElement = xsdDocument.getDocumentElement();
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(DynamicJAXBContextFactory.XML_SCHEMA_KEY, xsdElement);
    CustomEntityResolver re = new CustomEntityResolver(false);
    properties.put(DynamicJAXBContextFactory.ENTITY_RESOLVER_KEY, re);
    JAXBException caughtEx = null;
    try {
        DynamicJAXBContext jaxbContext = (DynamicJAXBContext) JAXBContext.newInstance("org.eclipse.persistence.testing.jaxb.dynamic", classLoader, properties);
        DynamicEntity person = jaxbContext.newDynamicEntity("mynamespace.Person");
        assertNotNull("Could not create Dynamic Entity.", person);
        DynamicEntity salary = jaxbContext.newDynamicEntity("banknamespace.CdnCurrency");
        assertNotNull("Could not create Dynamic Entity.", salary);
    } 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.XSD_IMPORT_NOT_SOURCE, jEx.getErrorCode());
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) HashMap(java.util.HashMap) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) JAXBException(jakarta.xml.bind.JAXBException) Document(org.w3c.dom.Document) DynamicException(org.eclipse.persistence.exceptions.DynamicException) JAXBException(jakarta.xml.bind.JAXBException) DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) CustomEntityResolver(org.eclipse.persistence.testing.jaxb.dynamic.util.CustomEntityResolver) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Example 38 with DynamicJAXBContext

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

the class DynamicWithStaticTestCases method getControlObject.

@Override
protected Object getControlObject() {
    DynamicEntity employee = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(PACKAGE + "." + "Customer");
    DynamicEntity phone1 = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(PACKAGE + "." + "PhoneNumber");
    DynamicEntity phone2 = ((DynamicJAXBContext) jaxbContext).newDynamicEntity(PACKAGE + "." + "PhoneNumber");
    phone1.set("value", "555-WORK");
    phone1.set("type", "work");
    phone2.set("value", "555-HOME");
    phone2.set("type", "home");
    ArrayList phones = new ArrayList();
    phones.add(phone1);
    phones.add(phone2);
    employee.set("phoneNumber", phones);
    Address address = new Address();
    address.city = "Any Town";
    address.street = "123 Some Street";
    employee.set("address", address);
    employee.set("name", "Jane Doe");
    return employee;
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) ArrayList(java.util.ArrayList)

Example 39 with DynamicJAXBContext

use of org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext in project aai-aai-common by onap.

the class OxmSchemaProvider method getVertexSchema.

@Override
public VertexSchema getVertexSchema(String vertexName, String schemaVersion) throws SchemaProviderException {
    DynamicJAXBContext jaxbContext = OxmSchemaLoader.getContextForVersion(schemaVersion);
    FromOxmVertexSchema vs = new FromOxmVertexSchema();
    try {
        vs.fromOxm(vertexName, jaxbContext, OxmSchemaLoader.getXmlLookupMap(schemaVersion));
    } catch (SchemaProviderException ex) {
        // Node doesn't exist in schema.  Return null.
        return null;
    }
    return vs;
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) SchemaProviderException(org.onap.aai.schemaif.SchemaProviderException)

Example 40 with DynamicJAXBContext

use of org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext in project aai-aai-common by onap.

the class StoreNotificationEventTest method testStoreDynamicEventNullObj.

@Test(expected = AAIException.class)
public void testStoreDynamicEventNullObj() throws AAIException {
    DynamicEntity eventHeader = Mockito.mock(DynamicEntity.class);
    DynamicJAXBContext notificationJaxbContext = nodeIngestor.getContextForVersion(schemaVersions.getEdgeLabelVersion());
    sne.storeDynamicEvent(notificationJaxbContext, "v12", eventHeader, null);
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) Test(org.junit.Test)

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