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