1   /*
2    * $Id: JXPathFilterTestCase.java 8413 2007-09-14 14:53:16Z alan.cassar $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.impl.MuleMessage;
14  import org.mule.tck.AbstractMuleTestCase;
15  
16  import java.io.InputStream;
17  import java.io.StringReader;
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import javax.xml.parsers.DocumentBuilderFactory;
22  
23  import org.apache.commons.io.IOUtils;
24  import org.dom4j.DocumentHelper;
25  import org.dom4j.InvalidXPathException;
26  import org.xml.sax.InputSource;
27  
28  public class JXPathFilterTestCase extends AbstractMuleTestCase
29  {
30      private String xmlStringInput = null;
31      private String xmlStringInputNS = null;
32      private org.dom4j.Document dom4jDocumentInput = null;
33      private org.dom4j.Document dom4jDocumentInputNS = null;
34      private org.w3c.dom.Document w3cDocumentInput = null;
35      private org.w3c.dom.Document w3cDocumentInputNS = null;
36      private JXPathFilter simpleFilter = null;
37      private JXPathFilter nsAwareFilter = null;
38  
39      // @SuppressWarnings("unchecked")
40      protected void doSetUp() throws Exception
41      {
42          final ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
43  
44          // Read No-Namespace Xml file
45          InputStream is = currentClassLoader.getResourceAsStream("cdcatalog.xml");
46          assertNotNull("Test resource 'cdcatalog.xml' not found.", is);
47          xmlStringInput = IOUtils.toString(is);
48          dom4jDocumentInput = DocumentHelper.parseText(xmlStringInput);
49          w3cDocumentInput = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
50              new InputSource(new StringReader(xmlStringInput)));
51          simpleFilter = new JXPathFilter();
52  
53          // Read Namespace Xml file
54          is = currentClassLoader.getResourceAsStream("cdcatalogNS.xml");
55          assertNotNull("Test resource 'cdcatalogNS.xml' not found.", is);
56          xmlStringInputNS = IOUtils.toString(is);
57          dom4jDocumentInputNS = DocumentHelper.parseText(xmlStringInputNS);
58          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
59          dbf.setNamespaceAware(true);
60          w3cDocumentInputNS = dbf.newDocumentBuilder().parse(
61              new InputSource(new StringReader(xmlStringInputNS)));
62          nsAwareFilter = new JXPathFilter();
63          Map namespaces = new HashMap();
64          namespaces.put("nsone", "http://one.org");
65          namespaces.put("nstwo", "http://two.org");
66          nsAwareFilter.setNamespaces(namespaces);
67      }
68  
69      public void testBogusExpression() throws Exception
70      {
71          try
72          {
73              JXPathFilter myFilter = new JXPathFilter();
74              myFilter.setExpression("foo/bar/");
75              myFilter.accept(new MuleMessage(xmlStringInput));
76              fail("Invalid XPath should have thrown an exception");
77          }
78          catch (InvalidXPathException e)
79          {
80              // expected
81          }
82      }
83  
84      private void doTestExpectedValueFilter(Object xmlData) throws Exception
85      {
86          simpleFilter.setExpression("catalog/cd[3]/title");
87          simpleFilter.setExpectedValue("Greatest Hits");
88          assertTrue(simpleFilter.accept(new MuleMessage(xmlData)));
89      }
90  
91      private void doTestBooleanFilter1(Object xmlData) throws Exception
92      {
93          simpleFilter.setExpression("(catalog/cd[3]/title) ='Greatest Hits'");
94          assertTrue(simpleFilter.accept(new MuleMessage(xmlData)));
95      }
96  
97      private void doTestBooleanFilter2(Object xmlData) throws Exception
98      {
99          simpleFilter.setExpression("count(catalog/cd) = 26");
100         assertTrue(simpleFilter.accept(new MuleMessage(xmlData)));
101     }
102 
103     private void doTestExpectedValueFilterNS(Object xmlData) throws Exception
104     {
105         nsAwareFilter.setExpression("nsone:catalog/nstwo:cd[3]/title");
106         nsAwareFilter.setExpectedValue("Greatest Hits");
107         assertTrue(nsAwareFilter.accept(new MuleMessage(xmlData)));
108     }
109 
110     private void doTestBooleanFilter1NS(Object xmlData) throws Exception
111     {
112         nsAwareFilter.setExpression("(nsone:catalog/nstwo:cd[3]/title) ='Greatest Hits'");
113         assertTrue(nsAwareFilter.accept(new MuleMessage(xmlData)));
114     }
115 
116     private void doTestBooleanFilter2NS(Object xmlData) throws Exception
117     {
118         nsAwareFilter.setExpression("count(nsone:catalog/nstwo:cd) = 26");
119         assertTrue(nsAwareFilter.accept(new MuleMessage(xmlData)));
120     }
121 
122     public void testFilterOnObject() throws Exception
123     {
124         Dummy d = new Dummy();
125         d.setId(10);
126         d.setContent("hello");
127 
128         simpleFilter.setExpression("id=10 and content='hello'");
129         assertTrue(simpleFilter.accept(new MuleMessage(d)));
130     }
131 
132     public void testExpectedValueFilterXmlString() throws Exception
133     {
134         doTestExpectedValueFilter(xmlStringInput);
135     }
136     
137     public void testExpectedValueFilterXmlByteArray() throws Exception
138     {
139         doTestExpectedValueFilter(xmlStringInput.getBytes());
140     }
141 
142     public void testBooleanFilter1XmlString() throws Exception
143     {
144         doTestBooleanFilter1(xmlStringInput);
145     }
146 
147     public void testBooleanFilter2XmlString() throws Exception
148     {
149         doTestBooleanFilter2(xmlStringInput);
150     }
151 
152     public void testExpectedValueFilterDom4JDocument() throws Exception
153     {
154         doTestExpectedValueFilter(dom4jDocumentInput);
155     }
156 
157     public void testBooleanFilter1Dom4JDocument() throws Exception
158     {
159         doTestBooleanFilter1(dom4jDocumentInput);
160     }
161 
162     public void testBooleanFilter2Dom4JDocument() throws Exception
163     {
164         doTestBooleanFilter2(dom4jDocumentInput);
165     }
166 
167     public void testExpectedValueFilterW3cDocument() throws Exception
168     {
169         doTestExpectedValueFilter(w3cDocumentInput);
170     }
171 
172     public void testBooleanFilter1W3cDocument() throws Exception
173     {
174         doTestBooleanFilter1(w3cDocumentInput);
175     }
176 
177     public void testBooleanFilter2W3cDocument() throws Exception
178     {
179         doTestBooleanFilter2(w3cDocumentInput);
180     }
181 
182     public void testExpectedValueFilterXmlStringNS() throws Exception
183     {
184         doTestExpectedValueFilterNS(xmlStringInputNS);
185     }
186 
187     public void testBooleanFilter1XmlStringNS() throws Exception
188     {
189         doTestBooleanFilter1NS(xmlStringInputNS);
190     }
191 
192     public void testBooleanFilter2XmlStringNS() throws Exception
193     {
194         doTestBooleanFilter2NS(xmlStringInputNS);
195     }
196 
197     public void testExpectedValueFilterDom4JDocumentNS() throws Exception
198     {
199         doTestExpectedValueFilterNS(dom4jDocumentInputNS);
200     }
201 
202     public void testBooleanFilter1Dom4JDocumentNS() throws Exception
203     {
204         doTestBooleanFilter1NS(dom4jDocumentInputNS);
205     }
206 
207     public void testBooleanFilter2Dom4JDocumentNS() throws Exception
208     {
209         doTestBooleanFilter2NS(dom4jDocumentInputNS);
210     }
211 
212     public void testExpectedValueFilterW3cDocumentNS() throws Exception
213     {
214         doTestExpectedValueFilterNS(w3cDocumentInputNS);
215     }
216 
217     public void testBooleanFilter1W3cDocumentNS() throws Exception
218     {
219         doTestBooleanFilter1NS(w3cDocumentInputNS);
220     }
221 
222     public void testBooleanFilter2W3cDocumentNS() throws Exception
223     {
224         doTestBooleanFilter2NS(w3cDocumentInputNS);
225     }
226 
227 }