KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > core > util > PrioritizedListTest


1 package com.thoughtworks.xstream.core.util;
2
3 import junit.framework.TestCase;
4
5 import java.util.Iterator JavaDoc;
6
7 public class PrioritizedListTest extends TestCase {
8
9     private void assertNextEquals(Object JavaDoc expected, Iterator JavaDoc iterator) {
10         assertTrue("Expected to pull of another item from iterator : " + expected, iterator.hasNext());
11         assertEquals(expected, iterator.next());
12     }
13
14     private void assertNoMore(Iterator JavaDoc iterator) {
15         assertFalse("Should be no more items in iterator", iterator.hasNext());
16     }
17
18     public void testIteratesOverElementsInReverseOrderTheyWereAdded() {
19         PrioritizedList list = new PrioritizedList();
20         list.add("one");
21         list.add("two");
22         list.add("three");
23
24         Iterator JavaDoc iterator = list.iterator();
25         assertNextEquals("three", iterator);
26         assertNextEquals("two", iterator);
27         assertNextEquals("one", iterator);
28         assertNoMore(iterator);
29     }
30
31     public void testHandlesMultipleIsolatedIterators() {
32         PrioritizedList list = new PrioritizedList();
33         list.add("one");
34         list.add("two");
35
36         Iterator JavaDoc iteratorOne = list.iterator();
37         assertNextEquals("two", iteratorOne);
38
39         Iterator JavaDoc iteratorTwo = list.iterator();
40         assertNextEquals("one", iteratorOne);
41
42         assertNextEquals("two", iteratorTwo);
43         assertNextEquals("one", iteratorTwo);
44
45         assertNoMore(iteratorTwo);
46         assertNoMore(iteratorOne);
47     }
48
49     public void testIteratesOverHighestPriorityItemsFirst() {
50         PrioritizedList list = new PrioritizedList();
51         list.add("medium one", 0);
52         list.add("high one", 1);
53         list.add("low one", -1);
54         list.add("very high", 4);
55         list.add("low two", -1);
56         list.add("medium two", 0);
57         list.add("VERY VERY high", 100);
58         list.add("high two", 1);
59         list.add("very low", -4);
60         list.add("VERY VERY low", -100);
61
62         Iterator JavaDoc iterator = list.iterator();
63         assertNextEquals("VERY VERY high", iterator);
64         assertNextEquals("very high", iterator);
65         assertNextEquals("high two", iterator);
66         assertNextEquals("high one", iterator);
67         assertNextEquals("medium two", iterator);
68         assertNextEquals("medium one", iterator);
69         assertNextEquals("low two", iterator);
70         assertNextEquals("low one", iterator);
71         assertNextEquals("very low", iterator);
72         assertNextEquals("VERY VERY low", iterator);
73         assertNoMore(iterator);
74     }
75
76     public void testAllowsIteratorToStartAtSpecificPriority() {
77         PrioritizedList list = new PrioritizedList();
78         list.add("low one", -1);
79         list.add("low two", -1);
80         list.add("medium one", 0);
81
82         assertEquals("low two", list.firstOfLowestPriority());
83     }
84 }
85
Popular Tags