1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.commons.collections;
18  
19  import java.util.ArrayList;
20  import java.util.Hashtable;
21  import java.util.List;
22  import java.util.StringTokenizer;
23  
24  import junit.framework.Assert;
25  import junit.framework.Test;
26  
27  /**
28   * Tests EnumerationUtils.
29   * 
30   * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
31   * @version $Id: TestEnumerationUtils.java 646780 2008-04-10 12:48:07Z niallp $
32   */
33  public class TestEnumerationUtils extends BulkTest {
34  
35      public TestEnumerationUtils(String name) {
36          super(name);
37      }
38  
39      public static final String TO_LIST_FIXTURE = "this is a test";
40      
41      public void testToListWithStringTokenizer() {
42          List expectedList1 = new ArrayList();
43          StringTokenizer st = new StringTokenizer(TO_LIST_FIXTURE);
44               while (st.hasMoreTokens()) {
45                   expectedList1.add(st.nextToken());
46               }        
47          List expectedList2 = new ArrayList();
48          expectedList2.add("this");
49          expectedList2.add("is");
50          expectedList2.add("a");
51          expectedList2.add("test");
52          List actualList = EnumerationUtils.toList(new StringTokenizer(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.elements());
65          Assert.assertEquals(expected.size(), actualEltList.size());
66          Assert.assertTrue(actualEltList.contains(new Integer(1)));
67          Assert.assertTrue(actualEltList.contains(new Integer(2)));
68          Assert.assertTrue(actualEltList.contains(new Integer(3)));
69          List expectedEltList = new ArrayList();
70          expectedEltList.add(new Integer(1));
71          expectedEltList.add(new Integer(2));
72          expectedEltList.add(new Integer(3));
73          Assert.assertTrue(actualEltList.containsAll(expectedEltList));
74  
75          // validate keys.
76          List actualKeyList = EnumerationUtils.toList(expected.keys());
77          Assert.assertEquals(expected.size(), actualEltList.size());
78          Assert.assertTrue(actualKeyList.contains("one"));
79          Assert.assertTrue(actualKeyList.contains("two"));
80          Assert.assertTrue(actualKeyList.contains("three"));
81          List expectedKeyList = new ArrayList();
82          expectedKeyList.add("one");
83          expectedKeyList.add("two");
84          expectedKeyList.add("three");
85          Assert.assertTrue(actualKeyList.containsAll(expectedKeyList));
86      }
87  
88      public static Test suite() {
89          return BulkTest.makeSuite(TestEnumerationUtils.class);
90      }
91  
92  }