1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 package org.jaxen.test;
51
52 import junit.framework.Test;
53 import junit.framework.TestCase;
54 import junit.framework.TestSuite;
55 import junit.textui.TestRunner;
56
57 import org.jaxen.JaxenException;
58 import org.jaxen.pattern.Pattern;
59 import org.jaxen.pattern.PatternParser;
60 import org.jaxen.saxpath.SAXPathException;
61 import org.jaxen.saxpath.helpers.XPathReaderFactory;
62
63 /*** Tests the use of priority in the Pattern implementations.
64 *
65 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
66 * @version $Revision$
67 */
68 public class PriorityTest extends TestCase
69 {
70 public PriorityTest(String name)
71 {
72 super( name );
73 }
74
75 public static void main(String[] args)
76 {
77 TestRunner.run( suite() );
78 }
79
80 public static Test suite()
81 {
82 return new TestSuite( PriorityTest.class );
83 }
84
85 public void setUp()
86 {
87 System.setProperty( XPathReaderFactory.DRIVER_PROPERTY,
88 "" );
89 }
90
91 public void testDocumentNode() throws Exception
92 {
93 testPriority( "/", -0.5, Pattern.DOCUMENT_NODE );
94 }
95
96 public void testNameNode() throws Exception
97 {
98 testPriority( "foo", 0, Pattern.ELEMENT_NODE );
99 }
100
101 public void testQNameNode() throws Exception
102 {
103 testPriority( "foo:bar", 0, Pattern.ELEMENT_NODE );
104 }
105
106 public void testFilter() throws Exception
107 {
108 testPriority( "foo[@id='123']", 0.5, Pattern.ELEMENT_NODE );
109 }
110
111 public void testURI() throws Exception
112 {
113 testPriority( "foo:*", -0.25, Pattern.ELEMENT_NODE );
114 }
115
116 public void testNodeType() throws Exception
117 {
118 testPriority( "text()", -0.5, Pattern.TEXT_NODE );
119 }
120
121 public void testAttribute() throws Exception
122 {
123 testPriority( "@*", -0.5, Pattern.ATTRIBUTE_NODE );
124 }
125
126 public void testAnyNode() throws Exception
127 {
128 testPriority( "*", -0.5, Pattern.ELEMENT_NODE );
129 }
130
131 protected void testPriority(String expr, double priority, short nodeType)
132 throws JaxenException, SAXPathException
133 {
134
135 Pattern pattern = PatternParser.parse( expr );
136 double d = pattern.getPriority();
137 short nt = pattern.getMatchType();
138
139 assertEquals( "expr: " + expr,
140 new Double( priority ),
141 new Double( d ) );
142
143 assertEquals( "nodeType: " + expr,
144 nodeType,
145 nt );
146 }
147 }