View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.utils;
20  
21  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.ByteArrayOutputStream;
26  
27  import org.junit.jupiter.api.Test;
28  
29  class CountingStreamTest {
30  
31      @Test
32      void testInput() throws Exception {
33          // I don't like "test all at once" tests either, but the class
34          // is so trivial
35          final ByteArrayInputStream bis = new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 });
36          try (CountingInputStream i = new CountingInputStream(bis)) {
37              assertEquals(1, i.read());
38              assertEquals(1, i.getBytesRead());
39              byte[] b = new byte[2];
40              i.read(b);
41              assertEquals(3, i.getBytesRead());
42              assertArrayEquals(new byte[] { 2, 3 }, b);
43              b = new byte[3];
44              i.read(b, 1, 1);
45              assertArrayEquals(new byte[] { 0, 4, 0 }, b);
46              assertEquals(4, i.getBytesRead());
47              i.count(-1);
48              assertEquals(4, i.getBytesRead());
49              i.count(-2);
50              assertEquals(2, i.getBytesRead());
51          }
52      }
53  
54      @Test
55      void testOutput() throws Exception {
56          // I don't like "test all at once" tests either, but the class
57          // is so trivial
58          final ByteArrayOutputStream bos = new ByteArrayOutputStream();
59          try (CountingOutputStream o = new CountingOutputStream(bos)) {
60              o.write(1);
61              assertEquals(1, o.getBytesWritten());
62              o.write(new byte[] { 2, 3 });
63              assertEquals(3, o.getBytesWritten());
64              o.write(new byte[] { 2, 3, 4, 5, }, 2, 1);
65              assertEquals(4, o.getBytesWritten());
66              o.count(-1);
67              assertEquals(4, o.getBytesWritten());
68              o.count(-2);
69              assertEquals(2, o.getBytesWritten());
70          }
71          assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bos.toByteArray());
72      }
73  
74  }