1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
50
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());
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
104
105
106
107 @Test public void testDefaultAttributeValue() throws Exception {
108 URL url = getClass().getResource("XmlConfig.xml");
109 Document d = validate(url.openStream());
110
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
118
119
120 }
121
122 @Override
123 protected String getModuleName() {
124 return "kew";
125 }
126 }