浏览代码

Move profile to core.

Issue: CLIENT-464
Change-Id: Idf8b7726de5132bf67836b7613ba7fd616f3d414
Reviewed-on: http://gerrit.dmdirc.com/3303
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
pull/1/head
Greg Holmes 10 年前
父节点
当前提交
a6b182da1e
共有 2 个文件被更改,包括 488 次插入0 次删除
  1. 237
    0
      src/com/dmdirc/actions/wrappers/Profile.java
  2. 251
    0
      test/com/dmdirc/actions/wrappers/ProfileTest.java

+ 237
- 0
src/com/dmdirc/actions/wrappers/Profile.java 查看文件

@@ -0,0 +1,237 @@
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.actions.wrappers;
24
+
25
+import com.dmdirc.interfaces.config.ConfigProvider;
26
+import com.dmdirc.interfaces.config.IdentityFactory;
27
+
28
+import com.google.common.base.Preconditions;
29
+
30
+import java.util.ArrayList;
31
+import java.util.Arrays;
32
+import java.util.List;
33
+import java.util.Objects;
34
+
35
+import javax.annotation.Nonnull;
36
+
37
+/**
38
+ * Profile wrapper class.
39
+ */
40
+public class Profile {
41
+
42
+    /** Identity backing this profile. */
43
+    private ConfigProvider configProvider;
44
+    /** Profile Name, must be a sanitised filename. */
45
+    private String name;
46
+    /** Real name. */
47
+    private String realname;
48
+    /** Ident. */
49
+    private String ident;
50
+    /** Nicknames. */
51
+    private List<String> nicknames;
52
+    /** Has this profile been marked deleted? */
53
+    private boolean deleted;
54
+    /** Factory to use to create profiles when saving. */
55
+    private final IdentityFactory identityFactory;
56
+
57
+    /**
58
+     * Creates a new profile.
59
+     *
60
+     * @param name            Profile name
61
+     * @param identityFactory The factory to use to create the profile's config file when saving.
62
+     */
63
+    public Profile(final String name, final IdentityFactory identityFactory) {
64
+        this.identityFactory = identityFactory;
65
+        this.configProvider = null;
66
+        this.name = name;
67
+        this.nicknames = new ArrayList<>(Arrays.asList(name.replaceAll(" ", "")));
68
+        this.realname = name;
69
+        this.ident = "";
70
+    }
71
+
72
+    /**
73
+     * Creates a new profile based off the specified Identity.
74
+     *
75
+     * @param identityFactory The factory to use to create the profile's config file when saving.
76
+     * @param configProvider  Provider to read existing profile from.
77
+     */
78
+    public Profile(final IdentityFactory identityFactory,
79
+            @Nonnull final ConfigProvider configProvider) {
80
+        Preconditions.checkNotNull(configProvider);
81
+        this.identityFactory = identityFactory;
82
+        this.configProvider = configProvider;
83
+        this.name = configProvider.getOption("identity", "name");
84
+        this.nicknames = configProvider.getOptionList("profile", "nicknames");
85
+        this.realname = configProvider.getOption("profile", "realname");
86
+        this.ident = configProvider.getOption("profile", "ident");
87
+    }
88
+
89
+    public String getName() {
90
+        return name;
91
+    }
92
+
93
+    public void setName(final String name) {
94
+        this.name = name;
95
+    }
96
+
97
+    public String getRealname() {
98
+        return realname;
99
+    }
100
+
101
+    public void setRealname(final String realname) {
102
+        this.realname = realname;
103
+    }
104
+
105
+    public String getIdent() {
106
+        return ident;
107
+    }
108
+
109
+    public void setIdent(final String ident) {
110
+        this.ident = ident;
111
+    }
112
+
113
+    public List<String> getNicknames() {
114
+        return nicknames;
115
+    }
116
+
117
+    public void setNicknames(final List<String> nicknames) {
118
+        this.nicknames = nicknames;
119
+    }
120
+
121
+    public boolean isDeleted() {
122
+        return deleted;
123
+    }
124
+
125
+    public void setDeleted(final boolean deleted) {
126
+        this.deleted = deleted;
127
+    }
128
+
129
+    /**
130
+     * Adds a nickname to this profile.
131
+     *
132
+     * @param nickname A new nickname for the profile
133
+     */
134
+    public void addNickname(final String nickname) {
135
+        if (!nicknames.contains(nickname)) {
136
+            nicknames.add(nickname);
137
+        }
138
+    }
139
+
140
+    /**
141
+     * Adds a nickname to this profile.
142
+     *
143
+     * @param nickname A new nickname for the profile
144
+     * @param position Position for the new alternate nickname
145
+     */
146
+    public void addNickname(final String nickname, final int position) {
147
+        if (!nicknames.contains(nickname)) {
148
+            nicknames.add(position, nickname);
149
+        }
150
+    }
151
+
152
+    /**
153
+     * Deletes a nickname from this profile.
154
+     *
155
+     * @param nickname An existing nickname from the profile
156
+     */
157
+    public void delNickname(final String nickname) {
158
+        nicknames.remove(nickname);
159
+    }
160
+
161
+    /**
162
+     * Edits a nickname in the list.
163
+     *
164
+     * @param nickname    Nickname to edit
165
+     * @param newNickname Edited nickname
166
+     */
167
+    public void editNickname(final String nickname, final String newNickname) {
168
+        if (nickname.isEmpty() || newNickname.isEmpty()) {
169
+            return;
170
+        }
171
+        if (!nickname.equals(newNickname)) {
172
+            final int index = nicknames.indexOf(nickname);
173
+            nicknames.remove(nickname);
174
+            nicknames.add(index, newNickname);
175
+        }
176
+    }
177
+
178
+    /** Saves this profile. */
179
+    public void save() {
180
+        if (configProvider == null) {
181
+            configProvider = identityFactory.createProfileConfig(name);
182
+        }
183
+
184
+        configProvider.setOption("identity", "name", name);
185
+        configProvider.setOption("profile", "nicknames", nicknames);
186
+        configProvider.setOption("profile", "realname", realname);
187
+        configProvider.setOption("profile", "ident", ident);
188
+    }
189
+
190
+    /**
191
+     * Deletes the profile.
192
+     */
193
+    public void delete() {
194
+        if (configProvider == null) {
195
+            return;
196
+        }
197
+        configProvider.delete();
198
+    }
199
+
200
+    @Override
201
+    public int hashCode() {
202
+        int hash = 7;
203
+        hash = 59 * hash + Objects.hashCode(this.name);
204
+        hash = 59 * hash + Objects.hashCode(this.realname);
205
+        hash = 59 * hash + Objects.hashCode(this.ident);
206
+        hash = 59 * hash + Objects.hashCode(this.nicknames);
207
+        return hash;
208
+    }
209
+
210
+    @Override
211
+    public boolean equals(final Object obj) {
212
+        if (obj == null) {
213
+            return false;
214
+        }
215
+        if (getClass() != obj.getClass()) {
216
+            return false;
217
+        }
218
+        final Profile other = (Profile) obj;
219
+        if (!Objects.equals(this.name, other.name)) {
220
+            return false;
221
+        }
222
+        if (!Objects.equals(this.realname, other.realname)) {
223
+            return false;
224
+        }
225
+        if (!Objects.equals(this.ident, other.ident)) {
226
+            return false;
227
+        }
228
+        return Objects.equals(this.nicknames, other.nicknames);
229
+    }
230
+
231
+    @Override
232
+    public String toString() {
233
+        return "Profile{" + "name=" + name + ", realname=" + realname
234
+                + ", ident=" + ident + ", nicknames=" + nicknames + ", deleted=" + deleted + '}';
235
+    }
236
+
237
+}

