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: TestPrefix.java,v 1.9 2004/06/25 08:03:51 maartenc Exp $
8    */
9   
10  package org.dom4j.xpath;
11  
12  import java.io.File;
13  import java.util.List;
14  
15  import junit.framework.Test;
16  import junit.framework.TestSuite;
17  import junit.textui.TestRunner;
18  
19  import org.dom4j.AbstractTestCase;
20  import org.dom4j.DocumentHelper;
21  import org.dom4j.XPath;
22  import org.dom4j.io.SAXReader;
23  import org.jaxen.SimpleNamespaceContext;
24  
25  /*** Tests finding items using a namespace prefix
26    *
27    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
28    * @version $Revision: 1.9 $
29    */
30  public class TestPrefix extends AbstractTestCase {
31  
32      protected static boolean VERBOSE = false;
33      
34      protected static String[] paths = {
35          "//xplt:anyElement",
36          "//xpl:insertText",
37          "/Template/Application1/xpl:insertText",
38          "/Template/Application2/xpl:insertText"
39      };
40      
41      
42      public static void main( String[] args ) {
43          TestRunner.run( suite() );
44      }
45      
46      public static Test suite() {
47          return new TestSuite( TestPrefix.class );
48      }
49      
50      public TestPrefix(String name) {
51          super(name);
52      }
53  
54      // Test case(s)
55      //-------------------------------------------------------------------------                    
56      public void testXPaths() throws Exception {        
57          int size = paths.length;
58          for ( int i = 0; i < size; i++ ) {
59              testXPath( paths[i] );
60          }
61      }
62          
63      // Implementation methods
64      //-------------------------------------------------------------------------                    
65      protected void testXPath(String xpathText) {
66          XPath xpath = DocumentHelper.createXPath(xpathText);
67          
68          SimpleNamespaceContext context = new SimpleNamespaceContext();
69          context.addNamespace( "xplt", "www.xxxx.com" );
70          context.addNamespace( "xpl", "www.xxxx.com" );
71          xpath.setNamespaceContext( context );
72          
73          List list = xpath.selectNodes( document );
74          
75          log( "Searched path: " + xpathText + " found: " + list.size() + " result(s)" );
76  
77          assertTrue( "Should have found at lest one result", list.size() > 0 );
78          
79          if ( VERBOSE ) {
80              log( "xpath: " + xpath );
81              log( "results: " + list );
82          }
83      }
84      
85      protected void setUp() throws Exception {
86          document = new SAXReader().read( new File( "xml/testNamespaces.xml" ) );
87      }
88  }
89  
90  
91  
92  
93  /*
94   * Redistribution and use of this software and associated documentation
95   * ("Software"), with or without modification, are permitted provided
96   * that the following conditions are met:
97   *
98   * 1. Redistributions of source code must retain copyright
99   *    statements and notices.  Redistributions must also contain a
100  *    copy of this document.
101  *
102  * 2. Redistributions in binary form must reproduce the
103  *    above copyright notice, this list of conditions and the
104  *    following disclaimer in the documentation and/or other
105  *    materials provided with the distribution.
106  *
107  * 3. The name "DOM4J" must not be used to endorse or promote
108  *    products derived from this Software without prior written
109  *    permission of MetaStuff, Ltd.  For written permission,
110  *    please contact dom4j-info@metastuff.com.
111  *
112  * 4. Products derived from this Software may not be called "DOM4J"
113  *    nor may "DOM4J" appear in their names without prior written
114  *    permission of MetaStuff, Ltd. DOM4J is a registered
115  *    trademark of MetaStuff, Ltd.
116  *
117  * 5. Due credit should be given to the DOM4J Project - 
118  *    http://www.dom4j.org
119  *
120  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
121  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
122  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
123  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
124  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
125  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
126  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
127  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
128  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
129  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
130  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
131  * OF THE POSSIBILITY OF SUCH DAMAGE.
132  *
133  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
134  *
135  * $Id: TestPrefix.java,v 1.9 2004/06/25 08:03:51 maartenc Exp $
136  */