View Javadoc
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.collections4;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.ArrayList;
25  import java.util.Enumeration;
26  import java.util.HashSet;
27  import java.util.Hashtable;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Set;
31  import java.util.StringTokenizer;
32  import java.util.Vector;
33  
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Tests EnumerationUtils.
38   */
39  public class EnumerationUtilsTest {
40  
41      public static final String TO_LIST_FIXTURE = "this is a test";
42  
43      @Test
44      public void testAsIterableFor() {
45          final Vector<String> vector = new Vector<>();
46          vector.addElement("zero");
47          vector.addElement("one");
48          final Enumeration<String> en = vector.elements();
49          final Iterator<String> iterator = EnumerationUtils.asIterable(en).iterator();
50          assertTrue(iterator.hasNext());
51          assertEquals("zero", iterator.next());
52          assertTrue(iterator.hasNext());
53          assertEquals("one", iterator.next());
54          assertFalse(iterator.hasNext());
55      }
56  
57      @Test
58      public void testAsIterableForNull() {
59          assertThrows(NullPointerException.class, () -> EnumerationUtils.asIterable((Enumeration) null).iterator().next());
60      }
61  
62      @Test
63      public void testGetFromEnumeration() throws Exception {
64          // Enumeration, entry exists
65          final Vector<String> vector = new Vector<>();
66          vector.addElement("zero");
67          vector.addElement("one");
68          Enumeration<String> en = vector.elements();
69          assertEquals("zero", EnumerationUtils.get(en, 0));
70          en = vector.elements();
71          assertEquals("one", EnumerationUtils.get(en, 1));
72  
73          // Enumerator, non-existent entry
74          final Enumeration<String> finalEn = en;
75          assertThrows(IndexOutOfBoundsException.class, () -> EnumerationUtils.get(finalEn, 3));
76  
77          assertFalse(en.hasMoreElements());
78      }
79  
80      @Test
81      public void testToListWithHashtable() {
82          final Hashtable<String, Integer> expected = new Hashtable<>();
83          expected.put("one", Integer.valueOf(1));
84          expected.put("two", Integer.valueOf(2));
85          expected.put("three", Integer.valueOf(3));
86          // validate elements.
87          final List<Integer> actualEltList = EnumerationUtils.toList(expected.elements());
88          assertEquals(expected.size(), actualEltList.size());
89          assertTrue(actualEltList.contains(Integer.valueOf(1)));
90          assertTrue(actualEltList.contains(Integer.valueOf(2)));
91          assertTrue(actualEltList.contains(Integer.valueOf(3)));
92          final List<Integer> expectedEltList = new ArrayList<>();
93          expectedEltList.add(Integer.valueOf(1));
94          expectedEltList.add(Integer.valueOf(2));
95          expectedEltList.add(Integer.valueOf(3));
96          assertTrue(actualEltList.containsAll(expectedEltList));
97  
98          // validate keys.
99          final List<String> actualKeyList = EnumerationUtils.toList(expected.keys());
100         assertEquals(expected.size(), actualEltList.size());
101         assertTrue(actualKeyList.contains("one"));
102         assertTrue(actualKeyList.contains("two"));
103         assertTrue(actualKeyList.contains("three"));
104         final List<String> expectedKeyList = new ArrayList<>();
105         expectedKeyList.add("one");
106         expectedKeyList.add("two");
107         expectedKeyList.add("three");
108         assertTrue(actualKeyList.containsAll(expectedKeyList));
109     }
110 
111     @Test
112     public void testToListWithStringTokenizer() {
113         final List<String> expectedList1 = new ArrayList<>();
114         final StringTokenizer st = new StringTokenizer(TO_LIST_FIXTURE);
115         while (st.hasMoreTokens()) {
116             expectedList1.add(st.nextToken());
117         }
118         final List<String> expectedList2 = new ArrayList<>();
119         expectedList2.add("this");
120         expectedList2.add("is");
121         expectedList2.add("a");
122         expectedList2.add("test");
123         final List<String> actualList = EnumerationUtils.toList(new StringTokenizer(TO_LIST_FIXTURE));
124         assertEquals(expectedList1, expectedList2);
125         assertEquals(expectedList1, actualList);
126         assertEquals(expectedList2, actualList);
127     }
128 
129     @Test
130     public void testToSetWithHashtable() {
131         final Hashtable<String, Integer> expected = new Hashtable<>();
132         expected.put("one", Integer.valueOf(1));
133         expected.put("two", Integer.valueOf(2));
134         expected.put("three", Integer.valueOf(3));
135         // validate elements.
136         final Set<Integer> actualEltSet = EnumerationUtils.toSet(expected.elements());
137         assertEquals(expected.size(), actualEltSet.size());
138         assertTrue(actualEltSet.contains(Integer.valueOf(1)));
139         assertTrue(actualEltSet.contains(Integer.valueOf(2)));
140         assertTrue(actualEltSet.contains(Integer.valueOf(3)));
141         final Set<Integer> expectedEltList = new HashSet<>();
142         expectedEltList.add(Integer.valueOf(1));
143         expectedEltList.add(Integer.valueOf(2));
144         expectedEltList.add(Integer.valueOf(3));
145         assertTrue(actualEltSet.containsAll(expectedEltList));
146 
147         // validate keys.
148         final Set<String> actualKeySet = EnumerationUtils.toSet(expected.keys());
149         assertEquals(expected.size(), actualEltSet.size());
150         assertTrue(actualKeySet.contains("one"));
151         assertTrue(actualKeySet.contains("two"));
152         assertTrue(actualKeySet.contains("three"));
153         final Set<String> expectedKeySet = new HashSet<>();
154         expectedKeySet.add("one");
155         expectedKeySet.add("two");
156         expectedKeySet.add("three");
157         assertTrue(actualKeySet.containsAll(expectedKeySet));
158     }
159 
160 }