Преглед изворни кода

Added new IgnoreList class

Tests now work with stupid versions of JUnit, like 3.x

git-svn-id: http://svn.dmdirc.com/trunk@2674 00569f92-eb28-0410-84fd-f71c24880f
tags/0.5.5
Chris Smith пре 16 година
родитељ
комит
847ac86f84

+ 149
- 0
src/com/dmdirc/IgnoreList.java Прегледај датотеку

@@ -0,0 +1,149 @@
1
+/*
2
+ * Copyright (c) 2006-2007 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;
24
+
25
+import com.dmdirc.parser.RegexStringList;
26
+import java.util.ArrayList;
27
+import java.util.List;
28
+
29
+/**
30
+ * Wraps around a RegexStringList to allow "simple" expressions to be used
31
+ * instead of more complex regular expressions.
32
+ * 
33
+ * @author chris
34
+ */
35
+public class IgnoreList extends RegexStringList {
36
+    
37
+    /**
38
+     * Retrieves a list of regular expressions in this ignore list.
39
+     * 
40
+     * @return All expressions in this ignore list
41
+     */
42
+    public List<String> getRegexList() {
43
+        return new ArrayList<String>(ignoreInfo);
44
+    }
45
+    
46
+    /**
47
+     * Retrieves a list of simple expressions in this ignore list.
48
+     * 
49
+     * @return All expressions in this ignore list, converted to simple expressions
50
+     * @throws UnsupportedOperationException if an expression can't be converted
51
+     */
52
+    public List<String> getSimpleList() throws UnsupportedOperationException {
53
+        final List<String> res = new ArrayList<String>();
54
+        
55
+        for (String regex : ignoreInfo) {
56
+            res.add(regexToSimple(regex));
57
+        }
58
+        
59
+        return res;
60
+    }    
61
+    
62
+    /**
63
+     * Converts a regular expression into a simple expression.
64
+     * 
65
+     * @param regex The regular expression to be converted
66
+     * @return A simple expression corresponding to the regex
67
+     * @throws UnsupportedOperationException if the regex cannot be converted
68
+     */
69
+    protected String regexToSimple(final String regex) throws UnsupportedOperationException {
70
+        final StringBuilder res = new StringBuilder(regex.length());
71
+        boolean escaped = false;
72
+        boolean inchar = false;
73
+        
74
+        for (char part : regex.toCharArray()) {
75
+            if (inchar) {
76
+                inchar = false;
77
+                
78
+                if (part == '*') {
79
+                    res.append(part);
80
+                    continue;
81
+                } else {
82
+                    res.append('?');
83
+                }
84
+            }
85
+            
86
+            if (escaped) {
87
+                if (part == '?' || part == '*') {
88
+                    throw new UnsupportedOperationException("Cannot convert to" +
89
+                            " simple expression: ? or * is escaped.");
90
+                }
91
+                
92
+                res.append(part);
93
+                escaped = false;
94
+            }  else if (part == '\\') {
95
+                escaped = true;
96
+            } else if (part == '.') {
97
+                inchar = true;
98
+            } else if (part == '.' || part == '^' || part == '$' || part == '['
99
+            || part == ']' || part == '\\' || part == '(' || part == ')'
100
+            || part == '{' || part == '}' || part == '|' || part == '+') {
101
+                throw new UnsupportedOperationException("Cannot convert to"
102
+                        + " simple expression: unescaped special char: " + part);
103
+            } else {
104
+                res.append(part);
105
+            }
106
+        }
107
+        
108
+        if (escaped) {
109
+            throw new UnsupportedOperationException("Cannot convert to "
110
+                    + "simple expression: trailing backslash");
111
+        } else if (inchar) {
112
+            res.append('?');
113
+        }
114
+        
115
+        return res.toString();
116
+    }
117
+    
118
+    /**
119
+     * Converts a simple expression to a regular expression.
120
+     * 
121
+     * @param regex The simple expression to be converted
122
+     * @return A corresponding regular expression
123
+     */
124
+    @SuppressWarnings("fallthrough")
125
+    protected String simpleToRegex(final String regex) {
126
+        final StringBuilder res = new StringBuilder(regex.length());
127
+        
128
+        for (char part : regex.toCharArray()) {
129
+            switch (part) {
130
+            case '.': case '^': case '$': case '[': case ']': case '\\':
131
+            case '(': case ')': case '{': case '}': case '|': case '+':
132
+                res.append('\\');
133
+                res.append(part);
134
+                break;
135
+            case '?':
136
+                res.append('.');
137
+                break;
138
+            case '*':
139
+                res.append('.');                
140
+            default:
141
+                res.append(part);
142
+                break;
143
+            }
144
+        }
145
+        
146
+        return res.toString();
147
+    }
148
+
149
+}

+ 7
- 0
src/com/dmdirc/parser/IRCParser.java Прегледај датотеку

