Browse Source

Fix some minor bugs with ActionCondition's equals/hashcode methods

Add three new unit tests

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

+ 7
- 5
src/com/dmdirc/actions/ActionCondition.java View File

@@ -41,10 +41,10 @@ public class ActionCondition {
41 41
     private ActionComparison comparison;
42 42
     
43 43
     /** The target of the comparison for this condition. */
44
-    private String target;
44
+    private String target = "";
45 45
     
46 46
     /** The source target for this comparison. */
47
-    private String starget;
47
+    private String starget = "";
48 48
     
49 49
     /**
50 50
      * Creates a new instance of ActionCondition that compares the output of
@@ -193,7 +193,7 @@ public class ActionCondition {
193 193
     @Override
194 194
     public String toString() {
195 195
         return "[ arg=" + arg + ", component=" + component + ", comparison=" 
196
-                + comparison + ", target=" + target + "]";
196
+                + comparison + ", target=" + target + ", starget=" + starget + " ]";
197 197
     }
198 198
 
199 199
     /** {@inheritDoc} */
@@ -206,13 +206,15 @@ public class ActionCondition {
206 206
         final ActionCondition o = (ActionCondition) obj;
207 207
         
208 208
         return arg == o.getArg() && component == o.getComponent()
209
-                && comparison == o.getComparison() && target.equals(o.getTarget());
209
+                && comparison == o.getComparison() && target.equals(o.getTarget())
210
+                && starget.equals(o.getStarget());
210 211
     }
211 212
     
212 213
     /** {@inheritDoc} */
213 214
     @Override
214 215
     public int hashCode() {
215
-        return arg & component.hashCode() & comparison.hashCode() & target.hashCode();
216
+        return arg + 100 * (arg == -1 ? starget.hashCode() : component.hashCode())
217
+                + 10000 * comparison.hashCode() + 100000 * target.hashCode();
216 218
     }
217 219
     
218 220
 }

+ 93
- 0
test/com/dmdirc/actions/ActionComponentChainTest.java View File

@@ -0,0 +1,93 @@
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.actions;
24
+
25
+import org.junit.Before;
26
+import org.junit.Test;
27
+import static org.junit.Assert.*;
28
+
29
+public class ActionComponentChainTest extends junit.framework.TestCase {
30
+
31
+    @Before
32
+    public void setUp() throws Exception {
33
+        ActionManager.init();
34
+    }
35
+
36
+    @Test
37
+    public void testSingle() {
38
+        final ActionComponentChain chain = new ActionComponentChain(String.class, "STRING_STRING");
39
+        assertEquals(chain.get("foo bar baz"), "foo bar baz");
40
+        assertEquals("STRING_STRING", chain.toString());
41
+    }
42
+
43
+    @Test
44
+    public void testDouble() {
45
+        final ActionComponentChain chain = new ActionComponentChain(String.class,
46
+                "STRING_STRING.STRING_STRING");
47
+        assertEquals(chain.get("foo bar baz"), "foo bar baz");
48
+        assertEquals("STRING_STRING.STRING_STRING", chain.toString());
49
+    }
50
+
51
+    @Test
52
+    public void testInvalidName() {
53
+        boolean iaed = false;
54
+
55
+        try {
56
+            final ActionComponentChain chain = new ActionComponentChain(String.class,
57
+                    "STRONG_STRING");
58
+        } catch (IllegalArgumentException iae) {
59
+            iaed = true;
60
+        }
61
+
62
+        assertTrue("Should throw an IAE", iaed);
63
+    }
64
+
65
+    @Test
66
+    public void testInvalidType() {
67
+        boolean iaed = false;
68
+
69
+        try {
70
+            final ActionComponentChain chain = new ActionComponentChain(String.class,
71
+                    "USER_MODES.STRING_STRING");
72
+        } catch (IllegalArgumentException iae) {
73
+            iaed = true;
74
+        }
75
+
76
+        assertTrue("Should throw an IAE", iaed);
77
+    }
78
+
79
+    @Test
80
+    public void testInvalidLink() {
81
+        boolean iaed = false;
82
+
83
+        try {
84
+            final ActionComponentChain chain = new ActionComponentChain(String.class,
85
+                    "STRING_STRING.USER_MODES");
86
+        } catch (IllegalArgumentException iae) {
87
+            iaed = true;
88
+        }
89
+
90
+        assertTrue("Should throw an IAE", iaed);
91
+    }
92
+
93
+}

