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  import static org.apache.commons.io.input.ReversedLinesFileReaderTestParamBlockSize.assertEqualsAndNoLineBreaks;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.io.UnsupportedEncodingException;
27  import java.net.URISyntaxException;
28  import java.nio.charset.StandardCharsets;
29  import java.util.List;
30  
31  import org.apache.commons.io.IOUtils;
32  import org.apache.commons.io.TestResources;
33  import org.junit.jupiter.api.Test;
34  
35  public class ReversedLinesFileReaderTestSimple {
36  
37      @Test
38      public void testFileSizeIsExactMultipleOfBlockSize() throws URISyntaxException, IOException {
39          final int blockSize = 10;
40          final File testFile20Bytes = TestResources.getFile("/test-file-20byteslength.bin");
41          try (ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader(testFile20Bytes, blockSize,
42                  StandardCharsets.ISO_8859_1.name())) {
43              assertEqualsAndNoLineBreaks("987654321", reversedLinesFileReader.readLine());
44              assertEqualsAndNoLineBreaks("123456789", reversedLinesFileReader.readLine());
45          }
46      }
47  
48      @Test
49      public void testLineCount() throws URISyntaxException, IOException {
50          final int blockSize = 10;
51          final File testFile20Bytes = TestResources.getFile("/test-file-20byteslength.bin");
52          try (ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader(testFile20Bytes, blockSize,
53                  StandardCharsets.ISO_8859_1.name())) {
54              assertThrows(IllegalArgumentException.class, () -> reversedLinesFileReader.readLines(-1));
55              assertTrue(reversedLinesFileReader.readLines(0).isEmpty());
56              final List<String> lines = reversedLinesFileReader.readLines(2);
57              assertEqualsAndNoLineBreaks("987654321", lines.get(0));
58              assertEqualsAndNoLineBreaks("123456789", lines.get(1));
59              assertTrue(reversedLinesFileReader.readLines(0).isEmpty());
60              assertTrue(reversedLinesFileReader.readLines(10000).isEmpty());
61          }
62      }
63  
64      @Test
65      public void testToString() throws URISyntaxException, IOException {
66          final int blockSize = 10;
67          final File testFile20Bytes = TestResources.getFile("/test-file-20byteslength.bin");
68          try (ReversedLinesFileReader reversedLinesFileReader = new ReversedLinesFileReader(testFile20Bytes, blockSize,
69                  StandardCharsets.ISO_8859_1.name())) {
70              assertThrows(IllegalArgumentException.class, () -> reversedLinesFileReader.toString(-1));
71              assertTrue(reversedLinesFileReader.readLines(0).isEmpty());
72              final String lines = reversedLinesFileReader.toString(2);
73              assertEquals("123456789" + System.lineSeparator() + "987654321" + System.lineSeparator(), lines);
74              assertTrue(reversedLinesFileReader.toString(0).isEmpty());
75              assertTrue(reversedLinesFileReader.toString(10000).isEmpty());
76          }
77      }
78  
79      @Test
80      public void testUnsupportedEncodingBig5() throws URISyntaxException {
81          final File testFileEncodingBig5 = TestResources.getFile("/test-file-empty.bin");
82          assertThrows(UnsupportedEncodingException.class,
83              () -> new ReversedLinesFileReader(testFileEncodingBig5, IOUtils.DEFAULT_BUFFER_SIZE, "Big5").close());
84      }
85  
86      @Test
87      public void testUnsupportedEncodingUTF16() throws URISyntaxException {
88          final File testFileEmpty = TestResources.getFile("/test-file-empty.bin");
89          assertThrows(UnsupportedEncodingException.class,
90              () -> new ReversedLinesFileReader(testFileEmpty, IOUtils.DEFAULT_BUFFER_SIZE, StandardCharsets.UTF_16.name()).close());
91      }
92  
93  }