1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.apache.commons.resources.util;
25
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29 import java.util.ArrayList;
30 import java.util.Hashtable;
31 import java.util.Iterator;
32 import java.util.Enumeration;
33
34
35
36
37 public class IteratorEnumerationTestCase extends TestCase {
38
39
40
41
42
43
44 public IteratorEnumerationTestCase(String theName) {
45 super(theName);
46 }
47
48
49
50
51
52
53 public static void main(String[] theArgs) {
54 junit.awtui.TestRunner.main(
55 new String[] { IteratorEnumerationTestCase.class.getName()});
56 }
57
58
59
60
61
62 public static Test suite() {
63 return new TestSuite(IteratorEnumerationTestCase.class);
64 }
65
66 public void testIterator() {
67
68
69 ArrayList list = new ArrayList();
70 list.add("one");
71 Enumeration enumeration = new IteratorEnumeration(list.iterator());
72 Iterator iterator = new IteratorEnumeration(list.iterator());
73
74
75 assertTrue("Enumeration.hasMoreElements(A)", enumeration.hasMoreElements());
76 assertEquals("Enumeration.nextElement", "one", enumeration.nextElement());
77 assertFalse("Enumeration.hasMoreElements(B)", enumeration.hasMoreElements());
78
79
80 assertTrue("Iterator.hasNext(A)", iterator.hasNext());
81 assertEquals("Iterator.next", "one", iterator.next());
82 assertFalse("Iterator.hasNext(B)", iterator.hasNext());
83 try {
84 iterator.remove();
85 fail("Expected UnsupportedOperationException");
86 } catch(UnsupportedOperationException e) {
87
88 }
89
90 }
91
92 public void testEnumeration() {
93
94
95 Hashtable hashtable = new Hashtable();
96 hashtable.put("one", "XXX");
97 Enumeration enumeration = new IteratorEnumeration(hashtable.keys());
98 Iterator iterator = new IteratorEnumeration(hashtable.keys());
99
100
101 assertTrue("Enumeration.hasMoreElements(A)", enumeration.hasMoreElements());
102 assertEquals("Enumeration.nextElement", "one", enumeration.nextElement());
103 assertFalse("Enumeration.hasMoreElements(B)", enumeration.hasMoreElements());
104
105
106 assertTrue("Iterator.hasNext(A)", iterator.hasNext());
107 assertEquals("Iterator.next", "one", iterator.next());
108 assertFalse("Iterator.hasNext(B)", iterator.hasNext());
109
110 try {
111 iterator.remove();
112 fail("Expected UnsupportedOperationException");
113 } catch(UnsupportedOperationException e) {
114
115 }
116
117
118 }
119 }