Browse Source

Added a couple of unit tests for the URL Catcher plugin

Added an extra test file + tests for the Logging Plugin's reverse file reader
Issue 1106

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

+ 121
- 79
test/com/dmdirc/addons/logging/ReverseFileReaderTest.java View File

@@ -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
 }

+ 2
- 0
test/com/dmdirc/addons/logging/test2.txt View File

@@ -0,0 +1,2 @@
1
+Normal line
2
+This is a line that is longer than 50 characters, so should cause the reader to have to scan back multiple times.

+ 66
- 0
test/com/dmdirc/addons/urlcatcher/UrlCatcherPluginTest.java View File

@@ -0,0 +1,66 @@
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.urlcatcher;
24
+
25
+import com.dmdirc.actions.CoreActionType;
26
+import com.dmdirc.config.IdentityManager;
27
+import org.junit.Test;
28
+import static org.junit.Assert.*;
29
+
30
+public class UrlCatcherPluginTest extends junit.framework.TestCase {
31
+
32
+    @Test
33
+    public void testURLCounting() {
34
+        IdentityManager.load();
35
+        
36
+        final UrlCatcherPlugin plugin = new UrlCatcherPlugin();
37
+
38
+        plugin.processEvent(CoreActionType.CHANNEL_MESSAGE, null,
39
+                null, "This is a message - http://www.google.com/ foo");
40
+        plugin.processEvent(CoreActionType.CHANNEL_MESSAGE, null,
41
+                null, "This is a message - http://www.google.com/ foo");
42
+        plugin.processEvent(CoreActionType.CHANNEL_MESSAGE, null,
43
+                null, "This is a message - http://www.google.com/ foo");
44
+        
45
+        assertEquals(1, plugin.getURLS().size());
46
+        assertEquals(3, (int) plugin.getURLS().get("http://www.google.com/"));
47
+    }
48
+    
49
+    @Test
50
+    public void testURLCatching() {
51
+        IdentityManager.load();
52
+        
53
+        final UrlCatcherPlugin plugin = new UrlCatcherPlugin();
54
+
55
+        plugin.processEvent(CoreActionType.CHANNEL_MESSAGE, null,
56
+                null, "http://www.google.com/ www.example.com foo://bar.baz");
57
+        plugin.processEvent(CoreActionType.CHANNEL_MESSAGE, null,
58
+                null, "No URLs here, no sir!");        
59
+        
60
+        assertEquals(3, plugin.getURLS().size());
61
+        assertTrue(plugin.getURLS().containsKey("http://www.google.com/"));
62
+        assertTrue(plugin.getURLS().containsKey("www.example.com"));
63
+        assertTrue(plugin.getURLS().containsKey("foo://bar.baz"));
64
+    }    
65
+
66
+}

Loading…
Cancel
Save