1   /*
2    * $Id: IteratorEnumerationTestCase.java 358692 2005-12-23 03:37:26Z niallp $
3    * $Revision: 358692 $
4    * $Date: 2005-12-23 03:37:26 +0000 (Fri, 23 Dec 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2005 The Apache Software Foundation
9    * 
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
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   * Unit tests for the <code>org.apache.commons.resources.util.IteratorEnumeration</code> class.
36   */
37  public class IteratorEnumerationTestCase extends TestCase {
38      
39      /**
40       * Defines the testcase name for JUnit.
41       *
42       * @param theName the testcase's name.
43       */
44      public IteratorEnumerationTestCase(String theName) {
45          super(theName);
46      }
47  
48      /**
49       * Start the tests.
50       *
51       * @param theArgs the arguments. Not used
52       */
53      public static void main(String[] theArgs) {
54          junit.awtui.TestRunner.main(
55              new String[] { IteratorEnumerationTestCase.class.getName()});
56      }
57  
58      /**
59       * @return a test suite (<code>TestSuite</code>) that includes all methods
60       *         starting with "test"
61       */
62      public static Test suite() {
63          return new TestSuite(IteratorEnumerationTestCase.class);
64      }
65  
66      public void testIterator() {
67  
68          // Wrap an Iterator
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          // Test Enumeration
75          assertTrue("Enumeration.hasMoreElements(A)", enumeration.hasMoreElements());
76          assertEquals("Enumeration.nextElement", "one", enumeration.nextElement());
77          assertFalse("Enumeration.hasMoreElements(B)", enumeration.hasMoreElements());
78  
79          // Test Iterator
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             // expected result
88          }
89  
90      }
91  
92      public void testEnumeration() {
93  
94          // Wrap an Enumeration
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         // Test Enumeration
101         assertTrue("Enumeration.hasMoreElements(A)", enumeration.hasMoreElements());
102         assertEquals("Enumeration.nextElement", "one", enumeration.nextElement());
103         assertFalse("Enumeration.hasMoreElements(B)", enumeration.hasMoreElements());
104 
105         // Test Iterator
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            // expected result
115         }
116 
117         
118     }
119 }