1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.Hashtable;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.StringTokenizer;
30 import java.util.Vector;
31
32 import org.junit.jupiter.api.Test;
33
34
35
36
37 public class EnumerationUtilsTest {
38
39 public static final String TO_LIST_FIXTURE = "this is a test";
40
41 @Test
42 public void testAsIterableFor() {
43 final Vector<String> vector = new Vector<>();
44 vector.addElement("zero");
45 vector.addElement("one");
46 final Enumeration<String> en = vector.elements();
47 final Iterator<String> iterator = EnumerationUtils.asIterable(en).iterator();
48 assertTrue(iterator.hasNext());
49 assertEquals("zero", iterator.next());
50 assertTrue(iterator.hasNext());
51 assertEquals("one", iterator.next());
52 assertFalse(iterator.hasNext());
53 }
54
55 @Test
56 public void testAsIterableForNull() {
57 assertThrows(NullPointerException.class, () -> EnumerationUtils.asIterable((Enumeration) null).iterator().next());
58 }
59
60 @Test
61 public void testGetFromEnumeration() throws Exception {
62
63 final Vector<String> vector = new Vector<>();
64 vector.addElement("zero");
65 vector.addElement("one");
66 Enumeration<String> en = vector.elements();
67 assertEquals("zero", EnumerationUtils.get(en, 0));
68 en = vector.elements();
69 assertEquals("one", EnumerationUtils.get(en, 1));
70
71
72 final Enumeration<String> finalEn = en;
73 assertThrows(IndexOutOfBoundsException.class, () -> EnumerationUtils.get(finalEn, 3));
74
75 assertFalse(en.hasMoreElements());
76 }
77
78 @Test
79 public void testToListWithHashtable() {
80 final Hashtable<String, Integer> expected = new Hashtable<>();
81 expected.put("one", Integer.valueOf(1));
82 expected.put("two", Integer.valueOf(2));
83 expected.put("three", Integer.valueOf(3));
84
85 final List<Integer> actualEltList = EnumerationUtils.toList(expected.elements());
86 assertEquals(expected.size(), actualEltList.size());
87 assertTrue(actualEltList.contains(Integer.valueOf(1)));
88 assertTrue(actualEltList.contains(Integer.valueOf(2)));
89 assertTrue(actualEltList.contains(Integer.valueOf(3)));
90 final List<Integer> expectedEltList = new ArrayList<>();
91 expectedEltList.add(Integer.valueOf(1));
92 expectedEltList.add(Integer.valueOf(2));
93 expectedEltList.add(Integer.valueOf(3));
94 assertTrue(actualEltList.containsAll(expectedEltList));
95
96
97 final List<String> actualKeyList = EnumerationUtils.toList(expected.keys());
98 assertEquals(expected.size(), actualEltList.size());
99 assertTrue(actualKeyList.contains("one"));
100 assertTrue(actualKeyList.contains("two"));
101 assertTrue(actualKeyList.contains("three"));
102 final List<String> expectedKeyList = new ArrayList<>();
103 expectedKeyList.add("one");
104 expectedKeyList.add("two");
105 expectedKeyList.add("three");
106 assertTrue(actualKeyList.containsAll(expectedKeyList));
107 }
108
109 @Test
110 public void testToListWithStringTokenizer() {
111 final List<String> expectedList1 = new ArrayList<>();
112 final StringTokenizer st = new StringTokenizer(TO_LIST_FIXTURE);
113 while (st.hasMoreTokens()) {
114 expectedList1.add(st.nextToken());
115 }
116 final List<String> expectedList2 = new ArrayList<>();
117 expectedList2.add("this");
118 expectedList2.add("is");
119 expectedList2.add("a");
120 expectedList2.add("test");
121 final List<String> actualList = EnumerationUtils.toList(new StringTokenizer(TO_LIST_FIXTURE));
122 assertEquals(expectedList1, expectedList2);
123 assertEquals(expectedList1, actualList);
124 assertEquals(expectedList2, actualList);
125 }
126
127 }