Procházet zdrojové kódy

Finish CoreProfilesDialogModel.

This model should happily cope with the existing profiles wrapper
being rewritten and when the UI uses this model should mean no UI
changes required to add support.

Change-Id: Iea3b7838cef94514865ecb77064c133ca4556030
Reviewed-on: http://gerrit.dmdirc.com/3619
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
pull/1/head
Greg Holmes před 10 roky
rodič
revize
5aae1e9dd6

+ 7
- 2
src/com/dmdirc/ui/core/profiles/CoreProfilesDialogModel.java Zobrazit soubor

@@ -53,7 +53,7 @@ public class CoreProfilesDialogModel implements ProfilesDialogModel {
53 53
     private final ListenerList listeners;
54 54
     private final IdentityFactory identityFactory;
55 55
     private final IdentityController identityController;
56
-    private HashMap<String, Profile> profiles;
56
+    private final HashMap<String, Profile> profiles;
57 57
     private Optional<Profile> selectedProfile = Optional.absent();
58 58
     private Optional<String> name = Optional.absent();
59 59
     private Optional<List<String>> nicknames = Optional.absent();
@@ -68,6 +68,7 @@ public class CoreProfilesDialogModel implements ProfilesDialogModel {
68 68
         this.identityController = identityController;
69 69
         listeners = new ListenerList();
70 70
         final List<ConfigProvider> identities = identityController.getProvidersByType("profile");
71
+        profiles = new HashMap<>(identities.size());
71 72
         for (ConfigProvider identity : identities) {
72 73
             profiles.put(identity.getName(), getProfile(identity));
73 74
         }
@@ -75,6 +76,10 @@ public class CoreProfilesDialogModel implements ProfilesDialogModel {
75 76
 
76 77
     private Profile getProfile(final ConfigProvider configProvider) {
77 78
         final Profile newProfile = new Profile(configProvider.getName(), identityFactory);
79
+        newProfile.setName(configProvider.getOption("identity", "name"));
80
+        newProfile.setRealname(configProvider.getOption("profile", "realname"));
81
+        newProfile.setIdent(configProvider.getOption("profile", "ident"));
82
+        newProfile.setNicknames(configProvider.getOptionList("profile", "nicknames"));
78 83
         this.name = Optional.fromNullable(configProvider.getOption("identity", "name"));
79 84
         this.nicknames = Optional.fromNullable(configProvider.getOptionList("profile", "nicknames"));
80 85
         this.realname = Optional.fromNullable(configProvider.getOption("profile", "realname"));
@@ -366,7 +371,7 @@ public class CoreProfilesDialogModel implements ProfilesDialogModel {
366 371
         checkState(selectedProfile.isPresent(), "There must be a profile selected");
367 372
         checkState(nicknames.isPresent(), "There must be nicknames present");
368 373
         checkArgument(nicknames.get().contains(nickname), "Nickname must exist");
369
-        nicknames.get().add(nickname);
374
+        nicknames.get().remove(nickname);
370 375
         listeners.getCallable(ProfilesDialogModelListener.class).selectedProfileNicknameRemoved(
371 376
                 nickname);
372 377
     }

+ 4
- 0
src/com/dmdirc/ui/core/profiles/EditSelectedNicknameValidator.java Zobrazit soubor

@@ -42,6 +42,10 @@ public class EditSelectedNicknameValidator implements Validator<String> {
42 42
         if (model.getSelectedProfile().isPresent()
43 43
                 && model.getSelectedProfileNicknames().isPresent()) {
44 44
             for (String nickname : model.getSelectedProfileNicknames().get()) {
45
+                if (model.getSelectedProfileSelectedNickname().isPresent()
46
+                        && model.getSelectedProfileSelectedNickname().get().equals(object)) {
47
+                    continue;
48
+                }
45 49
                 if (nickname.equals(object)) {
46 50
                     return new ValidationResponse("This valid already exists.");
47 51
                 }

+ 4
- 0
src/com/dmdirc/ui/core/profiles/EditSelectedProfileNameValidator.java Zobrazit soubor

@@ -41,6 +41,10 @@ public class EditSelectedProfileNameValidator implements Validator<String> {
41 41
     @Override
42 42
     public ValidationResponse validate(final String object) {
43 43
         for (Profile nickname : model.getProfileList()) {
44
+            if (model.getSelectedProfile().isPresent()
45
+                    && model.getSelectedProfile().get().getName().equals(object)) {
46
+                continue;
47
+            }
44 48
             if (nickname.getName().equals(object)) {
45 49
                 return new ValidationResponse("This valid already exists.");
46 50
             }

+ 80
- 0
test/com/dmdirc/ui/core/profiles/AddNicknameValidatorTest.java Zobrazit soubor

@@ -0,0 +1,80 @@
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.ui.core.profiles;
24
+
25
+import com.dmdirc.actions.wrappers.Profile;
26
+import com.dmdirc.interfaces.ui.ProfilesDialogModel;
27
+
28
+import com.google.common.base.Optional;
29
+
30
+import java.util.List;
31
+
32
+import org.junit.Before;
33
+import org.junit.Test;
34
+import org.junit.runner.RunWith;
35
+import org.mockito.Mock;
36
+import org.mockito.runners.MockitoJUnitRunner;
37
+
38
+import clover.com.google.common.collect.Lists;
39
+
40
+import static org.junit.Assert.assertFalse;
41
+import static org.junit.Assert.assertTrue;
42
+import static org.mockito.Mockito.when;
43
+
44
+@RunWith(MockitoJUnitRunner.class)
45
+public class AddNicknameValidatorTest {
46
+
47
+    @Mock private ProfilesDialogModel model;
48
+    @Mock private Profile profile;
49
+    private List<String> nicknames;
50
+
51
+    @Before
52
+    public void setupModel() {
53
+        nicknames = Lists.newArrayList("nickname1", "nickname2", "nickname3");
54
+        when(model.getSelectedProfile())
55
+                .thenReturn(Optional.fromNullable(profile));
56
+        when(model.getSelectedProfileNicknames())
57
+                .thenReturn(Optional.fromNullable(nicknames));
58
+        when(model.getSelectedProfileSelectedNickname())
59
+                .thenReturn(Optional.fromNullable("nickname2"));
60
+    }
61
+
62
+    @Test
63
+    public void testDuplicateName() {
64
+        final AddNicknameValidator instance = new AddNicknameValidator(model);
65
+        assertTrue("testDuplicateName", instance.validate("nickname1").isFailure());
66
+    }
67
+
68
+    @Test
69
+    public void testNonDuplicateName() {
70
+        final AddNicknameValidator instance = new AddNicknameValidator(model);
71
+        assertFalse("testNonDuplicateName", instance.validate("nickname4").isFailure());
72
+    }
73
+
74
+    @Test
75
+    public void testSelectedName() {
76
+        final AddNicknameValidator instance = new AddNicknameValidator(model);
77
+        assertTrue("testSelectedName", instance.validate("nickname2").isFailure());
78
+    }
79
+
80
+}

+ 307
- 0
test/com/dmdirc/ui/core/profiles/CoreProfilesDialogModelTest.java Zobrazit soubor

@@ -0,0 +1,307 @@
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.ui.core.profiles;
24
+
25
+import com.dmdirc.actions.wrappers.Profile;
26
+import com.dmdirc.interfaces.config.ConfigProvider;
27
+import com.dmdirc.interfaces.config.IdentityController;
28
+import com.dmdirc.interfaces.config.IdentityFactory;
29
+import com.dmdirc.interfaces.ui.ProfilesDialogModelListener;
30
+
31
+import com.google.common.base.Optional;
32
+
33
+import java.util.List;
34
+
35
+import org.junit.Before;
36
+import org.junit.Test;
37
+import org.junit.runner.RunWith;
38
+import org.mockito.Mock;
39
+import org.mockito.runners.MockitoJUnitRunner;
40
+
41
+import clover.com.google.common.collect.Lists;
42
+
43
+import static org.junit.Assert.assertEquals;
44
+import static org.junit.Assert.assertFalse;
45
+import static org.junit.Assert.assertTrue;
46
+import static org.mockito.Matchers.any;
47
+import static org.mockito.Matchers.anyString;
48
+import static org.mockito.Mockito.never;
49
+import static org.mockito.Mockito.verify;
50
+import static org.mockito.Mockito.when;
51
+
52
+@RunWith(MockitoJUnitRunner.class)
53
+public class CoreProfilesDialogModelTest {
54
+
55
+    @Mock private IdentityController identityController;
56
+    @Mock private ProfilesDialogModelListener listener;
57
+    @Mock private IdentityFactory identityFactory;
58
+    @Mock private ConfigProvider profileProvider1;
59
+    @Mock private ConfigProvider profileProvider2;
60
+    @Mock private ConfigProvider profileProvider3;
61
+    private List<ConfigProvider> profiles;
62
+
63
+    @Before
64
+    public void setupMocks() {
65
+        mockProfile("profile1", profileProvider1);
66
+        mockProfile("profile2", profileProvider2);
67
+        mockProfile("profile3", profileProvider3);
68
+        profiles = Lists.newArrayList(profileProvider1, profileProvider2, profileProvider3);
69
+        when(identityController.getProvidersByType(anyString()))
70
+                .thenReturn(profiles);
71
+    }
72
+
73
+    private void mockProfile(final String name, final ConfigProvider mock) {
74
+        when(mock.getName()).thenReturn(name);
75
+        when(mock.getOption("identity", "name")).thenReturn(name);
76
+        when(mock.getOptionList("profile", "nicknames"))
77
+                .thenReturn(Lists.newArrayList("nickname1", "nickname2", "nickname3"));
78
+        when(mock.getOption("profile", "realname")).thenReturn("realname");
79
+        when(mock.getOption("profile", "ident")).thenReturn("ident");
80
+        when(mock.toString()).thenReturn("Identity: " + name);
81
+    }
82
+
83
+    @Test
84
+    public void testGetProfileList() {
85
+        final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
86
+                identityFactory);
87
+        assertEquals("testGetProfileList", profiles.size(), instance.getProfileList().size());
88
+    }
89
+
90
+    @Test
91
+    public void testGetProfileNotExist() {
92
+        final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
93
+                identityFactory);
94
+        assertFalse("testGetProfileNotExist", instance.getProfile("").isPresent());
95
+    }
96
+
97
+    @Test
98
+    public void testGetProfileExist() {
99
+        final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
100
+                identityFactory);
101
+        assertTrue("testGetProfileExist", instance.getProfile("profile1").isPresent());
102
+    }
103
+
104
+    @Test
105
+    public void testIsProfileListValidEmptyList() {
106
+        when(identityController.getProvidersByType(anyString()))
107
+                .thenReturn(Lists.<ConfigProvider>newArrayList());
108
+        final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
109
+                identityFactory);
110
+        assertFalse("testIsProfileListValidEmptyList", instance.isProfileListValid());
111
+    }
112
+
113
+    @Test
114
+    public void testIsProfileListValid() {
115
+        final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
116
+                identityFactory);
117
+        assertTrue("testIsProfileListValid", instance.isProfileListValid());
118
+    }
119
+
120
+    @Test
121
+    public void testAddProfile() {
122
+        when(identityController.getProvidersByType(anyString()))
123
+                .thenReturn(Lists.<ConfigProvider>newArrayList());
124
+        final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
125
+                identityFactory);
126
+        assertFalse("testAddProfile", instance.getProfile("profile4").isPresent());
127
+        instance.addProfile("profile4", "realname", "ident", Lists.newArrayList("nickname"));
128
+        assertTrue("testAddProfile", instance.getProfile("profile4").isPresent());
129
+    }
130
+
131
+    @Test
132
+    public void testEditProfile() {
133
+        final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
134
+                identityFactory);
135
+        final Profile preEdit = instance.getProfile("profile1").get();
136
+        assertEquals("testEditProfile", "profile1", preEdit.getName());
137
+        assertEquals("testEditProfile", "realname", preEdit.getRealname());
138
+        assertEquals("testEditProfile", "ident", preEdit.getIdent());
139
+        assertEquals("testEditProfile", Lists.newArrayList("nickname1", "nickname2", "nickname3"),
140
+                preEdit.getNicknames());
141
+        instance.editProfile("profile1", "newRealname", "newIdent", Lists.newArrayList("nickname"));
142
+        final Profile postEdit = instance.getProfile("profile1").get();
143
+        assertEquals("testEditProfile", "profile1", postEdit.getName());
144
+        assertEquals("testEditProfile", "newRealname", postEdit.getRealname());
145
+        assertEquals("testEditProfile", "newIdent", postEdit.getIdent());
146
+        assertEquals("testEditProfile", Lists.newArrayList("nickname"),
147
+                postEdit.getNicknames());
148
+    }
149
+
150
+    @Test
151
+    public void testRenameProfile() {
152
+        final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
153
+                identityFactory);
154
+        assertTrue("testRenameProfile", instance.getProfile("profile1").isPresent());
155
+        assertFalse("testRenameProfile", instance.getProfile("profile4").isPresent());
156
+        instance.renameProfile("profile1", "profile4");
157
+        assertFalse("testRenameProfile", instance.getProfile("profile1").isPresent());
158
+        assertTrue("testRenameProfile", instance.getProfile("profile4").isPresent());
159
+    }
160
+
161
+    @Test
162
+    public void testRemoveProfile() {
163
+        final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
164
+                identityFactory);
165
+        assertEquals("testRemoveProfile", 3, instance.getProfileList().size());
166
+        assertTrue("testRemoveProfile", instance.getProfile("profile3").isPresent());
167
+        instance.removeProfile("profile3");
168
+        assertEquals("testRemoveProfile", 2, instance.getProfileList().size());
169
+        assertFalse("testRemoveProfile", instance.getProfile("profile3").isPresent());
170
+    }
171
+
172
+    @Test
173
+    public void testSave() {
174
+
175
+    }
176
+
177
+    @Test
178
+    public void testSetSelectedProfile() {
179
+    }
180
+
181
+    @Test
182
+    public void testGetSelectedProfile() {
183
+        final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
184
+                identityFactory);
185
+        assertFalse("testGetSelectedProfile", instance.getSelectedProfile().isPresent());
186
+        instance.setSelectedProfile(instance.getProfile("profile2"));
187
+        assertEquals("testGetSelectedProfile", "profile2",
188
+                instance.getSelectedProfile().get().getName());
189
+    }
190
+
191
+    @Test
192
+    public void testGetSelectedProfileDetails() {
193
+        final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
194
+                identityFactory);
195
+        instance.setSelectedProfile(instance.getProfile("profile1"));
196
+        assertEquals("testGetSelectedProfileDetails", "profile1",
197
+                instance.getSelectedProfileName().get());
198
+        assertEquals("testGetSelectedProfileDetails", "ident",
199
+                instance.getSelectedProfileIdent().get());
200
+        assertEquals("testGetSelectedProfileDetails", "realname",
201
+                instance.getSelectedProfileRealname().get());
202
+        assertEquals("testGetSelectedProfileDetails",
203
+                Lists.newArrayList("nickname1", "nickname2", "nickname3"),
204
+                instance.getSelectedProfileNicknames().get());
205
+    }
206
+
207
+    @Test
208
+    public void testSetSelectedProfileSelectedNickname() {
209
+        final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
210
+                identityFactory);
211
+        instance.setSelectedProfile(instance.getProfile("profile1"));
212
+        assertEquals("testSetSelectedProfileSelectedNickname",
213
+                Optional.absent(), instance.getSelectedProfileSelectedNickname());
214
+        instance.setSelectedProfileSelectedNickname(Optional.fromNullable("nickname2"));
215
+        assertEquals("testSetSelectedProfileSelectedNickname",
216
+                Optional.fromNullable("nickname2"),
217
+                instance.getSelectedProfileSelectedNickname());
218
+    }
219
+
220
+    @Test
221
+    public void testSetSelectedProfileDetails() {
222
+        final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
223
+                identityFactory);
224
+        instance.setSelectedProfile(instance.getProfile("profile1"));
225
+        instance.setSelectedProfileName(Optional.fromNullable("testName"));
226
+        instance.setSelectedProfileIdent(Optional.fromNullable("testIdent"));
227
+        instance.setSelectedProfileRealname(Optional.fromNullable("testRealname"));
228
+        instance.setSelectedProfileNicknames(Optional.fromNullable((List<String>) Lists.
229
+                newArrayList("testNickname")));
230
+        assertEquals("testGetSelectedProfileDetails", "testName",
231
+                instance.getSelectedProfileName().get());
232
+        assertEquals("testGetSelectedProfileDetails", "testIdent",
233
+                instance.getSelectedProfileIdent().get());
234
+        assertEquals("testGetSelectedProfileDetails", "testRealname",
235
+                instance.getSelectedProfileRealname().get());
236
+        assertEquals("testGetSelectedProfileDetails",
237
+                Lists.newArrayList("testNickname"),
238
+                instance.getSelectedProfileNicknames().get());
239
+    }
240
+
241
+    @Test
242
+    public void testAddSelectedProfileNickname() {
243
+        final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
244
+                identityFactory);
245
+        instance.setSelectedProfile(instance.getProfile("profile1"));
246
+        assertEquals("testAddSelectedProfileNickname",
247
+                Lists.newArrayList("nickname1", "nickname2", "nickname3"),
248
+                instance.getSelectedProfileNicknames().get());
249
+        instance.addSelectedProfileNickname("nickname4");
250
+        assertEquals("testAddSelectedProfileNickname",
251
+                Lists.newArrayList("nickname1", "nickname2", "nickname3", "nickname4"),
252
+                instance.getSelectedProfileNicknames().get());
253
+    }
254
+
255
+    @Test
256
+    public void testRemoveSelectedProfileNickname() {
257
+        final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
258
+                identityFactory);
259
+        instance.setSelectedProfile(instance.getProfile("profile1"));
260
+        assertEquals("testRemoveSelectedProfileNickname",
261
+                Lists.newArrayList("nickname1", "nickname2", "nickname3"),
262
+                instance.getSelectedProfileNicknames().get());
263
+        instance.removeSelectedProfileNickname("nickname3");
264
+        assertEquals("testRemoveSelectedProfileNickname",
265
+                Lists.newArrayList("nickname1", "nickname2"),
266
+                instance.getSelectedProfileNicknames().get());
267
+    }
268
+
269
+    @Test
270
+    public void testEditSelectedProfileSelectedNickname() {
271
+        final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
272
+                identityFactory);
273
+        instance.setSelectedProfile(instance.getProfile("profile1"));
274
+        instance.setSelectedProfileSelectedNickname(Optional.fromNullable("nickname2"));
275
+        assertEquals("testSetSelectedProfileSelectedNickname",
276
+                Optional.fromNullable("nickname2"),
277
+                instance.getSelectedProfileSelectedNickname());
278
+        instance.editSelectedProfileNickname("nickname2", "nickname4");
279
+        assertEquals("testAddSelectedProfileNickname",
280
+                Lists.newArrayList("nickname1", "nickname4", "nickname3"),
281
+                instance.getSelectedProfileNicknames().get());
282
+    }
283
+
284
+    @Test
285
+    public void testAddListener() {
286
+        final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
287
+                identityFactory);
288
+        instance.addListener(listener);
289
+        instance.addProfile("profile4", "realname", "ident", Lists.newArrayList("nickname"));
290
+        verify(listener).profileAdded(any(Profile.class));
291
+    }
292
+
293
+    @Test
294
+    public void testRemoveListener() {
295
+        final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
296
+                identityFactory);
297
+        instance.addListener(listener);
298
+        instance.addProfile("profile4", "realname", "ident", Lists.newArrayList("nickname"));
299
+        assertTrue(instance.getProfile("profile4").isPresent());
300
+        final Profile profile4 = instance.getProfile("profile4").get();
301
+        verify(listener).profileAdded(profile4);
302
+        instance.removeListener(listener);
303
+        instance.setSelectedProfile(Optional.fromNullable(profile4));
304
+        verify(listener, never()).profileRemoved(profile4);
305
+    }
306
+
307
+}

