KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestSetText


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  */

8
9 package test.dom4j;
10
11 import junit.framework.Test;
12 import junit.framework.TestSuite;
13 import junit.textui.TestRunner;
14 import org.dom4j.Element;
15 import org.dom4j.Node;
16
17 /** Tests the setText method
18   *
19   * @author <a HREF="mailto:maartenc@users.sourceforge.net">Maarten Coene</a>
20   */

21 public class TestSetText extends AbstractTestCase {
22
23     public static void main( String JavaDoc[] args ) {
24         TestRunner.run( suite() );
25     }
26
27     public static Test suite() {
28         return new TestSuite( TestSetText.class );
29     }
30
31     public TestSetText(String JavaDoc name) {
32         super(name);
33     }
34
35     // Test case(s)
36
//-------------------------------------------------------------------------
37

38     /*
39      * The structure of the test document is:
40      * [root]
41      * [author name="James" location="UK"]
42      * James Strachem
43      * [url]http://sourceforge.net/users/jstrachan/[/url]
44      * [/author]
45      * [author name="Bob" location="Canada"]
46      * Bob McWhirter
47      * [url]http://sourceforge.net/users/werken/[/url]
48      * [/author]
49      * [/root]
50      */

51     public void testSetText1() throws Exception JavaDoc {
52         String JavaDoc newURL = "newURL";
53
54         Node urlNode = document.selectSingleNode("//root/author[1]/url");
55         urlNode.setText(newURL);
56
57         assertEquals(newURL, urlNode.getText());
58         assertTrue(urlNode instanceof Element);
59
60         Element urlElement = (Element) urlNode;
61         assertEquals(0, urlElement.elements().size());
62     }
63
64     public void testSetText2() throws Exception JavaDoc {
65         String JavaDoc newName = "Strachem James";
66
67         Node authorNode = document.selectSingleNode("//root/author[1]");
68         authorNode.setText(newName);
69
70         assertEquals(newName, authorNode.getText());
71         assertTrue(authorNode instanceof Element);
72
73         Element urlElement = (Element) authorNode;
74         assertEquals(1, urlElement.elements().size());
75     }
76
77
78 }
79
80
81
82
83 /*
84  * Redistribution and use of this software and associated documentation
85  * ("Software"), with or without modification, are permitted provided
86  * that the following conditions are met:
87  *
88  * 1. Redistributions of source code must retain copyright
89  * statements and notices. Redistributions must also contain a
90  * copy of this document.
91  *
92  * 2. Redistributions in binary form must reproduce the
93  * above copyright notice, this list of conditions and the
94  * following disclaimer in the documentation and/or other
95  * materials provided with the distribution.
96  *
97  * 3. The name "DOM4J" must not be used to endorse or promote
98  * products derived from this Software without prior written
99  * permission of MetaStuff, Ltd. For written permission,
100  * please contact dom4j-info@metastuff.com.
101  *
102  * 4. Products derived from this Software may not be called "DOM4J"
103  * nor may "DOM4J" appear in their names without prior written
104  * permission of MetaStuff, Ltd. DOM4J is a registered
105  * trademark of MetaStuff, Ltd.
106  *
107  * 5. Due credit should be given to the DOM4J Project
108  * (http://dom4j.org/).
109  *
110  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
111  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
112  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
113  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
114  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
115  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
116  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
117  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
118  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
119  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
120  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
121  * OF THE POSSIBILITY OF SUCH DAMAGE.
122  *
123  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
124  *
125  * $Id: TestSetText.java,v 1.1 2003/07/07 10:30:29 per_nyfelt Exp $
126  */

127
Popular Tags