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 package org.jaxen.test;
49
50 import javax.xml.parsers.DocumentBuilder;
51 import javax.xml.parsers.DocumentBuilderFactory;
52 import javax.xml.parsers.ParserConfigurationException;
53
54 import junit.framework.TestCase;
55
56 import org.jaxen.JaxenException;
57 import org.jaxen.XPath;
58 import org.jaxen.dom.DOMXPath;
59 import org.w3c.dom.Document;
60 import org.w3c.dom.Element;
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public class NodesetEqualityTest extends TestCase {
75 private Document doc;
76
77 public void setUp() throws ParserConfigurationException {
78 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
79 factory.setNamespaceAware( true );
80 DocumentBuilder builder = factory.newDocumentBuilder();
81 doc = builder.newDocument();
82
83
84
85
86
87 Element a = doc.createElementNS( "", "a" );
88 doc.appendChild( a );
89 Element b1 = doc.createElementNS( "", "b" );
90 b1.appendChild( doc.createTextNode( "foo" ) );
91 Element b2 = doc.createElementNS( "", "b" );
92 b2.appendChild( doc.createTextNode( "bar" ) );
93 Element b3 = doc.createElementNS( "", "b" );
94 b3.appendChild( doc.createTextNode( "blort" ) );
95
96 a.appendChild( b1 );
97 a.appendChild( b2 );
98 a.appendChild( b3 );
99
100 Element c1 = doc.createElementNS( "", "c" );
101 Element c2 = doc.createElementNS( "", "c" );
102 Element c3 = doc.createElementNS( "", "c" );
103
104 c2.appendChild( doc.createTextNode( " 12.0 " ) );
105 c3.appendChild( doc.createTextNode( "bar" ) );
106
107 a.appendChild( c1 );
108 a.appendChild( c2 );
109 a.appendChild( c3 );
110
111 }
112
113 public void testEqualsTwoNodesets() throws JaxenException
114 {
115 XPath xpath = new DOMXPath( "//b = //c" );
116 Boolean result = (Boolean) xpath.evaluate( doc );
117 assertTrue( result.booleanValue() );
118 }
119
120 public void testNotEqualsTwoNodesets() throws JaxenException
121 {
122 XPath xpath = new DOMXPath( "//a != //b" );
123 Boolean result = (Boolean) xpath.evaluate( doc );
124 assertTrue( result.booleanValue() );
125 }
126
127 public void testEqualsStringNodeset() throws JaxenException
128 {
129 XPath xpath = new DOMXPath( "//b = 'blort'" );
130 Boolean result = (Boolean) xpath.evaluate( doc );
131 assertTrue(result.booleanValue());
132 }
133
134 public void testNotEqualsStringNodeset() throws JaxenException
135 {
136 XPath xpath = new DOMXPath( "//b != 'phooey'" );
137 Boolean result = (Boolean) xpath.evaluate( doc );
138 assertTrue(result.booleanValue());
139 }
140
141 public void testEqualsNumberNodeset() throws JaxenException
142 {
143 XPath xpath = new DOMXPath( "//c = 12" );
144 Boolean result = (Boolean) xpath.evaluate( doc );
145 assertTrue(result.booleanValue());
146 }
147
148 public void testNotEqualsNumberNodeset() throws JaxenException
149 {
150 XPath xpath = new DOMXPath( "//c != 256" );
151 Boolean result = (Boolean) xpath.evaluate( doc );
152 assertTrue(result.booleanValue());
153 }
154
155 public void testEqualsBooleanNodeset1() throws JaxenException
156 {
157 XPath xpath = new DOMXPath( "//c = true()" );
158 Boolean result = (Boolean) xpath.evaluate( doc );
159 assertTrue(result.booleanValue());
160 }
161
162 public void testEqualsBooleanNodeset2() throws JaxenException
163 {
164
165 XPath xpath = new DOMXPath( "//d = false()" );
166 Boolean result = (Boolean) xpath.evaluate( doc );
167 assertTrue(result.booleanValue());
168 }
169
170
171 public void testNotEqualsBooleanNodeset1() throws JaxenException
172 {
173 XPath xpath = new DOMXPath( "//c != false()" );
174 Boolean result = (Boolean) xpath.evaluate( doc );
175 assertTrue(result.booleanValue());
176 }
177
178 public void testNotEqualsBooleanNodeset2() throws JaxenException
179 {
180
181 XPath xpath = new DOMXPath( "//d != true()" );
182 Boolean result = (Boolean) xpath.evaluate( doc );
183 assertTrue(result.booleanValue());
184 }
185
186 }