Project

General

Profile

Download (2.9 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 EDIT
3
* European Distributed Institute of Taxonomy 
4
* http://www.e-taxonomy.eu
5
* 
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.io.jaxb;
10

    
11
import static org.junit.Assert.assertNotNull;
12

    
13
import java.io.IOException;
14
import java.io.Reader;
15
import java.io.StringReader;
16
import java.io.StringWriter;
17

    
18
import javax.xml.XMLConstants;
19
import javax.xml.bind.JAXBException;
20
import javax.xml.parsers.ParserConfigurationException;
21
import javax.xml.transform.stream.StreamSource;
22
import javax.xml.validation.SchemaFactory;
23

    
24
import org.custommonkey.xmlunit.jaxp13.Validator;
25
import org.junit.Before;
26
import org.junit.Test;
27
import org.xml.sax.SAXException;
28

    
29
public class SimpleMarshalTest {
30
	private String documentText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
31
	"<DataSet xmlns=\"http://etaxonomy.eu/cdm/model/1.0\">\n" +
32
	"</DataSet>";
33
	
34
	private CdmDocumentBuilder cdmDocumentBuilder;
35
    
36
	@Before
37
	public void setUp() {
38
		
39
		 try {
40
			cdmDocumentBuilder = new CdmDocumentBuilder();
41
		} catch (Exception e) {
42
			e.printStackTrace();
43
		} 
44
	}
45
	
46
    /**
47
     * test the parsing of a simple document
48
     * @throws JAXBException 
49
     * @throws SAXException 
50
     * @throws JAXBException 
51
     * @throws IOException 
52
     */
53
	@Test
54
    public void testUnmarshalEmptyDocument() throws JAXBException
55
    {	
56
		Reader reader = new StringReader(documentText);
57
		DataSet dataSet = cdmDocumentBuilder.unmarshal(DataSet.class, reader);	
58
    }
59
    
60
    /**
61
     * Test the creation of the document builder
62
     * @throws ParserConfigurationException 
63
     * @throws IOException 
64
     */
65
	@Test
66
    public void testCreation() throws JAXBException, SAXException, IOException, ParserConfigurationException
67
    {
68
    	assertNotNull("cdmDocumentBuilder should be initialized without any problems",cdmDocumentBuilder);
69
    }
70
	
71
	/**
72
	 * Check that we can marshal an empty document
73
	 * @throws JAXBException
74
	 * @throws IOException 
75
	 */
76
	@Test
77
	public void testMarshalEmptyDocument() throws JAXBException, IOException {
78
		DataSet dataSet = new DataSet();
79
		StringWriter writer = new StringWriter();
80
		cdmDocumentBuilder.marshal(dataSet, writer);
81
		
82
		SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
83
	    schemaFactory.setResourceResolver(new CdmResourceResolver());
84
	    
85
		Validator validator = new Validator(schemaFactory);
86
		for(String schemaName : CdmDocumentBuilder.CDM_SCHEMA_FILES) {
87
		    validator.addSchemaSource(new StreamSource(this.getClass().getResourceAsStream(schemaName)));
88
	    }
89
		StreamSource streamSource = new StreamSource(new StringReader(writer.toString()));
90
		//assertTrue("CdmDocumentBuilder.marshal should produce valid xml",validator.isInstanceValid(streamSource));
91
	}
92
}
(11-11/17)