/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package global.sandbox.xmlutilities; import java.io.InputStream; import junit.framework.TestCase; import org.w3c.dom.Document; /** * * @author Petr Hadraba */ public class XSDTest extends TestCase { private XMLUtilities xmlUtilities; private NamespaceContextImpl nsCtx; @Override protected void setUp() throws Exception { super.setUp(); // we should use default objects, no customizations this.xmlUtilities = new XMLUtilities(); this.nsCtx = createNamespaceContextImpl(); } @Override protected void tearDown() throws Exception { super.tearDown(); this.nsCtx = null; this.xmlUtilities = null; } /** * demonstrates validation * * @throws java.lang.Exception */ public void testValidateValidDocument() throws Exception { Document schema = loadSchema(); Document validXML = loadDocument("example.xml"); String result = xmlUtilities.validateXmlUsingSchema( XMLTools.documentToDOMSource(validXML), XMLTools.documentToDOMSource(schema)); System.err.println("The resulting String is set to null if validation" + " is OK: " + result); assertNull(result); } /* HELPER METHODS */ private Document loadSchema() throws Exception { return loadDocument("example.xsd"); } private Document loadDocument(String name) throws Exception { InputStream is = getClass().getResourceAsStream(name); try { return xmlUtilities.loadDocumentFromStream(is); } finally { is.close(); } } private NamespaceContextImpl createNamespaceContextImpl() throws Exception { NamespaceContextImpl nsctx = new NamespaceContextImpl(); nsctx.addNamespace("ns", "http://hadrabap.googlepages.com/projects/" + "xmlutilities/example.xsd"); return nsctx; } }