1
2
3
4
5
6
7
8
9
10
11 package org.mule.routing.filters.xml;
12
13 import org.mule.DefaultMuleMessage;
14 import org.mule.module.xml.filters.JXPathFilter;
15 import org.mule.module.xml.util.XMLTestUtils;
16 import org.mule.tck.AbstractMuleTestCase;
17
18 import java.io.InputStream;
19 import java.io.StringReader;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map;
24
25 import javax.xml.parsers.DocumentBuilderFactory;
26 import javax.xml.stream.XMLStreamReader;
27
28 import org.apache.commons.io.IOUtils;
29 import org.dom4j.DocumentHelper;
30 import org.xml.sax.InputSource;
31
32 public class JXPathFilterTestCase extends AbstractMuleTestCase
33 {
34 private String xmlStringInput = null;
35 private String xmlStringInputNS = null;
36 private org.dom4j.Document dom4jDocumentInput = null;
37 private org.dom4j.Document dom4jDocumentInputNS = null;
38 private org.w3c.dom.Document w3cDocumentInput = null;
39 private org.w3c.dom.Document w3cDocumentInputNS = null;
40 private JXPathFilter simpleFilter = null;
41 private JXPathFilter nsAwareFilter = null;
42
43
44 protected void doSetUp() throws Exception
45 {
46 final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
47
48
49 InputStream is = currentClassLoader.getResourceAsStream("cdcatalog.xml");
50 assertNotNull("Test resource 'cdcatalog.xml' not found.", is);
51 xmlStringInput = IOUtils.toString(is);
52 dom4jDocumentInput = DocumentHelper.parseText(xmlStringInput);
53 w3cDocumentInput = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
54 new InputSource(new StringReader(xmlStringInput)));
55 simpleFilter = new JXPathFilter();
56
57
58 is = currentClassLoader.getResourceAsStream("cdcatalogNS.xml");
59 assertNotNull("Test resource 'cdcatalogNS.xml' not found.", is);
60 xmlStringInputNS = IOUtils.toString(is);
61 dom4jDocumentInputNS = DocumentHelper.parseText(xmlStringInputNS);
62 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
63 dbf.setNamespaceAware(true);
64 w3cDocumentInputNS = dbf.newDocumentBuilder().parse(
65 new InputSource(new StringReader(xmlStringInputNS)));
66 nsAwareFilter = new JXPathFilter();
67 Map namespaces = new HashMap();
68 namespaces.put("nsone", "http://one.org");
69 namespaces.put("nstwo", "http://two.org");
70 nsAwareFilter.setNamespaces(namespaces);
71 }
72
73 public void testBogusExpression() throws Exception
74 {
75 try
76 {
77 JXPathFilter myFilter = new JXPathFilter();
78 myFilter.setPattern("foo/bar/");
79 myFilter.accept(new DefaultMuleMessage(xmlStringInput));
80 fail("Invalid XPath should have thrown an exception");
81 }
82
83 catch (Exception e)
84 {
85
86 }
87 }
88
89 private void doTestExpectedValueFilter(Object xmlData) throws Exception
90 {
91 simpleFilter.setPattern("catalog/cd[3]/title");
92 simpleFilter.setExpectedValue("Greatest Hits");
93 assertTrue(simpleFilter.accept(new DefaultMuleMessage(xmlData)));
94 }
95
96 private void doTestBooleanFilter1(Object xmlData) throws Exception
97 {
98 simpleFilter.setPattern("(catalog/cd[3]/title) ='Greatest Hits'");
99 assertTrue(simpleFilter.accept(new DefaultMuleMessage(xmlData)));
100 }
101
102 private void doTestBooleanFilter2(Object xmlData) throws Exception
103 {
104 simpleFilter.setPattern("count(catalog/cd) = 26");
105 assertTrue(simpleFilter.accept(new DefaultMuleMessage(xmlData)));
106 }
107
108 private void doTestExpectedValueFilterNS(Object xmlData) throws Exception
109 {
110 nsAwareFilter.setPattern("nsone:catalog/nstwo:cd[3]/title");
111 nsAwareFilter.setExpectedValue("Greatest Hits");
112 assertTrue(nsAwareFilter.accept(new DefaultMuleMessage(xmlData)));
113 }
114
115 private void doTestBooleanFilter1NS(Object xmlData) throws Exception
116 {
117 nsAwareFilter.setPattern("(nsone:catalog/nstwo:cd[3]/title) ='Greatest Hits'");
118 assertTrue(nsAwareFilter.accept(new DefaultMuleMessage(xmlData)));
119 }
120
121 private void doTestBooleanFilter2NS(Object xmlData) throws Exception
122 {
123 nsAwareFilter.setPattern("count(nsone:catalog/nstwo:cd) = 26");
124 assertTrue(nsAwareFilter.accept(new DefaultMuleMessage(xmlData)));
125 }
126
127 public void testFilterOnObject() throws Exception
128 {
129 Dummy d = new Dummy();
130 d.setId(10);
131 d.setContent("hello");
132
133 simpleFilter.setPattern("id=10 and content='hello'");
134 assertTrue(simpleFilter.accept(new DefaultMuleMessage(d)));
135 }
136
137 public void testExpectedValueFilterXmlString() throws Exception
138 {
139 doTestExpectedValueFilter(xmlStringInput);
140 }
141
142 public void testExpectedValueFilterXmlByteArray() throws Exception
143 {
144 doTestExpectedValueFilter(xmlStringInput.getBytes());
145 }
146
147 public void testBooleanFilter1XmlString() throws Exception
148 {
149 doTestBooleanFilter1(xmlStringInput);
150 }
151
152 public void testBooleanFilter2XmlString() throws Exception
153 {
154 doTestBooleanFilter2(xmlStringInput);
155 }
156
157 public void testExpectedValueFilterDom4JDocument() throws Exception
158 {
159 doTestExpectedValueFilter(dom4jDocumentInput);
160 }
161
162 public void testBooleanFilter1Dom4JDocument() throws Exception
163 {
164 doTestBooleanFilter1(dom4jDocumentInput);
165 }
166
167 public void testBooleanFilter2Dom4JDocument() throws Exception
168 {
169 doTestBooleanFilter2(dom4jDocumentInput);
170 }
171
172 public void testExpectedValueFilterW3cDocument() throws Exception
173 {
174 doTestExpectedValueFilter(w3cDocumentInput);
175 }
176
177 public void testBooleanFilter1W3cDocument() throws Exception
178 {
179 doTestBooleanFilter1(w3cDocumentInput);
180 }
181
182 public void testBooleanFilter2W3cDocument() throws Exception
183 {
184 doTestBooleanFilter2(w3cDocumentInput);
185 }
186
187 public void testExpectedValueFilterXmlStringNS() throws Exception
188 {
189 doTestExpectedValueFilterNS(xmlStringInputNS);
190 }
191
192 public void testBooleanFilter1XmlStringNS() throws Exception
193 {
194 doTestBooleanFilter1NS(xmlStringInputNS);
195 }
196
197 public void testBooleanFilter2XmlStringNS() throws Exception
198 {
199 doTestBooleanFilter2NS(xmlStringInputNS);
200 }
201
202 public void testExpectedValueFilterDom4JDocumentNS() throws Exception
203 {
204 doTestExpectedValueFilterNS(dom4jDocumentInputNS);
205 }
206
207 public void testBooleanFilter1Dom4JDocumentNS() throws Exception
208 {
209 doTestBooleanFilter1NS(dom4jDocumentInputNS);
210 }
211
212 public void testBooleanFilter2Dom4JDocumentNS() throws Exception
213 {
214 doTestBooleanFilter2NS(dom4jDocumentInputNS);
215 }
216
217 public void testExpectedValueFilterW3cDocumentNS() throws Exception
218 {
219 doTestExpectedValueFilterNS(w3cDocumentInputNS);
220 }
221
222 public void testBooleanFilter1W3cDocumentNS() throws Exception
223 {
224 doTestBooleanFilter1NS(w3cDocumentInputNS);
225 }
226
227 public void testBooleanFilter2W3cDocumentNS() throws Exception
228 {
229 doTestBooleanFilter2NS(w3cDocumentInputNS);
230 }
231
232 public void testSimpleFilterXmlMessageVariants() throws Exception
233 {
234 simpleFilter.setPattern("catalog/cd[3]/title");
235 simpleFilter.setExpectedValue("Greatest Hits");
236
237 List list = XMLTestUtils.getXmlMessageVariants("cdcatalog.xml");
238 Iterator it = list.iterator();
239
240 Object msg;
241 while (it.hasNext())
242 {
243 msg = it.next();
244
245 if (!(msg instanceof XMLStreamReader))
246 {
247 assertTrue("Test failed for message type: " + msg.getClass(), simpleFilter.accept(new DefaultMuleMessage(msg)));
248 }
249 }
250 }
251 }