Home | History | Annotate | Download | only in io
      1 /*
      2  * Copyright (C) 2007 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.ByteArrayOutputStream;
     20 
     21 /**
     22  * Unit tests for {@link CountingOutputStream}.
     23  *
     24  * @author Chris Nokleberg
     25  */
     26 public class CountingOutputStreamTest extends IoTestCase {
     27 
     28   public void testCount() throws Exception {
     29     int written = 0;
     30     ByteArrayOutputStream out = new ByteArrayOutputStream();
     31     CountingOutputStream counter = new CountingOutputStream(out);
     32     assertEquals(written, out.size());
     33     assertEquals(written, counter.getCount());
     34 
     35     counter.write(0);
     36     written += 1;
     37     assertEquals(written, out.size());
     38     assertEquals(written, counter.getCount());
     39 
     40     byte[] data = new byte[10];
     41     counter.write(data);
     42     written += 10;
     43     assertEquals(written, out.size());
     44     assertEquals(written, counter.getCount());
     45 
     46     counter.write(data, 0, 5);
     47     written += 5;
     48     assertEquals(written, out.size());
     49     assertEquals(written, counter.getCount());
     50 
     51     counter.write(data, 2, 5);
     52     written += 5;
     53     assertEquals(written, out.size());
     54     assertEquals(written, counter.getCount());
     55 
     56     // Test that illegal arguments do not affect count
     57     try {
     58       counter.write(data, 0, data.length + 1);
     59       fail("expected exception");
     60     } catch (IndexOutOfBoundsException expected) {
     61     }
     62     assertEquals(written, out.size());
     63     assertEquals(written, counter.getCount());
     64   }
     65 }
     66