1 17 package org.apache.ws.jaxme.xs.junit; 18 19 import java.net.URL ; 20 21 import javax.xml.parsers.DocumentBuilder ; 22 import javax.xml.parsers.DocumentBuilderFactory ; 23 import javax.xml.parsers.ParserConfigurationException ; 24 25 import org.apache.ws.jaxme.xs.XSParser; 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.Element ; 28 import org.w3c.dom.NodeList ; 29 import org.xml.sax.InputSource ; 30 31 import junit.framework.TestCase; 32 33 46 public class NISTTest extends TestCase { 47 private int numOk; 48 private int numFailed; 49 private boolean verbose; 50 51 public NISTTest(String pName) { 52 super(pName); 53 } 54 55 public void setUp() { 56 numOk = numFailed = 0; 57 verbose = Boolean.valueOf(System.getProperty("verbose")).booleanValue(); 58 } 59 60 protected void log(String pMessage) { 61 if (verbose) { 62 System.out.println(pMessage); 63 } 64 } 65 66 public DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { 67 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 68 dbf.setValidating(false); 69 dbf.setNamespaceAware(true); 70 return dbf.newDocumentBuilder(); 71 } 72 73 protected void runTest(URL pBaseURL, String pName, String pHref) throws Exception { 74 URL url = new URL (pBaseURL, pHref); 75 XSParser parser = new XSParser(); 76 parser.setValidating(false); 77 InputSource isource = new InputSource (url.openStream()); 78 isource.setSystemId(url.toString()); 79 String result; 80 try { 81 parser.parse(isource); 82 ++numOk; 83 result = "Ok"; 84 } catch (Exception e) { 85 ++numFailed; 86 result = e.getMessage(); 87 } 88 log("Running test " + pName + " with URL " + url+ ": " + result); 89 } 90 91 protected void runTests(URL pBaseURL, String pName, String pHref) throws Exception { 92 URL url = new URL (pBaseURL, pHref); 93 InputSource isource = new InputSource (url.openStream()); 94 isource.setSystemId(url.toString()); 95 Document document = getDocumentBuilder().parse(isource); 96 NodeList schemas = document.getElementsByTagNameNS(null, "Schema"); 97 for (int i = 0; i < schemas.getLength(); i++) { 98 Element schema = (Element ) schemas.item(i); 99 runTest(url, schema.getAttribute("name"), schema.getAttribute("href")); 100 } 101 } 102 103 public void testNIST() throws Exception { 104 String p = "NISTXMLSchemaTestSuite.location"; 105 String v = System.getProperty(p); 106 if (v == null || v.length() == 0) { 107 System.out.println("System property " + p + " is not set, skipping this test."); 108 return; 109 } 110 111 URL url = new URL (v); 112 url = new URL (url, "NISTXMLSchemaTestSuite.xml"); 113 InputSource isource = new InputSource (url.openStream()); 114 isource.setSystemId(url.toString()); 115 Document document = getDocumentBuilder().parse(isource); 116 NodeList links = document.getElementsByTagNameNS(null, "Link"); 117 for (int i = 0; i < links.getLength(); i++) { 118 Element link = (Element ) links.item(i); 119 runTests(url, link.getAttribute("name"), link.getAttribute("href")); 120 } 121 System.out.println("Result: Passed = " + numOk + ", Failed = " + numFailed); 122 } 123 } 124 | Popular Tags |