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