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.core.impl.impex.xml.ClassLoaderEntityResolver;
21  import org.kuali.rice.kew.rule.xmlrouting.WorkflowNamespaceContext;
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                      e.printStackTrace();
96                      fail("Valid test file '" + testFile + "' failed validation");
97                  }
98              }
99          }
100     }
101 
102     /**
103      * Tests that default attribute value is visible to XPath from a schema-validated W3C Document
104      * TODO: finish this test when we figure out how to conveniently use namespaces with
105      * XPath
106      */
107     @Test public void testDefaultAttributeValue() throws Exception {
108         URL url = getClass().getResource("XmlConfig.xml");
109         Document d = validate(url.openStream());
110         //d = XmlHelper.trimXml(url.openStream());
111         System.out.println(XmlJotter.jotNode(d));
112         XPath xpath = XPathFactory.newInstance().newXPath();
113         xpath.setNamespaceContext(new WorkflowNamespaceContext());
114         Node node = (Node) xpath.evaluate("/data/ruleAttributes/ruleAttribute[name='XMLSearchableAttribute']/searchingConfig/fieldDef[@name='givenname' and @workflowType='ALL']/@title",
115                 d, XPathConstants.NODE);
116         System.out.println("n: " + node);
117         //System.out.println("n: " + node.getNodeName());
118         //System.out.println("n: " + node.getLocalName());
119         //System.out.println("n: " + node.getNamespaceURI());
120     }
121 
122     @Override
123     protected String getModuleName() {
124         return "kew";
125     }
126 }