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   *   http://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.maven.shared.filtering;
20  
21  import java.io.BufferedReader;
22  import java.io.IOException;
23  import java.io.Reader;
24  import java.io.StringReader;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import static org.junit.jupiter.api.Assertions.assertEquals;
29  
30  class BoundedReaderTest {
31  
32      private final Reader sr = new BufferedReader(new StringReader("01234567890"));
33  
34      @Test
35      void readTillEnd() throws IOException {
36          try (BoundedReader mr = new BoundedReader(sr, 3)) {
37              mr.mark(3);
38              mr.read();
39              mr.read();
40              mr.read();
41              assertEquals(-1, mr.read());
42          }
43      }
44  
45      @Test
46      void readMulti() throws IOException {
47          char[] cbuf = new char[4];
48          for (int i = 0; i < cbuf.length; i++) {
49              cbuf[i] = 'X';
50          }
51  
52          try (BoundedReader mr = new BoundedReader(sr, 3)) {
53              final int read = mr.read(cbuf, 0, 4);
54              assertEquals(3, read);
55          }
56  
57          assertEquals('0', cbuf[0]);
58          assertEquals('1', cbuf[1]);
59          assertEquals('2', cbuf[2]);
60          assertEquals('X', cbuf[3]);
61      }
62  
63      @Test
64      void readMultiWithOffset() throws IOException {
65  
66          char[] cbuf = new char[4];
67          for (int i = 0; i < cbuf.length; i++) {
68              cbuf[i] = 'X';
69          }
70  
71          try (BoundedReader mr = new BoundedReader(sr, 3)) {
72              final int read = mr.read(cbuf, 1, 2);
73              assertEquals(2, read);
74          }
75  
76          assertEquals('X', cbuf[0]);
77          assertEquals('0', cbuf[1]);
78          assertEquals('1', cbuf[2]);
79          assertEquals('X', cbuf[3]);
80      }
81  
82      @Test
83      void resetWorks() throws IOException {
84          try (BoundedReader mr = new BoundedReader(sr, 3)) {
85              mr.read();
86              mr.read();
87              mr.read();
88              mr.reset();
89              mr.read();
90              mr.read();
91              mr.read();
92              assertEquals(-1, mr.read());
93          }
94      }
95  
96      @Test
97      void skipTest() throws IOException {
98          try (BoundedReader mr = new BoundedReader(sr, 3)) {
99              mr.skip(2);
100             mr.read();
101             assertEquals(-1, mr.read());
102         }
103     }
104 }