+ 251
- 0
test/com/dmdirc/actions/wrappers/ProfileTest.java 查看文件

@@ -0,0 +1,251 @@
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
20
+ * THE SOFTWARE.
21
+ */
22
+package com.dmdirc.actions.wrappers;
23
+
24
+import com.dmdirc.actions.wrappers.Profile;
25
+import com.dmdirc.interfaces.config.ConfigProvider;
26
+import com.dmdirc.interfaces.config.IdentityFactory;
27
+
28
+import java.util.ArrayList;
29
+import java.util.Arrays;
30
+import java.util.List;
31
+
32
+import org.junit.Test;
33
+import org.junit.runner.RunWith;
34
+import org.mockito.Mock;
35
+import org.mockito.runners.MockitoJUnitRunner;
36
+
37
+import static org.junit.Assert.assertEquals;
38
+import static org.junit.Assert.assertTrue;
39
+import static org.mockito.Mockito.mock;
40
+import static org.mockito.Mockito.verify;
41
+import static org.mockito.Mockito.when;
42
+
43
+/**
44
+ * Test profile class.
45
+ */
46
+@RunWith(MockitoJUnitRunner.class)
47
+public class ProfileTest {
48
+
49
+    @Mock private IdentityFactory identityFactory;
50
+
51
+    private Profile createProfile() {
52
+        final List<String> nicknames = new ArrayList<>();
53
+        nicknames.add("nickname1");
54
+        nicknames.add("nickname2");
55
+        final ConfigProvider configProvider = mock(ConfigProvider.class);
56
+        when(configProvider.getName()).thenReturn("profile");
57
+        when(configProvider.getOption("identity", "name")).thenReturn("profile");
58
+        when(configProvider.getOptionList("profile", "nicknames")).thenReturn(nicknames);
59
+        when(configProvider.getOption("profile", "realname")).thenReturn("realname");
60
+        when(configProvider.getOption("profile", "ident")).thenReturn("ident");
61
+
62
+        return new Profile(identityFactory, configProvider);
63
+    }
64
+
65
+    /**
66
+     * Test null profile constructor.
67
+     */
68
+    @Test
69
+    public void testEmptyConstructor() {
70
+        Profile instance = new Profile("New Profile", identityFactory);
71
+        assertEquals("", instance.getIdent());
72
+        assertEquals("New Profile", instance.getName());
73
+        assertEquals(new ArrayList<>(Arrays.asList("NewProfile")), instance.getNicknames());
74
+        assertEquals("New Profile", instance.getRealname());
75
+    }
76
+
77
+    /**
78
+     * Test null profile constructor.
79
+     */
80
+    @Test
81
+    public void testIdentityConstructor() {
82
+        Profile instance = createProfile();
83
+        assertEquals("ident", instance.getIdent());
84
+        assertEquals("profile", instance.getName());
85
+        assertTrue(Arrays.asList(new String[]{"nickname1", "nickname2", })
86
+                .equals(instance.getNicknames()));
87
+        assertEquals("realname", instance.getRealname());
88
+    }
89
+
90
+    /**
91
+     * Test of addNickname method, of class Profile.
92
+     */
93
+    @Test
94
+    public void testAddNickname_String() {
95
+        Profile instance = createProfile();
96
+        instance.addNickname("nickname3");
97
+        assertTrue(Arrays.asList(new String[]{"nickname1", "nickname2",
98
+            "nickname3"}).equals(instance.getNicknames()));
99
+    }
100
+
101
+    /**
102
+     * Test of addNickname method, of class Profile.
103
+     */
104
+    @Test
105
+    public void testAddNickname_String_Contains() {
106
+        Profile instance = createProfile();
107
+        instance.addNickname("nickname2");
108
+        assertTrue(Arrays.asList(new String[]{"nickname1", "nickname2"})
109
+                .equals(instance.getNicknames()));
110
+    }
111
+
112
+    /**
113
+     * Test of addNickname method, of class Profile.
114
+     */
115
+    @Test
116
+    public void testAddNickname_String_int() {
117
+        Profile instance = createProfile();
118
+        instance.addNickname("nickname3", 0);
119
+        assertTrue(Arrays.asList(new String[]{"nickname3", "nickname1",
120
+            "nickname2"}).equals(instance.getNicknames()));
121
+    }
122
+
123
+    /**
124
+     * Test of addNickname method, of class Profile.
125
+     */
126
+    @Test
127
+    public void testAddNickname_String_int_Contains() {
128
+        Profile instance = createProfile();
129
+        instance.addNickname("nickname2", 0);
130
+        assertTrue(Arrays.asList(new String[]{"nickname1","nickname2"})
131
+                .equals(instance.getNicknames()));
132
+    }
133
+
134
+    /**
135
+     * Test of delNickname method, of class Profile.
136
+     */
137
+    @Test
138
+    public void testDelNickname() {
139
+        Profile instance = createProfile();
140
+        instance.delNickname("nickname2");
141
+        assertTrue(Arrays.asList(new String[]{"nickname1"})
142
+                .equals(instance.getNicknames()));
143
+    }
144
+
145
+    /**
146
+     * Test of save method, of class Profile.
147
+     */
148
+    @Test
149
+    public void testSave() {
150
+        final List<String> nicknames = new ArrayList<>();
151
+        nicknames.add("nickname1");
152
+        nicknames.add("nickname2");
153
+        final ConfigProvider configProvider = mock(ConfigProvider.class);
154
+        when(configProvider.getName()).thenReturn("profile");
155
+        when(configProvider.getOption("identity", "name")).thenReturn("profile");
156
+        when(configProvider.getOptionList("profile", "nicknames")).thenReturn(nicknames);
157
+        when(configProvider.getOption("profile", "realname")).thenReturn("realname");
158
+        when(configProvider.getOption("profile", "ident")).thenReturn("ident");
159
+
160
+        Profile instance = new Profile(identityFactory, configProvider);
161
+        instance.save();
162
+        verify(configProvider).setOption("identity", "name", "profile");
163
+        verify(configProvider).setOption("profile", "nicknames", nicknames);
164
+        verify(configProvider).setOption("profile", "realname", "realname");
165
+        verify(configProvider).setOption("profile", "ident", "ident");
166
+    }
167
+
168
+    @Test
169
+    public void testSaveNoConfig() {
170
+        final ConfigProvider configProvider = mock(ConfigProvider.class);
171
+
172
+        when(identityFactory.createProfileConfig("New Profile")).thenReturn(configProvider);
173
+
174
+        Profile instance = new Profile("New Profile", identityFactory);
175
+        instance.save();
176
+        verify(identityFactory).createProfileConfig("New Profile");
177
+    }
178
+
179
+    /**
180
+     * Test of editNickname method, of class Profile.
181
+     */
182
+    @Test
183
+    public void testEditNicknameOldEmpty() {
184
+        Profile instance = createProfile();
185
+        instance.editNickname("", "nickname3");
186
+        assertTrue(Arrays.asList(new String[]{"nickname1", "nickname2"})
187
+                .equals(instance.getNicknames()));
188
+    }
189
+
190
+    /**
191
+     * Test of editNickname method, of class Profile.
192
+     */
193
+    @Test
194
+    public void testEditNicknameNewEmpty() {
195
+        Profile instance = createProfile();
196
+        instance.editNickname("nickname2", "");
197
+        assertTrue(Arrays.asList(new String[]{"nickname1", "nickname2"})
198
+                .equals(instance.getNicknames()));
199
+    }
200
+
201
+    /**
202
+     * Test of editNickname method, of class Profile.
203
+     */
204
+    @Test
205
+    public void testEditNicknameSame() {
206
+        Profile instance = createProfile();
207
+        instance.editNickname("nickname2", "nickname2");
208
+        assertTrue(Arrays.asList(new String[]{"nickname1", "nickname2"})
209
+                .equals(instance.getNicknames()));
210
+    }
211
+
212
+    /**
213
+     * Test of editNickname method, of class Profile.
214
+     */
215
+    @Test
216
+    public void testEditNickname() {
217
+        Profile instance = createProfile();
218
+        instance.editNickname("nickname2", "nickname3");
219
+        assertTrue(Arrays.asList(new String[]{"nickname1", "nickname3"})
220
+                .equals(instance.getNicknames()));
221
+    }
222
+
223
+    /**
224
+     * Test of delete method, of class Profile.
225
+     */
226
+    @Test
227
+    public void testDelete() {
228
+        final List<String> nicknames = new ArrayList<>();
229
+        nicknames.add("nickname1");
230
+        nicknames.add("nickname2");
231
+        final ConfigProvider configProvider = mock(ConfigProvider.class);
232
+        when(configProvider.getName()).thenReturn("profile");
233
+        when(configProvider.getOption("identity", "name")).thenReturn("profile");
234
+        when(configProvider.getOptionList("profile", "nicknames")).thenReturn(nicknames);
235
+        when(configProvider.getOption("profile", "realname")).thenReturn("realname");
236
+        when(configProvider.getOption("profile", "ident")).thenReturn("ident");
237
+
238
+        Profile instance = new Profile(identityFactory, configProvider);
239
+        instance.delete();
240
+        verify(configProvider).delete();
241
+    }
242
+
243
+    /**
244
+     * Test of delete method, of class Profile.
245
+     */
246
+    @Test
247
+    public void testDeleteNullIdentity() {
248
+        Profile instance = new Profile("New Profile", null);
249
+        instance.delete();
250
+    }
251
+}

正在加载...
取消
保存