KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > error > test > ErrorListTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.error.test;
8
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.List JavaDoc;
12
13 import junit.framework.TestCase;
14
15 import com.inversoft.error.BasicError;
16 import com.inversoft.error.ErrorList;
17 import com.inversoft.error.PropertyError;
18
19
20 /**
21  * <p>
22  * Tests the ErrorList class.
23  * </p>
24  *
25  * @author Brian Pontarelli
26  * @since 2.0
27  * @version 2.0
28  */

29 public class ErrorListTest extends TestCase {
30
31     /**
32      * Constructs a new <code>ErrorListTest</code>
33      *
34      * @param name The name of the test case currently being run
35      */

36     public ErrorListTest(String JavaDoc name) {
37         super(name);
38     }
39
40
41     /**
42      * Test that the error list gets populated correctly
43      */

44     public void testList() {
45         BasicError be1 = new BasicError("error1");
46         BasicError be2 = new BasicError("error2");
47         BasicError be3 = new BasicError("error3");
48         BasicError be4 = new BasicError("error4");
49         PropertyError pe1 = new PropertyError("prop1", "message1");
50         PropertyError pe2 = new PropertyError("prop2", "message2");
51         PropertyError pe3 = new PropertyError("prop3", "message3");
52
53         ErrorList list = new ErrorList();
54         assertTrue("List should be empty", list.isEmpty());
55         assertTrue("List should have no basic errors", !list.hasBasicErrors());
56         assertTrue("List should have no property errors", !list.hasPropertyErrors());
57
58         list.addError(be1);
59         assertTrue("List should have basic errors", list.hasBasicErrors());
60         assertTrue("List should have no property errors", !list.hasPropertyErrors());
61         list.addError(be2);
62         list.addError(be3);
63         list.addError(be4);
64         list.addError(pe1);
65         assertTrue("List should have basic errors", list.hasBasicErrors());
66         assertTrue("List should have property errors", list.hasPropertyErrors());
67         list.addError(pe2);
68         list.addError(pe3);
69
70         assertTrue("List should not be empty", !list.isEmpty());
71         assertEquals("List should be size 7", list.getAllErrors().size(), 7);
72         assertEquals("Basic list should be size 4", list.getBasicErrors().size(), 4);
73         assertEquals("Property list should be size 3", list.getPropertyErrors().size(), 3);
74         assertEquals("Should be message1", list.getFirstPropertyError("prop1").getMessage(), "message1");
75         assertEquals("Should be message2", list.getFirstPropertyError("prop2").getMessage(), "message2");
76         assertEquals("Should be message3", list.getFirstPropertyError("prop3").getMessage(), "message3");
77     }
78
79     public void testListLoad() {
80         List JavaDoc list = new ArrayList JavaDoc();
81         list.add("message1");
82         list.add(new BasicError("message2"));
83         list.add(new PropertyError("property1", "message3"));
84
85         ErrorList el = new ErrorList(list);
86         assertEquals(2, el.getAllErrors().size());
87         assertEquals(1, el.getBasicErrors().size());
88         assertEquals("message2", el.getError(0).getMessage());
89         assertEquals("message3", el.getError(1).getMessage());
90         assertTrue(el.getError(1) instanceof PropertyError);
91         assertEquals("message3", el.getFirstPropertyError("property1").getMessage());
92         assertEquals("property1", el.getFirstPropertyError("property1").getProperty());
93
94         el.addError("property1", "message4");
95         assertEquals(2, el.getPropertyErrors("property1").size());
96         PropertyError pe = (PropertyError) el.getPropertyErrors("property1").get(1);
97         assertEquals("message4", pe.getMessage());
98         assertEquals("property1", pe.getProperty());
99     }
100
101     public void testErrorListLoad() {
102         ErrorList list = new ErrorList();
103         list.addError("message1");
104         list.addError(new BasicError("message2"));
105         list.addError(new PropertyError("property1", "message3"));
106
107         ErrorList el = new ErrorList(list);
108         assertEquals(3, el.getAllErrors().size());
109         assertEquals(2, el.getBasicErrors().size());
110         assertEquals("message1", el.getError(0).getMessage());
111         assertEquals("message2", el.getError(1).getMessage());
112         assertEquals("message3", el.getError(2).getMessage());
113         assertTrue(el.getError(2) instanceof PropertyError);
114         assertEquals("message3", el.getFirstPropertyError("property1").getMessage());
115         assertEquals("property1", el.getFirstPropertyError("property1").getProperty());
116
117         el.addError("property1", "message4");
118         assertEquals(2, el.getPropertyErrors("property1").size());
119         PropertyError pe = (PropertyError) el.getPropertyErrors("property1").get(1);
120         assertEquals("message4", pe.getMessage());
121         assertEquals("property1", pe.getProperty());
122     }
123
124     public void testStringMethods() {
125         ErrorList el = new ErrorList();
126         el.addError("test");
127         el.addError("prop", "test2");
128
129         assertTrue(el.hasPropertyErrors("prop"));
130         assertTrue(el.hasPropertyErrors());
131         assertTrue(el.hasBasicErrors());
132         assertEquals(2, el.getAllErrors().size());
133         assertEquals("test", el.getBasicErrors().get(0).toString());
134         assertEquals("prop", el.getFirstPropertyError("prop").getProperty());
135         assertEquals("test2", el.getFirstPropertyError("prop").getMessage());
136
137         List JavaDoc props = el.removeErrors("prop");
138         assertEquals(1, props.size());
139         assertEquals(1, el.getAllErrors().size());
140         assertEquals(0, el.getPropertyErrors("prop").size());
141     }
142
143     /**
144      * Tests adding an error list to another error list
145      */

146     public void testErrorList() {
147         ErrorList el = new ErrorList();
148         ErrorList el2 = new ErrorList();
149
150         el2.addError("foo");
151         el2.addError("bar");
152
153         el.addErrorList(el2);
154
155         assertEquals(2, el.getAllErrors().size());
156         assertEquals("foo", el.getBasicErrors().get(0).toString());
157         assertEquals("bar", el.getBasicErrors().get(1).toString());
158         assertEquals("foo", el.getError(0).getMessage());
159         assertEquals("bar", el.getError(1).getMessage());
160         assertTrue(el.hasBasicErrors());
161         assertFalse(el.hasPropertyErrors());
162
163         // Adding empty and null should not effect the original list
164
el.addErrorList(null);
165         el.addErrorList(new ErrorList());
166         assertEquals(2, el.getAllErrors().size());
167         assertEquals("foo", el.getBasicErrors().get(0).toString());
168         assertEquals("bar", el.getBasicErrors().get(1).toString());
169         assertEquals("foo", el.getError(0).getMessage());
170         assertEquals("bar", el.getError(1).getMessage());
171         assertTrue(el.hasBasicErrors());
172         assertFalse(el.hasPropertyErrors());
173
174         // Removals should not effect new list
175
BasicError error = el2.removeError(0);
176         assertEquals("foo", error.getMessage());
177         assertEquals(1, el2.getAllErrors().size());
178         assertEquals("bar", el2.getError(0).getMessage());
179
180         assertEquals(2, el.getAllErrors().size());
181         assertEquals("foo", el.getBasicErrors().get(0).toString());
182         assertEquals("bar", el.getBasicErrors().get(1).toString());
183         assertEquals("foo", el.getError(0).getMessage());
184         assertEquals("bar", el.getError(1).getMessage());
185
186         el2.removeAllErrors();
187         assertTrue(el2.isEmpty());
188         assertFalse(el2.hasBasicErrors());
189         assertEquals(0, el2.getAllErrors().size());
190     }
191
192     public void testMultipleProperties() {
193         ErrorList el = new ErrorList();
194         el.addError("prop1", "foo");
195         el.addError("prop1", "bar");
196         el.addError("prop2", "jake");
197         el.addError("prop2", "jill");
198
199         assertTrue(el.hasPropertyErrors("prop1"));
200         assertTrue(el.hasPropertyErrors("prop2"));
201         assertTrue(el.hasPropertyErrors());
202         assertEquals(2, el.getPropertyErrors("prop1").size());
203         assertEquals(2, el.getPropertyErrors("prop2").size());
204
205         el.removeErrors("prop1");
206         assertFalse(el.hasPropertyErrors("prop1"));
207         assertTrue(el.hasPropertyErrors("prop2"));
208         assertTrue(el.hasPropertyErrors());
209         assertEquals(0, el.getPropertyErrors("prop1").size());
210         assertEquals(2, el.getPropertyErrors("prop2").size());
211
212         el.removeErrors("prop2");
213         assertFalse(el.hasPropertyErrors("prop1"));
214         assertFalse(el.hasPropertyErrors("prop2"));
215         assertFalse(el.hasPropertyErrors());
216         assertEquals(0, el.getPropertyErrors("prop1").size());
217         assertEquals(0, el.getPropertyErrors("prop2").size());
218     }
219
220     public void testOutOfBounds() {
221         ErrorList el = new ErrorList();
222         el.addError("prop1", "foo");
223
224         try {
225             el.getError(-1);
226             fail("Should have failed");
227         } catch (IndexOutOfBoundsException JavaDoc ioobe) {
228             // expected
229
}
230
231         try {
232             el.getError(1);
233             fail("Should have failed");
234         } catch (IndexOutOfBoundsException JavaDoc ioobe) {
235             // expected
236
}
237
238         try {
239             el.removeError(-1);
240             fail("Should have failed");
241         } catch (IndexOutOfBoundsException JavaDoc ioobe) {
242             // expected
243
}
244
245         try {
246             el.removeError(1);
247             fail("Should have failed");
248         } catch (IndexOutOfBoundsException JavaDoc ioobe) {
249             // expected
250
}
251     }
252
253     /**
254      * Test failure cases when there are no property errors under a name
255      */

256     public void testNoProperty() {
257         ErrorList el = new ErrorList();
258         el.addError("prop1", "foo");
259
260         PropertyError pe = el.getFirstPropertyError("noProp");
261         assertNull(pe);
262
263         List JavaDoc list = el.removeErrors("noProp");
264         assertNull(list);
265
266         el = new ErrorList();
267         el.addError("message");
268
269         list = el.getPropertyErrors();
270         assertEquals(0, list.size());
271     }
272
273     /**
274      * Tests that the clear method works correctly
275      */

276     public void testClear() {
277         ErrorList el = new ErrorList();
278         el.addError("prop1", "foo");
279         el.addError("message");
280
281         el.clear();
282
283         List JavaDoc list = el.getAllErrors();
284         assertEquals(0, list.size());
285
286         list = el.getPropertyErrors("prop1");
287         assertEquals(0, list.size());
288     }
289
290     /**
291      * Tests assertions
292      */

293     public void testAssert() {
294         ErrorList el = new ErrorList();
295         el.addError("prop1", "foo");
296
297         try {
298             el.removeErrors(null);
299             fail("Should have asserted");
300         } catch (AssertionError JavaDoc ae) {
301             // expected
302
}
303     }
304 }
Popular Tags