1 package eu
.etaxonomy
.cdm
.io
.jaxb
;
3 import static org
.junit
.Assert
.assertNotNull
;
4 import static org
.junit
.Assert
.assertTrue
;
6 import java
.io
.IOException
;
8 import java
.io
.StringReader
;
9 import java
.io
.StringWriter
;
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
;
17 import junit
.framework
.Assert
;
19 import org
.custommonkey
.xmlunit
.jaxp13
.Validator
;
20 import org
.junit
.Before
;
21 import org
.junit
.Test
;
22 import org
.xml
.sax
.SAXException
;
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" +
29 private CdmDocumentBuilder cdmDocumentBuilder
;
35 cdmDocumentBuilder
= new CdmDocumentBuilder();
36 } catch (Exception e
) {
42 * test the parsing of a simple document
43 * @throws JAXBException
44 * @throws SAXException
45 * @throws JAXBException
49 public void testUnmarshalEmptyDocument() throws JAXBException
51 Reader reader
= new StringReader(documentText
);
52 DataSet dataSet
= null;
53 dataSet
= cdmDocumentBuilder
.unmarshal(dataSet
, reader
);
57 * Test the creation of the document builder
58 * @throws ParserConfigurationException
62 public void testCreation() throws JAXBException
, SAXException
, IOException
, ParserConfigurationException
64 assertNotNull("cdmDocumentBuilder should be initialized without any problems",cdmDocumentBuilder
);
68 * Check that we can marshal an empty document
69 * @throws JAXBException
73 public void testMarshalEmptyDocument() throws JAXBException
, IOException
{
74 DataSet dataSet
= new DataSet();
75 StringWriter writer
= new StringWriter();
76 cdmDocumentBuilder
.marshal(dataSet
, writer
);
78 SchemaFactory schemaFactory
= SchemaFactory
.newInstance(XMLConstants
.W3C_XML_SCHEMA_NS_URI
);
79 schemaFactory
.setResourceResolver(new CdmResourceResolver());
81 Validator validator
= new Validator(schemaFactory
);
82 for(String schemaName
: CdmDocumentBuilder
.CDM_SCHEMA_FILES
) {
83 validator
.addSchemaSource(new StreamSource(this.getClass().getResourceAsStream(schemaName
)));
85 StreamSource streamSource
= new StreamSource(new StringReader(writer
.toString()));
86 assertTrue("CdmDocumentBuilder.marshal should produce valid xml",validator
.isInstanceValid(streamSource
));