ソースを参照

Improve some actions tests.

Change-Id: I286836ff0c1b23af34726fcaaa7bebd947b30c7d
Reviewed-on: http://gerrit.dmdirc.com/2685
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.8rc1
Chris Smith 10年前
コミット
4a05d0a32d

+ 5
- 2
src/com/dmdirc/actions/ActionComponentChain.java ファイルの表示

@@ -23,6 +23,7 @@
23 23
 package com.dmdirc.actions;
24 24
 
25 25
 import com.dmdirc.Precondition;
26
+import com.dmdirc.interfaces.ActionController;
26 27
 import com.dmdirc.interfaces.actions.ActionComponent;
27 28
 import com.dmdirc.logger.Logger;
28 29
 
@@ -38,7 +39,7 @@ public class ActionComponentChain implements ActionComponent {
38 39
     /**
39 40
      * A list of components in this chain.
40 41
      */
41
-    private final List<ActionComponent> components = new ArrayList<ActionComponent>();
42
+    private final List<ActionComponent> components = new ArrayList<>();
42 43
 
43 44
     /**
44 45
      * Creates a new component chain from the specified text representation.
@@ -46,7 +47,9 @@ public class ActionComponentChain implements ActionComponent {
46 47
      *
47 48
      * @param source The class that this chain needs to start with
48 49
      * @param chain The textual representation of the chain
50
+     * @deprecated Should specify an action controller.
49 51
      */
52
+    @Deprecated
50 53
     public ActionComponentChain(final Class<?> source, final String chain) {
51 54
         this(source, chain, ActionManager.getActionManager());
52 55
     }
@@ -60,7 +63,7 @@ public class ActionComponentChain implements ActionComponent {
60 63
      * @param manager The action manager to use to look up components
61 64
      */
62 65
     public ActionComponentChain(final Class<?> source, final String chain,
63
-            final ActionManager manager) {
66
+            final ActionController manager) {
64 67
         Class<?> current = source;
65 68
 
66 69
         for (String componentName : chain.split("\\.")) {

+ 24
- 9
src/com/dmdirc/actions/ActionSubstitutor.java ファイルの表示

@@ -29,6 +29,7 @@ import com.dmdirc.ServerState;
29 29
 import com.dmdirc.commandparser.CommandArguments;
30 30
 import com.dmdirc.config.ConfigManager;
31 31
 import com.dmdirc.config.IdentityManager;
32
+import com.dmdirc.interfaces.ActionController;
32 33
 import com.dmdirc.interfaces.actions.ActionComponent;
33 34
 import com.dmdirc.interfaces.actions.ActionType;
34 35
 import com.dmdirc.interfaces.ui.Window;
@@ -70,18 +71,33 @@ public class ActionSubstitutor {
70 71
     /** Pattern to determine if a substitution is a server component type. */
71 72
     private static final Pattern SERVER_PATTERN = Pattern.compile("[A-Z_]+(\\.[A-Z_]+)*");
72 73
 
74
+    /** The action controller to use to find components. */
75
+    private final ActionController actionController;
73 76
     /** The action type this substitutor is for. */
74 77
     private final ActionType type;
75 78
 
76 79
     /**
77 80
      * Creates a new substitutor for the specified action type.
78 81
      *
82
+     * @param actionController The action controller to use to find components.
79 83
      * @param type The action type this substitutor is for
80 84
      */
81
-    public ActionSubstitutor(final ActionType type) {
85
+    public ActionSubstitutor(final ActionController actionController, final ActionType type) {
86
+        this.actionController = actionController;
82 87
         this.type = type;
83 88
     }
84 89
 
90
+    /**
91
+     * Creates a new substitutor for the specified action type.
92
+     *
93
+     * @param type The action type this substitutor is for
94
+     * @deprecated Should use {@link #ActionSubstitutor(ActionController, ActionType)}.
95
+     */
96
+    @Deprecated
97
+    public ActionSubstitutor(final ActionType type) {
98
+        this(ActionManager.getActionManager(), type);
99
+    }
100
+
85 101
     /**
86 102
      * Retrieves a list of global config variables that will be substituted.
87 103
      * Note: does not include initial $.
@@ -100,12 +116,11 @@ public class ActionSubstitutor {
100 116
      * @return A map of component substitution names and their descriptions
101 117
      */
102 118
     public Map<String, String> getComponentSubstitutions() {
103
-        final Map<String, String> res = new HashMap<String, String>();
119
+        final Map<String, String> res = new HashMap<>();
104 120
 
105 121
         int i = 0;
106 122
         for (Class<?> myClass : type.getType().getArgTypes()) {
107
-            for (ActionComponent comp : ActionManager.getActionManager()
108
-                    .findCompatibleComponents(myClass)) {
123
+            for (ActionComponent comp : actionController.findCompatibleComponents(myClass)) {
109 124
                 final String key = "{" + i + "." + comp.toString() + "}";
110 125
                 final String desc = type.getType().getArgNames()[i] + "'s " + comp.getName();
111 126
 
@@ -126,11 +141,10 @@ public class ActionSubstitutor {
126 141
      * @return A map of server substitution names and their descriptions.
127 142
      */
128 143
     public Map<String, String> getServerSubstitutions() {
129
-        final Map<String, String> res = new HashMap<String, String>();
144
+        final Map<String, String> res = new HashMap<>();
130 145
 
131 146
         if (hasFrameContainer()) {
132
-            for (ActionComponent comp : ActionManager.getActionManager()
133
-                    .findCompatibleComponents(Server.class)) {
147
+            for (ActionComponent comp : actionController.findCompatibleComponents(Server.class)) {
134 148
                 final String key = "{" + comp.toString() + "}";
135 149
                 final String desc = "The connection's " + comp.getName();
136 150
 
@@ -248,7 +262,8 @@ public class ActionSubstitutor {
248 262
 
249 263
             try {
250 264
                 final ActionComponentChain chain = new ActionComponentChain(
251
-                        type.getType().getArgTypes()[argument], compMatcher.group(2));
265
+                        type.getType().getArgTypes()[argument], compMatcher.group(2),
266
+                        actionController);
252 267
                 return escape(checkConnection(chain, args, args[argument]));
253 268
             } catch (IllegalArgumentException ex) {
254 269
                 return ERR_ILLEGAL_COMPONENT;
@@ -267,7 +282,7 @@ public class ActionSubstitutor {
267 282
             if (server != null) {
268 283
                 try {
269 284
                     final ActionComponentChain chain = new ActionComponentChain(
270
-                        Server.class, substitution);
285
+                        Server.class, substitution, actionController);
271 286
                     return escape(checkConnection(chain, args, server));
272 287
                 } catch (IllegalArgumentException ex) {
273 288
                     return ERR_ILLEGAL_COMPONENT;

+ 0
- 3
test/com/dmdirc/actions/ActionConditionTest.java ファイルの表示

@@ -22,9 +22,6 @@
22 22
 
23 23
 package com.dmdirc.actions;
24 24
 
25
-import com.dmdirc.TestMain;
26
-
27
-import org.junit.BeforeClass;
28 25
 import org.junit.Test;
29 26
 
30 27
 import static org.junit.Assert.*;

+ 1
- 8
test/com/dmdirc/actions/ActionModelTest.java ファイルの表示

@@ -22,8 +22,6 @@
22 22
 
23 23
 package com.dmdirc.actions;
24 24
 
25
-import org.junit.BeforeClass;
26
-import com.dmdirc.TestMain;
27 25
 import com.dmdirc.interfaces.actions.ActionType;
28 26
 
29 27
 import java.util.ArrayList;
@@ -36,11 +34,6 @@ import static org.junit.Assert.*;
36 34
 
37 35
 public class ActionModelTest {
38 36
 
39
-    @BeforeClass
40
-    public static void setUp() throws Exception {
41
-        TestMain.getTestMain();
42
-    }
43
-
44 37
     @Test
45 38
     public void testConditions() {
46 39
         final ActionModel model = new ActionModel("group", "name");
@@ -48,7 +41,7 @@ public class ActionModelTest {
48 41
         assertTrue("ActionModel must start with no conditions",
49 42
                 model.getConditions().isEmpty());
50 43
 
51
-        final List<ActionCondition> conds = new ArrayList<ActionCondition>();
44
+        final List<ActionCondition> conds = new ArrayList<>();
52 45
 
53 46
         model.setConditions(conds);
54 47
 

+ 10
- 5
test/com/dmdirc/actions/ActionSubstitutorTest.java ファイルの表示

@@ -22,12 +22,12 @@
22 22
 
23 23
 package com.dmdirc.actions;
24 24
 
25
-import com.dmdirc.TestMain;
26 25
 import com.dmdirc.Channel;
27 26
 import com.dmdirc.Server;
28 27
 import com.dmdirc.ServerState;
29 28
 import com.dmdirc.config.ConfigManager;
30 29
 import com.dmdirc.config.InvalidIdentityFileException;
30
+import com.dmdirc.interfaces.ActionController;
31 31
 import com.dmdirc.parser.interfaces.ChannelClientInfo;
32 32
 
33 33
 import java.util.Arrays;
@@ -46,7 +46,7 @@ import static org.mockito.Mockito.*;
46 46
 @RunWith(Parameterized.class)
47 47
 public class ActionSubstitutorTest {
48 48
 
49
-    private static final Map<String, String> SETTINGS = new HashMap<String, String>();
49
+    private static final Map<String, String> SETTINGS = new HashMap<>();
50 50
 
51 51
     private final String input, expected;
52 52
     private final Channel channel;
@@ -55,8 +55,6 @@ public class ActionSubstitutorTest {
55 55
 
56 56
     @BeforeClass
57 57
     public static void setup() throws InvalidIdentityFileException {
58
-        TestMain.getTestMain();
59
-
60 58
         SETTINGS.put("alpha", "A");
61 59
         SETTINGS.put("bravo", "$alpha");
62 60
         SETTINGS.put("charlie", "${bravo}");
@@ -87,7 +85,14 @@ public class ActionSubstitutorTest {
87 85
             when(manager.getOption("actions", entry.getKey())).thenReturn(entry.getValue());
88 86
         }
89 87
 
90
-        substitutor = new ActionSubstitutor(CoreActionType.CHANNEL_MESSAGE);
88
+        final ActionController controller = mock(ActionController.class);
89
+        when(controller.getComponent("STRING_STRING")).thenReturn(CoreActionComponent.STRING_STRING);
90
+        when(controller.getComponent("STRING_LENGTH")).thenReturn(CoreActionComponent.STRING_LENGTH);
91
+        when(controller.getComponent("SERVER_NETWORK")).thenReturn(CoreActionComponent.SERVER_NETWORK);
92
+        when(controller.getComponent("SERVER_PROTOCOL")).thenReturn(CoreActionComponent.SERVER_PROTOCOL);
93
+        when(controller.getComponent("SERVER_MYAWAYREASON")).thenReturn(CoreActionComponent.SERVER_MYAWAYREASON);
94
+
95
+        substitutor = new ActionSubstitutor(controller, CoreActionType.CHANNEL_MESSAGE);
91 96
 
92 97
         args = new Object[]{
93 98
             channel,

読み込み中…
キャンセル
保存