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.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
52
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());
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
105
106
107
108 @Test public void testDefaultAttributeValue() throws Exception {
109 URL url = getClass().getResource("XmlConfig.xml");
110 Document d = validate(url.openStream());
111
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
119
120
121 }
122
123 @Override
124 protected String getModuleName() {
125 return "kew";
126 }
127 }