KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > TestEnumerationUtils


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Hashtable JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 import junit.framework.Assert;
24 import junit.framework.Test;
25
26 /**
27  * Tests EnumerationUtils.
28  *
29  * @author <a HREF="mailto:ggregory@seagullsw.com">Gary Gregory</a>
30  * @version $Id: TestEnumerationUtils.java,v 1.4 2004/02/18 01:20:35 scolebourne Exp $
31  */

32 public class TestEnumerationUtils extends BulkTest {
33
34     public TestEnumerationUtils(String JavaDoc name) {
35         super(name);
36     }
37
38     public static final String JavaDoc TO_LIST_FIXTURE = "this is a test";
39     
40     public void testToListWithStringTokenizer() {
41         List JavaDoc expectedList1 = new ArrayList JavaDoc();
42         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(TO_LIST_FIXTURE);
43              while (st.hasMoreTokens()) {
44                  expectedList1.add(st.nextToken());
45              }
46         List JavaDoc expectedList2 = new ArrayList JavaDoc();
47         expectedList2.add("this");
48         expectedList2.add("is");
49         expectedList2.add("a");
50         expectedList2.add("test");
51         List JavaDoc actualList = EnumerationUtils.toList(new StringTokenizer JavaDoc(TO_LIST_FIXTURE));
52         Assert.assertEquals(expectedList1, expectedList2);
53         Assert.assertEquals(expectedList1, actualList);
54         Assert.assertEquals(expectedList2, actualList);
55     }
56
57     public void testToListWithHashtable() {
58         Hashtable JavaDoc expected = new Hashtable JavaDoc();
59         expected.put("one", new Integer JavaDoc(1));
60         expected.put("two", new Integer JavaDoc(2));
61         expected.put("three", new Integer JavaDoc(3));
62         // validate elements.
63
List JavaDoc actualEltList = EnumerationUtils.toList(expected.elements());
64         Assert.assertEquals(expected.size(), actualEltList.size());
65         Assert.assertTrue(actualEltList.contains(new Integer JavaDoc(1)));
66         Assert.assertTrue(actualEltList.contains(new Integer JavaDoc(2)));
67         Assert.assertTrue(actualEltList.contains(new Integer JavaDoc(3)));
68         List JavaDoc expectedEltList = new ArrayList JavaDoc();
69         expectedEltList.add(new Integer JavaDoc(1));
70         expectedEltList.add(new Integer JavaDoc(2));
71         expectedEltList.add(new Integer JavaDoc(3));
72         Assert.assertTrue(actualEltList.containsAll(expectedEltList));
73
74         // validate keys.
75
List JavaDoc actualKeyList = EnumerationUtils.toList(expected.keys());
76         Assert.assertEquals(expected.size(), actualEltList.size());
77         Assert.assertTrue(actualKeyList.contains("one"));
78         Assert.assertTrue(actualKeyList.contains("two"));
79         Assert.assertTrue(actualKeyList.contains("three"));
80         List JavaDoc expectedKeyList = new ArrayList JavaDoc();
81         expectedKeyList.add("one");
82         expectedKeyList.add("two");
83         expectedKeyList.add("three");
84         Assert.assertTrue(actualKeyList.containsAll(expectedKeyList));
85     }
86
87     public static Test suite() {
88         return BulkTest.makeSuite(TestEnumerationUtils.class);
89     }
90
91 }
92
Popular Tags