Home | History | Annotate | Download | only in io
      1 /*
      2  * Copyright (C) 2008 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.io;
     18 
     19 import java.io.ByteArrayInputStream;
     20 import java.io.IOException;
     21 import java.io.InputStream;
     22 
     23 /**
     24  * Unit tests for {@link CountingInputStream}.
     25  *
     26  * @author Chris Nokleberg
     27  */
     28 public class CountingInputStreamTest extends IoTestCase {
     29   private CountingInputStream counter;
     30 
     31   @Override protected void setUp() throws Exception {
     32     super.setUp();
     33     counter = new CountingInputStream(new ByteArrayInputStream(new byte[20]));
     34   }
     35 
     36   public void testReadSingleByte() throws IOException {
     37     assertEquals(0, counter.getCount());
     38     assertEquals(0, counter.read());
     39     assertEquals(1, counter.getCount());
     40   }
     41 
     42   public void testReadArray() throws IOException {
     43     assertEquals(10, counter.read(new byte[10]));
     44     assertEquals(10, counter.getCount());
     45   }
     46 
     47   public void testReadArrayRange() throws IOException {
     48     assertEquals(3, counter.read(new byte[10], 1, 3));
     49     assertEquals(3, counter.getCount());
     50   }
     51 
     52   public void testSkip() throws IOException {
     53     assertEquals(10, counter.skip(10));
     54     assertEquals(10, counter.getCount());
     55   }
     56 
     57   public void testSkipEOF() throws IOException {
     58     assertEquals(20, counter.skip(30));
     59     assertEquals(20, counter.getCount());
     60     assertEquals(0, counter.skip(20));
     61     assertEquals(20, counter.getCount());
     62 
     63     // Test reading a single byte while we're in the right state
     64     assertEquals(-1, counter.read());
     65     assertEquals(20, counter.getCount());
     66   }
     67 
     68   public void testReadArrayEOF() throws IOException {
     69     assertEquals(20, counter.read(new byte[30]));
     70     assertEquals(20, counter.getCount());
     71     assertEquals(-1, counter.read(new byte[30]));
     72     assertEquals(20, counter.getCount());
     73   }
     74 
     75   public void testMark() throws Exception {
     76     assertTrue(counter.markSupported());
     77     assertEquals(10, counter.read(new byte[10]));
     78     assertEquals(10, counter.getCount());
     79     counter.mark(5);
     80     counter.read();
     81     assertEquals(11, counter.getCount());
     82     counter.reset();
     83     assertEquals(10, counter.getCount());
     84     assertEquals(10, counter.skip(100));
     85     assertEquals(20, counter.getCount());
     86   }
     87 
     88   public void testMarkNotSet() {
     89     try {
     90       counter.reset();
     91       fail();
     92     } catch (IOException expected) {
     93       assertEquals("Mark not set", expected.getMessage());
     94     }
     95   }
     96 
     97   public void testMarkNotSupported() {
     98     counter = new CountingInputStream(new UnmarkableInputStream());
     99 
    100     try {
    101       counter.reset();
    102       fail();
    103     } catch (IOException expected) {
    104       assertEquals("Mark not supported", expected.getMessage());
    105     }
    106   }
    107 
    108   private static class UnmarkableInputStream extends InputStream {
    109     @Override
    110     public int read() throws IOException {
    111       return 0;
    112     }
    113 
    114     @Override
    115     public boolean markSupported() {
    116       return false;
    117     }
    118   }
    119 }
    120