use of jakarta.xml.bind.JAXBContext in project jaxrs-api by eclipse-ee4j.
the class JAXRSClientIT method unmarshallTest.
/*
* @testName: unmarshallTest
*
* @assertion_ids: JAXRS:JAVADOC:815; JAXRS:JAVADOC:816; JAXRS:JAVADOC:818;
*
* @test_Strategy: Test whether a class with Link can be unmarshalled fine
*/
@Test
public void unmarshallTest() throws Fault {
Link link = RuntimeDelegate.getInstance().createLinkBuilder().uri(url).title(title).rel(rel).type(media).param(param_names[0], param_vals[0]).param(param_names[1], param_vals[1]).build();
Model model = new Model(link);
ByteArrayOutputStream ostream = new ByteArrayOutputStream(1000);
JAXBContext jc = null;
Marshaller marshaller = null;
byte[] array = null;
Unmarshaller unmarshaller = null;
ByteArrayInputStream istream = null;
Model unmarshalledModel = null;
try {
jc = JAXBContext.newInstance(Model.class);
marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(model, ostream);
array = ostream.toByteArray();
istream = new ByteArrayInputStream(array);
jc = JAXBContext.newInstance(Model.class);
unmarshaller = jc.createUnmarshaller();
unmarshalledModel = (Model) unmarshaller.unmarshal(istream);
link = unmarshalledModel.getLink();
assertContains(link.toString(), url, "unmarshalled link", link, "does not contain expected url", url);
assertContains(link.getRel(), rel, "unmarshalled link", link, "does not contain expected relation", rel);
assertContains(link.getTitle(), title, "unmarshalled link", link, "does not contain expected title", title);
assertContains(link.getType(), media, "unmarshalled link", link, "does not contain expected media type", media);
assertTrue(link.getParams().size() > 2, "unmarshalled link " + link + " does not contain expected parameters");
assertContains(link.getParams().get(param_names[0]), param_vals[0], "unmarshalled link", link, "does not contain expected parameter", param_names[0], "=", param_vals[0]);
assertContains(link.getParams().get(param_names[1]), param_vals[1], "unmarshalled link", link, "does not contain expected parameter", param_names[1], "=", param_vals[1]);
logMsg("unmarshalled Link contains expected url, title, media type, and parameters", link);
} catch (JAXBException e) {
throw new Fault(e);
}
}
use of jakarta.xml.bind.JAXBContext in project jaxrs-api by eclipse-ee4j.
the class JaxbLinkTest method testSerializationOfJaxbLink.
@Test
public void testSerializationOfJaxbLink() throws Exception {
JAXBContext jaxbContext = JAXBContext.newInstance(Link.JaxbLink.class);
final Marshaller marshaller = jaxbContext.createMarshaller();
final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Map<QName, Object> expectedParams = new HashMap<QName, Object>();
final QName qName = new QName("http://example.ns", "foo");
expectedParams.put(qName, "test");
final URI expectedUri = URI.create("/foo/bar");
Link.JaxbLink expected = new Link.JaxbLink(expectedUri, expectedParams);
final StringWriter writer = new StringWriter();
JAXBElement<Link.JaxbLink> jaxbLinkJAXBElement = new JAXBElement<Link.JaxbLink>(new QName("", "link"), Link.JaxbLink.class, expected);
marshaller.marshal(jaxbLinkJAXBElement, writer);
final Link.JaxbLink actual = unmarshaller.unmarshal(new StreamSource(new StringReader(writer.toString())), Link.JaxbLink.class).getValue();
assertEquals(expected, actual, "Unmarshalled JaxbLink instance not equal to the marshalled one.");
assertEquals(expectedUri, actual.getUri(), "Unmarshalled JaxbLink instance URI not equal to original.");
assertEquals(expectedParams, actual.getParams(), "Unmarshalled JaxbLink instance params not equal to original.");
}
Aggregations