Browse Source

Add AutoCommandHandlerTest.

Change-Id: I0ef123c502f77f4438e8afc62490ff4d364d7fa4
Reviewed-on: http://gerrit.dmdirc.com/3968
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
pull/1/head
Greg Holmes 9 years ago
parent
commit
ca7a2499ea
1 changed files with 164 additions and 0 deletions
  1. 164
    0
      test/com/dmdirc/commandparser/auto/AutoCommandHandlerTest.java

+ 164
- 0
test/com/dmdirc/commandparser/auto/AutoCommandHandlerTest.java View File

@@ -0,0 +1,164 @@
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.commandparser.auto;
24
+
25
+import com.dmdirc.FrameContainer;
26
+import com.dmdirc.GlobalWindow;
27
+import com.dmdirc.commandparser.parsers.CommandParser;
28
+import com.dmdirc.commandparser.parsers.GlobalCommandParser;
29
+import com.dmdirc.events.ClientOpenedEvent;
30
+import com.dmdirc.events.ServerConnectedEvent;
31
+import com.dmdirc.interfaces.CommandController;
32
+import com.dmdirc.interfaces.Connection;
33
+import com.dmdirc.interfaces.config.ConfigProvider;
34
+
35
+import com.google.common.base.Optional;
36
+
37
+import org.junit.Before;
38
+import org.junit.Test;
39
+import org.junit.runner.RunWith;
40
+import org.mockito.Mock;
41
+import org.mockito.runners.MockitoJUnitRunner;
42
+
43
+import static org.mockito.Mockito.never;
44
+import static org.mockito.Mockito.times;
45
+import static org.mockito.Mockito.verify;
46
+import static org.mockito.Mockito.when;
47
+
48
+@RunWith(MockitoJUnitRunner.class)
49
+public class AutoCommandHandlerTest {
50
+
51
+    @Mock private CommandController commandController;
52
+    @Mock private GlobalCommandParser globalCommandParser;
53
+    @Mock private GlobalWindow globalWindow;
54
+    @Mock private AutoCommand autoCommand;
55
+    @Mock private ClientOpenedEvent clientOpenedEvent;
56
+    @Mock private ServerConnectedEvent serverConnectedEvent;
57
+    @Mock private Connection connection;
58
+    @Mock private ConfigProvider profile;
59
+    @Mock private FrameContainer container;
60
+    @Mock private CommandParser commandParser;
61
+    private AutoCommandHandler autoCommandHandler;
62
+
63
+    @Before
64
+    public void setup() {
65
+        when(autoCommand.getProfile()).thenReturn(Optional.fromNullable("profile"));
66
+        when(autoCommand.getResponse()).thenReturn("Testing123");
67
+        when(autoCommand.getServer()).thenReturn(Optional.<String>absent());
68
+        when(autoCommand.getNetwork()).thenReturn(Optional.<String>absent());
69
+        when(serverConnectedEvent.getConnection()).thenReturn(connection);
70
+        when(connection.getProfile()).thenReturn(profile);
71
+        when(connection.getWindowModel()).thenReturn(container);
72
+        when(connection.getAddress()).thenReturn("irc.quakenet.org");
73
+        when(connection.getNetwork()).thenReturn("Quakenet");
74
+        when(profile.getName()).thenReturn("profile");
75
+        when(container.getCommandParser()).thenReturn(commandParser);
76
+        autoCommandHandler = new AutoCommandHandler(commandController, globalCommandParser,
77
+                globalWindow, autoCommand);
78
+    }
79
+
80
+    @Test
81
+    public void testCheckAutoCommandWithGlobal() {
82
+        autoCommandHandler.checkAutoCommand(clientOpenedEvent);
83
+        verify(globalCommandParser).parseCommand(globalWindow,
84
+                commandController.getCommandChar() + autoCommand.getResponse());
85
+    }
86
+
87
+    @Test
88
+    public void testCheckAutoCommandWithoutGlobal() {
89
+        when(autoCommand.getNetwork()).thenReturn(Optional.fromNullable("Quakenet"));
90
+        autoCommandHandler.checkAutoCommand(clientOpenedEvent);
91
+        verify(globalCommandParser, never()).parseCommand(globalWindow,
92
+                commandController.getCommandChar() + autoCommand.getResponse());
93
+    }
94
+
95
+    @Test
96
+    public void testCheckAutoCommandNoServerOrNetwork() {
97
+        autoCommandHandler.checkAutoCommand(serverConnectedEvent);
98
+        verify(globalCommandParser, never()).parseCommand(globalWindow,
99
+                commandController.getCommandChar() + autoCommand.getResponse());
100
+        verify(commandParser, never()).parseCommand(container,
101
+                commandController.getCommandChar() + autoCommand.getResponse());
102
+    }
103
+
104
+    @Test
105
+    public void testCheckAutoCommandNoProfile() {
106
+        when(autoCommand.getNetwork()).thenReturn(Optional.fromNullable("Quakenet"));
107
+        when(autoCommand.getProfile()).thenReturn(Optional.fromNullable("profile1"));
108
+        autoCommandHandler.checkAutoCommand(serverConnectedEvent);
109
+        verify(globalCommandParser, never()).parseCommand(globalWindow,
110
+                commandController.getCommandChar() + autoCommand.getResponse());
111
+        verify(commandParser, never()).parseCommand(container,
112
+                commandController.getCommandChar() + autoCommand.getResponse());
113
+    }
114
+
115
+    @Test
116
+    public void testCheckAutoCommandWithProfile() {
117
+        when(autoCommand.getNetwork()).thenReturn(Optional.fromNullable("Quakenet"));
118
+        autoCommandHandler.checkAutoCommand(serverConnectedEvent);
119
+        verify(globalCommandParser, never()).parseCommand(globalWindow,
120
+                commandController.getCommandChar() + autoCommand.getResponse());
121
+        verify(commandParser, times(1)).parseCommand(container,
122
+                commandController.getCommandChar() + autoCommand.getResponse());
123
+    }
124
+
125
+    @Test
126
+    public void testCheckAutoCommandWithProfileNoServer() {
127
+        when(autoCommand.getServer()).thenReturn(Optional.fromNullable("server"));
128
+        autoCommandHandler.checkAutoCommand(serverConnectedEvent);
129
+        verify(globalCommandParser, never()).parseCommand(globalWindow,
130
+                commandController.getCommandChar() + autoCommand.getResponse());
131
+        verify(commandParser, never()).parseCommand(container,
132
+                commandController.getCommandChar() + autoCommand.getResponse());
133
+    }
134
+
135
+    @Test
136
+    public void testCheckAutoCommandWithProfileNoServerOrNetwork() {
137
+        when(autoCommand.getNetwork()).thenReturn(Optional.fromNullable("network"));
138
+        autoCommandHandler.checkAutoCommand(serverConnectedEvent);
139
+        verify(globalCommandParser, never()).parseCommand(globalWindow,
140
+                commandController.getCommandChar() + autoCommand.getResponse());
141
+        verify(commandParser, never()).parseCommand(container,
142
+                commandController.getCommandChar() + autoCommand.getResponse());
143
+    }
144
+
145
+    @Test
146
+    public void testCheckAutoCommandWithProfileWithServer() {
147
+        when(autoCommand.getNetwork()).thenReturn(Optional.fromNullable("Quakenet"));
148
+        autoCommandHandler.checkAutoCommand(serverConnectedEvent);
149
+        verify(globalCommandParser, never()).parseCommand(globalWindow,
150
+                commandController.getCommandChar() + autoCommand.getResponse());
151
+        verify(commandParser, times(1)).parseCommand(container,
152
+                commandController.getCommandChar() + autoCommand.getResponse());
153
+    }
154
+
155
+    @Test
156
+    public void testCheckAutoCommandMultipleLines() {
157
+        when(autoCommand.getResponse()).thenReturn("Testing\n123");
158
+        autoCommandHandler.checkAutoCommand(clientOpenedEvent);
159
+        verify(globalCommandParser, times(1)).parseCommand(globalWindow,
160
+                commandController.getCommandChar() + "Testing");
161
+        verify(globalCommandParser, times(1)).parseCommand(globalWindow,
162
+                commandController.getCommandChar() + "123");
163
+    }
164
+}

Loading…
Cancel
Save