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