Browse Source

Merge pull request #414 from csmith/master

Add test for HighlightManager, fix issues.
pull/415/head
Greg Holmes 9 years ago
parent
commit
a72c76fcbe

+ 2
- 2
src/com/dmdirc/ui/messages/HighlightManager.java View File

@@ -102,9 +102,9 @@ public class HighlightManager {
102 102
         patterns.clear();
103 103
 
104 104
         event.getConnection().getProfile().getHighlights()
105
-                .parallelStream()
105
+                .stream()
106 106
                 .map(this::compile)
107
-                .map(patterns::add);
107
+                .forEach(patterns::add);
108 108
 
109 109
         event.getConnection().getLocalUser()
110 110
                 .map(User::getNickname)

+ 170
- 0
test/com/dmdirc/ui/messages/HighlightManagerTest.java View File

@@ -0,0 +1,170 @@
1
+/*
2
+ * Copyright (c) 2006-2015 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.ui.messages;
24
+
25
+import com.dmdirc.Channel;
26
+import com.dmdirc.DMDircMBassador;
27
+import com.dmdirc.config.profiles.Profile;
28
+import com.dmdirc.events.ChannelHighlightEvent;
29
+import com.dmdirc.events.ChannelMessageEvent;
30
+import com.dmdirc.events.ServerConnectedEvent;
31
+import com.dmdirc.events.ServerNickChangeEvent;
32
+import com.dmdirc.interfaces.Connection;
33
+import com.dmdirc.interfaces.GroupChatUser;
34
+import com.dmdirc.interfaces.User;
35
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
36
+
37
+import java.util.Collections;
38
+import java.util.Optional;
39
+
40
+import org.junit.Before;
41
+import org.junit.Test;
42
+import org.junit.runner.RunWith;
43
+import org.mockito.ArgumentCaptor;
44
+import org.mockito.Mock;
45
+import org.mockito.runners.MockitoJUnitRunner;
46
+
47
+import autovalue.shaded.com.google.common.common.collect.Lists;
48
+
49
+import static org.junit.Assert.assertSame;
50
+import static org.mockito.Matchers.any;
51
+import static org.mockito.Mockito.never;
52
+import static org.mockito.Mockito.only;
53
+import static org.mockito.Mockito.verify;
54
+import static org.mockito.Mockito.when;
55
+
56
+@RunWith(MockitoJUnitRunner.class)
57
+public class HighlightManagerTest {
58
+
59
+    @Mock private Connection connection;
60
+    @Mock private User user;
61
+    @Mock private Profile profile;
62
+
63
+    @Mock private Channel channel;
64
+    @Mock private GroupChatUser channelUser;
65
+    @Mock private DMDircMBassador eventBus;
66
+
67
+    @Mock private ColourManager colourManager;
68
+    @Mock private AggregateConfigProvider configProvider;
69
+    private HighlightManager manager;
70
+
71
+    @Before
72
+    public void setup() {
73
+        when(connection.getLocalUser()).thenReturn(Optional.of(user));
74
+        when(connection.getProfile()).thenReturn(profile);
75
+
76
+        when(channel.getEventBus()).thenReturn(eventBus);
77
+
78
+        manager = new HighlightManager(configProvider, colourManager);
79
+    }
80
+
81
+    @Test
82
+    public void testNicknameInChannel() {
83
+        when(user.getNickname()).thenReturn("nickName");
84
+        when(profile.getHighlights()).thenReturn(Collections.<String>emptyList());
85
+
86
+        final ChannelMessageEvent event = new ChannelMessageEvent(channel, channelUser,
87
+                "Hi, nickName!");
88
+
89
+        manager.handleConnected(new ServerConnectedEvent(connection));
90
+        manager.handleChannelMessage(event);
91
+
92
+        final ArgumentCaptor<ChannelHighlightEvent> captor =
93
+                ArgumentCaptor.forClass(ChannelHighlightEvent.class);
94
+
95
+        verify(eventBus).publishAsync(captor.capture());
96
+        assertSame(event, captor.getValue().getCause());
97
+    }
98
+
99
+    @Test
100
+    public void testNewNicknameInChannelAfterNickChange() {
101
+        when(user.getNickname()).thenReturn("nickName");
102
+        when(profile.getHighlights()).thenReturn(Collections.<String>emptyList());
103
+
104
+        final ChannelMessageEvent event = new ChannelMessageEvent(channel, channelUser,
105
+                "Hi, newName!");
106
+
107
+        manager.handleConnected(new ServerConnectedEvent(connection));
108
+        manager.handleNickChange(new ServerNickChangeEvent(connection, "nickName", "newName"));
109
+        manager.handleChannelMessage(event);
110
+
111
+        final ArgumentCaptor<ChannelHighlightEvent> captor =
112
+                ArgumentCaptor.forClass(ChannelHighlightEvent.class);
113
+
114
+        verify(eventBus).publishAsync(captor.capture());
115
+        assertSame(event, captor.getValue().getCause());
116
+    }
117
+
118
+    @Test
119
+    public void testOldNicknameInChannelAfterNickChange() {
120
+        when(user.getNickname()).thenReturn("nickName");
121
+        when(profile.getHighlights()).thenReturn(Collections.<String>emptyList());
122
+
123
+        final ChannelMessageEvent event = new ChannelMessageEvent(channel, channelUser,
124
+                "Hi, nickName!");
125
+
126
+        manager.handleConnected(new ServerConnectedEvent(connection));
127
+        manager.handleNickChange(new ServerNickChangeEvent(connection, "nickName", "newName"));
128
+        manager.handleChannelMessage(event);
129
+
130
+        verify(eventBus, never()).publishAsync(any());
131
+    }
132
+
133
+    @Test
134
+    public void testCustomHighlightInChannel() {
135
+        when(user.getNickname()).thenReturn("nickName");
136
+        when(profile.getHighlights()).thenReturn(Lists.newArrayList("dmdirc"));
137
+
138
+        final ChannelMessageEvent event = new ChannelMessageEvent(channel, channelUser,
139
+                "DMDirc is great.");
140
+
141
+        manager.handleConnected(new ServerConnectedEvent(connection));
142
+        manager.handleChannelMessage(event);
143
+
144
+        final ArgumentCaptor<ChannelHighlightEvent> captor =
145
+                ArgumentCaptor.forClass(ChannelHighlightEvent.class);
146
+
147
+        verify(eventBus).publishAsync(captor.capture());
148
+        assertSame(event, captor.getValue().getCause());
149
+    }
150
+
151
+    @Test
152
+    public void testMultipleCustomHighlightInChannel() {
153
+        when(user.getNickname()).thenReturn("nickName");
154
+        when(profile.getHighlights()).thenReturn(
155
+                Lists.newArrayList("dmdirc", "is", "great", "e"));
156
+
157
+        final ChannelMessageEvent event = new ChannelMessageEvent(channel, channelUser,
158
+                "DMDirc is great.");
159
+
160
+        manager.handleConnected(new ServerConnectedEvent(connection));
161
+        manager.handleChannelMessage(event);
162
+
163
+        final ArgumentCaptor<ChannelHighlightEvent> captor =
164
+                ArgumentCaptor.forClass(ChannelHighlightEvent.class);
165
+
166
+        verify(eventBus, only()).publishAsync(captor.capture());
167
+        assertSame(event, captor.getValue().getCause());
168
+    }
169
+
170
+}

Loading…
Cancel
Save