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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2006-2008 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 extends junit.framework.TestCase {
  30. @Test
  31. public void testIndividual() {
  32. try {
  33. final ReverseFileReader reader =
  34. new ReverseFileReader(new File(getClass().getClassLoader().
  35. getResource("com/dmdirc/addons/logging/test1.txt").
  36. toURI()));
  37. assertEquals("Line 7", reader.getNextLine());
  38. assertEquals("Line 6", reader.getNextLine());
  39. assertEquals("Line 5", reader.getNextLine());
  40. assertEquals("Line 4", reader.getNextLine());
  41. assertEquals("Line 3", reader.getNextLine());
  42. assertEquals("Line 2", reader.getNextLine());
  43. assertEquals("Line 1", reader.getNextLine());
  44. reader.close();
  45. } catch (URISyntaxException ex) {
  46. assertFalse(true);
  47. } catch (IOException ex) {
  48. assertFalse(true);
  49. }
  50. }
  51. @Test
  52. public void testStack() {
  53. try {
  54. final ReverseFileReader reader =
  55. new ReverseFileReader(new File(getClass().getClassLoader().
  56. getResource("com/dmdirc/addons/logging/test1.txt").
  57. toURI()));
  58. final Stack<String> lines = reader.getLines(10);
  59. assertEquals(7, lines.size());
  60. assertEquals("Line 1", lines.pop());
  61. assertEquals("Line 2", lines.pop());
  62. assertEquals("Line 3", lines.pop());
  63. assertEquals("Line 4", lines.pop());
  64. assertEquals("Line 5", lines.pop());
  65. assertEquals("Line 6", lines.pop());
  66. assertEquals("Line 7", lines.pop());
  67. reader.close();
  68. } catch (URISyntaxException ex) {
  69. assertFalse(true);
  70. } catch (IOException ex) {
  71. assertFalse(true);
  72. }
  73. }
  74. @Test
  75. public void testSmallStack() {
  76. try {
  77. final ReverseFileReader reader =
  78. new ReverseFileReader(new File(getClass().getClassLoader().
  79. getResource("com/dmdirc/addons/logging/test1.txt").
  80. toURI()));
  81. final Stack<String> lines = reader.getLines(3);
  82. assertEquals(3, lines.size());
  83. assertEquals("Line 5", lines.pop());
  84. assertEquals("Line 6", lines.pop());
  85. assertEquals("Line 7", lines.pop());
  86. reader.close();
  87. } catch (URISyntaxException ex) {
  88. assertFalse(true);
  89. } catch (IOException ex) {
  90. assertFalse(true);
  91. }
  92. }
  93. @Test
  94. public void testReset() {
  95. try {
  96. final ReverseFileReader reader =
  97. new ReverseFileReader(new File(getClass().getClassLoader().
  98. getResource("com/dmdirc/addons/logging/test1.txt").
  99. toURI()));
  100. assertEquals("Line 7", reader.getNextLine());
  101. assertEquals("Line 6", reader.getNextLine());
  102. reader.reset();
  103. assertEquals("Line 7", reader.getNextLine());
  104. assertEquals("Line 6", reader.getNextLine());
  105. assertEquals("Line 5", reader.getNextLine());
  106. reader.close();
  107. } catch (URISyntaxException ex) {
  108. assertFalse(true);
  109. } catch (IOException ex) {
  110. assertFalse(true);
  111. }
  112. }
  113. }