1   /*
2    * Copyright 2001-2005 (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   
8   package org.dom4j.xpath;
9   
10  import junit.textui.TestRunner;
11  
12  import java.util.Iterator;
13  import java.util.List;
14  
15  import org.dom4j.AbstractTestCase;
16  import org.dom4j.Attribute;
17  import org.dom4j.DocumentHelper;
18  import org.dom4j.XPath;
19  
20  /***
21   * Test harness for the attribute axis
22   * 
23   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
24   * @version $Revision: 1.3 $
25   */
26  public class AttributeTest extends AbstractTestCase {
27      protected static String[] paths = {"attribute::*",
28              "/root/author/attribute::*", "//attribute::*", "@name"};
29  
30      public static void main(String[] args) {
31          TestRunner.run(AttributeTest.class);
32      }
33  
34      // Test case(s)
35      // -------------------------------------------------------------------------
36      public void testXPaths() throws Exception {
37          int size = paths.length;
38  
39          for (int i = 0; i < size; i++) {
40              testXPath(paths[i]);
41          }
42      }
43  
44      // Implementation methods
45      // -------------------------------------------------------------------------
46      protected void testXPath(String xpathText) {
47          XPath xpath = DocumentHelper.createXPath(xpathText);
48          List list = xpath.selectNodes(document);
49  
50          log("Searched path: " + xpathText + " found: " + list.size()
51                  + " result(s)");
52  
53          for (Iterator iter = list.iterator(); iter.hasNext();) {
54              Object object = iter.next();
55  
56              log("Found Result: " + object);
57  
58              assertTrue("Results should be Attribute objects",
59                      object instanceof Attribute);
60  
61              Attribute attribute = (Attribute) object;
62  
63              assertTrue("Results should support the parent relationship",
64                      attribute.supportsParent());
65              assertTrue(
66                      "Results should contain reference to the parent element",
67                      attribute.getParent() != null);
68              assertTrue("Resulting document not correct", attribute
69                      .getDocument() != null);
70          }
71      }
72  }
73  
74  /*
75   * Redistribution and use of this software and associated documentation
76   * ("Software"), with or without modification, are permitted provided that the
77   * following conditions are met:
78   * 
79   * 1. Redistributions of source code must retain copyright statements and
80   * notices. Redistributions must also contain a copy of this document.
81   * 
82   * 2. Redistributions in binary form must reproduce the above copyright notice,
83   * this list of conditions and the following disclaimer in the documentation
84   * and/or other materials provided with the distribution.
85   * 
86   * 3. The name "DOM4J" must not be used to endorse or promote products derived
87   * from this Software without prior written permission of MetaStuff, Ltd. For
88   * written permission, please contact dom4j-info@metastuff.com.
89   * 
90   * 4. Products derived from this Software may not be called "DOM4J" nor may
91   * "DOM4J" appear in their names without prior written permission of MetaStuff,
92   * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
93   * 
94   * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
95   * 
96   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
97   * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
98   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
99   * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
100  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
101  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
102  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
103  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
104  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
105  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
106  * POSSIBILITY OF SUCH DAMAGE.
107  * 
108  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
109  */