View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.tools;
12  
13  import java.io.File;
14  import java.io.IOException;
15  import java.nio.charset.StandardCharsets;
16  import java.nio.file.Files;
17  
18  import org.junit.jupiter.api.Assertions;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * The Class BackwardsLineReaderTest.
23   */
24  class BackwardsLineReaderTest {
25  
26    /**
27     * Read lines backwards test.
28     *
29     * @throws IOException Signals that an I/O exception has occurred.
30     */
31    @Test
32    void readLinesBackwardsTest() throws IOException {
33      File file = Files.createTempFile("backwards-reader-", ".log").toFile();
34      Files.writeString(file.toPath(), "line1\nline2\nline3", StandardCharsets.UTF_8);
35  
36      try (BackwardsFileStream stream = new BackwardsFileStream(file)) {
37        BackwardsLineReader reader = new BackwardsLineReader(stream);
38        Assertions.assertEquals("line3", reader.readLine());
39        Assertions.assertEquals("line2", reader.readLine());
40        Assertions.assertEquals("line1", reader.readLine());
41        Assertions.assertNull(reader.readLine());
42        reader.close();
43      } finally {
44        Files.deleteIfExists(file.toPath());
45      }
46    }
47  
48    /**
49     * Read lines backwards with CRLF test.
50     *
51     * @throws IOException Signals that an I/O exception has occurred.
52     */
53    @Test
54    void readLinesBackwardsCrLfTest() throws IOException {
55      File file = Files.createTempFile("backwards-reader-crlf-", ".log").toFile();
56      Files.writeString(file.toPath(), "left\r\nright", StandardCharsets.UTF_8);
57  
58      try (BackwardsFileStream stream = new BackwardsFileStream(file)) {
59        BackwardsLineReader reader = new BackwardsLineReader(stream, StandardCharsets.UTF_8.name());
60        Assertions.assertEquals("right", reader.readLine());
61        Assertions.assertEquals("left", reader.readLine());
62        Assertions.assertNull(reader.readLine());
63        reader.close();
64      } finally {
65        Files.deleteIfExists(file.toPath());
66      }
67    }
68  
69    /**
70     * Backwards file stream from position test.
71     *
72     * @throws IOException Signals that an I/O exception has occurred.
73     */
74    @Test
75    void backwardsFileStreamFromPositionTest() throws IOException {
76      File file = Files.createTempFile("backwards-stream-pos-", ".txt").toFile();
77      Files.writeString(file.toPath(), "abcde", StandardCharsets.UTF_8);
78  
79      try (BackwardsFileStream stream = new BackwardsFileStream(file, 3)) {
80        Assertions.assertEquals('c', stream.read());
81        Assertions.assertEquals('b', stream.read());
82        Assertions.assertEquals('a', stream.read());
83        Assertions.assertEquals(-1, stream.read());
84      } finally {
85        Files.deleteIfExists(file.toPath());
86      }
87    }
88  }