+ 136
- 0
test/com/dmdirc/actions/ActionConditionTest.java View File

@@ -0,0 +1,136 @@
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.actions;
24
+
25
+import org.junit.Before;
26
+import org.junit.Test;
27
+import static org.junit.Assert.*;
28
+
29
+public class ActionConditionTest extends junit.framework.TestCase {
30
+
31
+    @Before
32
+    public void setUp() throws Exception {
33
+        ActionManager.init();
34
+    }
35
+
36
+    @Test
37
+    public void testConstructor1() {
38
+        final ActionCondition ac = new ActionCondition(1, CoreActionComponent.STRING_STRING,
39
+            CoreActionComparison.STRING_STARTSWITH, "foo");
40
+        assertEquals(1, ac.getArg());
41
+        assertEquals(CoreActionComponent.STRING_STRING, ac.getComponent());
42
+        assertEquals(CoreActionComparison.STRING_STARTSWITH, ac.getComparison());
43
+        assertEquals("foo", ac.getTarget());
44
+    }
45
+
46
+    @Test
47
+    public void testConstructor2() {
48
+        final ActionCondition ac = new ActionCondition("foobarbaz",
49
+            CoreActionComparison.STRING_STARTSWITH, "foo");
50
+        assertEquals("foobarbaz", ac.getStarget());
51
+        assertEquals(CoreActionComparison.STRING_STARTSWITH, ac.getComparison());
52
+        assertEquals("foo", ac.getTarget());
53
+    }
54
+
55
+    @Test
56
+    public void testTest1() {
57
+        final ActionCondition ac = new ActionCondition(0, CoreActionComponent.STRING_STRING,
58
+            CoreActionComparison.STRING_STARTSWITH, "foo");
59
+        assertTrue(ac.test(new ActionSubstitutor(CoreActionType.CLIENT_CLOSED), "foo bar"));
60
+    }
61
+
62
+    @Test
63
+    public void testTest2() {
64
+        final ActionCondition ac = new ActionCondition("foobarbaz",
65
+            CoreActionComparison.STRING_STARTSWITH, "foo");
66
+        assertTrue(ac.test(new ActionSubstitutor(CoreActionType.CLIENT_CLOSED)));
67
+    }
68
+
69
+    @Test
70
+    public void testSetters() {
71
+        final ActionCondition ac = new ActionCondition("foobarbaz",
72
+            CoreActionComparison.STRING_STARTSWITH, "foo");
73
+        assertEquals("foo", ac.getTarget());
74
+        ac.setTarget("bar");
75
+        assertEquals("bar", ac.getTarget());
76
+
77
+        assertEquals("foobarbaz", ac.getStarget());
78
+        ac.setStarget("bar");
79
+        assertEquals("bar", ac.getStarget());
80
+
81
+        assertEquals(CoreActionComparison.STRING_STARTSWITH, ac.getComparison());
82
+        ac.setComparison(CoreActionComparison.STRING_EQUALS);
83
+        assertEquals(CoreActionComparison.STRING_EQUALS, ac.getComparison());
84
+
85
+        ac.setComponent(CoreActionComponent.STRING_STRING);
86
+        assertEquals(CoreActionComponent.STRING_STRING, ac.getComponent());
87
+
88
+        ac.setArg(0);
89
+        assertEquals(0, ac.getArg());
90
+    }
91
+
92
+    @Test
93
+    public void testEquals() {
94
+        final ActionCondition ac1 = new ActionCondition("foobarbaz",
95
+            CoreActionComparison.STRING_STARTSWITH, "foo");
96
+        final ActionCondition ac2 = new ActionCondition("foobarbaz",
97
+            CoreActionComparison.STRING_STARTSWITH, "foo");
98
+
99
+        assertTrue(ac1.equals(ac2));
100
+        assertTrue(ac2.equals(ac1));
101
+        assertFalse(ac1.equals(null));
102
+        assertFalse(ac1.equals("foo"));
103
+        assertEquals(ac1.hashCode(), ac2.hashCode());
104
+        
105
+        ac2.setStarget("bar");
106
+        assertFalse(ac2.equals(ac1));
107
+        assertFalse(ac1.equals(ac2));
108
+        
109
+        ac2.setStarget("foobarbaz");
110
+        ac2.setComponent(CoreActionComponent.STRING_STRING);
111
+        ac2.setArg(1);
112
+        assertFalse(ac2.equals(ac1));
113
+        assertFalse(ac1.equals(ac2));
114
+
115
+        ac1.setComponent(CoreActionComponent.STRING_STRING);
116
+        ac1.setArg(1);
117
+        assertTrue(ac1.equals(ac2));
118
+        assertTrue(ac2.equals(ac1));
119
+        assertEquals(ac1.hashCode(), ac2.hashCode());
120
+
121
+        ac1.setComponent(CoreActionComponent.STRING_LENGTH);
122
+        assertFalse(ac2.equals(ac1));
123
+        assertFalse(ac1.equals(ac2));
124
+
125
+        ac1.setComponent(CoreActionComponent.STRING_STRING);
126
+        ac1.setComparison(CoreActionComparison.STRING_NCONTAINS);
127
+        assertFalse(ac2.equals(ac1));
128
+        assertFalse(ac1.equals(ac2));
129
+
130
+        ac1.setComparison(CoreActionComparison.STRING_STARTSWITH);
131
+        ac1.setTarget("flub");
132
+        assertFalse(ac2.equals(ac1));
133
+        assertFalse(ac1.equals(ac2));
134
+    }
135
+
136
+}

