001: /* Copyright 2003-2005 Elliotte Rusty Harold
002:
003: This library is free software; you can redistribute it and/or modify
004: it under the terms of version 2.1 of the GNU Lesser General Public
005: License as published by the Free Software Foundation.
006:
007: This library is distributed in the hope that it will be useful,
008: but WITHOUT ANY WARRANTY; without even the implied warranty of
009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: GNU Lesser General Public License for more details.
011:
012: You should have received a copy of the GNU Lesser General Public
013: License along with this library; if not, write to the
014: Free Software Foundation, Inc., 59 Temple Place, Suite 330,
015: Boston, MA 02111-1307 USA
016:
017: You can contact Elliotte Rusty Harold by sending e-mail to
018: elharo@metalab.unc.edu. Please include the word "XOM" in the
019: subject line. The XOM home page is located at http://www.xom.nu/
020: */
021:
022: package nu.xom.tests;
023:
024: import nu.xom.Comment;
025: import nu.xom.Element;
026: import nu.xom.Node;
027: import nu.xom.Nodes;
028: import nu.xom.Text;
029:
030: /**
031: * <p>
032: * Various basic tests for the <code>Nodes</code> class.
033: * </p>
034: *
035: * @author Elliotte Rusty Harold
036: * @version 1.1b2
037: *
038: */
039: public class NodesTest extends XOMTestCase {
040:
041: public NodesTest(String name) {
042: super (name);
043: }
044:
045: public void testNoArgsConstructor() {
046: Nodes nodes = new Nodes();
047: assertEquals(0, nodes.size());
048: }
049:
050: public void testOneArgConstructor() {
051: Element test = new Element("test");
052: Nodes nodes = new Nodes(test);
053: assertEquals(1, nodes.size());
054: Element stored = (Element) nodes.get(0);
055: assertEquals(test, stored);
056: }
057:
058: public void testConstructWithNull() {
059:
060: try {
061: new Nodes(null);
062: fail("constructed with null");
063: } catch (NullPointerException success) {
064: assertNotNull(success.getMessage());
065: }
066:
067: }
068:
069: public void testAppendNull() {
070:
071: Nodes nodes = new Nodes();
072: try {
073: nodes.append(null);
074: fail("appended null");
075: } catch (NullPointerException success) {
076: assertNotNull(success.getMessage());
077: }
078:
079: }
080:
081: public void testInsertNull() {
082:
083: Nodes nodes = new Nodes();
084: try {
085: nodes.insert(null, 0);
086: fail("inserted null");
087: } catch (NullPointerException success) {
088: assertNotNull(success.getMessage());
089: }
090:
091: }
092:
093: public void testIndexOutofBoundsException() {
094: Nodes nodes = new Nodes();
095: try {
096: nodes.get(0);
097: fail("Didn't throw IndexOutOfBoundsException for empty list");
098: } catch (IndexOutOfBoundsException success) {
099: assertNotNull(success.getMessage());
100: }
101:
102: nodes.append(new Comment("data"));
103: try {
104: nodes.get(-1);
105: fail("Didn't throw IndexOutOfBoundsException for -1");
106: } catch (IndexOutOfBoundsException success) {
107: assertNotNull(success.getMessage());
108: }
109: try {
110: nodes.get(1);
111: fail("Didn't throw IndexOutOfBoundsException for fence post");
112: } catch (IndexOutOfBoundsException success) {
113: assertNotNull(success.getMessage());
114: }
115:
116: }
117:
118: public void testAppendAndGet() {
119:
120: Nodes nodes = new Nodes();
121: int length = 10;
122: for (int i = 0; i < length; i++) {
123: nodes.append(new Text(String.valueOf(i)));
124: }
125: assertEquals(length, nodes.size());
126: for (int i = 0; i < length; i++) {
127: assertEquals(String.valueOf(i), nodes.get(i).getValue());
128: }
129:
130: }
131:
132: public void testInsertAtEnd() {
133:
134: Nodes nodes = new Nodes();
135: nodes.insert(new Text("test"), 0);
136: assertEquals("test", nodes.get(0).getValue());
137: nodes.insert(new Text("test2"), 1);
138: assertEquals("test2", nodes.get(1).getValue());
139:
140: }
141:
142: public void testInsert() {
143:
144: Nodes nodes = new Nodes();
145: int length = 10;
146: for (int i = 0; i < length; i++) {
147: nodes.append(new Text(String.valueOf(i)));
148: }
149: nodes.insert(new Comment("dTA"), 3);
150: nodes.insert(new Comment("dTA"), 5);
151: nodes.insert(new Comment("dTA"), 12);
152: assertEquals(length + 3, nodes.size());
153: for (int i = 0; i < 3; i++) {
154: assertEquals(String.valueOf(i), nodes.get(i).getValue());
155: }
156: assertEquals("dTA", nodes.get(3).getValue());
157: assertEquals("dTA", nodes.get(5).getValue());
158: assertEquals("dTA", nodes.get(12).getValue());
159: for (int i = 6; i < length + 2; i++) {
160: assertEquals(String.valueOf(i - 2), nodes.get(i).getValue());
161: }
162:
163: try {
164: nodes.insert(new Text("data"), 14);
165: } catch (IndexOutOfBoundsException ex) {
166: assertNotNull(ex.getMessage());
167: }
168:
169: try {
170: nodes.insert(new Text("data"), 140);
171: } catch (IndexOutOfBoundsException ex) {
172: assertNotNull(ex.getMessage());
173: }
174:
175: try {
176: nodes.insert(new Text("data"), -14);
177: } catch (IndexOutOfBoundsException ex) {
178: assertNotNull(ex.getMessage());
179: }
180:
181: }
182:
183: public void testDelete() {
184:
185: Nodes nodes = new Nodes();
186: int length = 10;
187: for (int i = 0; i < length; i++) {
188: nodes.append(new Text(String.valueOf(i)));
189: }
190:
191: Node result = nodes.remove(0);
192: assertEquals(length - 1, nodes.size());
193: assertEquals("0", result.getValue());
194:
195: for (int i = 0; i < nodes.size(); i++) {
196: assertEquals(String.valueOf(i + 1), nodes.get(i).getValue());
197: }
198: nodes.remove(nodes.size() - 1);
199: assertEquals(length - 2, nodes.size());
200: for (int i = 0; i < nodes.size(); i++) {
201: assertEquals(String.valueOf(i + 1), nodes.get(i).getValue());
202: }
203: nodes.remove(2);
204: for (int i = 0; i < 2; i++) {
205: assertEquals(String.valueOf(i + 1), nodes.get(i).getValue());
206: }
207: for (int i = 2; i < nodes.size(); i++) {
208: assertEquals(String.valueOf(i + 2), nodes.get(i).getValue());
209: }
210: assertEquals(length - 3, nodes.size());
211:
212: try {
213: nodes.remove(14);
214: } catch (IndexOutOfBoundsException ex) {
215: assertNotNull(ex.getMessage());
216: }
217:
218: try {
219: nodes.remove(nodes.size());
220: } catch (IndexOutOfBoundsException ex) {
221: assertNotNull(ex.getMessage());
222: }
223:
224: try {
225: nodes.remove(-14);
226: } catch (IndexOutOfBoundsException ex) {
227: assertNotNull(ex.getMessage());
228: }
229:
230: }
231:
232: }
|