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