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