1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ken.xpath;
17
18 import org.apache.commons.io.IOUtils;
19 import org.apache.xerces.jaxp.JAXPConstants;
20 import org.junit.Test;
21 import org.kuali.rice.ken.test.KENTestCase;
22 import org.kuali.rice.ken.util.DocumentNamespaceContext;
23 import org.kuali.rice.ken.util.SimpleErrorHandler;
24 import org.kuali.rice.ken.util.Util;
25 import org.kuali.rice.test.BaselineTestCase.BaselineMode;
26 import org.kuali.rice.test.BaselineTestCase.Mode;
27 import org.w3c.dom.Document;
28 import org.xml.sax.InputSource;
29
30 import javax.xml.parsers.DocumentBuilder;
31 import javax.xml.parsers.DocumentBuilderFactory;
32 import javax.xml.xpath.XPath;
33 import javax.xml.xpath.XPathConstants;
34 import javax.xml.xpath.XPathFactory;
35 import java.io.IOException;
36 import java.io.InputStream;
37
38 import static org.junit.Assert.assertEquals;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 @BaselineMode(Mode.CLEAR_DB)
60 public class XPathTest extends KENTestCase {
61 private static final String TEST_XML = "sample_message_event_type.xml";
62
63 protected InputSource getTestXMLInputSource() {
64 InputStream is = XPathTest.class.getResourceAsStream(TEST_XML);
65 if (is != null) {
66 try {
67 LOG.info(IOUtils.toString(is));
68 } catch (IOException e) {
69 throw new RuntimeException(e);
70 } finally {
71 IOUtils.closeQuietly(is);
72 }
73
74 is = XPathTest.class.getResourceAsStream(TEST_XML);
75 }
76 return new InputSource(is);
77 }
78
79 protected XPath getXPath(Document doc) {
80 XPath xpath = XPathFactory.newInstance().newXPath();
81 if (doc != null) {
82 xpath.setNamespaceContext(new DocumentNamespaceContext(doc));
83 } else {
84 xpath.setNamespaceContext(Util.NOTIFICATION_NAMESPACE_CONTEXT);
85 }
86 return xpath;
87 }
88
89 protected Document getDocument(boolean namespaceAware, boolean validate) throws Exception {
90
91 final InputSource source = getTestXMLInputSource();
92 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
93 dbf.setValidating(validate);
94 dbf.setNamespaceAware(namespaceAware);
95 dbf.setAttribute(JAXPConstants.JAXP_SCHEMA_LANGUAGE, JAXPConstants.W3C_XML_SCHEMA);
96 DocumentBuilder db = dbf.newDocumentBuilder();
97 LOG.info("Setting entityresolver");
98 db.setEntityResolver(Util.getNotificationEntityResolver(services.getNotificationContentTypeService()));
99 db.setErrorHandler(new SimpleErrorHandler(LOG));
100 return db.parse(source);
101 }
102
103 @Test
104 public void testXPathWithPlainDOM() throws Exception {
105 Document doc = getDocument(false, false);
106 XPath xpath = getXPath(null);
107 String channelName = (String) xpath.evaluate("/notification/channel", doc.getDocumentElement(), XPathConstants.STRING);
108 assertEquals("Test Channel #1", channelName);
109 }
110 @Test
111 public void testXPathWithNamespaceAwareDOM() throws Exception {
112 Document doc = getDocument(true, false);
113 XPath xpath = getXPath(null);
114 String channelName = (String) xpath.evaluate("/nreq:notification/nreq:channel", doc.getDocumentElement(), XPathConstants.STRING);
115 assertEquals("Test Channel #1", channelName);
116 }
117 @Test
118 public void testXPathWithValidatedDOMFixedNamespace() throws Exception {
119 LOG.debug("TEST");
120 Document doc = getDocument(true, true);
121 LOG.info("Default namespace: " + doc.lookupNamespaceURI(null));
122 XPath xpath = getXPath(null);
123 String channelName = (String) xpath.evaluate("/nreq:notification/nreq:channel", doc.getDocumentElement(), XPathConstants.STRING);
124 assertEquals("Test Channel #1", channelName);
125 }
126 @Test
127 public void testXPathWithValidatedDOMDocNamespace() throws Exception {
128 LOG.debug("TEST");
129 Document doc = getDocument(true, true);
130 LOG.info("Default namespace: " + doc.lookupNamespaceURI(null));
131 LOG.info("default prefix: " + doc.lookupPrefix(doc.lookupNamespaceURI(null)));
132 XPath xpath = XPathFactory.newInstance().newXPath();
133 xpath.setNamespaceContext(Util.getNotificationNamespaceContext(doc));
134 String channelName = (String) xpath.evaluate("/nreq:notification/nreq:channel", doc.getDocumentElement(), XPathConstants.STRING);
135 assertEquals("Test Channel #1", channelName);
136 }
137 }