KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > tests > NodesTest


1 /* Copyright 2003, 2004 Elliotte Rusty Harold
2    
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.
6    
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.
11    
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
16    
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/
20 */

21
22 package nu.xom.tests;
23
24 import nu.xom.Comment;
25 import nu.xom.Element;
26 import nu.xom.Node;
27 import nu.xom.Nodes;
28 import nu.xom.Text;
29
30 /**
31  * <p>
32  * Various basic tests for the <code>Nodes</code> class.
33  * </p>
34  *
35  * @author Elliotte Rusty Harold
36  * @version 1.0
37  *
38  */

39 public class NodesTest extends XOMTestCase {
40
41     
42     public NodesTest(String JavaDoc name) {
43         super(name);
44     }
45     
46     
47     public void testNoArgsConstructor() {
48         Nodes nodes = new Nodes();
49         assertEquals(0, nodes.size());
50     }
51
52     
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);
59     }
60     
61     
62     public void testIndexOutofBoundsException() {
63         Nodes nodes = new Nodes();
64         try {
65             nodes.get(0);
66             fail("Didn't throw IndexOutOfBoundsException for empty list");
67         }
68         catch (IndexOutOfBoundsException JavaDoc success) {
69             assertNotNull(success.getMessage());
70         }
71         
72         nodes.append(new Comment("data"));
73         try {
74             nodes.get(-1);
75             fail("Didn't throw IndexOutOfBoundsException for -1");
76         }
77         catch (IndexOutOfBoundsException JavaDoc success) {
78             assertNotNull(success.getMessage());
79         }
80         try {
81             nodes.get(1);
82             fail("Didn't throw IndexOutOfBoundsException for fence post");
83         }
84         catch (IndexOutOfBoundsException JavaDoc success) {
85             assertNotNull(success.getMessage());
86         }
87         
88     }
89
90     
91     public void testAppendAndGet() {
92         
93         Nodes nodes = new Nodes();
94         int length = 10;
95         for (int i = 0; i < length; i++) {
96             nodes.append(new Text(String.valueOf(i)));
97         }
98         assertEquals(length, nodes.size());
99         for (int i = 0; i < length; i++) {
100             assertEquals(String.valueOf(i), nodes.get(i).getValue());
101         }
102         
103     }
104     
105     
106     public void testInsertAtEnd() {
107         
108         Nodes nodes = new Nodes();
109         nodes.insert(new Text("test"), 0);
110         assertEquals("test", nodes.get(0).getValue());
111         nodes.insert(new Text("test2"), 1);
112         assertEquals("test2", nodes.get(1).getValue());
113         
114     }
115
116     
117     public void testInsert() {
118         
119         Nodes nodes = new Nodes();
120         int length = 10;
121         for (int i = 0; i < length; i++) {
122             nodes.append(new Text(String.valueOf(i)));
123         }
124         nodes.insert(new Comment("dTA"), 3);
125         nodes.insert(new Comment("dTA"), 5);
126         nodes.insert(new Comment("dTA"), 12);
127         assertEquals(length+3, nodes.size());
128         for (int i = 0; i < 3; i++) {
129             assertEquals(String.valueOf(i), nodes.get(i).getValue());
130         }
131         assertEquals("dTA", nodes.get(3).getValue());
132         assertEquals("dTA", nodes.get(5).getValue());
133         assertEquals("dTA", nodes.get(12).getValue());
134         for (int i = 6; i < length+2; i++) {
135             assertEquals(String.valueOf(i-2), nodes.get(i).getValue());
136         }
137         
138         try {
139             nodes.insert(new Text("data"), 14);
140         }
141         catch (IndexOutOfBoundsException JavaDoc ex) {
142             assertNotNull(ex.getMessage());
143         }
144                  
145         try {
146             nodes.insert(new Text("data"), 140);
147         }
148         catch (IndexOutOfBoundsException JavaDoc ex) {
149             assertNotNull(ex.getMessage());
150         }
151                  
152         try {
153             nodes.insert(new Text("data"), -14);
154         }
155         catch (IndexOutOfBoundsException JavaDoc ex) {
156             assertNotNull(ex.getMessage());
157         }
158                  
159     }
160     
161     
162     public void testDelete() {
163         
164         Nodes nodes = new Nodes();
165         int length = 10;
166         for (int i = 0; i < length; i++) {
167             nodes.append(new Text(String.valueOf(i)));
168         }
169         
170         Node result = nodes.remove(0);
171         assertEquals(length-1, nodes.size());
172         assertEquals("0", result.getValue());
173         
174         for (int i = 0; i < nodes.size(); i++) {
175             assertEquals(String.valueOf(i+1), nodes.get(i).getValue());
176         }
177         nodes.remove(nodes.size()-1);
178         assertEquals(length-2, nodes.size());
179         for (int i = 0; i < nodes.size(); i++) {
180             assertEquals(String.valueOf(i+1), nodes.get(i).getValue());
181         }
182         nodes.remove(2);
183         for (int i = 0; i < 2; i++) {
184             assertEquals(String.valueOf(i+1), nodes.get(i).getValue());
185         }
186         for (int i = 2; i < nodes.size(); i++) {
187             assertEquals(String.valueOf(i+2), nodes.get(i).getValue());
188         }
189         assertEquals(length-3, nodes.size());
190         
191         try {
192             nodes.remove(14);
193         }
194         catch (IndexOutOfBoundsException JavaDoc ex) {
195             assertNotNull(ex.getMessage());
196         }
197                  
198         try {
199             nodes.remove(nodes.size());
200         }
201         catch (IndexOutOfBoundsException JavaDoc ex) {
202             assertNotNull(ex.getMessage());
203         }
204                  
205         try {
206             nodes.remove(-14);
207         }
208         catch (IndexOutOfBoundsException JavaDoc ex) {
209             assertNotNull(ex.getMessage());
210         }
211           
212     }
213     
214     
215 }
216
Popular Tags