1   /*
2    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3    * 
4    * This software is open source. 
5    * See the bottom of this file for the licence.
6    * 
7    * $Id: TestSelectSingleNode.java,v 1.7 2004/06/25 08:03:51 maartenc Exp $
8    */
9   
10  package org.dom4j.xpath;
11  
12  import junit.framework.Test;
13  import junit.framework.TestSuite;
14  import junit.textui.TestRunner;
15  
16  import org.dom4j.AbstractTestCase;
17  import org.dom4j.Document;
18  import org.dom4j.Element;
19  import org.dom4j.Node;
20  import org.dom4j.io.SAXReader;
21  
22  /*** Tests the selectSingleNode method
23    *
24    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
25    * @version $Revision: 1.7 $
26    */
27  public class TestSelectSingleNode extends AbstractTestCase {
28  
29      
30      public static void main( String[] args ) {
31          TestRunner.run( suite() );
32      }
33      
34      public static Test suite() {
35          return new TestSuite( TestSelectSingleNode.class );
36      }
37      
38      public TestSelectSingleNode(String name) {
39          super(name);
40      }
41  
42      // Test case(s)
43      //-------------------------------------------------------------------------                    
44      public void testSelectSingleNode() throws Exception {        
45          Node node = document.selectSingleNode("/properties/client/threadsafe");
46          assertTrue( "Found a valid node", node != null );
47          
48          Element server = (Element) document.selectSingleNode( "/properties/server" );
49          assertTrue( "Found a valid server", server != null );
50  
51          server = (Element) document.getRootElement().selectSingleNode( "/properties/server" );
52          assertTrue( "Found a valid server", server != null );
53  
54          // try finding it via a relative path
55          server = (Element) document.selectSingleNode( "properties/server" );
56          assertTrue( "Found a valid server", server != null );
57          
58          // now lets use a relative path
59          Element connection = (Element) server.selectSingleNode( "db/connection" );
60          assertTrue( "Found a valid connection", connection != null );
61      }
62  
63      /*** Test out Steen's bug */
64      public void testSteensBug() throws Exception {        
65          Document document = new SAXReader().read(getClass().getResource("/xml/schema/personal.xsd"));
66          
67          assertNotNull( document.selectSingleNode( "/xs:schema/xs:element[@name='person']" ) );
68          
69          Element root = document.getRootElement();
70          
71          assertNotNull( root.selectSingleNode( "/xs:schema/xs:element[@name='person']" ) );        
72      }
73      
74      // Implementation methods
75      //-------------------------------------------------------------------------                    
76      protected void setUp() throws Exception {
77          document = new SAXReader().read(getClass().getResource("/xml/test/jimBrain.xml"));
78      }
79  }
80  
81  
82  
83  
84  /*
85   * Redistribution and use of this software and associated documentation
86   * ("Software"), with or without modification, are permitted provided
87   * that the following conditions are met:
88   *
89   * 1. Redistributions of source code must retain copyright
90   *    statements and notices.  Redistributions must also contain a
91   *    copy of this document.
92   *
93   * 2. Redistributions in binary form must reproduce the
94   *    above copyright notice, this list of conditions and the
95   *    following disclaimer in the documentation and/or other
96   *    materials provided with the distribution.
97   *
98   * 3. The name "DOM4J" must not be used to endorse or promote
99   *    products derived from this Software without prior written
100  *    permission of MetaStuff, Ltd.  For written permission,
101  *    please contact dom4j-info@metastuff.com.
102  *
103  * 4. Products derived from this Software may not be called "DOM4J"
104  *    nor may "DOM4J" appear in their names without prior written
105  *    permission of MetaStuff, Ltd. DOM4J is a registered
106  *    trademark of MetaStuff, Ltd.
107  *
108  * 5. Due credit should be given to the DOM4J Project - 
109  *    http://www.dom4j.org
110  *
111  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
112  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
113  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
114  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
115  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
116  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
117  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
118  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
119  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
120  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
121  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
122  * OF THE POSSIBILITY OF SUCH DAMAGE.
123  *
124  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
125  *
126  * $Id: TestSelectSingleNode.java,v 1.7 2004/06/25 08:03:51 maartenc Exp $
127  */