View Javadoc

1   /**
2    * Copyright 2005-2011 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.xml.XmlJotter;
20  import org.kuali.rice.kew.rule.xmlrouting.WorkflowNamespaceContext;
21  import org.kuali.rice.kew.xml.ClassLoaderEntityResolver;
22  import org.kuali.rice.test.RiceTestCase;
23  import org.w3c.dom.Document;
24  import org.w3c.dom.Node;
25  import org.xml.sax.ErrorHandler;
26  import org.xml.sax.SAXException;
27  import org.xml.sax.SAXParseException;
28  
29  import javax.xml.XMLConstants;
30  import javax.xml.parsers.DocumentBuilder;
31  import javax.xml.parsers.DocumentBuilderFactory;
32  import javax.xml.parsers.ParserConfigurationException;
33  import javax.xml.xpath.XPath;
34  import javax.xml.xpath.XPathConstants;
35  import javax.xml.xpath.XPathFactory;
36  import java.io.File;
37  import java.io.FileInputStream;
38  import java.io.IOException;
39  import java.io.InputStream;
40  import java.net.URL;
41  import java.util.Iterator;
42  import java.util.Map;
43  import java.util.Properties;
44  
45  import static org.junit.Assert.fail;
46  
47  
48  /**
49   * Test schema validation
50   * @author Kuali Rice Team (rice.collab@kuali.org)
51   */
52  public class XmlSchemaTest extends RiceTestCase {
53      private Document validate(InputStream stream) throws ParserConfigurationException, IOException , SAXException {
54  
55          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
56          dbf.setValidating(true);
57          dbf.setNamespaceAware(true);
58          dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", XMLConstants.W3C_XML_SCHEMA_NS_URI);
59          DocumentBuilder db = dbf.newDocumentBuilder();
60          db.setEntityResolver(new ClassLoaderEntityResolver()); //  new FileEntityResolver(new File("conf/schema")));
61          db.setErrorHandler(new ErrorHandler() {
62              public void warning(SAXParseException se) {
63                  log.warn("Warning parsing xml", se);
64              }
65              public void error(SAXParseException se) throws SAXException  {
66                  log.error("Error parsing xml", se);
67                  throw se;
68              }
69              public void fatalError(SAXParseException se) throws SAXException {
70                  log.error("Fatal error parsing xml", se);
71                  throw se;
72              }
73          });
74          return db.parse(stream);
75      }
76  
77      @Test public void testValidation() throws ParserConfigurationException, IOException, SAXException {
78          Properties filesToIngest = new Properties();
79          filesToIngest.load(getClass().getResourceAsStream("XmlSchemaTest.txt"));
80          Iterator entries = filesToIngest.entrySet().iterator();
81          while (entries.hasNext()) {
82              Map.Entry entry = (Map.Entry) entries.next();
83              String filePath = entry.getKey().toString();
84              filePath = filePath.replace("${basedir}", getBaseDir());
85              File testFile = new File(filePath);
86              boolean shouldSucceed = Boolean.valueOf(entry.getValue().toString()).booleanValue();
87              System.out.println("Validating " + testFile);
88              try {
89                  validate(new FileInputStream(testFile));
90                  if (!shouldSucceed) {
91                      fail("Invalid test file '" + testFile + "' passed validation");
92                  }
93              } catch (Exception e) {
94                  if (shouldSucceed) {
95                      fail("Valid test file '" + testFile + "' failed validation");
96                  }
97              }
98          }
99      }
100 
101     /**
102      * Tests that default attribute value is visible to XPath from a schema-validated W3C Document
103      * TODO: finish this test when we figure out how to conveniently use namespaces with
104      * XPath
105      */
106     @Test public void testDefaultAttributeValue() throws Exception {
107         URL url = getClass().getResource("XmlConfig.xml");
108         Document d = validate(url.openStream());
109         //d = XmlHelper.trimXml(url.openStream());
110         System.out.println(XmlJotter.jotNode(d));
111         XPath xpath = XPathFactory.newInstance().newXPath();
112         xpath.setNamespaceContext(new WorkflowNamespaceContext());
113         Node node = (Node) xpath.evaluate("/data/ruleAttributes/ruleAttribute[name='XMLSearchableAttribute']/searchingConfig/fieldDef[@name='givenname' and @workflowType='ALL']/@title",
114                 d, XPathConstants.NODE);
115         System.out.println("n: " + node);
116         //System.out.println("n: " + node.getNodeName());
117         //System.out.println("n: " + node.getLocalName());
118         //System.out.println("n: " + node.getNamespaceURI());
119     }
120 
121     @Override
122     protected String getModuleName() {
123         return "kew";
124     }
125 }