1 // $Id: SortedListTest.java,v 1.1.1.1 2002/09/29 17:26:07 powerpete Exp $
2 package de.jface.util;
3
4 import java.util.Comparator;
5
6 import junit.framework.TestCase;
7
8 /***
9 * The test case <code>SortedListTest</code>
10 * tests the [your text here].
11 *
12 * @author Moritz Petersen
13 * @version $Revision: 1.1.1.1 $
14 */
15 public class SortedListTest extends TestCase
16 {
17 /***
18 * This constructor creates a new
19 * SortedListTest test case.
20 */
21 public SortedListTest(String name)
22 {
23 super(name);
24 }
25
26 /***
27 * This method tests [your text here].
28 */
29 public void testGet()
30 {
31 // Test using Comparator
32 SortedList sl1 = new SortedList();
33 Test t1 = new Test("foo");
34 Test t2 = new Test("foo");
35 Test t3 = new Test("bar");
36
37 // Test insert into empty list.
38 Test rt1 = (Test) sl1.get(t1, new TestComparator());
39
40 assertTrue("Not the same instance.", t1 == rt1);
41
42 // Test insert again (should return the previous instance.
43 Test rt2 = (Test) sl1.get(t2, new TestComparator());
44
45 assertTrue("Must not be the same instance.", t2 != rt2);
46 assertTrue("Not the same instance.", t1 == rt2);
47
48 // Test insert another into list
49 Test rt3 = (Test) sl1.get(t3, new TestComparator());
50
51 assertTrue("Not the same instance.", t3 == rt3);
52
53 // Test using Comparable
54 SortedList sl2 = new SortedList();
55 TestComparable tc1 = new TestComparable("foo");
56 TestComparable tc2 = new TestComparable("foo");
57 TestComparable tc3 = new TestComparable("bar");
58
59 TestComparable rtc1 = (TestComparable) sl2.get(tc1);
60
61 assertTrue("Not the same instance.", tc1 == rtc1);
62
63 TestComparable rtc2 = (TestComparable) sl2.get(tc2);
64
65 assertTrue("Must not be the same instance.", tc2 != rtc2);
66 assertTrue("Not the same instance.", tc1 == rtc2);
67
68 TestComparable rtc3 = (TestComparable) sl2.get(tc3);
69
70 assertTrue("Not the same instance.", tc3 == rtc3);
71 }
72
73 private class Test
74 {
75 private String name;
76
77 public Test(String name)
78 {
79 this.name = name;
80 }
81
82 public String toString()
83 {
84 return name;
85 }
86 }
87
88 private class TestComparator
89 implements Comparator
90 {
91 public int compare(Object o1, Object o2)
92 {
93 Test t1 = (Test) o1;
94 Test t2 = (Test) o2;
95
96 return t1.toString().compareTo(t2.toString());
97 }
98 }
99
100 private class TestComparable
101 implements Comparable
102 {
103 private String name;
104
105 public TestComparable(String name)
106 {
107 this.name = name;
108 }
109
110 public int compareTo(Object o)
111 {
112 return name.compareTo(((TestComparable) o).name);
113 }
114
115 public String toString()
116 {
117 return name;
118 }
119 }
120 }
121
122 // ------1---------2---------3---------4---------5---------6---------7---------8
This page was automatically generated by Maven