View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.kew.batch;
17  
18  import org.junit.Test;
19  import org.kuali.rice.core.api.util.ClasspathOrFileResourceLoader;
20  import org.kuali.rice.core.api.util.xml.XmlJotter;
21  import org.kuali.rice.core.impl.impex.xml.ClassLoaderEntityResolver;
22  import org.kuali.rice.kew.rule.xmlrouting.WorkflowNamespaceContext;
23  import org.kuali.rice.kew.test.KEWTestCase;
24  import org.kuali.rice.test.RiceTestCase;
25  import org.w3c.dom.Document;
26  import org.w3c.dom.Node;
27  import org.xml.sax.ErrorHandler;
28  import org.xml.sax.SAXException;
29  import org.xml.sax.SAXParseException;
30  
31  import javax.xml.XMLConstants;
32  import javax.xml.parsers.DocumentBuilder;
33  import javax.xml.parsers.DocumentBuilderFactory;
34  import javax.xml.parsers.ParserConfigurationException;
35  import javax.xml.xpath.XPath;
36  import javax.xml.xpath.XPathConstants;
37  import javax.xml.xpath.XPathFactory;
38  import java.io.File;
39  import java.io.FileInputStream;
40  import java.io.IOException;
41  import java.io.InputStream;
42  import java.net.URL;
43  import java.util.Iterator;
44  import java.util.Map;
45  import java.util.Properties;
46  
47  import static org.junit.Assert.fail;
48  
49  
50  /**
51   * Test schema validation
52   * @author Kuali Rice Team (rice.collab@kuali.org)
53   */
54  public class XmlSchemaTest extends KEWTestCase {
55      private Document validate(InputStream stream) throws ParserConfigurationException, IOException , SAXException {
56  
57          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
58          dbf.setValidating(true);
59          dbf.setNamespaceAware(true);
60          dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", XMLConstants.W3C_XML_SCHEMA_NS_URI);
61          DocumentBuilder db = dbf.newDocumentBuilder();
62          db.setEntityResolver(new ClassLoaderEntityResolver()); //  new FileEntityResolver(new File("conf/schema")));
63          db.setErrorHandler(new ErrorHandler() {
64              public void warning(SAXParseException se) {
65                  log.warn("Warning parsing xml", se);
66              }
67              public void error(SAXParseException se) throws SAXException  {
68                  log.error("Error parsing xml", se);
69                  throw se;
70              }
71              public void fatalError(SAXParseException se) throws SAXException {
72                  log.error("Fatal error parsing xml", se);
73                  throw se;
74              }
75          });
76          return db.parse(stream);
77      }
78  
79      @Test public void testValidation() throws ParserConfigurationException, IOException, SAXException {
80          Properties filesToIngest = new Properties();
81          filesToIngest.load(getClass().getResourceAsStream("XmlSchemaTest.txt"));
82          Iterator entries = filesToIngest.entrySet().iterator();
83          while (entries.hasNext()) {
84              Map.Entry entry = (Map.Entry) entries.next();
85              String filePath = entry.getKey().toString();
86              File testFile = new ClasspathOrFileResourceLoader().getResource(filePath).getFile();
87              boolean shouldSucceed = Boolean.valueOf(entry.getValue().toString()).booleanValue();
88              System.out.println("Validating " + testFile);
89              try {
90                  validate(new FileInputStream(testFile));
91                  if (!shouldSucceed) {
92                      fail("Invalid test file '" + testFile + "' passed validation");
93                  }
94              } catch (Exception e) {
95                  if (shouldSucceed) {
96                      e.printStackTrace();
97                      fail("Valid test file '" + testFile + "' failed validation");
98                  }
99              }
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 }