01: /*
02: * Copyright 2001-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
19: import java.util.Hashtable;
20: import java.util.List;
21: import java.util.StringTokenizer;
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 155406 2005-02-26 12:55:26Z dirkv $
31: */
32: public class TestEnumerationUtils extends BulkTest {
33:
34: public TestEnumerationUtils(String name) {
35: super (name);
36: }
37:
38: public static final String TO_LIST_FIXTURE = "this is a test";
39:
40: public void testToListWithStringTokenizer() {
41: List expectedList1 = new ArrayList();
42: StringTokenizer st = new StringTokenizer(TO_LIST_FIXTURE);
43: while (st.hasMoreTokens()) {
44: expectedList1.add(st.nextToken());
45: }
46: List expectedList2 = new ArrayList();
47: expectedList2.add("this");
48: expectedList2.add("is");
49: expectedList2.add("a");
50: expectedList2.add("test");
51: List actualList = EnumerationUtils.toList(new StringTokenizer(
52: TO_LIST_FIXTURE));
53: Assert.assertEquals(expectedList1, expectedList2);
54: Assert.assertEquals(expectedList1, actualList);
55: Assert.assertEquals(expectedList2, actualList);
56: }
57:
58: public void testToListWithHashtable() {
59: Hashtable expected = new Hashtable();
60: expected.put("one", new Integer(1));
61: expected.put("two", new Integer(2));
62: expected.put("three", new Integer(3));
63: // validate elements.
64: List actualEltList = EnumerationUtils.toList(expected
65: .elements());
66: Assert.assertEquals(expected.size(), actualEltList.size());
67: Assert.assertTrue(actualEltList.contains(new Integer(1)));
68: Assert.assertTrue(actualEltList.contains(new Integer(2)));
69: Assert.assertTrue(actualEltList.contains(new Integer(3)));
70: List expectedEltList = new ArrayList();
71: expectedEltList.add(new Integer(1));
72: expectedEltList.add(new Integer(2));
73: expectedEltList.add(new Integer(3));
74: Assert.assertTrue(actualEltList.containsAll(expectedEltList));
75:
76: // validate keys.
77: List actualKeyList = EnumerationUtils.toList(expected.keys());
78: Assert.assertEquals(expected.size(), actualEltList.size());
79: Assert.assertTrue(actualKeyList.contains("one"));
80: Assert.assertTrue(actualKeyList.contains("two"));
81: Assert.assertTrue(actualKeyList.contains("three"));
82: List expectedKeyList = new ArrayList();
83: expectedKeyList.add("one");
84: expectedKeyList.add("two");
85: expectedKeyList.add("three");
86: Assert.assertTrue(actualKeyList.containsAll(expectedKeyList));
87: }
88:
89: public static Test suite() {
90: return BulkTest.makeSuite(TestEnumerationUtils.class);
91: }
92:
93: }
|