+ 80
- 0
test/com/dmdirc/ui/core/profiles/EditSelectedNicknameValidatorTest.java Zobrazit soubor

@@ -0,0 +1,80 @@
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.ui.core.profiles;
24
+
25
+import com.dmdirc.actions.wrappers.Profile;
26
+import com.dmdirc.interfaces.ui.ProfilesDialogModel;
27
+
28
+import com.google.common.base.Optional;
29
+
30
+import java.util.List;
31
+
32
+import org.junit.Before;
33
+import org.junit.Test;
34
+import org.junit.runner.RunWith;
35
+import org.mockito.Mock;
36
+import org.mockito.runners.MockitoJUnitRunner;
37
+
38
+import clover.com.google.common.collect.Lists;
39
+
40
+import static org.junit.Assert.assertFalse;
41
+import static org.junit.Assert.assertTrue;
42
+import static org.mockito.Mockito.when;
43
+
44
+@RunWith(MockitoJUnitRunner.class)
45
+public class EditSelectedNicknameValidatorTest {
46
+
47
+    @Mock private ProfilesDialogModel model;
48
+    @Mock private Profile profile;
49
+    private List<String> nicknames;
50
+
51
+    @Before
52
+    public void setupModel() {
53
+        nicknames = Lists.newArrayList("nickname1", "nickname2", "nickname3");
54
+        when(model.getSelectedProfile())
55
+                .thenReturn(Optional.fromNullable(profile));
56
+        when(model.getSelectedProfileNicknames())
57
+                .thenReturn(Optional.fromNullable(nicknames));
58
+        when(model.getSelectedProfileSelectedNickname())
59
+                .thenReturn(Optional.fromNullable("nickname2"));
60
+    }
61
+
62
+    @Test
63
+    public void testDuplicateName() {
64
+        final EditSelectedNicknameValidator instance = new EditSelectedNicknameValidator(model);
65
+        assertTrue("testDuplicateName", instance.validate("nickname1").isFailure());
66
+    }
67
+
68
+    @Test
69
+    public void testNonDuplicateName() {
70
+        final EditSelectedNicknameValidator instance = new EditSelectedNicknameValidator(model);
71
+        assertFalse("testNonDuplicateName", instance.validate("nickname4").isFailure());
72
+    }
73
+
74
+    @Test
75
+    public void testSelectedName() {
76
+        final EditSelectedNicknameValidator instance = new EditSelectedNicknameValidator(model);
77
+        assertFalse("testSelectedName", instance.validate("nickname2").isFailure());
78
+    }
79
+
80
+}

