Browse Source

Unit test for ReverseFileReader

git-svn-id: http://svn.dmdirc.com/trunk@3387 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6
Chris Smith 16 years ago
parent
commit
f9bdcbacff

+ 124
- 0
test/com/dmdirc/addons/logging/ReverseFileReaderTest.java View File

@@ -0,0 +1,124 @@
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
+
23
+package com.dmdirc.addons.logging;
24
+
25
+import java.io.File;
26
+import java.io.IOException;
27
+import java.net.URISyntaxException;
28
+
29
+import java.util.Stack;
30
+import org.junit.Test;
31
+import static org.junit.Assert.*;
32
+
33
+public class ReverseFileReaderTest extends junit.framework.TestCase {
34
+
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
+        }
55
+    }
56
+    
57
+    @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
+        }
80
+    }
81
+    
82
+    @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
+        }
101
+    }
102
+    
103
+    @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
+    }    
123
+
124
+}

+ 7
- 0
test/com/dmdirc/addons/logging/test1.txt View File

@@ -0,0 +1,7 @@
1
+Line 1
2
+Line 2
3
+Line 3
4
+Line 4
5
+Line 5
6
+Line 6
7
+Line 7

Loading…
Cancel
Save