001 /* 002 * $Id: IteratorEnumerationTestCase.java 358692 2005-12-23 03:37:26Z niallp $ 003 * $Revision: 358692 $ 004 * $Date: 2005-12-23 03:37:26 +0000 (Fri, 23 Dec 2005) $ 005 * 006 * ==================================================================== 007 * 008 * Copyright 2005 The Apache Software Foundation 009 * 010 * Licensed under the Apache License, Version 2.0 (the "License"); 011 * you may not use this file except in compliance with the License. 012 * You may obtain a copy of the License at 013 * 014 * http://www.apache.org/licenses/LICENSE-2.0 015 * 016 * Unless required by applicable law or agreed to in writing, software 017 * distributed under the License is distributed on an "AS IS" BASIS, 018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 019 * See the License for the specific language governing permissions and 020 * limitations under the License. 021 * 022 */ 023 024 package org.apache.commons.resources.util; 025 026 import junit.framework.Test; 027 import junit.framework.TestCase; 028 import junit.framework.TestSuite; 029 import java.util.ArrayList; 030 import java.util.Hashtable; 031 import java.util.Iterator; 032 import java.util.Enumeration; 033 034 /** 035 * Unit tests for the <code>org.apache.commons.resources.util.IteratorEnumeration</code> class. 036 */ 037 public class IteratorEnumerationTestCase extends TestCase { 038 039 /** 040 * Defines the testcase name for JUnit. 041 * 042 * @param theName the testcase's name. 043 */ 044 public IteratorEnumerationTestCase(String theName) { 045 super(theName); 046 } 047 048 /** 049 * Start the tests. 050 * 051 * @param theArgs the arguments. Not used 052 */ 053 public static void main(String[] theArgs) { 054 junit.awtui.TestRunner.main( 055 new String[] { IteratorEnumerationTestCase.class.getName()}); 056 } 057 058 /** 059 * @return a test suite (<code>TestSuite</code>) that includes all methods 060 * starting with "test" 061 */ 062 public static Test suite() { 063 return new TestSuite(IteratorEnumerationTestCase.class); 064 } 065 066 public void testIterator() { 067 068 // Wrap an Iterator 069 ArrayList list = new ArrayList(); 070 list.add("one"); 071 Enumeration enumeration = new IteratorEnumeration(list.iterator()); 072 Iterator iterator = new IteratorEnumeration(list.iterator()); 073 074 // Test Enumeration 075 assertTrue("Enumeration.hasMoreElements(A)", enumeration.hasMoreElements()); 076 assertEquals("Enumeration.nextElement", "one", enumeration.nextElement()); 077 assertFalse("Enumeration.hasMoreElements(B)", enumeration.hasMoreElements()); 078 079 // Test Iterator 080 assertTrue("Iterator.hasNext(A)", iterator.hasNext()); 081 assertEquals("Iterator.next", "one", iterator.next()); 082 assertFalse("Iterator.hasNext(B)", iterator.hasNext()); 083 try { 084 iterator.remove(); 085 fail("Expected UnsupportedOperationException"); 086 } catch(UnsupportedOperationException e) { 087 // expected result 088 } 089 090 } 091 092 public void testEnumeration() { 093 094 // Wrap an Enumeration 095 Hashtable hashtable = new Hashtable(); 096 hashtable.put("one", "XXX"); 097 Enumeration enumeration = new IteratorEnumeration(hashtable.keys()); 098 Iterator iterator = new IteratorEnumeration(hashtable.keys()); 099 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 }