KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > filter > FilterListTest


1 /*
2  * Created on 2003-okt-30
3  */

4 package org.columba.mail.filter;
5
6 import junit.framework.TestCase;
7
8 import org.columba.core.filter.Filter;
9 import org.columba.core.filter.FilterList;
10 import org.columba.core.filter.IFilter;
11 import org.columba.core.xml.XmlElement;
12
13 /**
14  * Tests for the <code>FilterList</code> class.
15  *
16  * @author redsolo
17  */

18 public class FilterListTest extends TestCase {
19     /**
20      * Test to add filters to the list. The method should be able to handle
21      * nulls as well.
22      */

23     public void testAdd() {
24         FilterList filterList = new FilterList(new XmlElement());
25         filterList.add(createNamedFilter("ONE"));
26         assertEquals("Wrong number of filters in the list.", 1, filterList
27                 .count());
28         filterList.add(null);
29         assertEquals("Wrong number of filters in the list.", 1, filterList
30                 .count());
31         filterList.add(createNamedFilter("ONE"));
32         assertEquals("Wrong number of filters in the list.", 2, filterList
33                 .count());
34     }
35
36     /**
37      * Test to remove filters from the list.
38      */

39     public void testRemoveFilter() {
40         FilterList filterList = new FilterList(new XmlElement());
41         IFilter filterTwo = createNamedFilter("TWO");
42         filterList.add(createNamedFilter("ONE"));
43         filterList.add(filterTwo);
44         filterList.add(createNamedFilter("THREE"));
45         assertEquals("Wrong number of filters in the list.", 3, filterList
46                 .count());
47
48         filterList.remove(filterTwo);
49         assertEquals("Wrong number of filters in the list.", 2, filterList
50                 .count());
51         filterList.remove(null);
52         assertEquals("Wrong number of filters in the list.", 2, filterList
53                 .count());
54     }
55
56     /**
57      * Test the count() method.
58      *
59      */

60     public void testCount() {
61         FilterList filterList = new FilterList(new XmlElement());
62         assertEquals("Expected an empty filter list", 0, filterList.count());
63         filterList.add(FilterList.createDefaultFilter());
64         assertEquals("Expected a filter list with one filter", 1, filterList
65                 .count());
66         filterList.remove(0);
67         assertEquals("Expected an empty filter list", 0, filterList.count());
68     }
69
70     /**
71      * Test for Filter#get(int)
72      */

73     public void testGetint() {
74         FilterList filterList = new FilterList(new XmlElement());
75         IFilter filterOne = createNamedFilter("ONE");
76         IFilter filterTwo = createNamedFilter("TWO");
77         IFilter filterThree = createNamedFilter("THREE");
78         filterList.add(filterOne);
79         filterList.add(filterTwo);
80         filterList.add(filterThree);
81         assertEquals("The get(int) method returned the wrong filter.",
82                 filterOne, filterList.get(0));
83         assertEquals("The get(int) method returned the wrong filter.",
84                 filterTwo, filterList.get(1));
85         assertEquals("The get(int) method returned the wrong filter.",
86                 filterThree, filterList.get(2));
87     }
88
89     /**
90      * Test for insert(Filter, int) method.
91      */

92     public void testInsert() {
93         FilterList filterList = new FilterList(new XmlElement());
94         IFilter filterOne = createNamedFilter("ONE");
95         IFilter filterTwo = createNamedFilter("TWO");
96         IFilter filterThree = createNamedFilter("THREE");
97         filterList.add(filterOne);
98         filterList.add(filterTwo);
99         filterList.add(filterThree);
100         assertEquals("The get(int) method returned the wrong filter.",
101                 filterOne, filterList.get(0));
102         assertEquals("The get(int) method returned the wrong filter.",
103                 filterTwo, filterList.get(1));
104
105         IFilter filterFour = createNamedFilter("FOUR");
106         filterList.insert(filterFour, 1);
107         assertEquals("The get(int) method returned the wrong filter.",
108                 filterOne, filterList.get(0));
109         assertEquals("The get(int) method returned the wrong filter.",
110                 filterFour, filterList.get(1));
111         assertEquals("The get(int) method returned the wrong filter.",
112                 filterTwo, filterList.get(2));
113     }
114
115     /**
116      * Test to move up a filter in the list.
117      */

118     public void testMoveUp() {
119         FilterList filterList = new FilterList(new XmlElement());
120         IFilter filterOne = createNamedFilter("ONE");
121         IFilter filterTwo = createNamedFilter("TWO");
122         IFilter filterThree = createNamedFilter("THREE");
123         filterList.add(filterOne);
124         filterList.add(filterTwo);
125         filterList.add(filterThree);
126         assertEquals("The get(int) method returned the wrong filter.",
127                 filterOne, filterList.get(0));
128         assertEquals("The get(int) method returned the wrong filter.",
129                 filterThree, filterList.get(2));
130         filterList.moveUp(filterThree);
131         assertEquals("The get(int) method returned the wrong filter.",
132                 filterThree, filterList.get(1));
133         filterList.moveUp(filterThree);
134         assertEquals("The get(int) method returned the wrong filter.",
135                 filterThree, filterList.get(0));
136         assertEquals("The get(int) method returned the wrong filter.",
137                 filterOne, filterList.get(1));
138         filterList.moveUp(filterThree);
139         assertEquals("The get(int) method returned the wrong filter.",
140                 filterThree, filterList.get(0));
141     }
142
143     /**
144      * Test to move down a filter in the list.
145      */

146     public void testMoveDown() {
147         FilterList filterList = new FilterList(new XmlElement());
148         IFilter filterOne = createNamedFilter("ONE");
149         IFilter filterTwo = createNamedFilter("TWO");
150         IFilter filterThree = createNamedFilter("THREE");
151         filterList.add(filterOne);
152         filterList.add(filterTwo);
153         filterList.add(filterThree);
154         assertEquals("The get(int) method returned the wrong filter.",
155                 filterOne, filterList.get(0));
156         assertEquals("The get(int) method returned the wrong filter.",
157                 filterThree, filterList.get(2));
158         filterList.moveDown(filterOne);
159         assertEquals("The get(int) method returned the wrong filter.",
160                 filterOne, filterList.get(1));
161         filterList.moveDown(filterOne);
162         assertEquals("The get(int) method returned the wrong filter.",
163                 filterOne, filterList.get(2));
164         assertEquals("The get(int) method returned the wrong filter.",
165                 filterThree, filterList.get(1));
166         filterList.moveDown(filterOne);
167         assertEquals("The get(int) method returned the wrong filter.",
168                 filterOne, filterList.get(2));
169     }
170
171     /**
172      * Test for indexOf() method.
173      */

174     public void testIndexOf() {
175         FilterList filterList = new FilterList(new XmlElement());
176         IFilter filterOne = createNamedFilter("ONE");
177         IFilter filterTwo = createNamedFilter("TWO");
178         IFilter filterThree = createNamedFilter("THREE");
179         filterList.add(filterOne);
180         filterList.add(filterTwo);
181         filterList.add(filterThree);
182
183         assertEquals("The indexof() method did not return the right index", -1,
184                 filterList.indexOf(null));
185         assertEquals("The indexof() method did not return the right index", 0,
186                 filterList.indexOf(filterOne));
187         assertEquals("The indexof() method did not return the right index", 1,
188                 filterList.indexOf(filterTwo));
189         assertEquals("The indexof() method did not return the right index", 2,
190                 filterList.indexOf(filterThree));
191         assertEquals("The indexof() method did not return the right index", -1,
192                 filterList.indexOf(createNamedFilter("NONE")));
193     }
194
195     /**
196      * Returns an empty filter with a specified name.
197      *
198      * @param name
199      * the name of the filter.
200      * @return a <code>Filter</code> with the specified name.
201      */

202     private IFilter createNamedFilter(String JavaDoc name) {
203         IFilter filter = FilterList.createDefaultFilter();
204         filter.setName(name);
205
206         return filter;
207     }
208
209     /**
210      * Asserts that the filters are the same.
211      *
212      * @param msg
213      * the message to output if the assertion fails.
214      * @param expected
215      * the expected filter.
216      * @param actual
217      * the actual filter.
218      */

219     private void assertEquals(String JavaDoc msg, Filter expected, Filter actual) {
220         assertEquals(msg, expected.getName(), actual.getName());
221     }
222 }
Popular Tags