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  @RunWith(Parameterized.class)
42  public class ReversedLinesFileReaderTestParamFile {
43  
44      @Parameters(name = "{0}, charset={1}")
45      public static Collection<Object[]> blockSizes() {
46          return Arrays.asList(new Object[][]{
47                  {"test-file-20byteslength.bin", "ISO_8859_1", null},
48                  {"test-file-iso8859-1-shortlines-win-linebr.bin", "ISO_8859_1", null},
49                  {"test-file-iso8859-1.bin", "ISO_8859_1", null},
50                  {"test-file-shiftjis.bin", "Shift_JIS", null},
51                  {"test-file-utf16be.bin", "UTF-16BE", null},
52                  {"test-file-utf16le.bin", "UTF-16LE", null},
53                  {"test-file-utf8-cr-only.bin", "UTF-8", null},
54                  {"test-file-utf8-win-linebr.bin", "UTF-8", null},
55                  {"test-file-utf8-win-linebr.bin", "UTF-8", 1},
56                  {"test-file-utf8-win-linebr.bin", "UTF-8", 2},
57                  {"test-file-utf8-win-linebr.bin", "UTF-8", 3},
58                  {"test-file-utf8-win-linebr.bin", "UTF-8", 4},
59                  {"test-file-utf8.bin", "UTF-8", null},
60                  {"test-file-windows-31j.bin", "windows-31j", null},
61                  {"test-file-gbk.bin", "gbk", null},
62                  {"test-file-x-windows-949.bin", "x-windows-949", null},
63                  {"test-file-x-windows-950.bin", "x-windows-950", null},
64          });
65      }
66  
67      private ReversedLinesFileReader reversedLinesFileReader;
68      private BufferedReader bufferedReader;
69  
70      private final String fileName;
71      private final String encoding;
72      private final int buffSize;
73  
74      public ReversedLinesFileReaderTestParamFile(final String fileName, final String encoding, final Integer buffsize) {
75          this.fileName = fileName;
76          this.encoding = encoding;
77          this.buffSize = buffsize == null ? 4096 : buffsize;
78      }
79  
80      @Test
81      public void testDataIntegrityWithBufferedReader() throws URISyntaxException, IOException {
82          final File testFileIso = new File(this.getClass().getResource("/" + fileName).toURI());
83          reversedLinesFileReader = new ReversedLinesFileReader(testFileIso, buffSize, encoding);
84  
85          final Stack<String> lineStack = new Stack<String>();
86  
87          bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(testFileIso), encoding));
88          String line = null;
89  
90          // read all lines in normal order
91          while ((line = bufferedReader.readLine()) != null) {
92              lineStack.push(line);
93          }
94  
95          // read in reverse order and compare with lines from stack
96          while ((line = reversedLinesFileReader.readLine()) != null) {
97              final String lineFromBufferedReader = lineStack.pop();
98              assertEquals(lineFromBufferedReader, line);
99          }
100 
101     }
102 
103     @After
104     public void closeReader() {
105         try {
106             bufferedReader.close();
107         } catch (final Exception e) {
108             // ignore
109         }
110         try {
111             reversedLinesFileReader.close();
112         } catch (final Exception e) {
113             // ignore
114         }
115     }
116 
117 
118 }