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