+ 84
- 0
test/com/dmdirc/ui/core/profiles/EditSelectedProfileNameValidatorTest.java Zobrazit soubor

@@ -0,0 +1,84 @@
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.ui.core.profiles;
24
+
25
+import com.dmdirc.actions.wrappers.Profile;
26
+import com.dmdirc.interfaces.ui.ProfilesDialogModel;
27
+
28
+import com.google.common.base.Optional;
29
+
30
+import java.util.List;
31
+
32
+import org.junit.Before;
33
+import org.junit.Test;
34
+import org.junit.runner.RunWith;
35
+import org.mockito.Mock;
36
+import org.mockito.runners.MockitoJUnitRunner;
37
+
38
+import clover.com.google.common.collect.Lists;
39
+
40
+import static org.junit.Assert.assertFalse;
41
+import static org.junit.Assert.assertTrue;
42
+import static org.mockito.Mockito.when;
43
+
44
+@RunWith(MockitoJUnitRunner.class)
45
+public class EditSelectedProfileNameValidatorTest {
46
+
47
+    @Mock private ProfilesDialogModel model;
48
+    @Mock private Profile profile1;
49
+    @Mock private Profile profile2;
50
+    @Mock private Profile profile3;
51
+    private List<Profile> profiles;
52
+
53
+    @Before
54
+    public void setupModel() {
55
+        profiles = Lists.newArrayList(profile1, profile2, profile3);
56
+        when(profile1.getName()).thenReturn("profile1");
57
+        when(profile2.getName()).thenReturn("profile2");
58
+        when(profile3.getName()).thenReturn("profile3");
59
+        when(model.getProfileList()).thenReturn(profiles);
60
+        when(model.getSelectedProfile()).thenReturn(Optional.fromNullable(profile2));
61
+    }
62
+
63
+    @Test
64
+    public void testDuplicateName() {
65
+        final EditSelectedProfileNameValidator instance
66
+                = new EditSelectedProfileNameValidator(model);
67
+        assertTrue("testDuplicateName", instance.validate("profile1").isFailure());
68
+    }
69
+
70
+    @Test
71
+    public void testNonDuplicateName() {
72
+        final EditSelectedProfileNameValidator instance
73
+                = new EditSelectedProfileNameValidator(model);
74
+        assertFalse("testNonDuplicateName", instance.validate("profile4").isFailure());
75
+    }
76
+
77
+    @Test
78
+    public void testSelectedName() {
79
+        final EditSelectedProfileNameValidator instance
80
+                = new EditSelectedProfileNameValidator(model);
81
+        assertFalse("testSelectedName", instance.validate("profile2").isFailure());
82
+    }
83
+
84
+}

