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

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