Browse Source

Remove/tidy some iffy tests

Change-Id: I79f722c55277f8b54221558af44e43e133f1daf7
Reviewed-on: http://gerrit.dmdirc.com/2445
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.7rc1
Chris Smith 12 years ago
parent
commit
0a6af6d64f

+ 12
- 4
src/com/dmdirc/actions/wrappers/AliasWrapper.java View File

@@ -30,6 +30,7 @@ import com.dmdirc.actions.ActionCondition;
30 30
 import com.dmdirc.actions.ActionGroup;
31 31
 import com.dmdirc.actions.CoreActionType;
32 32
 import com.dmdirc.commandparser.CommandManager;
33
+import com.dmdirc.interfaces.CommandController;
33 34
 import com.dmdirc.logger.ErrorLevel;
34 35
 import com.dmdirc.logger.Logger;
35 36
 import com.dmdirc.ui.input.TabCompletionType;
@@ -48,11 +49,18 @@ public final class AliasWrapper extends ActionGroup {
48 49
     /** A list of registered alias names. */
49 50
     private final List<String> aliases = new ArrayList<String>();
50 51
 
52
+    /** Command controller to get command info from. */
53
+    private final CommandController commandController;
54
+
51 55
     /**
52 56
      * Creates a new instance of AliasWrapper.
57
+     *
58
+     * @param commandController Command controller to get command info from.
53 59
      */
54
-    private AliasWrapper() {
60
+    public AliasWrapper(final CommandController commandController) {
55 61
         super("aliases");
62
+
63
+        this.commandController = commandController;
56 64
     }
57 65
 
58 66
     /**
@@ -62,7 +70,7 @@ public final class AliasWrapper extends ActionGroup {
62 70
      */
63 71
     public static synchronized AliasWrapper getAliasWrapper() {
64 72
         if (me == null) {
65
-            me = new AliasWrapper();
73
+            me = new AliasWrapper(CommandManager.getCommandManager());
66 74
         }
67 75
 
68 76
         return me;
@@ -129,10 +137,10 @@ public final class AliasWrapper extends ActionGroup {
129 137
      * @return The command name for the specified alias, or null if it has
130 138
      *         no appropriate conditions.
131 139
      */
132
-    public static String getCommandName(final Action action) {
140
+    public String getCommandName(final Action action) {
133 141
         for (ActionCondition condition : action.getConditions()) {
134 142
             if (condition.getArg() == 1) {
135
-                return CommandManager.getCommandManager().getCommandChar()
143
+                return commandController.getCommandChar()
136 144
                         + condition.getTarget();
137 145
             }
138 146
         }

+ 5
- 5
src/com/dmdirc/commandparser/commands/global/AliasCommand.java View File

@@ -78,8 +78,8 @@ public class AliasCommand extends Command implements IntelligentCommand {
78 78
                 ? args.getArguments()[0].substring(1) : args.getArguments()[0];
79 79
 
80 80
         for (Action alias : AliasWrapper.getAliasWrapper()) {
81
-            if (AliasWrapper.getCommandName(alias).substring(1).equalsIgnoreCase(
82
-                    name)) {
81
+            if (AliasWrapper.getAliasWrapper().getCommandName(alias)
82
+                    .substring(1).equalsIgnoreCase(name)) {
83 83
                 sendLine(origin, args.isSilent(), FORMAT_ERROR, "Alias '" + name
84 84
                         + "' already exists.");
85 85
                 return;
@@ -102,8 +102,8 @@ public class AliasCommand extends Command implements IntelligentCommand {
102 102
      */
103 103
     private static boolean doRemove(final String name) {
104 104
         for (Action alias : AliasWrapper.getAliasWrapper()) {
105
-            if (AliasWrapper.getCommandName(alias).substring(1).equalsIgnoreCase(
106
-                    name)) {
105
+            if (AliasWrapper.getAliasWrapper().getCommandName(alias)
106
+                    .substring(1).equalsIgnoreCase(name)) {
107 107
                 alias.delete();
108 108
                 ActionManager.getActionManager().removeAction(alias);
109 109
 
@@ -124,7 +124,7 @@ public class AliasCommand extends Command implements IntelligentCommand {
124 124
             res.add("--remove");
125 125
         } else if (arg == 1 && context.getPreviousArgs().get(0).equals("--remove")) {
126 126
             for (Action alias : AliasWrapper.getAliasWrapper()) {
127
-                res.add(AliasWrapper.getCommandName(alias));
127
+                res.add(AliasWrapper.getAliasWrapper().getCommandName(alias));
128 128
             }
129 129
         } else if (arg >= 1 && !context.getPreviousArgs().get(0).equals("--remove")) {
130 130
             return TabCompleter.getIntelligentResults(arg, context, 1);

+ 19
- 19
test/com/dmdirc/InviteTest.java View File

@@ -24,7 +24,7 @@ package com.dmdirc;
24 24
 
25 25
 import java.util.Date;
26 26
 
27
-import org.junit.BeforeClass;
27
+import org.junit.Before;
28 28
 import org.junit.Test;
29 29
 
30 30
 import static org.junit.Assert.*;
@@ -32,55 +32,55 @@ import static org.mockito.Mockito.*;
32 32
 
33 33
 public class InviteTest {
34 34
 
35
-    private static Server server;
36
-    private static Invite test;
37
-    private static long ts;
35
+    private Server server;
36
+    private Invite invite;
37
+    private long ts;
38 38
 
39
-    @BeforeClass
40
-    public static void setUp() throws Exception {
39
+    @Before
40
+    public void setUp() {
41 41
         server = mock(Server.class);
42 42
 
43 43
         when(server.parseHostmask("nick!ident@host"))
44 44
                 .thenReturn(new String[] {"nick", "ident", "host"});
45 45
 
46
-        test = new Invite(server, "#channel", "nick!ident@host");
46
+        invite = new Invite(server, "#channel", "nick!ident@host");
47 47
         ts = new Date().getTime();
48 48
     }
49 49
 
50 50
     @Test
51 51
     public void testGetServer() {
52
-        assertSame(server, test.getServer());
52
+        assertSame(server, invite.getServer());
53 53
     }
54 54
 
55 55
     @Test
56 56
     public void testGetChannel() {
57
-        assertEquals("#channel", test.getChannel());
57
+        assertEquals("#channel", invite.getChannel());
58 58
     }
59 59
 
60 60
     @Test
61 61
     public void testGetTimestamp() {
62
-        assertTrue(test.getTimestamp() - ts < 10000);
63
-        assertTrue(test.getTimestamp() - ts > -10000);
62
+        assertTrue(invite.getTimestamp() - ts < 10000);
63
+        assertTrue(invite.getTimestamp() - ts > -10000);
64 64
     }
65 65
 
66 66
     @Test
67 67
     public void testGetSource() {
68
-        assertEquals(3, test.getSource().length);
69
-        assertEquals("nick", test.getSource()[0]);
70
-        assertEquals("ident", test.getSource()[1]);
71
-        assertEquals("host", test.getSource()[2]);
68
+        assertEquals(3, invite.getSource().length);
69
+        assertEquals("nick", invite.getSource()[0]);
70
+        assertEquals("ident", invite.getSource()[1]);
71
+        assertEquals("host", invite.getSource()[2]);
72 72
     }
73 73
 
74 74
     @Test
75 75
     public void testAccept() {
76
-        test.accept();
77
-        verify(server).acceptInvites(test);
76
+        invite.accept();
77
+        verify(server).acceptInvites(invite);
78 78
     }
79 79
 
80 80
     @Test
81 81
     public void testDecline() {
82
-        test.decline();
83
-        verify(server).removeInvite(test);
82
+        invite.decline();
83
+        verify(server).removeInvite(invite);
84 84
     }
85 85
 
86 86
 }

+ 0
- 112
test/com/dmdirc/actions/ActionManagerTest.java View File

@@ -1,112 +0,0 @@
1
-/*
2
- * Copyright (c) 2006-2012 DMDirc Developers
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 com.dmdirc.config.IdentityManager;
25
-import com.dmdirc.interfaces.actions.ActionType;
26
-
27
-import java.io.File;
28
-import java.util.ArrayList;
29
-
30
-import org.junit.AfterClass;
31
-import org.junit.BeforeClass;
32
-import org.junit.Test;
33
-
34
-import static org.junit.Assert.*;
35
-
36
-public class ActionManagerTest {
37
-
38
-    @BeforeClass
39
-    public static void setUpClass() throws Exception {
40
-        IdentityManager.getIdentityManager().initialise();
41
-        ActionManager.getActionManager().initialise();
42
-        ActionManager.getActionManager().loadUserActions();
43
-
44
-        tearDownClass();
45
-    }
46
-
47
-    @AfterClass
48
-    public static void tearDownClass() throws Exception {
49
-        if (ActionManager.getActionManager().getGroupsMap().containsKey("unit-test")) {
50
-            ActionManager.getActionManager().deleteGroup("unit-test");
51
-        }
52
-
53
-        if (ActionManager.getActionManager().getGroupsMap().containsKey("unit-test-two")) {
54
-            ActionManager.getActionManager().deleteGroup("unit-test-two");
55
-        }
56
-    }
57
-
58
-    @Test
59
-    public void testMakeGroup() {
60
-        ActionManager.getActionManager().createGroup("unit-test");
61
-
62
-        assertTrue("makeGroup must create the group directory",
63
-                new File(ActionManager.getDirectory() + "unit-test").isDirectory());
64
-    }
65
-
66
-    @Test
67
-    public void testGetGroup() {
68
-        final ActionGroup group = ActionManager.getActionManager().getOrCreateGroup("unit-test");
69
-
70
-        assertEquals("getGroup must return an ActionGroup with the right name",
71
-                "unit-test", group.getName());
72
-    }
73
-
74
-    @Test
75
-    public void testRenameGroup() {
76
-        ActionManager.getActionManager().changeGroupName("unit-test", "unit-test-two");
77
-        assertFalse("renameGroup must unlink the old directory",
78
-                new File(ActionManager.getDirectory() + "unit-test").isDirectory());
79
-        assertTrue("renameGroup must create the target directory",
80
-                new File(ActionManager.getDirectory() + "unit-test-two").isDirectory());
81
-    }
82
-
83
-    @Test
84
-    public void testRenameGroupWithAction() {
85
-        final Action action = new Action("unit-test-two", "test1", new ActionType[0],
86
-                new String[0], new ArrayList<ActionCondition>(), null);
87
-        assertSame("Creating a new action must add it to the correct group",
88
-                action, ActionManager.getActionManager().getOrCreateGroup("unit-test-two").get(0));
89
-
90
-        ActionManager.getActionManager().changeGroupName("unit-test-two", "unit-test");
91
-        assertFalse("renameGroup must unlink the old directory",
92
-                new File(ActionManager.getDirectory() + "unit-test-two").isDirectory());
93
-        assertTrue("renameGroup must create the target directory",
94
-                new File(ActionManager.getDirectory() + "unit-test").isDirectory());
95
-        assertSame("renameGroup must move actions to new group",
96
-                action, ActionManager.getActionManager().getOrCreateGroup("unit-test").get(0));
97
-        assertEquals("renameGroup must remove actions from old group",
98
-                0, ActionManager.getActionManager().getOrCreateGroup("unit-test-two").size());
99
-    }
100
-
101
-    @Test
102
-    public void testRemoveGroup() {
103
-        ActionManager.getActionManager().deleteGroup("unit-test");
104
-        assertFalse("removeGroup must unlink directory",
105
-                new File(ActionManager.getDirectory() + "unit-test").isDirectory());
106
-
107
-        ActionManager.getActionManager().saveAllActions();
108
-        assertFalse("saveActions must not restore removed groups",
109
-                new File(ActionManager.getDirectory() + "unit-test").isDirectory());
110
-    }
111
-
112
-}

+ 0
- 8
test/com/dmdirc/actions/ActionModelTest.java View File

@@ -22,26 +22,18 @@
22 22
 
23 23
 package com.dmdirc.actions;
24 24
 
25
-import com.dmdirc.config.IdentityManager;
26
-import com.dmdirc.config.InvalidIdentityFileException;
27 25
 import com.dmdirc.interfaces.actions.ActionType;
28 26
 
29 27
 import java.util.ArrayList;
30 28
 import java.util.Arrays;
31 29
 import java.util.List;
32 30
 
33
-import org.junit.BeforeClass;
34 31
 import org.junit.Test;
35 32
 
36 33
 import static org.junit.Assert.*;
37 34
 
38 35
 public class ActionModelTest {
39 36
 
40
-    @BeforeClass
41
-    public static void setUpClass() throws InvalidIdentityFileException {
42
-        IdentityManager.getIdentityManager().initialise();
43
-    }
44
-
45 37
     @Test
46 38
     public void testConditions() {
47 39
         final ActionModel model = new ActionModel("group", "name");

+ 63
- 27
test/com/dmdirc/actions/wrappers/AliasWrapperTest.java View File

@@ -27,50 +27,86 @@ import com.dmdirc.actions.ActionCondition;
27 27
 import com.dmdirc.actions.CoreActionComparison;
28 28
 import com.dmdirc.actions.CoreActionComponent;
29 29
 import com.dmdirc.actions.CoreActionType;
30
-import com.dmdirc.config.IdentityManager;
31
-import com.dmdirc.config.InvalidIdentityFileException;
30
+import com.dmdirc.interfaces.CommandController;
31
+import com.dmdirc.interfaces.actions.ActionComparison;
32 32
 import com.dmdirc.interfaces.actions.ActionType;
33 33
 
34 34
 import java.util.ArrayList;
35 35
 import java.util.List;
36 36
 
37
-import org.junit.BeforeClass;
37
+import org.junit.Before;
38 38
 import org.junit.Test;
39 39
 
40 40
 import static org.junit.Assert.*;
41
+import static org.mockito.Mockito.*;
41 42
 
42 43
 public class AliasWrapperTest {
43 44
 
44
-    @BeforeClass
45
-    public static void setupClass() throws InvalidIdentityFileException {
46
-        IdentityManager.getIdentityManager().initialise();
45
+    private Action action;
46
+    private ActionCondition condition;
47
+    private List<ActionCondition> conditions;
48
+
49
+    private AliasWrapper aliasWrapper;
50
+
51
+    @Before
52
+    public void setup() {
53
+        final CommandController controller = mock(CommandController.class);
54
+        when(controller.getCommandChar()).thenReturn('!');
55
+
56
+        action = mock(Action.class);
57
+        condition = mock(ActionCondition.class);
58
+        conditions = new ArrayList<ActionCondition>();
59
+        aliasWrapper = new AliasWrapper(controller);
60
+
61
+        when(condition.getArg()).thenReturn(1);
62
+        when(condition.getComparison()).thenReturn(CoreActionComparison.STRING_EQUALS);
63
+        when(condition.getComponent()).thenReturn(CoreActionComponent.STRING_STRING);
64
+        when(condition.getTarget()).thenReturn("name");
65
+        when(action.getConditions()).thenReturn(conditions);
66
+        when(action.getTriggers()).thenReturn(new ActionType[] { CoreActionType.UNKNOWN_COMMAND });
67
+    }
68
+
69
+    @Test
70
+    public void testAddsAlias() {
71
+        conditions.add(condition);
72
+
73
+        aliasWrapper.add(action);
74
+        assertFalse(aliasWrapper.getAliases().isEmpty());
47 75
     }
48 76
 
49 77
     @Test
50
-    public void testNormal() {
51
-        final List<ActionCondition> conditions = new ArrayList<ActionCondition>();
52
-        conditions.add(new ActionCondition(1, CoreActionComponent.STRING_STRING,
53
-                CoreActionComparison.STRING_EQUALS, "testing123"));
54
-        final Action action = new Action("unit-test", "foo",
55
-                new ActionType[]{CoreActionType.UNKNOWN_COMMAND}, new String[0],
56
-                conditions, null);
57
-
58
-        assertTrue(AliasWrapper.getAliasWrapper().getAliases().isEmpty());
59
-        AliasWrapper.getAliasWrapper().add(action);
60
-        assertFalse(AliasWrapper.getAliasWrapper().getAliases().isEmpty());
61
-        assertEquals("/testing123", AliasWrapper.getAliasWrapper().getAliases().get(0));
78
+    public void testDoesNotAddActionWithNoConditions() {
79
+        aliasWrapper.add(action);
80
+        assertTrue(aliasWrapper.getAliases().isEmpty());
81
+    }
82
+
83
+    @Test
84
+    public void testDoesNotAddActionWithWrongTrigger() {
85
+        when(action.getTriggers()).thenReturn(new ActionType[] { CoreActionType.ACTION_CREATED });
86
+
87
+        aliasWrapper.add(action);
88
+        assertTrue(aliasWrapper.getAliases().isEmpty());
89
+    }
90
+
91
+    @Test
92
+    public void testGetsCommandName() {
93
+        conditions.add(condition);
94
+        assertEquals("!name", aliasWrapper.getCommandName(action));
95
+    }
96
+
97
+    @Test
98
+    public void testGetsCommandNameWithMultipleConditions() {
99
+        final ActionCondition otherCondition = mock(ActionCondition.class);
100
+        when(otherCondition.getArg()).thenReturn(7);
101
+
102
+        conditions.add(otherCondition);
103
+        conditions.add(condition);
104
+        assertEquals("!name", aliasWrapper.getCommandName(action));
62 105
     }
63 106
 
64 107
     @Test
65
-    public void testNoConditions() {
66
-        final List<ActionCondition> conditions = new ArrayList<ActionCondition>();
67
-        final Action action = new Action("unit-test", "foo",
68
-                new ActionType[]{CoreActionType.UNKNOWN_COMMAND}, new String[0],
69
-                conditions, null);
70
-
71
-        final int size = AliasWrapper.getAliasWrapper().getAliases().size();
72
-        AliasWrapper.getAliasWrapper().add(action);
73
-        assertEquals(size, AliasWrapper.getAliasWrapper().getAliases().size());
108
+    public void testGetCommandNameReturnsNullIfUnknown() {
109
+        assertNull(aliasWrapper.getCommandName(action));
74 110
     }
75 111
 
76 112
 }

Loading…
Cancel
Save