KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestElementByID


1 /*
2  * Copyright 2001 (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.2 2003/07/12 12:13:06 per_nyfelt Exp $
8  */

9
10 package test.dom4j;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.dom4j.Element;
16 import org.ozoneDB.xml.dom4j.O3DocumentHelper;
17
18 import java.io.FileInputStream JavaDoc;
19
20
21 /** Tests the elementByID() method
22   *
23   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
24   * @version $Revision: 1.2 $
25   */

26 public class TestElementByID extends AbstractTestCase {
27
28     /** Input XML file to read */
29     protected static String JavaDoc INPUT_XML_FILE = "xml/test/elementByID.xml";
30
31     public static void main( String JavaDoc[] args ) {
32         TestRunner.run( suite() );
33     }
34
35     public static Test suite() {
36         return new TestSuite( TestElementByID.class );
37     }
38
39     public TestElementByID(String JavaDoc name) {
40         super(name);
41     }
42
43
44     // Test case(s)
45
//-------------------------------------------------------------------------
46
public void testElementByID() throws Exception JavaDoc {
47         String JavaDoc 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 JavaDoc {
68         //SAXReader reader = new O3SAXReader(db);
69
document = O3DocumentHelper.parse(new FileInputStream JavaDoc(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://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 (C) MetaStuff, Ltd. All Rights Reserved.
117  *
118  * $Id: TestElementByID.java,v 1.2 2003/07/12 12:13:06 per_nyfelt Exp $
119  */

120
Popular Tags