123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- /*
- * Copyright (c) 2006-2014 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.util.io;
-
- import java.io.IOException;
- import java.net.URISyntaxException;
- import java.nio.file.Paths;
- import java.util.Stack;
-
- import org.junit.Test;
-
- import static org.junit.Assert.assertEquals;
-
- public class ReverseFileReaderTest {
-
- @Test
- public void testIndividual() throws IOException, URISyntaxException {
- try (ReverseFileReader reader = new ReverseFileReader(
- Paths.get(getClass().getResource("test1.txt").toURI()))) {
- assertEquals("Line 7", reader.getNextLine());
- assertEquals("Line 6", reader.getNextLine());
- assertEquals("Line 5", reader.getNextLine());
- assertEquals("Line 4", reader.getNextLine());
- assertEquals("Line 3", reader.getNextLine());
- assertEquals("Line 2", reader.getNextLine());
- assertEquals("Line 1", reader.getNextLine());
- }
- }
-
- @Test
- public void testCarriageReturn() throws IOException, URISyntaxException {
- try (ReverseFileReader reader = new ReverseFileReader(
- Paths.get(getClass().getResource("test4.txt").toURI()))) {
- reader.getNextLine();
- assertEquals("Normal line", reader.getNextLine());
- }
- }
-
- @Test
- public void testLongLine() throws IOException, URISyntaxException {
- try (ReverseFileReader reader = new ReverseFileReader(
- Paths.get(getClass().getResource("test4.txt").toURI()))) {
- assertEquals("This is a line that is longer than 50 characters, so " +
- "should cause the reader to have to scan back multiple times.",
- reader.getNextLine());
- }
- }
-
- @Test
- public void testStack() throws IOException, URISyntaxException {
- try (ReverseFileReader reader = new ReverseFileReader(
- Paths.get(getClass().getResource("test1.txt").toURI()))) {
- final Stack<String> lines = reader.getLines(10);
-
- assertEquals(7, lines.size());
- assertEquals("Line 1", lines.pop());
- assertEquals("Line 2", lines.pop());
- assertEquals("Line 3", lines.pop());
- assertEquals("Line 4", lines.pop());
- assertEquals("Line 5", lines.pop());
- assertEquals("Line 6", lines.pop());
- assertEquals("Line 7", lines.pop());
- }
- }
-
- @Test
- public void testSmallStack() throws IOException, URISyntaxException {
- try (ReverseFileReader reader = new ReverseFileReader(
- Paths.get(getClass().getResource("test1.txt").toURI()))) {
- final Stack<String> lines = reader.getLines(3);
- assertEquals(3, lines.size());
- assertEquals("Line 5", lines.pop());
- assertEquals("Line 6", lines.pop());
- assertEquals("Line 7", lines.pop());
- }
- }
-
- @Test
- public void testgetLinesAsString() throws IOException, URISyntaxException {
- try (ReverseFileReader reader = new ReverseFileReader(
- Paths.get(getClass().getResource("test5.txt").toURI()))) {
- final String lines = reader.getLinesAsString(3);
- assertEquals("Line 1\nLine 2\nLine 3\n", lines);
- }
- }
-
- @Test
- public void testgetLinesAsStringLeadingNewline() throws IOException, URISyntaxException {
- try (ReverseFileReader reader = new ReverseFileReader(
- Paths.get(getClass().getResource("test5.txt").toURI()))) {
- final String lines = reader.getLinesAsString(4);
- assertEquals("Line 1\nLine 2\nLine 3\n", lines);
- }
- }
-
- @Test
- public void testIllegalGetLinesAsString() throws IOException, URISyntaxException {
- final ReverseFileReader reader = new ReverseFileReader(
- Paths.get((getClass().getResource("test5.txt").toURI())));
- reader.close();
- final String lines = reader.getLinesAsString(4);
- assertEquals("", lines);
- }
-
- @Test
- public void testReset() throws IOException, URISyntaxException {
- try (ReverseFileReader reader = new ReverseFileReader(
- Paths.get(getClass().getResource("test1.txt").toURI()))) {
- assertEquals("Line 7", reader.getNextLine());
- assertEquals("Line 6", reader.getNextLine());
- reader.reset();
- assertEquals("Line 7", reader.getNextLine());
- assertEquals("Line 6", reader.getNextLine());
- assertEquals("Line 5", reader.getNextLine());
- }
- }
-
- public void testCloseIsIdempotent() throws URISyntaxException, IOException {
- final ReverseFileReader reader = new ReverseFileReader(
- Paths.get(getClass().getResource("test1.txt").toURI()));
- reader.close();
- reader.close();
- }
-
- @Test(expected=IOException.class)
- public void testIllegalReset() throws URISyntaxException, IOException {
- final ReverseFileReader reader = new ReverseFileReader(
- Paths.get(getClass().getResource("test1.txt").toURI()));
- reader.close();
- reader.reset();
- }
-
- @Test(expected=IOException.class)
- public void testIllegalGetNextLine() throws URISyntaxException, IOException {
- final ReverseFileReader reader = new ReverseFileReader(
- Paths.get(getClass().getResource("test1.txt").toURI()));
- reader.close();
- reader.getNextLine();
- }
-
- @Test
- public void testSeekLength() throws IOException, URISyntaxException {
- try (ReverseFileReader reader = new ReverseFileReader(
- Paths.get(getClass().getResource("test1.txt").toURI()))) {
- reader.setSeekLength((byte) 100);
- assertEquals((byte) 100, reader.getSeekLength());
- }
- }
-
- }
|