Browse Source

Merge pull request #147 from greboid/master

Add tests + fix profiles migrator.
pull/149/head
Chris Smith 9 years ago
parent
commit
264af7bb9c

+ 1
- 1
src/com/dmdirc/config/profiles/IdentitiesProfileMigrator.java View File

@@ -68,7 +68,7 @@ public class IdentitiesProfileMigrator implements Migrator {
68 68
             }
69 69
             profileManager.addProfile(
70 70
                     new Profile(p.getName(), p.getOption(DOMAIN_PROFILE, "realname"), ident,
71
-                            Lists.newArrayList(p.getOption(DOMAIN_PROFILE, "nicknames"))));
71
+                            p.getOptionList(DOMAIN_PROFILE, "nicknames")));
72 72
             try {
73 73
                 p.delete();
74 74
             } catch (IOException e) {

+ 118
- 0
test/com/dmdirc/config/profiles/IdentitiesProfileMigratorTest.java View File

@@ -0,0 +1,118 @@
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.config.profiles;
24
+
25
+import com.dmdirc.config.IdentityManager;
26
+import com.dmdirc.interfaces.config.ConfigProvider;
27
+
28
+import com.google.common.collect.Lists;
29
+
30
+import java.util.Optional;
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 static junit.framework.TestCase.assertFalse;
39
+import static junit.framework.TestCase.assertTrue;
40
+import static org.mockito.Mockito.verify;
41
+import static org.mockito.Mockito.when;
42
+
43
+@RunWith(MockitoJUnitRunner.class)
44
+public class IdentitiesProfileMigratorTest {
45
+
46
+    @Mock private IdentityManager identityManager;
47
+    @Mock private ProfileManager profileManager;
48
+    @Mock private ConfigProvider configProvider1;
49
+    @Mock private ConfigProvider configProvider2;
50
+    @Mock private ConfigProvider configProvider3;
51
+
52
+    private IdentitiesProfileMigrator instance;
53
+
54
+    @Before
55
+    public void setup() {
56
+        instance = new IdentitiesProfileMigrator(identityManager, profileManager);
57
+        when(configProvider1.getName()).thenReturn("name1");
58
+        when(configProvider1.getOption("profile", "realname")).thenReturn("realname1");
59
+        when(configProvider1.hasOptionString("profile", "ident1")).thenReturn(false);
60
+        when(configProvider1.getOptionList("profile", "nicknames")).thenReturn(
61
+                Lists.newArrayList("nickname1")
62
+        );
63
+        when(configProvider2.getName()).thenReturn("name2");
64
+        when(configProvider2.getOption("profile", "realname")).thenReturn("realname2");
65
+        when(configProvider2.hasOptionString("profile", "ident")).thenReturn(true);
66
+        when(configProvider2.getOption("profile", "ident")).thenReturn("ident2");
67
+        when(configProvider2.getOptionList("profile", "nicknames")).thenReturn(
68
+                Lists.newArrayList("nickname2")
69
+        );
70
+        when(configProvider3.getName()).thenReturn("name3");
71
+        when(configProvider3.getOption("profile", "realname")).thenReturn("realname3");
72
+        when(configProvider3.hasOptionString("profile", "ident")).thenReturn(true);
73
+        when(configProvider3.getOption("profile", "ident")).thenReturn("ident3");
74
+        when(configProvider3.getOptionList("profile", "nicknames")).thenReturn(
75
+                Lists.newArrayList("nickname31", "nickname32", "nickname33")
76
+        );
77
+    }
78
+
79
+    @Test
80
+    public void testNeedsMigration_NoProfiles() {
81
+        when(identityManager.getProvidersByType("profile")).thenReturn(Lists.newArrayList());
82
+        assertFalse(instance.needsMigration());
83
+    }
84
+
85
+    @Test
86
+    public void testNeedsMigration_Profiles() {
87
+        when(identityManager.getProvidersByType("profile")).thenReturn(
88
+                Lists.newArrayList(configProvider1));
89
+        assertTrue(instance.needsMigration());
90
+    }
91
+
92
+    @Test
93
+    public void testMigrate_NoIdent() {
94
+        when(identityManager.getProvidersByType("profile")).thenReturn(
95
+                Lists.newArrayList(configProvider1));
96
+        instance.migrate();
97
+        verify(profileManager).addProfile(new Profile("name1", "realname1", Optional.empty(),
98
+                Lists.newArrayList("nickname1")));
99
+    }
100
+
101
+    @Test
102
+    public void testMigrate_Ident() {
103
+        when(identityManager.getProvidersByType("profile")).thenReturn(
104
+                Lists.newArrayList(configProvider2));
105
+        instance.migrate();
106
+        verify(profileManager).addProfile(new Profile("name2", "realname2", Optional.of("ident2"),
107
+                Lists.newArrayList("nickname2")));
108
+    }
109
+
110
+    @Test
111
+    public void testMigrate_MultipleNicknames() {
112
+        when(identityManager.getProvidersByType("profile")).thenReturn(
113
+                Lists.newArrayList(configProvider3));
114
+        instance.migrate();
115
+        verify(profileManager).addProfile(new Profile("name3", "realname3", Optional.of("ident3"),
116
+                Lists.newArrayList("nickname31", "nickname32", "nickname33")));
117
+    }
118
+}

+ 25
- 0
test/com/dmdirc/config/profiles/ProfileManagerTest.java View File

@@ -26,6 +26,10 @@ import com.dmdirc.DMDircMBassador;
26 26
 import com.dmdirc.events.ProfileAddedEvent;
27 27
 import com.dmdirc.events.ProfileDeletedEvent;
28 28
 
29
+import com.google.common.collect.Lists;
30
+
31
+import java.util.Optional;
32
+
29 33
 import org.junit.Before;
30 34
 import org.junit.Test;
31 35
 import org.junit.runner.RunWith;
@@ -88,4 +92,25 @@ public class ProfileManagerTest {
88 92
         instance.deleteProfile(profile1);
89 93
         verify(eventBus).publishAsync(any(ProfileDeletedEvent.class));
90 94
     }
95
+
96
+    @Test
97
+    public void testGetDefaultProfile_EmptyList() {
98
+        final String nick = System.getProperty("user.name").replace(' ', '_');
99
+        final Profile profile = new Profile(nick, nick, Optional.empty(), Lists.newArrayList(nick));
100
+        assertEquals(profile, instance.getDefault());
101
+    }
102
+
103
+    @Test
104
+    public void testGetDefaultProfile_1() {
105
+        instance.addProfile(profile1);
106
+        instance.addProfile(profile2);
107
+        assertEquals(profile1, instance.getDefault());
108
+    }
109
+
110
+    @Test
111
+    public void testGetDefaultProfile_2() {
112
+        instance.addProfile(profile2);
113
+        instance.addProfile(profile1);
114
+        assertEquals(profile2, instance.getDefault());
115
+    }
91 116
 }

Loading…
Cancel
Save