offline building of site deploy to improve performance.
[utils] / support / general / src / test / java / org / wamblee / xml / XMLSchemaTest.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.xml;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.URI;
23 import java.net.URISyntaxException;
24
25 import org.junit.Test;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Element;
28 import org.w3c.dom.Node;
29
30 import static junit.framework.Assert.*;
31 import static junit.framework.TestCase.*;
32
33 import static org.wamblee.xml.XMLDocument.*;
34
35 public class XMLSchemaTest {
36     
37     private URI getResourceUri(String aResource) throws URISyntaxException { 
38         return getClass().getResource(aResource).toURI();
39     }
40     
41     private InputStream getResource(String aResource) { 
42         return getClass().getResourceAsStream(aResource);
43     }
44     
45     @Test
46     public void testReadAndValidateOkUseUri() throws Exception { 
47         
48         XMLDocument xmldoc = xmldocument("test.xml", getResource("test.xml")).validate(getResourceUri("test.xsd"));
49         Document doc = xmldoc.getDocument();
50         Element element = doc.getDocumentElement();
51         assertEquals("http://wamblee.org/test", element.getNamespaceURI());
52         
53         String val = xmldoc.print(false);
54         // parse the written document
55         Document doc2 = xmldocument("input.xml", new ByteArrayInputStream(val.getBytes())).getDocument();
56         XmlUtils.assertEquals("", doc, doc2);
57     }
58     
59     @Test
60     public void testReadAndValidateOkUseClasspath() throws Exception { 
61         
62         XMLDocument xmldoc = xmldocument("test.xml", getResource("test.xml")).validate(
63             "test.xsd", new ClasspathUriResolver("org/wamblee/xml"));
64         Document doc = xmldoc.getDocument();
65         Element element = doc.getDocumentElement();
66         assertEquals("http://wamblee.org/test", element.getNamespaceURI());
67         
68         String val = xmldoc.print(false);
69         // parse the written document
70         Document doc2 = xmldocument("input.xml", new ByteArrayInputStream(val.getBytes())).getDocument();
71         XmlUtils.assertEquals("", doc, doc2);
72     }
73     
74     @Test(expected = XMLException.class)
75     public void testSchemaNotFoundUseClasspath() throws Exception { 
76         
77         XMLDocument xmldoc = xmldocument("test.xml", getResource("test.xml")).validate(
78             "test.xsd.unknown", new ClasspathUriResolver("org/wamblee/xml"));
79     }
80     
81     
82     @Test(expected = XMLException.class)
83     public void testReadAndValidateNotOk() throws Exception {
84         XMLDocument xmldoc = xmldocument("testInvalid.xml", getResource("testInvalid.xml")).validate(getResourceUri("test.xsd"));
85     }
86 }