@@ -389,6 +389,13 @@ public final class IRCParser implements Runnable {
389 389
 	 * @return a reference to the ignorelist
390 390
 	 */
391 391
 	public RegexStringList getIgnoreList() { return myIgnoreList; }
392
+
393
+	/**
394
+	 * Replaces the current ignorelist with a new one.
395
+	 *
396
+	 * @param ignoreList Replacement ignorelist
397
+	 */
398
+	public void setIgnoreList(final RegexStringList ignoreList) { myIgnoreList = ignoreList; }
392 399
 	
393 400
 	//---------------------------------------------------------------------------
394 401
 	// Start Callbacks

+ 2
- 1
src/com/dmdirc/parser/RegexStringList.java Прегледај датотеку

@@ -25,6 +25,7 @@
25 25
 package com.dmdirc.parser;
26 26
 
27 27
 import java.util.ArrayList;
28
+import java.util.List;
28 29
 
29 30
 /**
30 31
  * IRC Parser Ignore list.
@@ -35,7 +36,7 @@ import java.util.ArrayList;
35 36
 public class RegexStringList {
36 37
 	
37 38
 	/** Arraylist storing ignore patterns */
38
-	private final ArrayList<String> ignoreInfo = new ArrayList<String>();
39
+	protected final List<String> ignoreInfo = new ArrayList<String>();
39 40
 	
40 41
 	/**
41 42
 	 * Add a new ignore pattern to the ignore list.

+ 56
- 0
test/com/dmdirc/IgnoreListTest.java Прегледај датотеку

@@ -0,0 +1,56 @@
1
+/*
2
+ * Copyright (c) 2006-2007 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;
24
+
25
+import org.junit.Test;
26
+import static org.junit.Assert.*;
27
+
28
+
29
+public class IgnoreListTest extends junit.framework.TestCase {
30
+    
31
+    private final String[][] tests = {
32
+        {"a@b.c", "a@b\\.c"},
33
+        {"*chris*", ".*chris.*"},
34
+        {"c???s", "c...s"},
35
+        {"c*?*", "c.*..*"},
36
+    };  
37
+    
38
+    private final IgnoreList il = new IgnoreList();
39
+
40
+    @Test
41
+    public void testToRegex() {
42
+        for (String[] test : tests) {
43
+            final String convert1 = il.simpleToRegex(test[0]);
44
+            assertEquals(test[1], convert1);
45
+        }
46
+    }
47
+    
48
+    @Test
49
+    public void testToSimple() {
50
+        for (String[] test : tests) {
51
+            final String convert2 = il.regexToSimple(test[1]);
52
+            assertEquals(test[0], convert2);
53
+        }
54
+    }    
55
+    
56
+}

+ 1
- 1
test/com/dmdirc/ServerTest.java Прегледај датотеку

@@ -28,7 +28,7 @@ import static org.junit.Assert.*;
28 28
 public class ServerTest extends junit.framework.TestCase {
29 29
 
30 30
     @Test
31
-    public void getNetworkFromServerName() {
31
+    public void testGetNetworkFromServerName() {
32 32
         final String[][] tests = {
33 33
             {"foo.com", "foo.com"},
34 34
             {"bar.foo.com", "foo.com"},

+ 3
- 2
test/com/dmdirc/actions/ActionModelTest.java Прегледај датотеку

@@ -23,6 +23,7 @@
23 23
 package com.dmdirc.actions;
24 24
 
25 25
 import java.util.ArrayList;
26
+import java.util.Arrays;
26 27
 import java.util.List;
27 28
 import org.junit.Test;
28 29
 import static org.junit.Assert.*;
@@ -56,7 +57,7 @@ public class ActionModelTest extends junit.framework.TestCase {
56 57
 
57 58
         model.setTriggers(triggers);
58 59
 
59
-        assertEquals(triggers, model.getTriggers());
60
+        assertEquals(Arrays.asList(triggers), Arrays.asList(model.getTriggers()));
60 61
     }
61 62
 
62 63
     @Test
@@ -84,7 +85,7 @@ public class ActionModelTest extends junit.framework.TestCase {
84 85
 
85 86
         model.setResponse(newResponse);
86 87
 
87
-        assertEquals(newResponse, model.getResponse());
88
+        assertEquals(Arrays.asList(newResponse), Arrays.asList(model.getResponse()));
88 89
     }
89 90
 
90 91
     @Test

+ 5
- 5
test/com/dmdirc/actions/ConditionTreeTest.java Прегледај датотеку

@@ -28,7 +28,7 @@ import static org.junit.Assert.*;
28 28
 public class ConditionTreeTest extends junit.framework.TestCase {
29 29
 
30 30
     @Test
31
-    public void parseString() {
31
+    public void testParseString() {
32 32
         String[][] testCases = {
33 33
             {"1", "1"},
34 34
             {"50", "50"},
@@ -62,7 +62,7 @@ public class ConditionTreeTest extends junit.framework.TestCase {
62 62
     }
63 63
     
64 64
     @Test
65
-    public void evaluate() {
65
+    public void testEvaluate() {
66 66
         System.out.println();
67 67
         
68 68
         final String target = "((0&1&2)|3)&(!4)";
@@ -116,7 +116,7 @@ public class ConditionTreeTest extends junit.framework.TestCase {
116 116
     }
117 117
     
118 118
     @Test
119
-    public void getNumArgs() {
119
+    public void testGetNumArgs() {
120 120
         final String target = "((0&1&2)|3)&(!4)";
121 121
         final ConditionTree tree = ConditionTree.parseString(target);
122 122
         assertNotNull(tree);        
@@ -125,7 +125,7 @@ public class ConditionTreeTest extends junit.framework.TestCase {
125 125
     }
126 126
     
127 127
     @Test
128
-    public void createConjunction() {
128
+    public void testCreateConjunction() {
129 129
         final String expected = "(((0&1)&2)&3)";
130 130
         final ConditionTree tree = ConditionTree.createConjunction(4);
131 131
         
@@ -134,7 +134,7 @@ public class ConditionTreeTest extends junit.framework.TestCase {
134 134
     }
135 135
     
136 136
     @Test
137
-    public void createDisjunction() {
137
+    public void testCreateDisjunction() {
138 138
         final String expected = "(((0|1)|2)|3)";
139 139
         final ConditionTree tree = ConditionTree.createDisjunction(4);
140 140
         

Loading…
Откажи
Сачувај