001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.batch;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.net.URL;
024: import java.util.Iterator;
025: import java.util.Map;
026: import java.util.Properties;
027:
028: import javax.xml.XMLConstants;
029: import javax.xml.parsers.DocumentBuilder;
030: import javax.xml.parsers.DocumentBuilderFactory;
031: import javax.xml.parsers.ParserConfigurationException;
032: import javax.xml.xpath.XPath;
033: import javax.xml.xpath.XPathConstants;
034: import javax.xml.xpath.XPathFactory;
035:
036: import org.junit.Test;
037: import org.kuali.rice.test.LoggableTestCase;
038: import org.w3c.dom.Document;
039: import org.w3c.dom.Node;
040: import org.xml.sax.ErrorHandler;
041: import org.xml.sax.SAXException;
042: import org.xml.sax.SAXParseException;
043:
044: import edu.iu.uis.eden.routetemplate.xmlrouting.WorkflowNamespaceContext;
045: import edu.iu.uis.eden.util.XmlHelper;
046: import edu.iu.uis.eden.xml.ClassLoaderEntityResolver;
047:
048: /**
049: * Test schema validation
050: * @author Aaron Hamid (arh14 at cornell dot edu)
051: */
052: public class XmlSchemaTest extends LoggableTestCase {
053: private Document validate(InputStream stream)
054: throws ParserConfigurationException, IOException,
055: SAXException {
056: /*DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
057: factory.setIgnoringElementContentWhitespace(true);
058: DocumentBuilder builder = factory.newDocumentBuilder();
059: org.w3c.dom.Document oldDocument = builder.parse(input);
060: org.w3c.dom.Element naviElement=oldDocument.getDocumentElement();
061: trimElement(naviElement);
062: return oldDocument;*/
063:
064: DocumentBuilderFactory dbf = DocumentBuilderFactory
065: .newInstance();
066: dbf.setValidating(true);
067: dbf.setNamespaceAware(true);
068: dbf
069: .setAttribute(
070: "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
071: XMLConstants.W3C_XML_SCHEMA_NS_URI);
072: DocumentBuilder db = dbf.newDocumentBuilder();
073: db.setEntityResolver(new ClassLoaderEntityResolver()); // new FileEntityResolver(new File("conf/schema")));
074: db.setErrorHandler(new ErrorHandler() {
075: public void warning(SAXParseException se) {
076: log.warn("Warning parsing xml", se);
077: }
078:
079: public void error(SAXParseException se) throws SAXException {
080: log.error("Error parsing xml", se);
081: throw se;
082: }
083:
084: public void fatalError(SAXParseException se)
085: throws SAXException {
086: log.error("Fatal error parsing xml", se);
087: throw se;
088: }
089: });
090: return db.parse(stream);
091: }
092:
093: @Test
094: public void testValidation() throws ParserConfigurationException,
095: IOException, SAXException {
096: Properties filesToIngest = new Properties();
097: filesToIngest.load(getClass().getResourceAsStream(
098: "XmlSchemaTest.txt"));
099: Iterator entries = filesToIngest.entrySet().iterator();
100: while (entries.hasNext()) {
101: Map.Entry entry = (Map.Entry) entries.next();
102: File testFile = new File(entry.getKey().toString());
103: boolean shouldSucceed = Boolean.valueOf(
104: entry.getValue().toString()).booleanValue();
105: System.out.println("Validating " + testFile);
106: try {
107: validate(new FileInputStream(testFile));
108: if (!shouldSucceed) {
109: fail("Invalid test file '" + testFile
110: + "' passed validation");
111: }
112: } catch (Exception e) {
113: if (shouldSucceed) {
114: fail("Valid test file '" + testFile
115: + "' failed validation");
116: }
117: }
118: }
119: }
120:
121: /**
122: * Tests that default attribute value is visible to XPath from a schema-validated W3C Document
123: * TODO: finish this test when we figure out how to conveniently use namespaces with
124: * XPath
125: */
126: @Test
127: public void testDefaultAttributeValue() throws Exception {
128: URL url = getClass().getResource("XmlConfig.xml");
129: Document d = validate(url.openStream());
130: //d = XmlHelper.trimXml(url.openStream());
131: System.out.println(XmlHelper.jotNode(d));
132: XPath xpath = XPathFactory.newInstance().newXPath();
133: xpath.setNamespaceContext(new WorkflowNamespaceContext());
134: Node node = (Node) xpath
135: .evaluate(
136: "/data/ruleAttributes/ruleAttribute[name='XMLSearchableAttribute']/searchingConfig/fieldDef[@name='givenname' and @workflowType='ALL']/@title",
137: d, XPathConstants.NODE);
138: System.out.println("n: " + node);
139: //System.out.println("n: " + node.getNodeName());
140: //System.out.println("n: " + node.getLocalName());
141: //System.out.println("n: " + node.getNamespaceURI());
142: }
143: }
|