1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.tika.sax.xpath;
18  
19  import junit.framework.TestCase;
20  
21  public class XPathParserTest extends TestCase {
22  
23      private static final String NS = "test namespace";
24  
25      private XPathParser parser;
26  
27      protected void setUp() {
28          parser = new XPathParser();
29          parser.addPrefix(null, null);
30          parser.addPrefix("prefix", NS);
31      }
32  
33      public void testText() {
34          Matcher matcher = parser.parse("/text()");
35          assertTrue(matcher.matchesText());
36          assertFalse(matcher.matchesElement());
37          assertFalse(matcher.matchesAttribute(NS, "name"));
38          assertEquals(Matcher.FAIL, matcher.descend(NS, "name"));
39      }
40  
41      public void testAnyAttribute() {
42          Matcher matcher = parser.parse("/@*");
43          assertFalse(matcher.matchesText());
44          assertFalse(matcher.matchesElement());
45          assertTrue(matcher.matchesAttribute(null, "name"));
46          assertTrue(matcher.matchesAttribute(NS, "name"));
47          assertTrue(matcher.matchesAttribute(NS, "eman"));
48          assertEquals(Matcher.FAIL, matcher.descend(NS, "name"));
49      }
50  
51      public void testNamedAttribute() {
52          Matcher matcher = parser.parse("/@name");
53          assertFalse(matcher.matchesText());
54          assertFalse(matcher.matchesElement());
55          assertTrue(matcher.matchesAttribute(null, "name"));
56          assertFalse(matcher.matchesAttribute(NS, "name"));
57          assertFalse(matcher.matchesAttribute(NS, "eman"));
58          assertEquals(Matcher.FAIL, matcher.descend(NS, "name"));
59      }
60  
61      public void testPrefixedAttribute() {
62          Matcher matcher = parser.parse("/@prefix:name");
63          assertFalse(matcher.matchesText());
64          assertFalse(matcher.matchesElement());
65          assertFalse(matcher.matchesAttribute(null, "name"));
66          assertTrue(matcher.matchesAttribute(NS, "name"));
67          assertFalse(matcher.matchesAttribute(NS, "eman"));
68          assertEquals(Matcher.FAIL, matcher.descend(NS, "name"));
69      }
70  
71      public void testAnyElement() {
72          Matcher matcher = parser.parse("/*");
73          assertFalse(matcher.matchesText());
74          assertFalse(matcher.matchesElement());
75          assertFalse(matcher.matchesAttribute(null, "name"));
76          assertFalse(matcher.matchesAttribute(NS, "name"));
77          assertFalse(matcher.matchesAttribute(NS, "eman"));
78          matcher = matcher.descend(NS, "name");
79          assertFalse(matcher.matchesText());
80          assertTrue(matcher.matchesElement());
81          assertFalse(matcher.matchesAttribute(null, "name"));
82          assertFalse(matcher.matchesAttribute(NS, "name"));
83          assertFalse(matcher.matchesAttribute(NS, "eman"));
84          assertEquals(Matcher.FAIL, matcher.descend(NS, "name"));
85      }
86  
87      public void testNamedElement() {
88          Matcher matcher = parser.parse("/name");
89          assertFalse(matcher.matchesText());
90          assertFalse(matcher.matchesElement());
91          assertFalse(matcher.matchesAttribute(null, "name"));
92          assertFalse(matcher.matchesAttribute(NS, "name"));
93          assertFalse(matcher.matchesAttribute(NS, "eman"));
94          assertEquals(Matcher.FAIL, matcher.descend(NS, "name"));
95          assertEquals(Matcher.FAIL, matcher.descend(null, "enam"));
96          matcher = matcher.descend(null, "name");
97          assertFalse(matcher.matchesText());
98          assertTrue(matcher.matchesElement());
99          assertFalse(matcher.matchesAttribute(null, "name"));
100         assertFalse(matcher.matchesAttribute(NS, "name"));
101         assertFalse(matcher.matchesAttribute(NS, "eman"));
102     }
103 
104     public void testPrefixedElement() {
105         Matcher matcher = parser.parse("/prefix:name");
106         assertFalse(matcher.matchesText());
107         assertFalse(matcher.matchesElement());
108         assertFalse(matcher.matchesAttribute(null, "name"));
109         assertFalse(matcher.matchesAttribute(NS, "name"));
110         assertFalse(matcher.matchesAttribute(NS, "eman"));
111         assertEquals(Matcher.FAIL, matcher.descend(null, "name"));
112         assertEquals(Matcher.FAIL, matcher.descend(NS, "enam"));
113         matcher = matcher.descend(NS, "name");
114         assertFalse(matcher.matchesText());
115         assertTrue(matcher.matchesElement());
116         assertFalse(matcher.matchesAttribute(null, "name"));
117         assertFalse(matcher.matchesAttribute(NS, "name"));
118         assertFalse(matcher.matchesAttribute(NS, "eman"));
119     }
120 
121 }