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