Bladeren bron

Some tidying + testing in Time plugin.

pull/181/head
Greg Holmes 9 jaren geleden
bovenliggende
commit
aad57eae82

+ 15
- 15
time/src/com/dmdirc/addons/time/TimeActionMetaType.java Bestand weergeven

33
 
33
 
34
     /** Time type. */
34
     /** Time type. */
35
     TIME_TIME {
35
     TIME_TIME {
36
-                @Override
37
-                public int getArity() {
38
-                    return 1;
39
-                }
40
-
41
-                @Override
42
-                public Class<?>[] getArgTypes() {
43
-                    return new Class<?>[]{Calendar.class};
44
-                }
45
-
46
-                @Override
47
-                public String[] getArgNames() {
48
-                    return new String[]{"Date"};
49
-                }
50
-            };
36
+        @Override
37
+        public int getArity() {
38
+            return 1;
39
+        }
40
+
41
+        @Override
42
+        public Class<?>[] getArgTypes() {
43
+            return new Class<?>[]{Calendar.class};
44
+        }
45
+
46
+        @Override
47
+        public String[] getArgNames() {
48
+            return new String[]{"Date"};
49
+        }
50
+    };
51
 
51
 
52
     @Override
52
     @Override
53
     public String getGroup() {
53
     public String getGroup() {

+ 15
- 26
time/src/com/dmdirc/addons/time/TimedCommand.java Bestand weergeven

22
 
22
 
23
 package com.dmdirc.addons.time;
23
 package com.dmdirc.addons.time;
24
 
24
 
25
-import com.dmdirc.DMDircMBassador;
26
 import com.dmdirc.FrameContainer;
25
 import com.dmdirc.FrameContainer;
27
-import com.dmdirc.commandparser.parsers.CommandParser;
28
-import com.dmdirc.commandparser.parsers.GlobalCommandParser;
29
-import com.dmdirc.interfaces.CommandController;
30
 
26
 
31
 import java.util.Timer;
27
 import java.util.Timer;
32
 import java.util.TimerTask;
28
 import java.util.TimerTask;
42
     private final String command;
38
     private final String command;
43
     /** The container to use for executing commands. */
39
     /** The container to use for executing commands. */
44
     private final FrameContainer origin;
40
     private final FrameContainer origin;
45
-    /** The timer we're using for scheduling this command. */
46
-    private final Timer timer;
41
+    /** The number of seconds between each execution. */
42
+    private final int delay;
47
     /** The key for this timer in the Timer Manager. */
43
     /** The key for this timer in the Timer Manager. */
48
     private final int timerKey;
44
     private final int timerKey;
49
     /** The manager for this timer. */
45
     /** The manager for this timer. */
50
     private final TimerManager manager;
46
     private final TimerManager manager;
51
-    /** The command controller to use when executing global commands. */
52
-    private final CommandController commandController;
53
-    /** Event bus to post events on. */
54
-    private final DMDircMBassador eventBus;
47
+    /** The timer we're using for scheduling this command. */
48
+    private Timer timer;
55
 
49
 
56
     /**
50
     /**
57
      * Creates a new instance of TimedCommand.
51
      * Creates a new instance of TimedCommand.
58
      *
52
      *
59
      * @param manager           The manager that is controlling this command.
53
      * @param manager           The manager that is controlling this command.
60
-     * @param commandController The command controller to use when executing global commands.
61
      * @param timerKey          The key for this timer in the Timer Manager.
54
      * @param timerKey          The key for this timer in the Timer Manager.
62
      * @param repetitions       The number of times this command will be executed
55
      * @param repetitions       The number of times this command will be executed
63
      * @param delay             The number of seconds between each execution
56
      * @param delay             The number of seconds between each execution
64
      * @param command           The command to be executed
57
      * @param command           The command to be executed
65
      * @param origin            The frame container to use for the execution
58
      * @param origin            The frame container to use for the execution
66
-     * @param eventBus          The Event bus to post events on
67
      */
59
      */
68
     public TimedCommand(
60
     public TimedCommand(
69
             final TimerManager manager,
61
             final TimerManager manager,
70
-            final CommandController commandController,
71
             final int timerKey,
62
             final int timerKey,
72
             final int repetitions,
63
             final int repetitions,
73
             final int delay,
64
             final int delay,
74
             final String command,
65
             final String command,
75
-            final FrameContainer origin,
76
-            final DMDircMBassador eventBus) {
77
-        this.commandController = commandController;
66
+            final FrameContainer origin) {
78
         this.timerKey = timerKey;
67
         this.timerKey = timerKey;
79
         this.repetitions = repetitions;
68
         this.repetitions = repetitions;
80
         this.command = command;
69
         this.command = command;
81
         this.origin = origin;
70
         this.origin = origin;
82
         this.manager = manager;
71
         this.manager = manager;
83
-        this.eventBus = eventBus;
72
+        this.delay = delay;
73
+    }
84
 
74
 
85
-        timer = new Timer("Timed Command Timer");
75
+    public void schedule(final TimerFactory timerFactory) {
76
+        timer = timerFactory.getTimer("Timed Command Timer");
86
         timer.schedule(this, delay * 1000L, delay * 1000L);
77
         timer.schedule(this, delay * 1000L, delay * 1000L);
87
     }
78
     }
88
 
79
 
100
      */
91
      */
101
     public void cancelTimer() {
92
     public void cancelTimer() {
102
         manager.removeTimer(timerKey);
93
         manager.removeTimer(timerKey);
103
-        timer.cancel();
94
+        if (timer != null) {
95
+            timer.cancel();
96
+        }
104
     }
97
     }
105
 
98
 
106
     @Override
99
     @Override
107
     public void run() {
100
     public void run() {
108
-        final CommandParser parser;
109
-        if (origin == null) {
110
-            parser = new GlobalCommandParser(origin.getConfigManager(), commandController, eventBus);
111
-        } else {
112
-            parser = origin.getCommandParser();
101
+        if (timer == null) {
102
+            return;
113
         }
103
         }
114
-
115
-        parser.parseCommand(origin, command);
104
+        origin.getCommandParser().parseCommand(origin, command);
116
 
105
 
117
         if (--repetitions <= 0) {
106
         if (--repetitions <= 0) {
118
             manager.removeTimer(timerKey);
107
             manager.removeTimer(timerKey);

+ 57
- 50
time/src/com/dmdirc/addons/time/TimerCommand.java Bestand weergeven

34
 
34
 
35
 import java.util.Map.Entry;
35
 import java.util.Map.Entry;
36
 import java.util.Set;
36
 import java.util.Set;
37
+import java.util.stream.Collectors;
37
 
38
 
38
 import javax.annotation.Nonnull;
39
 import javax.annotation.Nonnull;
39
 import javax.inject.Inject;
40
 import javax.inject.Inject;
74
         if (args.getArguments().length > 0) {
75
         if (args.getArguments().length > 0) {
75
             switch (args.getArguments()[0]) {
76
             switch (args.getArguments()[0]) {
76
                 case "--cancel":
77
                 case "--cancel":
77
-                    final int timerKey;
78
-                    try {
79
-                        timerKey = Integer.parseInt(args.getArgumentsAsString(1));
80
-                    } catch (NumberFormatException ex) {
81
-                        doUsage(origin, args.isSilent());
82
-                        return;
83
-                    }
84
-                    if (manager.hasTimerWithID(timerKey)) {
85
-                        manager.getTimerByID(timerKey).cancelTimer();
86
-                        sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Timer cancelled");
87
-                    } else {
88
-                        sendLine(origin, args.isSilent(), FORMAT_ERROR, "There is currently"
89
-                                + " no timer with that ID");
90
-                        return;
91
-                    }
78
+                    doCancel(origin, args.isSilent(), args.getArgumentsAsString(1));
92
                     break;
79
                     break;
93
                 case "--list":
80
                 case "--list":
94
-                    final Set<Entry<Integer, TimedCommand>> timerList = manager.listTimers();
95
-                    if (!timerList.isEmpty()) {
96
-                        for (Entry<Integer, TimedCommand> entry : timerList) {
97
-                            sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Timer ID: "
98
-                                    + entry.getKey() + " - " + entry.getValue().getCommand());
99
-                        }
100
-                    } else {
101
-                        sendLine(origin, args.isSilent(), FORMAT_ERROR, "There are "
102
-                                + "currently no active timers");
103
-                    }
81
+                    doList(origin, args.isSilent());
104
                     break;
82
                     break;
105
                 default:
83
                 default:
106
                     if (args.getArguments().length < 3) {
84
                     if (args.getArguments().length < 3) {
107
                         doUsage(origin, args.isSilent());
85
                         doUsage(origin, args.isSilent());
108
                     } else {
86
                     } else {
109
-                        final int repetitions;
110
-                        final int interval;
87
+                        doCommand(origin, args);
88
+                    }
89
+                    break;
90
+            }
91
+        } else {
92
+            doUsage(origin, args.isSilent());
93
+        }
94
+    }
95
+
96
+    private void doCommand(final FrameContainer origin, final CommandArguments args) {
97
+        final int repetitions;
98
+        final int interval;
99
+
100
+        final String command = args.getArgumentsAsString(2);
111
 
101
 
112
-                        final String command = args.getArgumentsAsString(2);
102
+        try {
103
+            repetitions = Integer.parseInt(args.getArguments()[0]);
104
+            interval = Integer.parseInt(args.getArguments()[1]);
105
+        } catch (NumberFormatException ex) {
106
+            doUsage(origin, args.isSilent());
107
+            return;
108
+        }
113
 
109
 
114
-                        try {
115
-                            repetitions = Integer.parseInt(args.getArguments()[0]);
116
-                            interval = Integer.parseInt(args.getArguments()[1]);
117
-                        } catch (NumberFormatException ex) {
118
-                            doUsage(origin, args.isSilent());
119
-                            return;
120
-                        }
110
+        if (interval < 1) {
111
+            sendLine(origin, args.isSilent(), FORMAT_ERROR, "Cannot use intervals below 1");
112
+            return;
113
+        }
121
 
114
 
122
-                        if (interval < 1) {
123
-                            sendLine(origin, args.isSilent(), FORMAT_ERROR, "Cannot use"
124
-                                    + " intervals below 1");
125
-                            return;
126
-                        }
115
+        manager.addTimer(repetitions, interval, command, origin);
127
 
116
 
128
-                        manager.addTimer(repetitions, interval, command,
129
-                                origin);
117
+        sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Command scheduled.");
118
+    }
130
 
119
 
131
-                        sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Command scheduled.");
132
-                    }
133
-                    break;
134
-            }
120
+    private void doCancel(final FrameContainer origin, final boolean isSilent, final String arg) {
121
+        final int timerKey;
122
+        try {
123
+            timerKey = Integer.parseInt(arg);
124
+        } catch (NumberFormatException ex) {
125
+            doUsage(origin, isSilent);
126
+            return;
127
+        }
128
+        if (manager.hasTimerWithID(timerKey)) {
129
+            manager.getTimerByID(timerKey).cancelTimer();
130
+            sendLine(origin, isSilent, FORMAT_OUTPUT, "Timer cancelled");
135
         } else {
131
         } else {
136
-            doUsage(origin, args.isSilent());
132
+            sendLine(origin, isSilent, FORMAT_ERROR, "There is currently no timer with that ID");
133
+        }
134
+    }
135
+
136
+    private void doList(final FrameContainer origin, final boolean isSilent) {
137
+        final Set<Entry<Integer, TimedCommand>> timerList = manager.listTimers();
138
+        if (timerList.isEmpty()) {
139
+            sendLine(origin, isSilent, FORMAT_ERROR, "There are " + "currently no active timers");
140
+        } else {
141
+            for (Entry<Integer, TimedCommand> entry : timerList) {
142
+                sendLine(origin, isSilent, FORMAT_OUTPUT,
143
+                        "Timer ID: " + entry.getKey() + " - " + entry.getValue().getCommand());
144
+            }
137
         }
145
         }
138
     }
146
     }
139
 
147
 
158
             targets.add("--list");
166
             targets.add("--list");
159
             targets.add("--cancel");
167
             targets.add("--cancel");
160
         } else if (arg == 1 && "--cancel".equals(context.getPreviousArgs().get(0))) {
168
         } else if (arg == 1 && "--cancel".equals(context.getPreviousArgs().get(0))) {
161
-            for (Integer i : manager.getTimerIDs()) {
162
-                targets.add(i.toString());
163
-            }
169
+            targets.addAll(manager.getTimerIDs().stream()
170
+                    .map(Object::toString).collect(Collectors.toList()));
164
         }
171
         }
165
 
172
 
166
         return targets;
173
         return targets;

+ 42
- 0
time/src/com/dmdirc/addons/time/TimerFactory.java Bestand weergeven

1
+/*
2
+ * Copyright (c) 2006-2014 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
+
23
+package com.dmdirc.addons.time;
24
+
25
+import java.util.Timer;
26
+
27
+import javax.inject.Inject;
28
+import javax.inject.Singleton;
29
+
30
+/**
31
+ * Factory to create {@link Timer}s.
32
+ */
33
+@Singleton
34
+public class TimerFactory {
35
+
36
+    @Inject
37
+    public TimerFactory() {}
38
+
39
+    public Timer getTimer(final String name) {
40
+        return new Timer(name);
41
+    }
42
+}

+ 8
- 13
time/src/com/dmdirc/addons/time/TimerManager.java Bestand weergeven

22
 
22
 
23
 package com.dmdirc.addons.time;
23
 package com.dmdirc.addons.time;
24
 
24
 
25
-import com.dmdirc.DMDircMBassador;
26
 import com.dmdirc.FrameContainer;
25
 import com.dmdirc.FrameContainer;
27
 import com.dmdirc.interfaces.ActionController;
26
 import com.dmdirc.interfaces.ActionController;
28
-import com.dmdirc.interfaces.CommandController;
29
 
27
 
30
 import java.util.Calendar;
28
 import java.util.Calendar;
31
 import java.util.HashMap;
29
 import java.util.HashMap;
44
 
42
 
45
     /** Map of all the timers that are running. */
43
     /** Map of all the timers that are running. */
46
     private final Map<Integer, TimedCommand> timerList = new HashMap<>();
44
     private final Map<Integer, TimedCommand> timerList = new HashMap<>();
47
-    /** The command controller to use when executing global commands. */
48
-    private final CommandController commandController;
49
     /** Action controller. */
45
     /** Action controller. */
50
     private final ActionController actionController;
46
     private final ActionController actionController;
51
-    /** Event bus to post events on . */
52
-    private final DMDircMBassador eventBus;
53
     /** Have we registered our types already? */
47
     /** Have we registered our types already? */
54
     private static boolean registered;
48
     private static boolean registered;
49
+    /** The timer factory to create timers with. */
50
+    private final TimerFactory timerFactory;
55
     /** The timer to use for scheduling. */
51
     /** The timer to use for scheduling. */
56
     private Timer timer;
52
     private Timer timer;
57
 
53
 
58
     @Inject
54
     @Inject
59
-    public TimerManager(final CommandController commandController,
60
-            final ActionController actionController,
61
-            final DMDircMBassador eventBus) {
62
-        this.commandController = commandController;
55
+    public TimerManager(final ActionController actionController, final TimerFactory timerFactory) {
63
         this.actionController = actionController;
56
         this.actionController = actionController;
64
-        this.eventBus = eventBus;
57
+        this.timerFactory = timerFactory;
65
     }
58
     }
66
 
59
 
67
     public void load() {
60
     public void load() {
103
 
96
 
104
         synchronized (this) {
97
         synchronized (this) {
105
             final int timerKey = findFreeKey();
98
             final int timerKey = findFreeKey();
106
-            timerList.put(timerKey, new TimedCommand(this, commandController, timerKey,
107
-                    repetitions, interval, command, origin, eventBus));
99
+            final TimedCommand timedCommand = new TimedCommand(this, timerKey,
100
+                    repetitions, interval, command, origin);
101
+            timerList.put(timerKey, timedCommand);
102
+            timedCommand.schedule(timerFactory);
108
         }
103
         }
109
     }
104
     }
110
 
105
 

+ 112
- 0
time/test/com/dmdirc/addons/time/TimedCommandTest.java Bestand weergeven

1
+/*
2
+ * Copyright (c) 2006-2014 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
+
23
+package com.dmdirc.addons.time;
24
+
25
+import com.dmdirc.FrameContainer;
26
+import com.dmdirc.commandparser.parsers.CommandParser;
27
+
28
+import java.util.Timer;
29
+
30
+import org.junit.Before;
31
+import org.junit.Test;
32
+import org.junit.runner.RunWith;
33
+import org.mockito.Mock;
34
+import org.mockito.runners.MockitoJUnitRunner;
35
+
36
+import static junit.framework.TestCase.assertEquals;
37
+import static org.mockito.Matchers.anyString;
38
+import static org.mockito.Mockito.never;
39
+import static org.mockito.Mockito.times;
40
+import static org.mockito.Mockito.verify;
41
+import static org.mockito.Mockito.when;
42
+
43
+@RunWith(MockitoJUnitRunner.class)
44
+public class TimedCommandTest {
45
+
46
+    @Mock private TimerManager timerManager;
47
+    @Mock private FrameContainer origin;
48
+    @Mock private CommandParser commandParser;
49
+    @Mock private TimerFactory timerFactory;
50
+    @Mock private Timer timer;
51
+
52
+    private TimedCommand instance;
53
+
54
+    @Before
55
+    public void setUp() throws Exception {
56
+        when(timerFactory.getTimer(anyString())).thenReturn(timer);
57
+        when(origin.getCommandParser()).thenReturn(commandParser);
58
+        instance = new TimedCommand(timerManager, 1, 2, 3, "command", origin);
59
+    }
60
+
61
+    @Test
62
+    public void testSchedule() throws Exception {
63
+        instance.schedule(timerFactory);
64
+        verify(timer).schedule(instance, 3000, 3000);
65
+    }
66
+
67
+    @Test
68
+    public void testGetCommand() throws Exception {
69
+        assertEquals("command", instance.getCommand());
70
+    }
71
+
72
+    @Test
73
+    public void testCancelTimer_WithoutSchedule() throws Exception {
74
+        instance.cancelTimer();
75
+        verify(timerManager).removeTimer(1);
76
+    }
77
+
78
+    @Test
79
+    public void testCancelTimer() throws Exception {
80
+        instance.schedule(timerFactory);
81
+        instance.cancelTimer();
82
+        verify(timerManager).removeTimer(1);
83
+        verify(timer).cancel();
84
+    }
85
+
86
+    @Test
87
+    public void testRun_withoutSchedule() throws Exception {
88
+        instance.run();
89
+        verify(commandParser, never()).parseCommand(origin, "command");
90
+        verify(timerManager, never()).removeTimer(1);
91
+        verify(timer, never()).cancel();
92
+    }
93
+
94
+    @Test
95
+    public void testRun_LessThanRepetitions() throws Exception {
96
+        instance.schedule(timerFactory);
97
+        instance.run();
98
+        verify(commandParser, times(1)).parseCommand(origin, "command");
99
+        verify(timerManager, never()).removeTimer(1);
100
+        verify(timer, never()).cancel();
101
+    }
102
+
103
+    @Test
104
+    public void testRun_AllRepetitions() throws Exception {
105
+        instance.schedule(timerFactory);
106
+        instance.run();
107
+        instance.run();
108
+        verify(commandParser, times(2)).parseCommand(origin, "command");
109
+        verify(timerManager, times(1)).removeTimer(1);
110
+        verify(timer, times(1)).cancel();
111
+    }
112
+}

Laden…
Annuleren
Opslaan