KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > utils > IndexOfComparatorTest


1 package com.tonbeller.wcf.utils;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collections JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7
8 import junit.framework.TestCase;
9
10 /**
11  * @author av
12  * @since 07.03.2005
13  */

14 public class IndexOfComparatorTest extends TestCase {
15   public void testIndexOfComparator() {
16     assertEquals("abcd", toString(asList("abcd")));
17     check("abcd", "abcd", "abcd");
18     check("dcba", "abcd", "abcd");
19     check("xbcy", "abcd", "bcxy");
20     check("", "", "");
21     check("abc", "", "abc");
22     check("", "abc", "");
23     check("dcba", "", "dcba");
24     check("dcba", "a", "adcb");
25     check("dcba", "ab", "abdc");
26     check("dcba", "abc", "abcd");
27     check("dcba", "abcd", "abcd");
28   }
29
30   private void check(String JavaDoc s1, String JavaDoc s2, String JavaDoc s3) {
31     List JavaDoc l1 = asList(s1);
32     List JavaDoc l2 = asList(s2);
33     IndexOfComparator c = new IndexOfComparator(l2);
34     Collections.sort(l1, c);
35     assertEquals(s3, toString(l1));
36   }
37   
38   List JavaDoc asList(String JavaDoc s) {
39     List JavaDoc l = new ArrayList JavaDoc();
40     char[] c = s.toCharArray();
41     for (int i = 0;i < c.length; i++)
42       l.add(new String JavaDoc("" + c[i]));
43     return l;
44   }
45   
46   String JavaDoc toString(List JavaDoc l) {
47     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
48     for (Iterator JavaDoc it = l.iterator(); it.hasNext();)
49       sb.append(it.next());
50     return sb.toString();
51   }
52   
53 }
54
Popular Tags