1   /*
2    * $Header$
3    * $Revision$
4    * $Date$
5    *
6    * ====================================================================
7    *
8    * Copyright 2008 Andrew Sales
9    * All rights reserved.
10   *
11   * Redistribution and use in source and binary forms, with or without
12   * modification, are permitted provided that the following conditions are
13   * met:
14   * 
15   *   * Redistributions of source code must retain the above copyright
16   *     notice, this list of conditions and the following disclaimer.
17   * 
18   *   * Redistributions in binary form must reproduce the above copyright
19   *     notice, this list of conditions and the following disclaimer in the
20   *     documentation and/or other materials provided with the distribution.
21   * 
22   *   * Neither the name of the Jaxen Project nor the names of its
23   *     contributors may be used to endorse or promote products derived 
24   *     from this software without specific prior written permission.
25   * 
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27   * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28   * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
30   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   *
38   * ====================================================================
39   * This software consists of voluntary contributions made by many 
40   * individuals on behalf of the Jaxen Project and was originally 
41   * created by bob mcwhirter <bob@werken.com> and 
42   * James Strachan <jstrachan@apache.org>.  For more information on the 
43   * Jaxen Project, please see <http://www.jaxen.org/>.
44   * 
45   * $Id$
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   * <p>Tests comparison of nodesets using the = and != operators.</p>
64   * 
65   * <blockquote>If both objects to be compared are node-sets, then the comparison 
66   * will be true if and only if there is a node in the first node-set and a node 
67   * in the second node-set such that the result of performing the comparison 
68   * on the string-values of the two nodes is true</blockquote>
69   * 
70   * @author Andrew Sales
71   *
72   * $Id$
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           <a><b>foo</b><b>bar</b><b>blort</b><c/><c>12.0</c><c>blort</c></a> 
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         //an empty nodeset should be equal to false()
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         //an empty nodeset should be not equal to true()  
181         XPath xpath = new DOMXPath( "//d != true()" );
182         Boolean result = (Boolean) xpath.evaluate( doc );
183         assertTrue(result.booleanValue());
184     }
185 
186 }