View Javadoc

1   /*
2    * $Id: JXPathFilterTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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      // @SuppressWarnings("unchecked")
44      protected void doSetUp() throws Exception
45      {
46          final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
47  
48          // Read No-Namespace Xml file
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          simpleFilter.setMuleContext(muleContext);
57  
58          // Read Namespace Xml file
59          is = currentClassLoader.getResourceAsStream("cdcatalogNS.xml");
60          assertNotNull("Test resource 'cdcatalogNS.xml' not found.", is);
61          xmlStringInputNS = IOUtils.toString(is);
62          dom4jDocumentInputNS = DocumentHelper.parseText(xmlStringInputNS);
63          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
64          dbf.setNamespaceAware(true);
65          w3cDocumentInputNS = dbf.newDocumentBuilder().parse(
66                  new InputSource(new StringReader(xmlStringInputNS)));
67          nsAwareFilter = new JXPathFilter();
68          Map namespaces = new HashMap();
69          namespaces.put("nsone", "http://one.org");
70          namespaces.put("nstwo", "http://two.org");
71          nsAwareFilter.setNamespaces(namespaces);
72          nsAwareFilter.setMuleContext(muleContext);
73          muleContext.start();
74      }
75  
76      public void testBogusExpression() throws Exception
77      {
78          try
79          {
80              JXPathFilter myFilter = createObject(JXPathFilter.class);
81              myFilter.setPattern("foo/bar/");
82              myFilter.accept(new DefaultMuleMessage(xmlStringInput, muleContext));
83              fail("Invalid XPath should have thrown an exception");
84          }
85          //Now we have Jaxen on the class path we get a Jaxen exception, but this is an unchecked exception
86          catch (Exception e)
87          {
88              // expected
89          }
90      }
91  
92      private void doTestExpectedValueFilter(Object xmlData) throws Exception
93      {
94          simpleFilter.setPattern("catalog/cd[3]/title");
95          simpleFilter.setExpectedValue("Greatest Hits");
96          assertTrue(simpleFilter.accept(new DefaultMuleMessage(xmlData, muleContext)));
97      }
98  
99      private void doTestBooleanFilter1(Object xmlData) throws Exception
100     {
101         simpleFilter.setPattern("(catalog/cd[3]/title) ='Greatest Hits'");
102         assertTrue(simpleFilter.accept(new DefaultMuleMessage(xmlData, muleContext)));
103     }
104 
105     private void doTestBooleanFilter2(Object xmlData) throws Exception
106     {
107         simpleFilter.setPattern("count(catalog/cd) = 26");
108         assertTrue(simpleFilter.accept(new DefaultMuleMessage(xmlData, muleContext)));
109     }
110 
111     private void doTestExpectedValueFilterNS(Object xmlData) throws Exception
112     {
113         nsAwareFilter.setPattern("nsone:catalog/nstwo:cd[3]/title");
114         nsAwareFilter.setExpectedValue("Greatest Hits");
115         assertTrue(nsAwareFilter.accept(new DefaultMuleMessage(xmlData, muleContext)));
116     }
117 
118     private void doTestBooleanFilter1NS(Object xmlData) throws Exception
119     {
120         nsAwareFilter.setPattern("(nsone:catalog/nstwo:cd[3]/title) ='Greatest Hits'");
121         assertTrue(nsAwareFilter.accept(new DefaultMuleMessage(xmlData, muleContext)));
122     }
123 
124     private void doTestBooleanFilter2NS(Object xmlData) throws Exception
125     {
126         nsAwareFilter.setPattern("count(nsone:catalog/nstwo:cd) = 26");
127         assertTrue(nsAwareFilter.accept(new DefaultMuleMessage(xmlData, muleContext)));
128     }
129 
130     public void testFilterOnObject() throws Exception
131     {
132         Dummy d = new Dummy();
133         d.setId(10);
134         d.setContent("hello");
135 
136         simpleFilter.setPattern("id=10 and content='hello'");
137         assertTrue(simpleFilter.accept(new DefaultMuleMessage(d, muleContext)));
138     }
139 
140     public void testExpectedValueFilterXmlString() throws Exception
141     {
142         doTestExpectedValueFilter(xmlStringInput);
143     }
144 
145     public void testExpectedValueFilterXmlByteArray() throws Exception
146     {
147         doTestExpectedValueFilter(xmlStringInput.getBytes());
148     }
149 
150     public void testBooleanFilter1XmlString() throws Exception
151     {
152         doTestBooleanFilter1(xmlStringInput);
153     }
154 
155     public void testBooleanFilter2XmlString() throws Exception
156     {
157         doTestBooleanFilter2(xmlStringInput);
158     }
159 
160     public void testExpectedValueFilterDom4JDocument() throws Exception
161     {
162         doTestExpectedValueFilter(dom4jDocumentInput);
163     }
164 
165     public void testBooleanFilter1Dom4JDocument() throws Exception
166     {
167         doTestBooleanFilter1(dom4jDocumentInput);
168     }
169 
170     public void testBooleanFilter2Dom4JDocument() throws Exception
171     {
172         doTestBooleanFilter2(dom4jDocumentInput);
173     }
174 
175     public void testExpectedValueFilterW3cDocument() throws Exception
176     {
177         doTestExpectedValueFilter(w3cDocumentInput);
178     }
179 
180     public void testBooleanFilter1W3cDocument() throws Exception
181     {
182         doTestBooleanFilter1(w3cDocumentInput);
183     }
184 
185     public void testBooleanFilter2W3cDocument() throws Exception
186     {
187         doTestBooleanFilter2(w3cDocumentInput);
188     }
189 
190     public void testExpectedValueFilterXmlStringNS() throws Exception
191     {
192         doTestExpectedValueFilterNS(xmlStringInputNS);
193     }
194 
195     public void testBooleanFilter1XmlStringNS() throws Exception
196     {
197         doTestBooleanFilter1NS(xmlStringInputNS);
198     }
199 
200     public void testBooleanFilter2XmlStringNS() throws Exception
201     {
202         doTestBooleanFilter2NS(xmlStringInputNS);
203     }
204 
205     public void testExpectedValueFilterDom4JDocumentNS() throws Exception
206     {
207         doTestExpectedValueFilterNS(dom4jDocumentInputNS);
208     }
209 
210     public void testBooleanFilter1Dom4JDocumentNS() throws Exception
211     {
212         doTestBooleanFilter1NS(dom4jDocumentInputNS);
213     }
214 
215     public void testBooleanFilter2Dom4JDocumentNS() throws Exception
216     {
217         doTestBooleanFilter2NS(dom4jDocumentInputNS);
218     }
219 
220     public void testExpectedValueFilterW3cDocumentNS() throws Exception
221     {
222         doTestExpectedValueFilterNS(w3cDocumentInputNS);
223     }
224 
225     public void testBooleanFilter1W3cDocumentNS() throws Exception
226     {
227         doTestBooleanFilter1NS(w3cDocumentInputNS);
228     }
229 
230     public void testBooleanFilter2W3cDocumentNS() throws Exception
231     {
232         doTestBooleanFilter2NS(w3cDocumentInputNS);
233     }
234 
235     public void testSimpleFilterXmlMessageVariants() throws Exception
236     {
237         simpleFilter.setPattern("catalog/cd[3]/title");
238         simpleFilter.setExpectedValue("Greatest Hits");
239         
240         List list = XMLTestUtils.getXmlMessageVariants("cdcatalog.xml");
241         Iterator it = list.iterator();
242         
243         Object msg;
244         while (it.hasNext())
245         {
246             msg = it.next();
247             // TODO Not working for XMLStreamReader
248             if (!(msg instanceof XMLStreamReader))
249             {
250                 assertTrue("Test failed for message type: " + msg.getClass(), simpleFilter.accept(new DefaultMuleMessage(msg, muleContext)));
251             }
252         }
253     }
254 }