001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections.primitives; 018 019import java.util.Iterator; 020import java.util.NoSuchElementException; 021 022import org.apache.commons.collections.iterators.AbstractTestIterator; 023import org.apache.commons.collections.primitives.adapters.ByteIteratorIterator; 024 025/** 026 * @version $Revision: 480451 $ $Date: 2006-11-29 02:45:08 -0500 (Wed, 29 Nov 2006) $ 027 * @author Rodney Waldhoff 028 */ 029public abstract class TestByteIterator extends AbstractTestIterator { 030 031 // conventional 032 // ------------------------------------------------------------------------ 033 034 public TestByteIterator(String testName) { 035 super(testName); 036 } 037 038 // collections testing framework 039 // ------------------------------------------------------------------------ 040 041 public Iterator makeEmptyIterator() { 042 return ByteIteratorIterator.wrap(makeEmptyByteIterator()); 043 } 044 045 public Iterator makeFullIterator() { 046 return ByteIteratorIterator.wrap(makeFullByteIterator()); 047 } 048 049 050 protected abstract ByteIterator makeEmptyByteIterator(); 051 protected abstract ByteIterator makeFullByteIterator(); 052 protected abstract byte[] getFullElements(); 053 054 // tests 055 // ------------------------------------------------------------------------ 056 057 public void testNextHasNextRemove() { 058 byte[] elements = getFullElements(); 059 ByteIterator iter = makeFullByteIterator(); 060 for(int i=0;i<elements.length;i++) { 061 assertTrue(iter.hasNext()); 062 assertEquals(elements[i],iter.next(),0f); 063 if(supportsRemove()) { 064 iter.remove(); 065 } 066 } 067 assertTrue(! iter.hasNext() ); 068 } 069 070 public void testEmptyByteIterator() { 071 assertTrue( ! makeEmptyByteIterator().hasNext() ); 072 try { 073 makeEmptyByteIterator().next(); 074 fail("Expected NoSuchElementException"); 075 } catch(NoSuchElementException e) { 076 // expected 077 } 078 if(supportsRemove()) { 079 try { 080 makeEmptyByteIterator().remove(); 081 fail("Expected IllegalStateException"); 082 } catch(IllegalStateException e) { 083 // expected 084 } 085 } 086 } 087 088 public void testRemoveBeforeNext() { 089 if(supportsRemove()) { 090 try { 091 makeFullByteIterator().remove(); 092 fail("Expected IllegalStateException"); 093 } catch(IllegalStateException e) { 094 // expected 095 } 096 } 097 } 098 099 public void testRemoveAfterRemove() { 100 if(supportsRemove()) { 101 ByteIterator iter = makeFullByteIterator(); 102 iter.next(); 103 iter.remove(); 104 try { 105 iter.remove(); 106 fail("Expected IllegalStateException"); 107 } catch(IllegalStateException e) { 108 // expected 109 } 110 } 111 } 112}