Browse Source

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 years ago
parent
commit
4a05d0a32d

+ 5
- 2
src/com/dmdirc/actions/ActionComponentChain.java View File

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

+ 24
- 9
src/com/dmdirc/actions/ActionSubstitutor.java View File

29
 import com.dmdirc.commandparser.CommandArguments;
29
 import com.dmdirc.commandparser.CommandArguments;
30
 import com.dmdirc.config.ConfigManager;
30
 import com.dmdirc.config.ConfigManager;
31
 import com.dmdirc.config.IdentityManager;
31
 import com.dmdirc.config.IdentityManager;
32
+import com.dmdirc.interfaces.ActionController;
32
 import com.dmdirc.interfaces.actions.ActionComponent;
33
 import com.dmdirc.interfaces.actions.ActionComponent;
33
 import com.dmdirc.interfaces.actions.ActionType;
34
 import com.dmdirc.interfaces.actions.ActionType;
34
 import com.dmdirc.interfaces.ui.Window;
35
 import com.dmdirc.interfaces.ui.Window;
70
     /** Pattern to determine if a substitution is a server component type. */
71
     /** Pattern to determine if a substitution is a server component type. */
71
     private static final Pattern SERVER_PATTERN = Pattern.compile("[A-Z_]+(\\.[A-Z_]+)*");
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
     /** The action type this substitutor is for. */
76
     /** The action type this substitutor is for. */
74
     private final ActionType type;
77
     private final ActionType type;
75
 
78
 
76
     /**
79
     /**
77
      * Creates a new substitutor for the specified action type.
80
      * Creates a new substitutor for the specified action type.
78
      *
81
      *
82
+     * @param actionController The action controller to use to find components.
79
      * @param type The action type this substitutor is for
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
         this.type = type;
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
      * Retrieves a list of global config variables that will be substituted.
102
      * Retrieves a list of global config variables that will be substituted.
87
      * Note: does not include initial $.
103
      * Note: does not include initial $.
100
      * @return A map of component substitution names and their descriptions
116
      * @return A map of component substitution names and their descriptions
101
      */
117
      */
102
     public Map<String, String> getComponentSubstitutions() {
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
         int i = 0;
121
         int i = 0;
106
         for (Class<?> myClass : type.getType().getArgTypes()) {
122
         for (Class<?> myClass : type.getType().getArgTypes()) {
107
-            for (ActionComponent comp : ActionManager.getActionManager()
108
-                    .findCompatibleComponents(myClass)) {
123
+            for (ActionComponent comp : actionController.findCompatibleComponents(myClass)) {
109
                 final String key = "{" + i + "." + comp.toString() + "}";
124
                 final String key = "{" + i + "." + comp.toString() + "}";
110
                 final String desc = type.getType().getArgNames()[i] + "'s " + comp.getName();
125
                 final String desc = type.getType().getArgNames()[i] + "'s " + comp.getName();
111
 
126
 
126
      * @return A map of server substitution names and their descriptions.
141
      * @return A map of server substitution names and their descriptions.
127
      */
142
      */
128
     public Map<String, String> getServerSubstitutions() {
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
         if (hasFrameContainer()) {
146
         if (hasFrameContainer()) {
132
-            for (ActionComponent comp : ActionManager.getActionManager()
133
-                    .findCompatibleComponents(Server.class)) {
147
+            for (ActionComponent comp : actionController.findCompatibleComponents(Server.class)) {
134
                 final String key = "{" + comp.toString() + "}";
148
                 final String key = "{" + comp.toString() + "}";
135
                 final String desc = "The connection's " + comp.getName();
149
                 final String desc = "The connection's " + comp.getName();
136
 
150
 
248
 
262
 
249
             try {
263
             try {
250
                 final ActionComponentChain chain = new ActionComponentChain(
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
                 return escape(checkConnection(chain, args, args[argument]));
267
                 return escape(checkConnection(chain, args, args[argument]));
253
             } catch (IllegalArgumentException ex) {
268
             } catch (IllegalArgumentException ex) {
254
                 return ERR_ILLEGAL_COMPONENT;
269
                 return ERR_ILLEGAL_COMPONENT;
267
             if (server != null) {
282
             if (server != null) {
268
                 try {
283
                 try {
269
                     final ActionComponentChain chain = new ActionComponentChain(
284
                     final ActionComponentChain chain = new ActionComponentChain(
270
-                        Server.class, substitution);
285
+                        Server.class, substitution, actionController);
271
                     return escape(checkConnection(chain, args, server));
286
                     return escape(checkConnection(chain, args, server));
272
                 } catch (IllegalArgumentException ex) {
287
                 } catch (IllegalArgumentException ex) {
273
                     return ERR_ILLEGAL_COMPONENT;
288
                     return ERR_ILLEGAL_COMPONENT;

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

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

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

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

+ 10
- 5
test/com/dmdirc/actions/ActionSubstitutorTest.java View File

22
 
22
 
23
 package com.dmdirc.actions;
23
 package com.dmdirc.actions;
24
 
24
 
25
-import com.dmdirc.TestMain;
26
 import com.dmdirc.Channel;
25
 import com.dmdirc.Channel;
27
 import com.dmdirc.Server;
26
 import com.dmdirc.Server;
28
 import com.dmdirc.ServerState;
27
 import com.dmdirc.ServerState;
29
 import com.dmdirc.config.ConfigManager;
28
 import com.dmdirc.config.ConfigManager;
30
 import com.dmdirc.config.InvalidIdentityFileException;
29
 import com.dmdirc.config.InvalidIdentityFileException;
30
+import com.dmdirc.interfaces.ActionController;
31
 import com.dmdirc.parser.interfaces.ChannelClientInfo;
31
 import com.dmdirc.parser.interfaces.ChannelClientInfo;
32
 
32
 
33
 import java.util.Arrays;
33
 import java.util.Arrays;
46
 @RunWith(Parameterized.class)
46
 @RunWith(Parameterized.class)
47
 public class ActionSubstitutorTest {
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
     private final String input, expected;
51
     private final String input, expected;
52
     private final Channel channel;
52
     private final Channel channel;
55
 
55
 
56
     @BeforeClass
56
     @BeforeClass
57
     public static void setup() throws InvalidIdentityFileException {
57
     public static void setup() throws InvalidIdentityFileException {
58
-        TestMain.getTestMain();
59
-
60
         SETTINGS.put("alpha", "A");
58
         SETTINGS.put("alpha", "A");
61
         SETTINGS.put("bravo", "$alpha");
59
         SETTINGS.put("bravo", "$alpha");
62
         SETTINGS.put("charlie", "${bravo}");
60
         SETTINGS.put("charlie", "${bravo}");
87
             when(manager.getOption("actions", entry.getKey())).thenReturn(entry.getValue());
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
         args = new Object[]{
97
         args = new Object[]{
93
             channel,
98
             channel,

Loading…
Cancel
Save