Search in sources :

Example 61 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project ORCID-Source by ORCID.

the class MarshallingTest method testMarshallingV2_0.

@Test
public void testMarshallingV2_0() throws JAXBException, IOException, SAXException {
    JAXBContext context = JAXBContext.newInstance("org.orcid.jaxb.model.notification.permission_v2");
    Unmarshaller unmarshaller = context.createUnmarshaller();
    InputStream inputStream = MarshallingTest.class.getResourceAsStream(SAMPLE_PATH_V2);
    org.orcid.jaxb.model.notification.permission_v2.NotificationPermission notification = (org.orcid.jaxb.model.notification.permission_v2.NotificationPermission) unmarshaller.unmarshal(inputStream);
    assertNotNull(notification);
    assertEquals(org.orcid.jaxb.model.notification_v2.NotificationType.PERMISSION, notification.getNotificationType());
    assertEquals(2, notification.getItems().getItems().size());
    assertEquals("2014-01-01T14:45:32", notification.getSentDate().toXMLFormat());
    // Back the other way
    String expected = IOUtils.toString(getClass().getResourceAsStream(SAMPLE_PATH_V2), "UTF-8");
    Pattern pattern = Pattern.compile("<!--.*?-->\\s*", Pattern.DOTALL);
    expected = pattern.matcher(expected).replaceAll("");
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.orcid.org/ns/notification ../notification-permission-2.0.xsd");
    StringWriter writer = new StringWriter();
    marshaller.marshal(notification, writer);
    XMLUnit.setIgnoreWhitespace(true);
    Diff diff = new Diff(expected, writer.toString());
    assertTrue(diff.identical());
}
Also used : Pattern(java.util.regex.Pattern) Marshaller(javax.xml.bind.Marshaller) Diff(org.custommonkey.xmlunit.Diff) InputStream(java.io.InputStream) JAXBContext(javax.xml.bind.JAXBContext) StringWriter(java.io.StringWriter) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test)

Example 62 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project ORCID-Source by ORCID.

the class MarshallingTest method getOrcidMessage.

private OrcidMessage getOrcidMessage() throws JAXBException {
    JAXBContext context = JAXBContext.newInstance("org.orcid.jaxb.model.message");
    Unmarshaller unmarshaller = context.createUnmarshaller();
    InputStream inputStream = MarshallingTest.class.getResourceAsStream("/orcid-internal-full-message-latest.xml");
    return (OrcidMessage) unmarshaller.unmarshal(inputStream);
}
Also used : InputStream(java.io.InputStream) OrcidMessage(org.orcid.jaxb.model.message.OrcidMessage) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 63 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project ORCID-Source by ORCID.

the class MarshallingTest method testUnMarshallingV2.

@Test
public void testUnMarshallingV2() throws JAXBException {
    JAXBContext context = JAXBContext.newInstance("org.orcid.jaxb.model.notification.custom_v2");
    Unmarshaller unmarshaller = context.createUnmarshaller();
    InputStream inputStream = MarshallingTest.class.getResourceAsStream("/notification_2.0/samples/notification-custom-2.0.xml");
    org.orcid.jaxb.model.notification.custom_v2.NotificationCustom notification = (org.orcid.jaxb.model.notification.custom_v2.NotificationCustom) unmarshaller.unmarshal(inputStream);
    assertNotNull(notification);
    assertEquals(org.orcid.jaxb.model.notification_v2.NotificationType.CUSTOM, notification.getNotificationType());
    assertEquals("Important Notification from ORCID", notification.getSubject());
    assertEquals("This is an email with important info.\n    ", notification.getBodyText());
    assertEquals("\n        <p>\n            This is an email with <em>important</em> info.\n        </p>\n    ", notification.getBodyHtml());
    assertEquals("2014-01-01T14:45:32", notification.getSentDate().toXMLFormat());
    assertEquals("en-gb", notification.getLang());
}
Also used : InputStream(java.io.InputStream) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test)

Example 64 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project ORCID-Source by ORCID.

the class JpaJaxbEducationAdapterTest method getEducation.

private Education getEducation(boolean full) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(new Class[] { Education.class });
    Unmarshaller unmarshaller = context.createUnmarshaller();
    String name = "/record_2.0/samples/read_samples/education-2.0.xml";
    if (full) {
        name = "/record_2.0/samples/read_samples/education-full-2.0.xml";
    }
    InputStream inputStream = getClass().getResourceAsStream(name);
    return (Education) unmarshaller.unmarshal(inputStream);
}
Also used : Education(org.orcid.jaxb.model.record_v2.Education) InputStream(java.io.InputStream) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 65 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project ORCID-Source by ORCID.

the class ConvertVrc3ToVrc4Test method upgradeAddressesToVrc3Test.

@Test
public void upgradeAddressesToVrc3Test() throws JAXBException {
    JAXBContext jaxbContext1 = JAXBContext.newInstance(Addresses.class);
    JAXBContext jaxbContext2 = JAXBContext.newInstance(org.orcid.jaxb.model.record_rc4.Addresses.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext1.createUnmarshaller();
    InputStream rc3Stream = ConvertVrc3ToVrc4Test.class.getClassLoader().getResourceAsStream("test-addresses-2.0_rc3.xml");
    InputStream rc4Stream = ConvertVrc3ToVrc4Test.class.getClassLoader().getResourceAsStream("test-addresses-2.0_rc4.xml");
    Addresses rc4Element = (Addresses) jaxbUnmarshaller.unmarshal(rc3Stream);
    jaxbUnmarshaller = jaxbContext2.createUnmarshaller();
    org.orcid.jaxb.model.record_rc4.Addresses rc4Element1 = (org.orcid.jaxb.model.record_rc4.Addresses) jaxbUnmarshaller.unmarshal(rc4Stream);
    V2Convertible result = versionConverterV2_0_rc3ToV2_0_rc4.upgrade(new V2Convertible(rc4Element, "v2_rc3"));
    org.orcid.jaxb.model.record_rc4.Addresses rc4Element2 = (org.orcid.jaxb.model.record_rc4.Addresses) result.getObjectToConvert();
    assertEquals(rc4Element1, rc4Element2);
}
Also used : Addresses(org.orcid.jaxb.model.record_rc3.Addresses) V2Convertible(org.orcid.core.version.V2Convertible) InputStream(java.io.InputStream) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test) BaseTest(org.orcid.core.BaseTest)

Aggregations

Unmarshaller (javax.xml.bind.Unmarshaller)292 JAXBContext (javax.xml.bind.JAXBContext)240 JAXBException (javax.xml.bind.JAXBException)97 InputStream (java.io.InputStream)91 Test (org.junit.Test)79 StringReader (java.io.StringReader)40 BaseTest (org.orcid.core.BaseTest)39 V2Convertible (org.orcid.core.version.V2Convertible)39 File (java.io.File)33 InputSource (org.xml.sax.InputSource)22 IOException (java.io.IOException)21 JAXBElement (javax.xml.bind.JAXBElement)18 Marshaller (javax.xml.bind.Marshaller)18 ByteArrayInputStream (java.io.ByteArrayInputStream)17 SAXSource (javax.xml.transform.sax.SAXSource)17 SAXParserFactory (javax.xml.parsers.SAXParserFactory)13 XMLInputFactory (javax.xml.stream.XMLInputFactory)13 XMLStreamException (javax.xml.stream.XMLStreamException)13 XMLStreamReader (javax.xml.stream.XMLStreamReader)13 Schema (javax.xml.validation.Schema)13