Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ReverseFileReaderTest.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2006-2013 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.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 = new ReverseFileReader(
  33. new File(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. reader.close();
  42. }
  43. @Test
  44. public void testCarriageReturn() throws IOException, URISyntaxException {
  45. final ReverseFileReader reader = new ReverseFileReader(
  46. new File(getClass().getResource("test4.txt").toURI()));
  47. reader.getNextLine();
  48. assertEquals("Normal line", reader.getNextLine());
  49. reader.close();
  50. }
  51. @Test
  52. public void testLongLine() throws IOException, URISyntaxException {
  53. final ReverseFileReader reader = new ReverseFileReader(
  54. new File(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. reader.close();
  59. }
  60. @Test
  61. public void testStack() throws IOException, URISyntaxException {
  62. final ReverseFileReader reader = new ReverseFileReader(
  63. new File(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. reader.close();
  74. }
  75. @Test
  76. public void testSmallStack() throws IOException, URISyntaxException {
  77. final ReverseFileReader reader = new ReverseFileReader(
  78. new File(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. reader.close();
  85. }
  86. @Test
  87. public void testReset() throws IOException, URISyntaxException {
  88. final ReverseFileReader reader = new ReverseFileReader(
  89. new File(getClass().getResource("test1.txt").toURI()));
  90. assertEquals("Line 7", reader.getNextLine());
  91. assertEquals("Line 6", reader.getNextLine());
  92. reader.reset();
  93. assertEquals("Line 7", reader.getNextLine());
  94. assertEquals("Line 6", reader.getNextLine());
  95. assertEquals("Line 5", reader.getNextLine());
  96. reader.close();
  97. }
  98. @Test(expected=IOException.class)
  99. public void testIllegalClose() throws URISyntaxException, IOException {
  100. final ReverseFileReader reader = new ReverseFileReader(
  101. new File(getClass().getResource("test1.txt").toURI()));
  102. reader.close();
  103. reader.close();
  104. }
  105. @Test(expected=IOException.class)
  106. public void testIllegalReset() throws URISyntaxException, IOException {
  107. final ReverseFileReader reader = new ReverseFileReader(
  108. new File(getClass().getResource("test1.txt").toURI()));
  109. reader.close();
  110. reader.reset();
  111. }
  112. @Test(expected=IOException.class)
  113. public void testIllegalGetNextLine() throws URISyntaxException, IOException {
  114. final ReverseFileReader reader = new ReverseFileReader(
  115. new File(getClass().getResource("test1.txt").toURI()));
  116. reader.close();
  117. reader.getNextLine();
  118. }
  119. @Test
  120. public void testSeekLength() throws IOException, URISyntaxException {
  121. final ReverseFileReader reader = new ReverseFileReader(
  122. new File(getClass().getResource("test1.txt").toURI()));
  123. reader.setSeekLength((byte) 100);
  124. assertEquals((byte) 100, reader.getSeekLength());
  125. }
  126. }