Search in sources :

Example 1 with DynamicException

use of org.eclipse.persistence.exceptions.DynamicException in project eclipselink by eclipse-ee4j.

the class DynamicJAXBContextCreationTestCases method testNewInstanceXSDExternalBindings.

public void testNewInstanceXSDExternalBindings() 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());
    InputStream xjbStream2 = classLoader.getResourceAsStream(EXTERNAL_BINDINGS_2);
    Source xjbSource2 = new StreamSource(xjbStream2);
    // Set SYSTEM_ID to be the same as the XSD
    xjbSource2.setSystemId(xsdSource.getSystemId());
    ArrayList<Source> extBindings = new ArrayList<Source>(3);
    extBindings.add(xjbSource);
    extBindings.add(xjbSource2);
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(DynamicJAXBContextFactory.XML_SCHEMA_KEY, xsdSource);
    properties.put(DynamicJAXBContextFactory.EXTERNAL_BINDINGS_KEY, extBindings);
    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");
        emp.set("lastNameCommaFirstName", "Neilson, Leslie");
    } catch (DynamicException e) {
        fail("External bindings file not applied.");
    }
}
Also used : 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) ArrayList(java.util.ArrayList) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) DynamicException(org.eclipse.persistence.exceptions.DynamicException) DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext)

Example 2 with DynamicException

use of org.eclipse.persistence.exceptions.DynamicException 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)

Example 3 with DynamicException

use of org.eclipse.persistence.exceptions.DynamicException in project eclipselink by eclipse-ee4j.

the class XRDynamicEntityTestSuite method testEntityOps.

@Test
public void testEntityOps() {
    // test #1
    Object field = entity1.get(FIELD_1);
    assertNull(FIELD_1 + " should be null", field);
    assertFalse(FIELD_2 + " shouldn't be set", entity1.isSet(FIELD_2));
    // test #2
    DynamicEntity e = entity1.set(FIELD_1, TEST_STRING);
    assertSame(e, entity1);
    e = entity1.set(FIELD_2, 17);
    assertSame(e, entity1);
    // test #3
    String test = entity1.<String>get(FIELD_1);
    assertEquals(FIELD_1 + " incorrect value", test, TEST_STRING);
    Integer i = entity1.<Integer>get(FIELD_2);
    assertEquals(FIELD_2 + " incorrect value", i, Integer.valueOf(17));
    // test #4
    boolean expectedExceptionOccurred = false;
    try {
        String s = entity1.<String>get("field2");
        System.identityHashCode(s);
    } catch (ClassCastException cce) {
        expectedExceptionOccurred = true;
    }
    assertTrue("The expected ClassCastException did not occur", expectedExceptionOccurred);
    // test #5
    expectedExceptionOccurred = false;
    try {
        entity1.<String>get("field3");
    } catch (DynamicException de) {
        expectedExceptionOccurred = true;
    }
    assertTrue("The expected DynamicException did not occur", expectedExceptionOccurred);
}
Also used : XRDynamicEntity(org.eclipse.persistence.internal.xr.XRDynamicEntity) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) DynamicException(org.eclipse.persistence.exceptions.DynamicException) Test(org.junit.Test)

Example 4 with DynamicException

use of org.eclipse.persistence.exceptions.DynamicException in project eclipselink by eclipse-ee4j.

the class DynamicClassLoaderTestSuite method duplicateAddClassWithDifferentParent.

/**
 * Verify that a second request to create a class with the same name and
 * different parents fails.
 */
@Test
public void duplicateAddClassWithDifferentParent() throws Exception {
    DynamicClassLoader dcl = new DynamicClassLoader(Thread.currentThread().getContextClassLoader());
    dcl.addClass(MY_CLASSNAME, DefaultConstructor.class);
    Class<?> dynamicClass = dcl.loadClass(MY_CLASSNAME);
    assertNotNull(dynamicClass);
    assertSame(dynamicClass, dcl.loadClass(MY_CLASSNAME));
    assertSame(DefaultConstructor.class, dynamicClass.getSuperclass());
    DefaultConstructor entity = (DefaultConstructor) dynamicClass.getConstructor().newInstance();
    assertNotNull(entity);
    assertNotNull("DCL does not contain expected writer", dcl.getClassWriter(MY_CLASSNAME));
    try {
        dcl.addClass(MY_CLASSNAME, WriteReplace.class);
    } catch (DynamicException de) {
        String errorMessage = de.getMessage();
        int errorCode = de.getErrorCode();
        assertTrue("Incorrect dynamic exception", errorMessage.startsWith("\r\nException Description: Duplicate addClass request with incompatible writer:"));
        assertEquals("Unexpected error code", INCOMPATIBLE_DYNAMIC_CLASSWRITERS, errorCode);
        return;
    }
    fail("No DynamicException thrown for duplicate addClass with different parent");
}
Also used : DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) DynamicException(org.eclipse.persistence.exceptions.DynamicException) Test(org.junit.Test)

Aggregations

DynamicException (org.eclipse.persistence.exceptions.DynamicException)4 DynamicEntity (org.eclipse.persistence.dynamic.DynamicEntity)3 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 Source (javax.xml.transform.Source)2 DOMSource (javax.xml.transform.dom.DOMSource)2 StreamSource (javax.xml.transform.stream.StreamSource)2 DynamicJAXBContext (org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 DynamicClassLoader (org.eclipse.persistence.dynamic.DynamicClassLoader)1 XRDynamicEntity (org.eclipse.persistence.internal.xr.XRDynamicEntity)1