Search in sources :

Example 1 with MotherElement

use of org.codice.ddf.parser.xml.domain.MotherElement in project ddf by codice.

the class TestXmlParser method testUnmarshal.

@Test
public void testUnmarshal() throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    parser.marshal(configurator, mother, os);
    ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
    MotherElement unmarshal = parser.unmarshal(configurator, MotherElement.class, is);
    assertEquals(mother.getAge(), unmarshal.getAge());
    assertEquals(mother.getFirstname(), unmarshal.getFirstname());
    assertEquals(mother.getLastname(), unmarshal.getLastname());
    assertEquals(mother.getChild().size(), unmarshal.getChild().size());
    assertEquals(luke.getFirstname(), unmarshal.getChild().get(0).getFirstname());
    assertEquals(leia.getAge(), unmarshal.getChild().get(1).getAge());
    configurator.setHandler(new DefaultValidationEventHandler());
    is = new ByteArrayInputStream(os.toByteArray());
    unmarshal = parser.unmarshal(configurator, MotherElement.class, is);
    assertEquals(mother.getAge(), unmarshal.getAge());
    configurator.addProperty("UnknownProperty", Boolean.TRUE);
    is = new ByteArrayInputStream(os.toByteArray());
    thrown.expect(ParserException.class);
    parser.unmarshal(configurator, MotherElement.class, is);
}
Also used : MotherElement(org.codice.ddf.parser.xml.domain.MotherElement) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DefaultValidationEventHandler(javax.xml.bind.helpers.DefaultValidationEventHandler) Test(org.junit.Test)

Example 2 with MotherElement

use of org.codice.ddf.parser.xml.domain.MotherElement in project ddf by codice.

the class TestXmlParser method setUp.

@Before
public void setUp() throws Exception {
    parser = new XmlParser();
    List<String> ctxPath = ImmutableList.of(MotherElement.class.getPackage().getName());
    configurator = parser.configureParser(ctxPath, TestXmlParser.class.getClassLoader());
    mother = new MotherElement();
    mother.setFirstname("Padme");
    mother.setLastname("Skywalker");
    mother.setAge(25);
    luke = new ChildElement();
    luke.setAge(2);
    luke.setFirstname("Luke");
    luke.setLastname("Skywalker");
    leia = new ChildElement();
    leia.setAge(2);
    leia.setFirstname("Leia");
    leia.setLastname("Organa");
    mother.getChild().add(luke);
    mother.getChild().add(leia);
}
Also used : MotherElement(org.codice.ddf.parser.xml.domain.MotherElement) ChildElement(org.codice.ddf.parser.xml.domain.ChildElement) Before(org.junit.Before)

Example 3 with MotherElement

use of org.codice.ddf.parser.xml.domain.MotherElement in project ddf by codice.

the class TestXmlParser method testUnmarshalSource.

@Test
public void testUnmarshalSource() throws Exception {
    JAXBContext motherContext = JAXBContext.newInstance(MotherElement.class);
    @SuppressWarnings("unchecked") JAXBElement<MotherElement> motherElementJAXBElement = new JAXBElement(new QName("mother"), MotherElement.class, mother);
    JAXBSource motherSource = new JAXBSource(motherContext, motherElementJAXBElement);
    MotherElement unmarshal = parser.unmarshal(configurator, MotherElement.class, motherSource);
    assertEquals(mother.getAge(), unmarshal.getAge());
    assertEquals(mother.getFirstname(), unmarshal.getFirstname());
    assertEquals(mother.getLastname(), unmarshal.getLastname());
    assertEquals(mother.getChild().size(), unmarshal.getChild().size());
    assertEquals(luke.getFirstname(), unmarshal.getChild().get(0).getFirstname());
    assertEquals(leia.getAge(), unmarshal.getChild().get(1).getAge());
}
Also used : MotherElement(org.codice.ddf.parser.xml.domain.MotherElement) QName(javax.xml.namespace.QName) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) JAXBSource(javax.xml.bind.util.JAXBSource) Test(org.junit.Test)

Example 4 with MotherElement

use of org.codice.ddf.parser.xml.domain.MotherElement in project ddf by codice.

the class TestXmlParser method testUnmarshalSourceJAXBException.

@Test
public void testUnmarshalSourceJAXBException() throws Exception {
    JAXBContext motherContext = JAXBContext.newInstance(MotherElement.class);
    @SuppressWarnings("unchecked") JAXBElement<MotherElement> motherElementJAXBElement = new JAXBElement(new QName("mother"), MotherElement.class, mother);
    JAXBSource motherSource = new JAXBSource(motherContext, motherElementJAXBElement);
    configurator.addProperty("BadKey", "BadValue");
    thrown.expect(ParserException.class);
    parser.unmarshal(configurator, MotherElement.class, motherSource);
}
Also used : MotherElement(org.codice.ddf.parser.xml.domain.MotherElement) QName(javax.xml.namespace.QName) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) JAXBSource(javax.xml.bind.util.JAXBSource) Test(org.junit.Test)

Example 5 with MotherElement

use of org.codice.ddf.parser.xml.domain.MotherElement in project ddf by codice.

the class TestXmlParser method testUnmarshalNode.

@Test
public void testUnmarshalNode() throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbf.newDocumentBuilder();
    Document doc = builder.newDocument();
    parser.marshal(configurator, mother, doc);
    MotherElement unmarshal = parser.unmarshal(configurator, MotherElement.class, doc);
    assertEquals(mother.getAge(), unmarshal.getAge());
    assertEquals(mother.getFirstname(), unmarshal.getFirstname());
    assertEquals(mother.getLastname(), unmarshal.getLastname());
    assertEquals(mother.getChild().size(), unmarshal.getChild().size());
    assertEquals(luke.getFirstname(), unmarshal.getChild().get(0).getFirstname());
    assertEquals(leia.getAge(), unmarshal.getChild().get(1).getAge());
}
Also used : MotherElement(org.codice.ddf.parser.xml.domain.MotherElement) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Document(org.w3c.dom.Document) Test(org.junit.Test)

Aggregations

MotherElement (org.codice.ddf.parser.xml.domain.MotherElement)5 Test (org.junit.Test)4 JAXBContext (javax.xml.bind.JAXBContext)2 JAXBElement (javax.xml.bind.JAXBElement)2 JAXBSource (javax.xml.bind.util.JAXBSource)2 QName (javax.xml.namespace.QName)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DefaultValidationEventHandler (javax.xml.bind.helpers.DefaultValidationEventHandler)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ChildElement (org.codice.ddf.parser.xml.domain.ChildElement)1 Before (org.junit.Before)1 Document (org.w3c.dom.Document)1