+ 92
- 0
test/com/dmdirc/actions/ActionGroupTest.java View File

@@ -0,0 +1,92 @@
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.actions;
23
+
24
+import java.util.ArrayList;
25
+import java.util.List;
26
+import org.junit.Test;
27
+import static org.junit.Assert.*;
28
+
29
+public class ActionGroupTest extends junit.framework.TestCase {
30
+
31
+    @Test
32
+    public void testGetAuthor() {
33
+        ActionGroup instance = new ActionGroup("moo");
34
+        instance.setAuthor("foo");
35
+        
36
+        String expResult = "foo";
37
+        String result = instance.getAuthor();
38
+        assertEquals(expResult, result);
39
+    }
40
+
41
+    @Test
42
+    public void testGetDescription() {
43
+        ActionGroup instance = new ActionGroup("bar");
44
+        instance.setDescription("Tra-la-la-la-la");
45
+                
46
+        String expResult = "Tra-la-la-la-la";
47
+        String result = instance.getDescription();
48
+        assertEquals(expResult, result);
49
+    }
50
+
51
+    @Test
52
+    public void testGetName() {
53
+        ActionGroup instance = new ActionGroup("foobar");
54
+        
55
+        String expResult = "foobar";
56
+        String result = instance.getName();
57
+        assertEquals(expResult, result);
58
+    }
59
+
60
+    @Test
61
+    public void testGetSettings() {
62
+        ActionGroup instance = new ActionGroup("foo");
63
+        
64
+        List<ActionSetting> expResult = new ArrayList<ActionSetting>();
65
+        List<ActionSetting> result = instance.getSettings();
66
+        assertEquals(expResult, result);
67
+        
68
+        result.add(new ActionSetting(ActionSetting.TYPE.BOOLEAN, "", "", "", ""));
69
+        assertEquals(1, instance.getSettings().size());
70
+    }
71
+
72
+    @Test
73
+    public void testGetVersion() {
74
+        ActionGroup instance = new ActionGroup("vtest");
75
+        instance.setVersion(73);
76
+        
77
+        int expResult = 73;
78
+        int result = instance.getVersion();
79
+        assertEquals(expResult, result);
80
+    }
81
+
82
+    @Test
83
+    public void testGetComponent() {
84
+        ActionGroup instance = new ActionGroup("zzz");
85
+        instance.setComponent(69);
86
+        
87
+        int expResult = 69;
88
+        int result = instance.getComponent();
89
+        assertEquals(expResult, result);
90
+    }
91
+
92
+}

Loading…
Cancel
Save