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.adapters.io;
018
019import java.io.ByteArrayInputStream;
020import java.io.IOException;
021import java.io.InputStream;
022
023import junit.framework.Test;
024import junit.framework.TestSuite;
025
026import org.apache.commons.collections.primitives.ByteIterator;
027import org.apache.commons.collections.primitives.TestByteIterator;
028
029/**
030 * @version $Revision: 480451 $ $Date: 2006-11-29 02:45:08 -0500 (Wed, 29 Nov 2006) $
031 * @author Rodney Waldhoff
032 */
033public class TestInputStreamByteIterator extends TestByteIterator {
034
035    // conventional
036    // ------------------------------------------------------------------------
037
038    public TestInputStreamByteIterator(String testName) {
039        super(testName);
040    }
041
042    public static Test suite() {
043        return new TestSuite(TestInputStreamByteIterator.class);
044    }
045
046    // ------------------------------------------------------------------------
047    
048    public boolean supportsRemove() {
049        return false;
050    }
051
052    protected ByteIterator makeEmptyByteIterator() {
053        return new InputStreamByteIterator(new ByteArrayInputStream(new byte[0]));
054    }
055
056    protected ByteIterator makeFullByteIterator() {
057        return new InputStreamByteIterator(new ByteArrayInputStream(getFullElements()));
058    }
059
060    protected byte[] getFullElements() {
061        byte[] bytes = new byte[256];
062        for(int i=0; i < 256; i++) {
063            bytes[i] = (byte)(i-128);
064        }
065        return bytes;
066    }
067
068
069    // ------------------------------------------------------------------------
070    
071    public void testErrorThrowingStream() {
072        InputStream errStream = new InputStream() {
073            public int read() throws IOException {
074                throw new IOException();
075            }
076        };
077        
078        ByteIterator iter = new InputStreamByteIterator(errStream);
079        try {
080            iter.hasNext();
081            fail("Expected RuntimeException");
082        } catch(RuntimeException e) {
083            // expected
084        } 
085        try {
086            iter.next();
087            fail("Expected RuntimeException");
088        } catch(RuntimeException e) {
089            // expected
090        } 
091    }
092    
093    public void testAdaptNull() {
094        assertNull(InputStreamByteIterator.adapt(null));
095    }
096
097    public void testAdaptNonNull() {
098        assertNotNull(InputStreamByteIterator.adapt(new ByteArrayInputStream(new byte[0])));
099    }
100}