Committing large number of changes relating to versioning implementation (#108) ...
[cdmlib.git] / cdmlib-io / src / test / java / eu / etaxonomy / cdm / io / jaxb / SimpleMarshalTest.java
1 package eu.etaxonomy.cdm.io.jaxb;
2
3 import static org.junit.Assert.assertNotNull;
4 import static org.junit.Assert.assertTrue;
5
6 import java.io.IOException;
7 import java.io.Reader;
8 import java.io.StringReader;
9 import java.io.StringWriter;
10
11 import javax.xml.XMLConstants;
12 import javax.xml.bind.JAXBException;
13 import javax.xml.parsers.ParserConfigurationException;
14 import javax.xml.transform.stream.StreamSource;
15 import javax.xml.validation.SchemaFactory;
16
17 import junit.framework.Assert;
18
19 import org.custommonkey.xmlunit.jaxp13.Validator;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.xml.sax.SAXException;
23
24 public class SimpleMarshalTest {
25 private String documentText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
26 "<DataSet xmlns=\"http://etaxonomy.eu/cdm/model/1.0\">\n" +
27 "</DataSet>";
28
29 private CdmDocumentBuilder cdmDocumentBuilder;
30
31 @Before
32 public void setUp() {
33
34 try {
35 cdmDocumentBuilder = new CdmDocumentBuilder();
36 } catch (Exception e) {
37 e.printStackTrace();
38 }
39 }
40
41 /**
42 * test the parsing of a simple document
43 * @throws JAXBException
44 * @throws SAXException
45 * @throws JAXBException
46 * @throws IOException
47 */
48 @Test
49 public void testUnmarshalEmptyDocument() throws JAXBException
50 {
51 Reader reader = new StringReader(documentText);
52 DataSet dataSet = null;
53 dataSet = cdmDocumentBuilder.unmarshal(dataSet, reader);
54 }
55
56 /**
57 * Test the creation of the document builder
58 * @throws ParserConfigurationException
59 * @throws IOException
60 */
61 @Test
62 public void testCreation() throws JAXBException, SAXException, IOException, ParserConfigurationException
63 {
64 assertNotNull("cdmDocumentBuilder should be initialized without any problems",cdmDocumentBuilder);
65 }
66
67 /**
68 * Check that we can marshal an empty document
69 * @throws JAXBException
70 * @throws IOException
71 */
72 @Test
73 public void testMarshalEmptyDocument() throws JAXBException, IOException {
74 DataSet dataSet = new DataSet();
75 StringWriter writer = new StringWriter();
76 cdmDocumentBuilder.marshal(dataSet, writer);
77
78 SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
79 schemaFactory.setResourceResolver(new CdmResourceResolver());
80
81 Validator validator = new Validator(schemaFactory);
82 for(String schemaName : CdmDocumentBuilder.CDM_SCHEMA_FILES) {
83 validator.addSchemaSource(new StreamSource(this.getClass().getResourceAsStream(schemaName)));
84 }
85 StreamSource streamSource = new StreamSource(new StringReader(writer.toString()));
86 assertTrue("CdmDocumentBuilder.marshal should produce valid xml",validator.isInstanceValid(streamSource));
87 }
88 }