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: TestBackedList.java,v 1.15 2004/07/02 19:12:05 maartenc Exp $
8 */
9
10 package org.dom4j;
11
12 import java.util.Collections;
13 import java.util.Comparator;
14 import java.util.List;
15
16 import junit.framework.Test;
17 import junit.framework.TestSuite;
18 import junit.textui.TestRunner;
19 import org.dom4j.Node;
20
21 import org.dom4j.io.XMLWriter;
22
23 /*** A test harness to test the backed list feature of DOM4J
24 *
25 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
26 * @version $Revision: 1.15 $
27 */
28 public class TestBackedList extends AbstractTestCase {
29
30 public static void main( String[] args ) {
31 TestRunner.run( suite() );
32 }
33
34 public static Test suite() {
35 return new TestSuite( TestBackedList.class );
36 }
37
38 public TestBackedList(String name) {
39 super(name);
40 }
41
42 // Test case(s)
43 //-------------------------------------------------------------------------
44 public void testXPaths() throws Exception {
45 Element element = (Element) document.selectSingleNode( "/root" );
46 mutate(element);
47 element = (Element) document.selectSingleNode( "//author" );
48 mutate(element);
49 }
50
51 public void testAddRemove() throws Exception {
52 Element parentElement = (Element) document.selectSingleNode( "/root" );
53 List children = parentElement.elements();
54 int lastPos = children.size() - 1;
55 Element child = (Element) children.get(lastPos);
56 try {
57 // should throw an exception cause we cannot add same child twice
58 children.add(0, child);
59 fail();
60 } catch (IllegalAddException e) {}
61 }
62
63 public void testAddWithIndex() throws Exception {
64 DocumentFactory factory = DocumentFactory.getInstance();
65
66 Element root = (Element) document.selectSingleNode( "/root" );
67 List children = root.elements(); // return a list of 2 author elements
68
69 assertEquals(2, children.size());
70
71 children.add(1, factory.createElement("dummy1"));
72 children = root.elements();
73
74 assertEquals(3, children.size());
75
76 children = root.elements("author");
77
78 assertEquals(2, children.size());
79
80 children.add(1, factory.createElement("dummy2"));
81
82 children = root.elements();
83
84 assertEquals(4, children.size());
85 assertEquals("dummy1", ((Node) children.get(1)).getName());
86 assertEquals("dummy2", ((Node) children.get(2)).getName());
87
88 /*
89 * Some tests for issue reported at
90 * http://sourceforge.net/tracker/index.php?func=detail&aid=853714&group_id=16035&atid=316035
91 */
92 children.add(children.size(), factory.createElement("dummy3"));
93 children = root.elements("author");
94 children.add(children.size(), factory.createElement("dummy4"));
95 }
96
97 // Implementation methods
98 //-------------------------------------------------------------------------
99 protected void mutate(Element element) throws Exception {
100 DocumentFactory factory = DocumentFactory.getInstance();
101
102 List list = element.elements();
103 list.add(factory.createElement("last" ));
104 list.add(0, factory.createElement("first" ));
105
106 List list2 = element.elements();
107
108 assertTrue( "Both lists should contain same number of elements", list.size() == list2.size() );
109
110 XMLWriter writer = new XMLWriter( System.out );
111
112 log( "Element content is now: " + element.content() );
113 writer.write( element );
114 }
115
116 }
117
118
119
120
121 /*
122 * Redistribution and use of this software and associated documentation
123 * ("Software"), with or without modification, are permitted provided
124 * that the following conditions are met:
125 *
126 * 1. Redistributions of source code must retain copyright
127 * statements and notices. Redistributions must also contain a
128 * copy of this document.
129 *
130 * 2. Redistributions in binary form must reproduce the
131 * above copyright notice, this list of conditions and the
132 * following disclaimer in the documentation and/or other
133 * materials provided with the distribution.
134 *
135 * 3. The name "DOM4J" must not be used to endorse or promote
136 * products derived from this Software without prior written
137 * permission of MetaStuff, Ltd. For written permission,
138 * please contact dom4j-info@metastuff.com.
139 *
140 * 4. Products derived from this Software may not be called "DOM4J"
141 * nor may "DOM4J" appear in their names without prior written
142 * permission of MetaStuff, Ltd. DOM4J is a registered
143 * trademark of MetaStuff, Ltd.
144 *
145 * 5. Due credit should be given to the DOM4J Project -
146 * http://www.dom4j.org
147 *
148 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
149 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
150 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
151 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
152 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
153 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
154 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
155 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
156 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
157 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
158 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
159 * OF THE POSSIBILITY OF SUCH DAMAGE.
160 *
161 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
162 *
163 * $Id: TestBackedList.java,v 1.15 2004/07/02 19:12:05 maartenc Exp $
164 */