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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.addons.logging;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.net.URISyntaxException;
  26. import java.util.Stack;
  27. import org.junit.Test;
  28. import static org.junit.Assert.*;
  29. public class ReverseFileReaderTest {
  30. @Test
  31. public void testIndividual() throws IOException, URISyntaxException {
  32. final ReverseFileReader reader =
  33. new ReverseFileReader(new File(getClass().getClassLoader().
  34. getResource("com/dmdirc/addons/logging/test1.txt").
  35. toURI()));
  36. assertEquals("Line 7", reader.getNextLine());
  37. assertEquals("Line 6", reader.getNextLine());
  38. assertEquals("Line 5", reader.getNextLine());
  39. assertEquals("Line 4", reader.getNextLine());
  40. assertEquals("Line 3", reader.getNextLine());
  41. assertEquals("Line 2", reader.getNextLine());
  42. assertEquals("Line 1", reader.getNextLine());
  43. reader.close();
  44. }
  45. @Test
  46. public void testCarriageReturn() throws IOException, URISyntaxException {
  47. final ReverseFileReader reader =
  48. new ReverseFileReader(new File(getClass().getClassLoader().
  49. getResource("com/dmdirc/addons/logging/test2.txt").
  50. toURI()));
  51. reader.getNextLine();
  52. assertEquals("Normal line", reader.getNextLine());
  53. reader.close();
  54. }
  55. @Test
  56. public void testLongLine() throws IOException, URISyntaxException {
  57. final ReverseFileReader reader =
  58. new ReverseFileReader(new File(getClass().getClassLoader().
  59. getResource("com/dmdirc/addons/logging/test2.txt").
  60. toURI()));
  61. assertEquals("This is a line that is longer than 50 characters, so " +
  62. "should cause the reader to have to scan back multiple times.",
  63. reader.getNextLine());
  64. reader.close();
  65. }
  66. @Test
  67. public void testStack() throws IOException, URISyntaxException {
  68. final ReverseFileReader reader =
  69. new ReverseFileReader(new File(getClass().getClassLoader().
  70. getResource("com/dmdirc/addons/logging/test1.txt").
  71. toURI()));
  72. final Stack<String> lines = reader.getLines(10);
  73. assertEquals(7, lines.size());
  74. assertEquals("Line 1", lines.pop());
  75. assertEquals("Line 2", lines.pop());
  76. assertEquals("Line 3", lines.pop());
  77. assertEquals("Line 4", lines.pop());
  78. assertEquals("Line 5", lines.pop());
  79. assertEquals("Line 6", lines.pop());
  80. assertEquals("Line 7", lines.pop());
  81. reader.close();
  82. }
  83. @Test
  84. public void testSmallStack() throws IOException, URISyntaxException {
  85. final ReverseFileReader reader =
  86. new ReverseFileReader(new File(getClass().getClassLoader().
  87. getResource("com/dmdirc/addons/logging/test1.txt").
  88. toURI()));
  89. final Stack<String> lines = reader.getLines(3);
  90. assertEquals(3, lines.size());
  91. assertEquals("Line 5", lines.pop());
  92. assertEquals("Line 6", lines.pop());
  93. assertEquals("Line 7", lines.pop());
  94. reader.close();
  95. }
  96. @Test
  97. public void testReset() throws IOException, URISyntaxException {
  98. final ReverseFileReader reader =
  99. new ReverseFileReader(new File(getClass().getClassLoader().
  100. getResource("com/dmdirc/addons/logging/test1.txt").
  101. toURI()));
  102. assertEquals("Line 7", reader.getNextLine());
  103. assertEquals("Line 6", reader.getNextLine());
  104. reader.reset();
  105. assertEquals("Line 7", reader.getNextLine());
  106. assertEquals("Line 6", reader.getNextLine());
  107. assertEquals("Line 5", reader.getNextLine());
  108. reader.close();
  109. }
  110. @Test(expected=IOException.class)
  111. public void testIllegalClose() throws URISyntaxException, IOException {
  112. final ReverseFileReader reader =
  113. new ReverseFileReader(new File(getClass().getClassLoader().
  114. getResource("com/dmdirc/addons/logging/test1.txt").
  115. toURI()));
  116. reader.close();
  117. reader.close();
  118. }
  119. @Test(expected=IOException.class)
  120. public void testIllegalReset() throws URISyntaxException, IOException {
  121. final ReverseFileReader reader =
  122. new ReverseFileReader(new File(getClass().getClassLoader().
  123. getResource("com/dmdirc/addons/logging/test1.txt").
  124. toURI()));
  125. reader.close();
  126. reader.reset();
  127. }
  128. @Test(expected=IOException.class)
  129. public void testIllegalGetNextLine() throws URISyntaxException, IOException {
  130. final ReverseFileReader reader =
  131. new ReverseFileReader(new File(getClass().getClassLoader().
  132. getResource("com/dmdirc/addons/logging/test1.txt").
  133. toURI()));
  134. reader.close();
  135. reader.getNextLine();
  136. }
  137. @Test
  138. public void testSeekLength() throws IOException, URISyntaxException {
  139. final ReverseFileReader reader =
  140. new ReverseFileReader(new File(getClass().getClassLoader().
  141. getResource("com/dmdirc/addons/logging/test1.txt").
  142. toURI()));
  143. reader.setSeekLength((byte) 100);
  144. assertEquals((byte) 100, reader.getSeekLength());
  145. }
  146. }