• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.google.common.truth.Truth.assertThat;
20 import static org.junit.Assert.assertThrows;
21 
22 import java.io.ByteArrayInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 
26 /**
27  * Unit tests for {@link CountingInputStream}.
28  *
29  * @author Chris Nokleberg
30  */
31 public class CountingInputStreamTest extends IoTestCase {
32   private CountingInputStream counter;
33 
34   @Override
setUp()35   protected void setUp() throws Exception {
36     super.setUp();
37     counter = new CountingInputStream(new ByteArrayInputStream(new byte[20]));
38   }
39 
testReadSingleByte()40   public void testReadSingleByte() throws IOException {
41     assertEquals(0, counter.getCount());
42     assertEquals(0, counter.read());
43     assertEquals(1, counter.getCount());
44   }
45 
testReadArray()46   public void testReadArray() throws IOException {
47     assertEquals(10, counter.read(new byte[10]));
48     assertEquals(10, counter.getCount());
49   }
50 
testReadArrayRange()51   public void testReadArrayRange() throws IOException {
52     assertEquals(3, counter.read(new byte[10], 1, 3));
53     assertEquals(3, counter.getCount());
54   }
55 
testSkip()56   public void testSkip() throws IOException {
57     assertEquals(10, counter.skip(10));
58     assertEquals(10, counter.getCount());
59   }
60 
testSkipEOF()61   public void testSkipEOF() throws IOException {
62     assertEquals(20, counter.skip(30));
63     assertEquals(20, counter.getCount());
64     assertEquals(0, counter.skip(20));
65     assertEquals(20, counter.getCount());
66 
67     // Test reading a single byte while we're in the right state
68     assertEquals(-1, counter.read());
69     assertEquals(20, counter.getCount());
70   }
71 
testReadArrayEOF()72   public void testReadArrayEOF() throws IOException {
73     assertEquals(20, counter.read(new byte[30]));
74     assertEquals(20, counter.getCount());
75     assertEquals(-1, counter.read(new byte[30]));
76     assertEquals(20, counter.getCount());
77   }
78 
79   @SuppressWarnings("CheckReturnValue") // calling read() to skip a byte
testMark()80   public void testMark() throws Exception {
81     assertTrue(counter.markSupported());
82     assertEquals(10, counter.read(new byte[10]));
83     assertEquals(10, counter.getCount());
84     counter.mark(5);
85     counter.read();
86     assertEquals(11, counter.getCount());
87     counter.reset();
88     assertEquals(10, counter.getCount());
89     assertEquals(10, counter.skip(100));
90     assertEquals(20, counter.getCount());
91   }
92 
testMarkNotSet()93   public void testMarkNotSet() {
94     IOException expected = assertThrows(IOException.class, () -> counter.reset());
95     assertThat(expected).hasMessageThat().isEqualTo("Mark not set");
96   }
97 
testMarkNotSupported()98   public void testMarkNotSupported() {
99     counter = new CountingInputStream(new UnmarkableInputStream());
100 
101     IOException expected = assertThrows(IOException.class, () -> counter.reset());
102     assertThat(expected).hasMessageThat().isEqualTo("Mark not supported");
103   }
104 
105   private static class UnmarkableInputStream extends InputStream {
106     @Override
read()107     public int read() throws IOException {
108       return 0;
109     }
110 
111     @Override
markSupported()112     public boolean markSupported() {
113       return false;
114     }
115   }
116 }
117