You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReverseFileReaderTest.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util.io;
  23. import java.io.IOException;
  24. import java.net.URISyntaxException;
  25. import java.nio.file.Paths;
  26. import java.util.Stack;
  27. import org.junit.Test;
  28. import static org.junit.Assert.assertEquals;
  29. public class ReverseFileReaderTest {
  30. @Test
  31. public void testIndividual() throws IOException, URISyntaxException {
  32. try (ReverseFileReader reader = new ReverseFileReader(
  33. Paths.get(getClass().getResource("test1.txt").toURI()))) {
  34. assertEquals("Line 7", reader.getNextLine());
  35. assertEquals("Line 6", reader.getNextLine());
  36. assertEquals("Line 5", reader.getNextLine());
  37. assertEquals("Line 4", reader.getNextLine());
  38. assertEquals("Line 3", reader.getNextLine());
  39. assertEquals("Line 2", reader.getNextLine());
  40. assertEquals("Line 1", reader.getNextLine());
  41. }
  42. }
  43. @Test
  44. public void testCarriageReturn() throws IOException, URISyntaxException {
  45. try (ReverseFileReader reader = new ReverseFileReader(
  46. Paths.get(getClass().getResource("test4.txt").toURI()))) {
  47. reader.getNextLine();
  48. assertEquals("Normal line", reader.getNextLine());
  49. }
  50. }
  51. @Test
  52. public void testLongLine() throws IOException, URISyntaxException {
  53. try (ReverseFileReader reader = new ReverseFileReader(
  54. Paths.get(getClass().getResource("test4.txt").toURI()))) {
  55. assertEquals("This is a line that is longer than 50 characters, so " +
  56. "should cause the reader to have to scan back multiple times.",
  57. reader.getNextLine());
  58. }
  59. }
  60. @Test
  61. public void testStack() throws IOException, URISyntaxException {
  62. try (ReverseFileReader reader = new ReverseFileReader(
  63. Paths.get(getClass().getResource("test1.txt").toURI()))) {
  64. final Stack<String> lines = reader.getLines(10);
  65. assertEquals(7, lines.size());
  66. assertEquals("Line 1", lines.pop());
  67. assertEquals("Line 2", lines.pop());
  68. assertEquals("Line 3", lines.pop());
  69. assertEquals("Line 4", lines.pop());
  70. assertEquals("Line 5", lines.pop());
  71. assertEquals("Line 6", lines.pop());
  72. assertEquals("Line 7", lines.pop());
  73. }
  74. }
  75. @Test
  76. public void testSmallStack() throws IOException, URISyntaxException {
  77. try (ReverseFileReader reader = new ReverseFileReader(
  78. Paths.get(getClass().getResource("test1.txt").toURI()))) {
  79. final Stack<String> lines = reader.getLines(3);
  80. assertEquals(3, lines.size());
  81. assertEquals("Line 5", lines.pop());
  82. assertEquals("Line 6", lines.pop());
  83. assertEquals("Line 7", lines.pop());
  84. }
  85. }
  86. @Test
  87. public void testgetLinesAsString() throws IOException, URISyntaxException {
  88. try (ReverseFileReader reader = new ReverseFileReader(
  89. Paths.get(getClass().getResource("test5.txt").toURI()))) {
  90. final String lines = reader.getLinesAsString(3);
  91. assertEquals("Line 1\nLine 2\nLine 3\n", lines);
  92. }
  93. }
  94. @Test
  95. public void testgetLinesAsStringLeadingNewline() throws IOException, URISyntaxException {
  96. try (ReverseFileReader reader = new ReverseFileReader(
  97. Paths.get(getClass().getResource("test5.txt").toURI()))) {
  98. final String lines = reader.getLinesAsString(4);
  99. assertEquals("Line 1\nLine 2\nLine 3\n", lines);
  100. }
  101. }
  102. @Test
  103. public void testIllegalGetLinesAsString() throws IOException, URISyntaxException {
  104. final ReverseFileReader reader = new ReverseFileReader(
  105. Paths.get((getClass().getResource("test5.txt").toURI())));
  106. reader.close();
  107. final String lines = reader.getLinesAsString(4);
  108. assertEquals("", lines);
  109. }
  110. @Test
  111. public void testReset() throws IOException, URISyntaxException {
  112. try (ReverseFileReader reader = new ReverseFileReader(
  113. Paths.get(getClass().getResource("test1.txt").toURI()))) {
  114. assertEquals("Line 7", reader.getNextLine());
  115. assertEquals("Line 6", reader.getNextLine());
  116. reader.reset();
  117. assertEquals("Line 7", reader.getNextLine());
  118. assertEquals("Line 6", reader.getNextLine());
  119. assertEquals("Line 5", reader.getNextLine());
  120. }
  121. }
  122. public void testCloseIsIdempotent() throws URISyntaxException, IOException {
  123. final ReverseFileReader reader = new ReverseFileReader(
  124. Paths.get(getClass().getResource("test1.txt").toURI()));
  125. reader.close();
  126. reader.close();
  127. }
  128. @Test(expected=IOException.class)
  129. public void testIllegalReset() throws URISyntaxException, IOException {
  130. final ReverseFileReader reader = new ReverseFileReader(
  131. Paths.get(getClass().getResource("test1.txt").toURI()));
  132. reader.close();
  133. reader.reset();
  134. }
  135. @Test(expected=IOException.class)
  136. public void testIllegalGetNextLine() throws URISyntaxException, IOException {
  137. final ReverseFileReader reader = new ReverseFileReader(
  138. Paths.get(getClass().getResource("test1.txt").toURI()));
  139. reader.close();
  140. reader.getNextLine();
  141. }
  142. @Test
  143. public void testSeekLength() throws IOException, URISyntaxException {
  144. try (ReverseFileReader reader = new ReverseFileReader(
  145. Paths.get(getClass().getResource("test1.txt").toURI()))) {
  146. reader.setSeekLength((byte) 100);
  147. assertEquals((byte) 100, reader.getSeekLength());
  148. }
  149. }
  150. }