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());
}
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);
}
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());
}
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());
}
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.");
}
}
Aggregations