~ubuntu-branches/ubuntu/precise/xom/precise

« back to all changes in this revision

Viewing changes to src/nu/xom/tests/NodesTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-11-25 15:50:40 UTC
  • Revision ID: james.westby@ubuntu.com-20071125155040-r75ikcqf1vu0cei7
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2003-2005 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.1b2
 
37
 *
 
38
 */
 
39
public class NodesTest extends XOMTestCase {
 
40
 
 
41
    
 
42
    public NodesTest(String 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 testConstructWithNull() {
 
63
        
 
64
        try {
 
65
            new Nodes(null);
 
66
            fail("constructed with null");
 
67
        }
 
68
        catch (NullPointerException success) {
 
69
            assertNotNull(success.getMessage());
 
70
        } 
 
71
        
 
72
    }
 
73
    
 
74
    
 
75
    public void testAppendNull() {
 
76
        
 
77
        Nodes nodes = new Nodes();
 
78
        try {
 
79
            nodes.append(null);
 
80
            fail("appended null");
 
81
        }
 
82
        catch (NullPointerException success) {
 
83
            assertNotNull(success.getMessage());
 
84
        } 
 
85
        
 
86
    }
 
87
    
 
88
    
 
89
    public void testInsertNull() {
 
90
        
 
91
        Nodes nodes = new Nodes();
 
92
        try {
 
93
            nodes.insert(null, 0);
 
94
            fail("inserted null");
 
95
        }
 
96
        catch (NullPointerException success) {
 
97
            assertNotNull(success.getMessage());
 
98
        } 
 
99
        
 
100
    }
 
101
    
 
102
    
 
103
    public void testIndexOutofBoundsException() {
 
104
        Nodes nodes = new Nodes();
 
105
        try {
 
106
            nodes.get(0);
 
107
            fail("Didn't throw IndexOutOfBoundsException for empty list");
 
108
        }
 
109
        catch (IndexOutOfBoundsException success) {
 
110
            assertNotNull(success.getMessage());   
 
111
        }  
 
112
        
 
113
        nodes.append(new Comment("data"));
 
114
        try {
 
115
            nodes.get(-1);
 
116
            fail("Didn't throw IndexOutOfBoundsException for -1");
 
117
        }
 
118
        catch (IndexOutOfBoundsException success) {
 
119
            assertNotNull(success.getMessage());   
 
120
        }  
 
121
        try {
 
122
            nodes.get(1);
 
123
            fail("Didn't throw IndexOutOfBoundsException for fence post");
 
124
        }
 
125
        catch (IndexOutOfBoundsException success) {
 
126
            assertNotNull(success.getMessage());   
 
127
        }   
 
128
        
 
129
    }
 
130
 
 
131
    
 
132
    public void testAppendAndGet() {
 
133
        
 
134
        Nodes nodes = new Nodes();
 
135
        int length = 10;
 
136
        for (int i = 0; i < length; i++) {
 
137
            nodes.append(new Text(String.valueOf(i)));   
 
138
        }
 
139
        assertEquals(length, nodes.size());
 
140
        for (int i = 0; i < length; i++) {
 
141
            assertEquals(String.valueOf(i), nodes.get(i).getValue());   
 
142
        }     
 
143
        
 
144
    }
 
145
    
 
146
    
 
147
    public void testInsertAtEnd() {
 
148
        
 
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());
 
154
        
 
155
    }
 
156
 
 
157
    
 
158
    public void testInsert() {
 
159
        
 
160
        Nodes nodes = new Nodes();
 
161
        int length = 10;
 
162
        for (int i = 0; i < length; i++) {
 
163
            nodes.append(new Text(String.valueOf(i)));   
 
164
        }
 
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());   
 
171
        }     
 
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());   
 
177
        } 
 
178
        
 
179
        try {
 
180
            nodes.insert(new Text("data"), 14);   
 
181
        }
 
182
        catch (IndexOutOfBoundsException ex) {
 
183
            assertNotNull(ex.getMessage());
 
184
        }
 
185
                 
 
186
        try {
 
187
            nodes.insert(new Text("data"), 140);   
 
188
        }
 
189
        catch (IndexOutOfBoundsException ex) {
 
190
            assertNotNull(ex.getMessage());
 
191
        }
 
192
                 
 
193
        try {
 
194
            nodes.insert(new Text("data"), -14);   
 
195
        }
 
196
        catch (IndexOutOfBoundsException ex) {
 
197
            assertNotNull(ex.getMessage());
 
198
        }
 
199
                 
 
200
    }
 
201
    
 
202
    
 
203
    public void testDelete() {
 
204
        
 
205
        Nodes nodes = new Nodes();
 
206
        int length = 10;
 
207
        for (int i = 0; i < length; i++) {
 
208
            nodes.append(new Text(String.valueOf(i)));   
 
209
        }     
 
210
        
 
211
        Node result = nodes.remove(0);
 
212
        assertEquals(length-1, nodes.size());
 
213
        assertEquals("0", result.getValue());
 
214
        
 
215
        for (int i = 0; i < nodes.size(); i++) {
 
216
            assertEquals(String.valueOf(i+1), nodes.get(i).getValue());   
 
217
        }  
 
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());   
 
222
        }
 
223
        nodes.remove(2); 
 
224
        for (int i = 0; i < 2; i++) {
 
225
            assertEquals(String.valueOf(i+1), nodes.get(i).getValue());   
 
226
        }        
 
227
        for (int i = 2; i < nodes.size(); i++) {
 
228
            assertEquals(String.valueOf(i+2), nodes.get(i).getValue());   
 
229
        }
 
230
        assertEquals(length-3, nodes.size());        
 
231
        
 
232
        try {
 
233
            nodes.remove(14);   
 
234
        }
 
235
        catch (IndexOutOfBoundsException ex) {
 
236
            assertNotNull(ex.getMessage());
 
237
        }
 
238
                 
 
239
        try {
 
240
            nodes.remove(nodes.size());   
 
241
        }
 
242
        catch (IndexOutOfBoundsException ex) {
 
243
            assertNotNull(ex.getMessage());
 
244
        }
 
245
                 
 
246
        try {
 
247
            nodes.remove(-14);   
 
248
        }
 
249
        catch (IndexOutOfBoundsException ex) {
 
250
            assertNotNull(ex.getMessage());
 
251
        }
 
252
          
 
253
    }
 
254
    
 
255
    
 
256
}