+ 84
- 0
test/com/dmdirc/ui/core/profiles/NewProfileNameValidatorTest.java Zobrazit soubor

@@ -0,0 +1,84 @@
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.ui.core.profiles;
24
+
25
+import com.dmdirc.actions.wrappers.Profile;
26
+import com.dmdirc.interfaces.ui.ProfilesDialogModel;
27
+
28
+import com.google.common.base.Optional;
29
+
30
+import java.util.List;
31
+
32
+import org.junit.Before;
33
+import org.junit.Test;
34
+import org.junit.runner.RunWith;
35
+import org.mockito.Mock;
36
+import org.mockito.runners.MockitoJUnitRunner;
37
+
38
+import clover.com.google.common.collect.Lists;
39
+
40
+import static org.junit.Assert.assertFalse;
41
+import static org.junit.Assert.assertTrue;
42
+import static org.mockito.Mockito.when;
43
+
44
+@RunWith(MockitoJUnitRunner.class)
45
+public class NewProfileNameValidatorTest {
46
+
47
+    @Mock private ProfilesDialogModel model;
48
+    @Mock private Profile profile1;
49
+    @Mock private Profile profile2;
50
+    @Mock private Profile profile3;
51
+    private List<Profile> profiles;
52
+
53
+    @Before
54
+    public void setupModel() {
55
+        profiles = Lists.newArrayList(profile1, profile2, profile3);
56
+        when(profile1.getName()).thenReturn("profile1");
57
+        when(profile2.getName()).thenReturn("profile2");
58
+        when(profile3.getName()).thenReturn("profile3");
59
+        when(model.getProfileList()).thenReturn(profiles);
60
+        when(model.getSelectedProfile()).thenReturn(Optional.fromNullable(profile2));
61
+    }
62
+
63
+    @Test
64
+    public void testDuplicateName() {
65
+        final NewProfileNameValidator instance
66
+                = new NewProfileNameValidator(model);
67
+        assertTrue("testDuplicateName", instance.validate("profile1").isFailure());
68
+    }
69
+
70
+    @Test
71
+    public void testNonDuplicateName() {
72
+        final NewProfileNameValidator instance
73
+                = new NewProfileNameValidator(model);
74
+        assertFalse("testNonDuplicateName", instance.validate("profile4").isFailure());
75
+    }
76
+
77
+    @Test
78
+    public void testSelectedName() {
79
+        final NewProfileNameValidator instance
80
+                = new NewProfileNameValidator(model);
81
+        assertTrue("testSelectedName", instance.validate("profile2").isFailure());
82
+    }
83
+
84
+}

Načítá se…
Zrušit
Uložit