View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.io.input;
18  
19  
20  import static org.junit.Assert.assertEquals;
21  
22  import java.io.BufferedReader;
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.IOException;
26  import java.io.InputStreamReader;
27  import java.net.URISyntaxException;
28  import java.util.Arrays;
29  import java.util.Collection;
30  import java.util.Stack;
31  
32  import org.junit.After;
33  import org.junit.Test;
34  import org.junit.runner.RunWith;
35  import org.junit.runners.Parameterized;
36  import org.junit.runners.Parameterized.Parameters;
37  
38  /**
39   * Test checks symmetric behaviour with  BufferedReader
40   *
41   */
42  @RunWith(Parameterized.class)
43  public class ReversedLinesFileReaderTestParamFile {
44  
45      @Parameters
46      public static Collection<Object[]> blockSizes() {
47              return Arrays.asList(new Object[][] {
48                      {"test-file-20byteslength.bin", "ISO_8859_1", null},
49                      {"test-file-iso8859-1-shortlines-win-linebr.bin", "ISO_8859_1", null},
50                      {"test-file-iso8859-1.bin", "ISO_8859_1", null},
51                      {"test-file-shiftjis.bin", "Shift_JIS", null},
52                      {"test-file-utf16be.bin", "UTF-16BE", null},
53                      {"test-file-utf16le.bin", "UTF-16LE", null},
54                      {"test-file-utf8-cr-only.bin", "UTF-8", null},
55                      {"test-file-utf8-win-linebr.bin", "UTF-8", null},
56                      {"test-file-utf8-win-linebr.bin", "UTF-8", Integer.valueOf(1)},
57                      {"test-file-utf8-win-linebr.bin", "UTF-8", Integer.valueOf(2)},
58                      {"test-file-utf8-win-linebr.bin", "UTF-8", Integer.valueOf(3)},
59                      {"test-file-utf8-win-linebr.bin", "UTF-8", Integer.valueOf(4)},
60                      {"test-file-utf8.bin", "UTF-8", null},
61              });
62      }
63  
64      private ReversedLinesFileReader reversedLinesFileReader;
65      private BufferedReader bufferedReader;
66  
67      private final String fileName;
68      private final String encoding;
69      private final int buffSize;
70  
71      public ReversedLinesFileReaderTestParamFile(final String fileName, final String encoding, final Integer buffsize) {
72          this.fileName = fileName;
73          this.encoding = encoding;
74          this.buffSize = buffsize == null ? 4096 : buffsize.intValue();
75      }
76  
77      @Test
78      public void testDataIntegrityWithBufferedReader() throws URISyntaxException, IOException {
79          final File testFileIso = new File(this.getClass().getResource("/"+fileName).toURI());
80          reversedLinesFileReader = new ReversedLinesFileReader(testFileIso, buffSize, encoding);
81  
82          final Stack<String> lineStack = new Stack<String>();
83  
84          bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(testFileIso), encoding));
85          String line = null;
86  
87          // read all lines in normal order
88          while((line = bufferedReader.readLine())!=null) {
89              lineStack.push(line);
90          }
91  
92          // read in reverse order and compare with lines from stack
93          while((line = reversedLinesFileReader.readLine())!=null) {
94              final String lineFromBufferedReader = lineStack.pop();
95              assertEquals(lineFromBufferedReader, line);
96          }
97  
98      }
99  
100     @After
101     public void closeReader() {
102         try {
103             bufferedReader.close();
104         } catch(final Exception e) {
105             // ignore
106         }
107         try {
108             reversedLinesFileReader.close();
109         } catch(final Exception e) {
110             // ignore
111         }
112     }
113 
114 
115 
116 }