1
/* Copyright 2003-2005 Elliotte Rusty Harold
3
This library is free software; you can redistribute it and/or modify
4
it under the terms of version 2.1 of the GNU Lesser General Public
5
License as published by the Free Software Foundation.
7
This library is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU Lesser General Public License for more details.
12
You should have received a copy of the GNU Lesser General Public
13
License along with this library; if not, write to the
14
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15
Boston, MA 02111-1307 USA
17
You can contact Elliotte Rusty Harold by sending e-mail to
18
elharo@metalab.unc.edu. Please include the word "XOM" in the
19
subject line. The XOM home page is located at http://www.xom.nu/
24
import nu.xom.Comment;
25
import nu.xom.Element;
32
* Various basic tests for the <code>Nodes</code> class.
35
* @author Elliotte Rusty Harold
39
public class NodesTest extends XOMTestCase {
42
public NodesTest(String name) {
47
public void testNoArgsConstructor() {
48
Nodes nodes = new Nodes();
49
assertEquals(0, nodes.size());
53
public void testOneArgConstructor() {
54
Element test = new Element("test");
55
Nodes nodes = new Nodes(test);
56
assertEquals(1, nodes.size());
57
Element stored = (Element) nodes.get(0);
58
assertEquals(test, stored);
62
public void testConstructWithNull() {
66
fail("constructed with null");
68
catch (NullPointerException success) {
69
assertNotNull(success.getMessage());
75
public void testAppendNull() {
77
Nodes nodes = new Nodes();
80
fail("appended null");
82
catch (NullPointerException success) {
83
assertNotNull(success.getMessage());
89
public void testInsertNull() {
91
Nodes nodes = new Nodes();
93
nodes.insert(null, 0);
94
fail("inserted null");
96
catch (NullPointerException success) {
97
assertNotNull(success.getMessage());
103
public void testIndexOutofBoundsException() {
104
Nodes nodes = new Nodes();
107
fail("Didn't throw IndexOutOfBoundsException for empty list");
109
catch (IndexOutOfBoundsException success) {
110
assertNotNull(success.getMessage());
113
nodes.append(new Comment("data"));
116
fail("Didn't throw IndexOutOfBoundsException for -1");
118
catch (IndexOutOfBoundsException success) {
119
assertNotNull(success.getMessage());
123
fail("Didn't throw IndexOutOfBoundsException for fence post");
125
catch (IndexOutOfBoundsException success) {
126
assertNotNull(success.getMessage());
132
public void testAppendAndGet() {
134
Nodes nodes = new Nodes();
136
for (int i = 0; i < length; i++) {
137
nodes.append(new Text(String.valueOf(i)));
139
assertEquals(length, nodes.size());
140
for (int i = 0; i < length; i++) {
141
assertEquals(String.valueOf(i), nodes.get(i).getValue());
147
public void testInsertAtEnd() {
149
Nodes nodes = new Nodes();
150
nodes.insert(new Text("test"), 0);
151
assertEquals("test", nodes.get(0).getValue());
152
nodes.insert(new Text("test2"), 1);
153
assertEquals("test2", nodes.get(1).getValue());
158
public void testInsert() {
160
Nodes nodes = new Nodes();
162
for (int i = 0; i < length; i++) {
163
nodes.append(new Text(String.valueOf(i)));
165
nodes.insert(new Comment("dTA"), 3);
166
nodes.insert(new Comment("dTA"), 5);
167
nodes.insert(new Comment("dTA"), 12);
168
assertEquals(length+3, nodes.size());
169
for (int i = 0; i < 3; i++) {
170
assertEquals(String.valueOf(i), nodes.get(i).getValue());
172
assertEquals("dTA", nodes.get(3).getValue());
173
assertEquals("dTA", nodes.get(5).getValue());
174
assertEquals("dTA", nodes.get(12).getValue());
175
for (int i = 6; i < length+2; i++) {
176
assertEquals(String.valueOf(i-2), nodes.get(i).getValue());
180
nodes.insert(new Text("data"), 14);
182
catch (IndexOutOfBoundsException ex) {
183
assertNotNull(ex.getMessage());
187
nodes.insert(new Text("data"), 140);
189
catch (IndexOutOfBoundsException ex) {
190
assertNotNull(ex.getMessage());
194
nodes.insert(new Text("data"), -14);
196
catch (IndexOutOfBoundsException ex) {
197
assertNotNull(ex.getMessage());
203
public void testDelete() {
205
Nodes nodes = new Nodes();
207
for (int i = 0; i < length; i++) {
208
nodes.append(new Text(String.valueOf(i)));
211
Node result = nodes.remove(0);
212
assertEquals(length-1, nodes.size());
213
assertEquals("0", result.getValue());
215
for (int i = 0; i < nodes.size(); i++) {
216
assertEquals(String.valueOf(i+1), nodes.get(i).getValue());
218
nodes.remove(nodes.size()-1);
219
assertEquals(length-2, nodes.size());
220
for (int i = 0; i < nodes.size(); i++) {
221
assertEquals(String.valueOf(i+1), nodes.get(i).getValue());
224
for (int i = 0; i < 2; i++) {
225
assertEquals(String.valueOf(i+1), nodes.get(i).getValue());
227
for (int i = 2; i < nodes.size(); i++) {
228
assertEquals(String.valueOf(i+2), nodes.get(i).getValue());
230
assertEquals(length-3, nodes.size());
235
catch (IndexOutOfBoundsException ex) {
236
assertNotNull(ex.getMessage());
240
nodes.remove(nodes.size());
242
catch (IndexOutOfBoundsException ex) {
243
assertNotNull(ex.getMessage());
249
catch (IndexOutOfBoundsException ex) {
250
assertNotNull(ex.getMessage());