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