001: /*
002: * Created on 2003-okt-30
003: */
004: package org.columba.mail.filter;
005:
006: import junit.framework.TestCase;
007:
008: import org.columba.core.filter.Filter;
009: import org.columba.core.filter.FilterList;
010: import org.columba.core.filter.IFilter;
011: import org.columba.core.xml.XmlElement;
012:
013: /**
014: * Tests for the <code>FilterList</code> class.
015: *
016: * @author redsolo
017: */
018: public class FilterListTest extends TestCase {
019: /**
020: * Test to add filters to the list. The method should be able to handle
021: * nulls as well.
022: */
023: public void testAdd() {
024: FilterList filterList = new FilterList(new XmlElement());
025: filterList.add(createNamedFilter("ONE"));
026: assertEquals("Wrong number of filters in the list.", 1,
027: filterList.count());
028: filterList.add(null);
029: assertEquals("Wrong number of filters in the list.", 1,
030: filterList.count());
031: filterList.add(createNamedFilter("ONE"));
032: assertEquals("Wrong number of filters in the list.", 2,
033: filterList.count());
034: }
035:
036: /**
037: * Test to remove filters from the list.
038: */
039: public void testRemoveFilter() {
040: FilterList filterList = new FilterList(new XmlElement());
041: IFilter filterTwo = createNamedFilter("TWO");
042: filterList.add(createNamedFilter("ONE"));
043: filterList.add(filterTwo);
044: filterList.add(createNamedFilter("THREE"));
045: assertEquals("Wrong number of filters in the list.", 3,
046: filterList.count());
047:
048: filterList.remove(filterTwo);
049: assertEquals("Wrong number of filters in the list.", 2,
050: filterList.count());
051: filterList.remove(null);
052: assertEquals("Wrong number of filters in the list.", 2,
053: filterList.count());
054: }
055:
056: /**
057: * Test the count() method.
058: *
059: */
060: public void testCount() {
061: FilterList filterList = new FilterList(new XmlElement());
062: assertEquals("Expected an empty filter list", 0, filterList
063: .count());
064: filterList.add(FilterList.createDefaultFilter());
065: assertEquals("Expected a filter list with one filter", 1,
066: filterList.count());
067: filterList.remove(0);
068: assertEquals("Expected an empty filter list", 0, filterList
069: .count());
070: }
071:
072: /**
073: * Test for Filter#get(int)
074: */
075: public void testGetint() {
076: FilterList filterList = new FilterList(new XmlElement());
077: IFilter filterOne = createNamedFilter("ONE");
078: IFilter filterTwo = createNamedFilter("TWO");
079: IFilter filterThree = createNamedFilter("THREE");
080: filterList.add(filterOne);
081: filterList.add(filterTwo);
082: filterList.add(filterThree);
083: assertEquals("The get(int) method returned the wrong filter.",
084: filterOne, filterList.get(0));
085: assertEquals("The get(int) method returned the wrong filter.",
086: filterTwo, filterList.get(1));
087: assertEquals("The get(int) method returned the wrong filter.",
088: filterThree, filterList.get(2));
089: }
090:
091: /**
092: * Test for insert(Filter, int) method.
093: */
094: public void testInsert() {
095: FilterList filterList = new FilterList(new XmlElement());
096: IFilter filterOne = createNamedFilter("ONE");
097: IFilter filterTwo = createNamedFilter("TWO");
098: IFilter filterThree = createNamedFilter("THREE");
099: filterList.add(filterOne);
100: filterList.add(filterTwo);
101: filterList.add(filterThree);
102: assertEquals("The get(int) method returned the wrong filter.",
103: filterOne, filterList.get(0));
104: assertEquals("The get(int) method returned the wrong filter.",
105: filterTwo, filterList.get(1));
106:
107: IFilter filterFour = createNamedFilter("FOUR");
108: filterList.insert(filterFour, 1);
109: assertEquals("The get(int) method returned the wrong filter.",
110: filterOne, filterList.get(0));
111: assertEquals("The get(int) method returned the wrong filter.",
112: filterFour, filterList.get(1));
113: assertEquals("The get(int) method returned the wrong filter.",
114: filterTwo, filterList.get(2));
115: }
116:
117: /**
118: * Test to move up a filter in the list.
119: */
120: public void testMoveUp() {
121: FilterList filterList = new FilterList(new XmlElement());
122: IFilter filterOne = createNamedFilter("ONE");
123: IFilter filterTwo = createNamedFilter("TWO");
124: IFilter filterThree = createNamedFilter("THREE");
125: filterList.add(filterOne);
126: filterList.add(filterTwo);
127: filterList.add(filterThree);
128: assertEquals("The get(int) method returned the wrong filter.",
129: filterOne, filterList.get(0));
130: assertEquals("The get(int) method returned the wrong filter.",
131: filterThree, filterList.get(2));
132: filterList.moveUp(filterThree);
133: assertEquals("The get(int) method returned the wrong filter.",
134: filterThree, filterList.get(1));
135: filterList.moveUp(filterThree);
136: assertEquals("The get(int) method returned the wrong filter.",
137: filterThree, filterList.get(0));
138: assertEquals("The get(int) method returned the wrong filter.",
139: filterOne, filterList.get(1));
140: filterList.moveUp(filterThree);
141: assertEquals("The get(int) method returned the wrong filter.",
142: filterThree, filterList.get(0));
143: }
144:
145: /**
146: * Test to move down a filter in the list.
147: */
148: public void testMoveDown() {
149: FilterList filterList = new FilterList(new XmlElement());
150: IFilter filterOne = createNamedFilter("ONE");
151: IFilter filterTwo = createNamedFilter("TWO");
152: IFilter filterThree = createNamedFilter("THREE");
153: filterList.add(filterOne);
154: filterList.add(filterTwo);
155: filterList.add(filterThree);
156: assertEquals("The get(int) method returned the wrong filter.",
157: filterOne, filterList.get(0));
158: assertEquals("The get(int) method returned the wrong filter.",
159: filterThree, filterList.get(2));
160: filterList.moveDown(filterOne);
161: assertEquals("The get(int) method returned the wrong filter.",
162: filterOne, filterList.get(1));
163: filterList.moveDown(filterOne);
164: assertEquals("The get(int) method returned the wrong filter.",
165: filterOne, filterList.get(2));
166: assertEquals("The get(int) method returned the wrong filter.",
167: filterThree, filterList.get(1));
168: filterList.moveDown(filterOne);
169: assertEquals("The get(int) method returned the wrong filter.",
170: filterOne, filterList.get(2));
171: }
172:
173: /**
174: * Test for indexOf() method.
175: */
176: public void testIndexOf() {
177: FilterList filterList = new FilterList(new XmlElement());
178: IFilter filterOne = createNamedFilter("ONE");
179: IFilter filterTwo = createNamedFilter("TWO");
180: IFilter filterThree = createNamedFilter("THREE");
181: filterList.add(filterOne);
182: filterList.add(filterTwo);
183: filterList.add(filterThree);
184:
185: assertEquals(
186: "The indexof() method did not return the right index",
187: -1, filterList.indexOf(null));
188: assertEquals(
189: "The indexof() method did not return the right index",
190: 0, filterList.indexOf(filterOne));
191: assertEquals(
192: "The indexof() method did not return the right index",
193: 1, filterList.indexOf(filterTwo));
194: assertEquals(
195: "The indexof() method did not return the right index",
196: 2, filterList.indexOf(filterThree));
197: assertEquals(
198: "The indexof() method did not return the right index",
199: -1, filterList.indexOf(createNamedFilter("NONE")));
200: }
201:
202: /**
203: * Returns an empty filter with a specified name.
204: *
205: * @param name
206: * the name of the filter.
207: * @return a <code>Filter</code> with the specified name.
208: */
209: private IFilter createNamedFilter(String name) {
210: IFilter filter = FilterList.createDefaultFilter();
211: filter.setName(name);
212:
213: return filter;
214: }
215:
216: /**
217: * Asserts that the filters are the same.
218: *
219: * @param msg
220: * the message to output if the assertion fails.
221: * @param expected
222: * the expected filter.
223: * @param actual
224: * the actual filter.
225: */
226: private void assertEquals(String msg, Filter expected, Filter actual) {
227: assertEquals(msg, expected.getName(), actual.getName());
228: }
229: }
|