Search in sources :

Example 11 with JAXBContext

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);
    }
}
Also used : Marshaller(jakarta.xml.bind.Marshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) JAXBException(jakarta.xml.bind.JAXBException) JAXBContext(jakarta.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Unmarshaller(jakarta.xml.bind.Unmarshaller) Link(jakarta.ws.rs.core.Link) Test(org.junit.jupiter.api.Test)

Example 12 with JAXBContext

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.");
}
Also used : Marshaller(jakarta.xml.bind.Marshaller) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) StreamSource(javax.xml.transform.stream.StreamSource) JAXBContext(jakarta.xml.bind.JAXBContext) JAXBElement(jakarta.xml.bind.JAXBElement) URI(java.net.URI) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) Unmarshaller(jakarta.xml.bind.Unmarshaller) Test(org.junit.jupiter.api.Test)

Aggregations

JAXBContext (jakarta.xml.bind.JAXBContext)12 JAXBException (jakarta.xml.bind.JAXBException)9 Marshaller (jakarta.xml.bind.Marshaller)7 Unmarshaller (jakarta.xml.bind.Unmarshaller)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Test (org.junit.jupiter.api.Test)3 Link (jakarta.ws.rs.core.Link)2 JAXBElement (jakarta.xml.bind.JAXBElement)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 QName (javax.xml.namespace.QName)2 HttpMessageConversionException (org.springframework.http.converter.HttpMessageConversionException)2 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 GZIPInputStream (java.util.zip.GZIPInputStream)1