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