Browse Source

Rewrite the ConfigFileBackedConfigProviderTest.

(aka the "IdentityTest")

Make it a lot less dumb, use a virtual FS so we can test reloading
and saving. Fix a bunch of bugs the tests highlighted.

Only things not really tested are the global config specific
behaviours.

Change-Id: I96448eb9c401ce864cd6f39fd95088a4a8a09727
Reviewed-on: http://gerrit.dmdirc.com/4015
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
pull/1/head
Chris Smith 9 years ago
parent
commit
8c4160a590

+ 24
- 6
src/com/dmdirc/config/ConfigFileBackedConfigProvider.java View File

@@ -42,6 +42,7 @@ import java.util.LinkedList;
42 42
 import java.util.List;
43 43
 import java.util.Map;
44 44
 import java.util.Set;
45
+import java.util.Objects;
45 46
 
46 47
 import javax.annotation.Nullable;
47 48
 
@@ -210,7 +211,7 @@ public class ConfigFileBackedConfigProvider extends BaseConfigProvider implement
210 211
         final Collection<String[]> changes = new LinkedList<>();
211 212
 
212 213
         synchronized (this) {
213
-            final Map<String, Map<String, String>> oldProps = file.getKeyDomains();
214
+            final Map<String, Map<String, String>> oldProps = new HashMap<>(file.getKeyDomains());
214 215
 
215 216
             file.read();
216 217
 
@@ -221,20 +222,34 @@ public class ConfigFileBackedConfigProvider extends BaseConfigProvider implement
221 222
                     final String key = subentry.getKey();
222 223
                     final String value = subentry.getValue();
223 224
 
224
-                    if (!oldProps.containsKey(domain)) {
225
+                    if (!oldProps.containsKey(domain) || !oldProps.get(domain).containsKey(key)) {
226
+                        // Newly added (didn't exist in the old file)
225 227
                         changes.add(new String[]{domain, key});
226
-                    } else if (!oldProps.get(domain).containsKey(key)
227
-                            || !oldProps.get(domain).get(key).equals(value)) {
228
+                    } else if (!oldProps.get(domain).get(key).equals(value)) {
229
+                        // Modified in some way
228 230
                         changes.add(new String[]{domain, key});
229 231
                         oldProps.get(domain).remove(key);
232
+                    } else {
233
+                        // Not modified
234
+                        oldProps.get(domain).remove(key);
230 235
                     }
231 236
                 }
232 237
 
238
+                // Anything left in the domain must have been moved
233 239
                 if (oldProps.containsKey(domain)) {
234 240
                     for (String key : oldProps.get(domain).keySet()) {
235 241
                         changes.add(new String[]{domain, key});
236 242
                     }
237 243
                 }
244
+
245
+                oldProps.remove(domain);
246
+            }
247
+
248
+            // Any domains left must have been removed
249
+            for (Map.Entry<String, Map<String, String>> entry : oldProps.entrySet()) {
250
+                for (String key : entry.getValue().keySet()) {
251
+                    changes.add(new String[]{entry.getKey(), key});
252
+                }
238 253
             }
239 254
         }
240 255
 
@@ -360,8 +375,7 @@ public class ConfigFileBackedConfigProvider extends BaseConfigProvider implement
360 375
 
361 376
         // Fire any setting change listeners now we're no longer holding
362 377
         // a lock on this identity.
363
-        if (unset || oldValue == null && value != null
364
-                || oldValue != null && !oldValue.equals(value)) {
378
+        if (unset || !Objects.equals(oldValue, value)) {
365 379
             fireSettingChange(domain, option);
366 380
         }
367 381
     }
@@ -391,6 +405,10 @@ public class ConfigFileBackedConfigProvider extends BaseConfigProvider implement
391 405
 
392 406
     @Override
393 407
     public void unsetOption(final String domain, final String option) {
408
+        if (!file.isKeyDomain(domain) || !file.getKeyDomain(domain).containsKey(option)) {
409
+            return;
410
+        }
411
+
394 412
         synchronized (this) {
395 413
             file.getKeyDomain(domain).remove(option);
396 414
             needSave = true;

+ 1
- 0
test-res/com/dmdirc/config/invalid-config-file View File

@@ -0,0 +1 @@
1
+Hello!

test-res/com/dmdirc/config/identity1 → test-res/com/dmdirc/config/no-name View File


test-res/com/dmdirc/config/identity2 → test-res/com/dmdirc/config/no-target View File


+ 11
- 0
test-res/com/dmdirc/config/profile-new View File

@@ -0,0 +1,11 @@
1
+keysections:
2
+  identity
3
+  profile
4
+
5
+identity:
6
+  name=New Profile
7
+
8
+profile:
9
+  nicknames=nick1\nnick2\nnick3
10
+  ident=ident
11
+  realname=Guy Incognito

+ 12
- 0
test-res/com/dmdirc/config/profile-old View File

@@ -0,0 +1,12 @@
1
+keysections:
2
+  identity
3
+  profile
4
+
5
+identity:
6
+  name=New Profile
7
+
8
+profile:
9
+  nickname=nick1
10
+  altnicks=nick2\nnick3
11
+  ident=ident
12
+  realname=Guy Incognito

+ 11
- 0
test-res/com/dmdirc/config/profile-old-no-alts View File

@@ -0,0 +1,11 @@
1
+keysections:
2
+  identity
3
+  profile
4
+
5
+identity:
6
+  name=New Profile
7
+
8
+profile:
9
+  nickname=nick1
10
+  ident=ident
11
+  realname=Guy Incognito

test-res/com/dmdirc/config/identity3 → test-res/com/dmdirc/config/simple-ircd View File

@@ -6,9 +6,11 @@ keysections:
6 6
 identity:
7 7
   name=unit test
8 8
   ircd=DMDircircd!
9
+  order=5000
9 10
 
10 11
 meep:
11 12
   moop=2
13
+  mop=yes
12 14
 
13 15
 unit:
14 16
   test=true

+ 19
- 0
test-res/com/dmdirc/config/simple-ircd-extra View File

@@ -0,0 +1,19 @@
1
+keysections:
2
+  identity
3
+  meep
4
+  more
5
+
6
+identity:
7
+  name=unit test
8
+  ircd=DMDircircd!
9
+  order=5000
10
+
11
+meep:
12
+  moop=3
13
+  mop=yes
14
+
15
+more:
16
+  settings=here
17
+  because=they
18
+  are=very
19
+  fun=11111

+ 10
- 0
test-res/com/dmdirc/config/simple-server View File

@@ -0,0 +1,10 @@
1
+keysections:
2
+  identity
3
+  meep
4
+
5
+identity:
6
+  name=unit test
7
+  server=test123
8
+
9
+meep:
10
+  moop=2

+ 350
- 0
test/com/dmdirc/config/ConfigFileBackedConfigProviderTest.java View File

@@ -0,0 +1,350 @@
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;
24
+
25
+import com.dmdirc.interfaces.config.ConfigChangeListener;
26
+import com.dmdirc.interfaces.config.ConfigProvider;
27
+import com.dmdirc.util.io.InvalidConfigFileException;
28
+import com.dmdirc.util.validators.NumericalValidator;
29
+import com.dmdirc.util.validators.PermissiveValidator;
30
+
31
+import com.google.common.collect.Lists;
32
+import com.google.common.jimfs.Configuration;
33
+import com.google.common.jimfs.Jimfs;
34
+
35
+import java.io.IOException;
36
+import java.nio.file.FileSystem;
37
+import java.nio.file.Files;
38
+import java.nio.file.StandardCopyOption;
39
+
40
+import org.junit.Before;
41
+import org.junit.Test;
42
+import org.junit.runner.RunWith;
43
+import org.mockito.Mock;
44
+import org.mockito.runners.MockitoJUnitRunner;
45
+
46
+import static org.junit.Assert.assertEquals;
47
+import static org.junit.Assert.assertFalse;
48
+import static org.junit.Assert.assertNull;
49
+import static org.junit.Assert.assertTrue;
50
+import static org.mockito.Matchers.anyString;
51
+import static org.mockito.Mockito.never;
52
+import static org.mockito.Mockito.verify;
53
+
54
+@RunWith(MockitoJUnitRunner.class)
55
+public class ConfigFileBackedConfigProviderTest {
56
+
57
+    private static final String[] FILES = {
58
+            "simple-ircd", "simple-server", "profile-new", "profile-old", "profile-old-no-alts",
59
+            "no-name", "no-target", "invalid-config-file", "simple-ircd-extra"
60
+    };
61
+
62
+    public static final PermissiveValidator<String> PERMISSIVE_VALIDATOR =
63
+            new PermissiveValidator<>();
64
+
65
+    @Mock private IdentityManager identityManager;
66
+    @Mock private ConfigChangeListener changeListener;
67
+
68
+    private FileSystem fs;
69
+
70
+    @Before
71
+    public void setUp() throws Exception {
72
+        fs = Jimfs.newFileSystem(Configuration.unix());
73
+
74
+        for (String file: FILES) {
75
+            Files.copy(getClass().getResourceAsStream(file), fs.getPath(file));
76
+        }
77
+    }
78
+
79
+    @Test(expected = InvalidIdentityFileException.class)
80
+    public void testNoName() throws IOException, InvalidIdentityFileException {
81
+        new ConfigFileBackedConfigProvider(identityManager, fs.getPath("no-name"), false);
82
+    }
83
+
84
+    @Test(expected = InvalidIdentityFileException.class)
85
+    public void testNoTarget() throws IOException, InvalidIdentityFileException {
86
+        new ConfigFileBackedConfigProvider(identityManager, fs.getPath("no-target"), false);
87
+    }
88
+
89
+    @Test(expected = InvalidIdentityFileException.class)
90
+    public void testInvalidConfigFile() throws IOException, InvalidIdentityFileException {
91
+        new ConfigFileBackedConfigProvider(identityManager, fs.getPath("invalid-config-file"),
92
+                false);
93
+    }
94
+
95
+    @Test
96
+    public void testProfileSimple() throws IOException, InvalidIdentityFileException {
97
+        final ConfigFileBackedConfigProvider provider = getProvider("profile-new");
98
+        assertTrue(provider.isProfile());
99
+        assertEquals("nick1\nnick2\nnick3", provider.getOption("profile", "nicknames"));
100
+        assertEquals("ident", provider.getOption("profile", "ident"));
101
+        assertEquals("Guy Incognito", provider.getOption("profile", "realname"));
102
+    }
103
+
104
+    @Test
105
+    public void testProfileOldWithAltNicks() throws IOException, InvalidIdentityFileException {
106
+        final ConfigFileBackedConfigProvider provider = getProvider("profile-old");
107
+        assertTrue(provider.isProfile());
108
+        assertEquals("nick1\nnick2\nnick3", provider.getOption("profile", "nicknames"));
109
+        assertEquals("ident", provider.getOption("profile", "ident"));
110
+        assertEquals("Guy Incognito", provider.getOption("profile", "realname"));
111
+
112
+        assertFalse(provider.hasOptionString("profile", "nickname"));
113
+        assertFalse(provider.hasOptionString("profile", "altnicknames"));
114
+    }
115
+
116
+    @Test
117
+    public void testProfileOldWithNoAlts() throws IOException, InvalidIdentityFileException {
118
+        final ConfigFileBackedConfigProvider provider = getProvider("profile-old-no-alts");
119
+        assertTrue(provider.isProfile());
120
+        assertEquals("nick1", provider.getOption("profile", "nicknames"));
121
+        assertEquals("ident", provider.getOption("profile", "ident"));
122
+        assertEquals("Guy Incognito", provider.getOption("profile", "realname"));
123
+
124
+        assertFalse(provider.hasOptionString("profile", "nickname"));
125
+        assertFalse(provider.hasOptionString("profile", "altnicknames"));
126
+    }
127
+
128
+    @Test
129
+    public void testProfileNonProfileConfig() throws IOException, InvalidIdentityFileException {
130
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
131
+        assertFalse(provider.isProfile());
132
+    }
133
+
134
+    @Test
135
+    public void testReadsIrcdTarget() throws IOException, InvalidIdentityFileException {
136
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
137
+        assertEquals(ConfigTarget.TYPE.IRCD, provider.getTarget().getType());
138
+        assertEquals("DMDircircd!", provider.getTarget().getData());
139
+    }
140
+
141
+    @Test
142
+    public void testReadsServerTarget() throws IOException, InvalidIdentityFileException {
143
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-server");
144
+        assertEquals(ConfigTarget.TYPE.SERVER, provider.getTarget().getType());
145
+        assertEquals("test123", provider.getTarget().getData());
146
+    }
147
+
148
+    @Test
149
+    public void testSetsOrder() throws IOException, InvalidIdentityFileException {
150
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
151
+        assertEquals(5000, provider.getTarget().getOrder());
152
+    }
153
+
154
+    @Test
155
+    public void testHasOptionPermissiveValidator()
156
+            throws IOException, InvalidIdentityFileException {
157
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
158
+        assertTrue(provider.hasOption("unit", "test", PERMISSIVE_VALIDATOR));
159
+        assertFalse(provider.hasOption("unit", "untest", PERMISSIVE_VALIDATOR));
160
+        assertFalse(provider.hasOption("foo", "test", PERMISSIVE_VALIDATOR));
161
+    }
162
+
163
+    @Test
164
+    public void testHasOptionFailingValidator() throws IOException, InvalidIdentityFileException {
165
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
166
+        assertFalse(provider.hasOption("unit", "test", new NumericalValidator(0, 100)));
167
+    }
168
+
169
+    @Test
170
+    public void testGetOptionPermissioveValidator()
171
+            throws IOException, InvalidIdentityFileException {
172
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
173
+        assertEquals("true", provider.getOption("unit", "test", PERMISSIVE_VALIDATOR));
174
+    }
175
+
176
+    @Test
177
+    public void testGetOptionFailingValidator() throws IOException, InvalidIdentityFileException {
178
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
179
+        assertNull(provider.getOption("unit", "test", new NumericalValidator(0, 100)));
180
+    }
181
+
182
+    @Test
183
+    public void testUnsetOption() throws IOException, InvalidIdentityFileException {
184
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
185
+        provider.unsetOption("unit", "test");
186
+        assertFalse(provider.hasOption("unit", "test", PERMISSIVE_VALIDATOR));
187
+    }
188
+
189
+    @Test
190
+    public void testSetOptionBoolean() throws IOException, InvalidIdentityFileException {
191
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
192
+        provider.setOption("new", "option", true);
193
+        assertEquals("true", provider.getOption("new", "option", PERMISSIVE_VALIDATOR));
194
+    }
195
+
196
+    @Test
197
+    public void testSetOptionInt() throws IOException, InvalidIdentityFileException {
198
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
199
+        provider.setOption("new", "option", 1234);
200
+        assertEquals("1234", provider.getOption("new", "option", PERMISSIVE_VALIDATOR));
201
+    }
202
+
203
+    @Test
204
+    public void testSetOptionList() throws IOException, InvalidIdentityFileException {
205
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
206
+        provider.setOption("new", "option", Lists.newArrayList("first", "second"));
207
+        assertEquals("first\nsecond", provider.getOption("new", "option", PERMISSIVE_VALIDATOR));
208
+    }
209
+
210
+    @Test
211
+    public void testSetOptionListWithSingleEntry()
212
+            throws IOException, InvalidIdentityFileException {
213
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
214
+        provider.setOption("new", "option", Lists.newArrayList("first"));
215
+        assertEquals("first", provider.getOption("new", "option", PERMISSIVE_VALIDATOR));
216
+    }
217
+
218
+    @Test
219
+    public void testSaveSimple() throws IOException, InvalidIdentityFileException {
220
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
221
+        provider.setOption("newdomain", "test123", 47);
222
+        provider.save();
223
+
224
+        final ConfigFileBackedConfigProvider loaded = getProvider("simple-ircd");
225
+        assertTrue(loaded.hasOptionInt("newdomain", "test123"));
226
+        assertEquals(Integer.valueOf(47), loaded.getOptionInt("newdomain", "test123"));
227
+
228
+        // Check old settings are preserved
229
+        assertTrue(loaded.hasOptionString("unit", "test"));
230
+        assertEquals(ConfigTarget.TYPE.IRCD, loaded.getTarget().getType());
231
+        assertEquals("DMDircircd!", loaded.getTarget().getData());
232
+    }
233
+
234
+    @Test
235
+    public void testFiresSettingChanged() throws IOException, InvalidIdentityFileException {
236
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
237
+        provider.addListener(changeListener);
238
+        provider.setOption("new", "option", "boo");
239
+        verify(changeListener).configChanged("new", "option");
240
+    }
241
+
242
+    @Test
243
+    public void testDoesNotFireSettingChangedIfValueIsSame()
244
+            throws IOException, InvalidIdentityFileException {
245
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
246
+        provider.setOption("new", "option", "boo");
247
+        provider.addListener(changeListener);
248
+        provider.setOption("new", "option", "boo");
249
+        verify(changeListener, never()).configChanged(anyString(), anyString());
250
+    }
251
+
252
+    @Test
253
+    public void testFiresSettingChangedWhenUnset()
254
+            throws IOException, InvalidIdentityFileException {
255
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
256
+        provider.setOption("new", "option", "boo");
257
+        provider.addListener(changeListener);
258
+        provider.unsetOption("new", "option");
259
+        verify(changeListener).configChanged("new", "option");
260
+    }
261
+
262
+    @Test
263
+    public void testDoesNotFireSettingChangedIfNonExistantOptionUnset()
264
+            throws IOException, InvalidIdentityFileException {
265
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
266
+        provider.addListener(changeListener);
267
+        provider.unsetOption("new", "option");
268
+        verify(changeListener, never()).configChanged(anyString(), anyString());
269
+    }
270
+
271
+    @Test
272
+    public void testDoesNotFireSettingChangedIfRemoved()
273
+            throws IOException, InvalidIdentityFileException {
274
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
275
+        provider.addListener(changeListener);
276
+        provider.removeListener(changeListener);
277
+        provider.setOption("new", "option", "boo");
278
+        verify(changeListener, never()).configChanged(anyString(), anyString());
279
+    }
280
+
281
+    @Test
282
+    public void testReloadLoadsExtraSettings()
283
+            throws IOException, InvalidConfigFileException, InvalidIdentityFileException {
284
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
285
+        copyFileAndReload(provider);
286
+        assertTrue(provider.hasOption("more", "settings", PERMISSIVE_VALIDATOR));
287
+        assertEquals("here", provider.getOption("more", "settings", PERMISSIVE_VALIDATOR));
288
+    }
289
+
290
+    @Test
291
+    public void testReloadUnsetsMissingSettings()
292
+            throws IOException, InvalidConfigFileException, InvalidIdentityFileException {
293
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
294
+        copyFileAndReload(provider);
295
+        assertFalse(provider.hasOption("unit", "test", PERMISSIVE_VALIDATOR));
296
+    }
297
+
298
+    @Test
299
+    public void testReloadFiresSettingChangedForChangedOptions()
300
+            throws IOException, InvalidConfigFileException, InvalidIdentityFileException {
301
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
302
+        provider.addListener(changeListener);
303
+        copyFileAndReload(provider);
304
+        verify(changeListener).configChanged("meep", "moop");
305
+    }
306
+
307
+    @Test
308
+    public void testReloadFiresSettingChangedForNewOptions()
309
+            throws IOException, InvalidConfigFileException, InvalidIdentityFileException {
310
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
311
+        provider.addListener(changeListener);
312
+        copyFileAndReload(provider);
313
+        verify(changeListener).configChanged("more", "settings");
314
+        verify(changeListener).configChanged("more", "because");
315
+        verify(changeListener).configChanged("more", "are");
316
+        verify(changeListener).configChanged("more", "fun");
317
+    }
318
+
319
+    @Test
320
+    public void testReloadFiresSettingChangedForRemovedOptions()
321
+            throws IOException, InvalidConfigFileException, InvalidIdentityFileException {
322
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
323
+        provider.addListener(changeListener);
324
+        copyFileAndReload(provider);
325
+        verify(changeListener).configChanged("unit", "test");
326
+    }
327
+
328
+    @Test
329
+    public void testReloadDoesNotFireSettingChangedForUnchangedOptions()
330
+            throws IOException, InvalidConfigFileException, InvalidIdentityFileException {
331
+        final ConfigFileBackedConfigProvider provider = getProvider("simple-ircd");
332
+        provider.addListener(changeListener);
333
+        copyFileAndReload(provider);
334
+        verify(changeListener, never()).configChanged("meep", "mop");
335
+    }
336
+
337
+    private void copyFileAndReload(final ConfigProvider provider)
338
+            throws IOException, InvalidConfigFileException {
339
+        Files.copy(fs.getPath("simple-ircd-extra"), fs.getPath("simple-ircd"),
340
+                StandardCopyOption.REPLACE_EXISTING);
341
+        provider.reload();
342
+    }
343
+
344
+    private ConfigFileBackedConfigProvider getProvider(final String file)
345
+            throws IOException, InvalidIdentityFileException {
346
+        return new ConfigFileBackedConfigProvider(identityManager,
347
+                fs.getPath(file), false);
348
+    }
349
+
350
+}

+ 0
- 204
test/com/dmdirc/config/IdentityTest.java View File

@@ -1,204 +0,0 @@
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;
24
-
25
-import com.dmdirc.interfaces.config.ConfigChangeListener;
26
-import com.dmdirc.util.io.ConfigFile;
27
-import com.dmdirc.util.validators.PermissiveValidator;
28
-
29
-import java.io.IOException;
30
-import java.util.Map;
31
-
32
-import org.junit.Before;
33
-import org.junit.Ignore;
34
-import org.junit.Test;
35
-import org.junit.runner.RunWith;
36
-import org.mockito.Mock;
37
-import org.mockito.runners.MockitoJUnitRunner;
38
-
39
-import static org.junit.Assert.*;
40
-import static org.mockito.Mockito.*;
41
-
42
-@RunWith(MockitoJUnitRunner.class)
43
-public class IdentityTest {
44
-
45
-    @Mock private IdentityManager identityManager;
46
-    private ConfigFileBackedConfigProvider myIdent;
47
-    private ConfigTarget target;
48
-
49
-    @Before
50
-    public void setUp() throws Exception {
51
-        target = new ConfigTarget();
52
-        target.setChannel("#unittest@unittest");
53
-
54
-        myIdent = new ConfigFileBackedConfigProvider(identityManager,
55
-                new ConfigFile(getClass().getResourceAsStream("identity2")), target);
56
-    }
57
-
58
-    @Test
59
-    public void testToString() {
60
-        assertEquals(myIdent.getName(), myIdent.toString());
61
-    }
62
-
63
-    @Test
64
-    public void testIsProfile() {
65
-        assertFalse(myIdent.isProfile());
66
-
67
-        myIdent.setOption("profile", "nickname", "foo");
68
-        myIdent.setOption("profile", "realname", "foo");
69
-
70
-        assertTrue(myIdent.isProfile());
71
-
72
-        myIdent.unsetOption("profile", "nickname");
73
-        myIdent.unsetOption("profile", "realname");
74
-    }
75
-
76
-    @Test
77
-    public void testHasOption() {
78
-        assertFalse(myIdent.hasOption("has", "option", new PermissiveValidator<String>()));
79
-
80
-        myIdent.setOption("has", "option", "");
81
-
82
-        assertTrue(myIdent.hasOption("has", "option", new PermissiveValidator<String>()));
83
-
84
-        myIdent.unsetOption("has", "option");
85
-    }
86
-
87
-    @Test
88
-    public void testGetOption() {
89
-        myIdent.setOption("domain", "option", "value");
90
-        final Map<String, String> props = myIdent.getOptions("domain");
91
-
92
-        assertEquals(props.get("option"), myIdent.getOption("domain", "option"));
93
-
94
-        myIdent.unsetOption("domain", "option");
95
-    }
96
-
97
-    @Test
98
-    public void testSetOption() {
99
-        final int count = myIdent.getOptions("foo").size();
100
-
101
-        myIdent.setOption("foo", "bar", "baz");
102
-
103
-        assertEquals(count + 1, myIdent.getOptions("foo").size());
104
-
105
-        myIdent.unsetOption("foo", "bar");
106
-    }
107
-
108
-    @Test
109
-    public void testSetOptionInt() {
110
-        myIdent.setOption("foo", "baz", 123);
111
-        assertEquals("123", myIdent.getOption("foo", "baz"));
112
-    }
113
-
114
-    @Test
115
-    public void testSetOptionBool() {
116
-        myIdent.setOption("foo", "baz", false);
117
-        assertEquals("false", myIdent.getOption("foo", "baz"));
118
-        myIdent.setOption("foo", "baz", true);
119
-        assertEquals("true", myIdent.getOption("foo", "baz"));
120
-    }
121
-
122
-    @Test
123
-    public void testRemoveOption() {
124
-        final Map<String, String> props = myIdent.getOptions("foo");
125
-        final int count = props.size();
126
-
127
-        myIdent.setOption("foo", "bar", "baz");
128
-
129
-        assertEquals(count + 1, myIdent.getOptions("foo").size());
130
-
131
-        myIdent.unsetOption("foo", "bar");
132
-
133
-        assertEquals(count, myIdent.getOptions("foo").size());
134
-    }
135
-
136
-    @Test
137
-    @Ignore("Needs to be rewritten to work without a file system, and not be completely nuts")
138
-    public void testSave() throws IOException, InvalidIdentityFileException {
139
-        myIdent.setOption("foo", "bar", "baz!");
140
-
141
-        myIdent.save();
142
-
143
-        //myIdent = new ConfigFileBackedConfigProvider(identityManager,
144
-        //        myIdent.file.getFile().toPath(), false);
145
-
146
-        assertEquals("baz!", myIdent.getOption("foo", "bar"));
147
-    }
148
-
149
-    @Test
150
-    public void testGetTarget() {
151
-        assertEquals(target.getData(), myIdent.getTarget().getData());
152
-        assertEquals(target.getType(), myIdent.getTarget().getType());
153
-    }
154
-
155
-    @Test(expected = InvalidIdentityFileException.class)
156
-    public void testNoName() throws IOException, InvalidIdentityFileException {
157
-        new ConfigFileBackedConfigProvider(getClass().getResourceAsStream("identity1"), false);
158
-    }
159
-
160
-    @Test(expected = InvalidIdentityFileException.class)
161
-    public void testNoTarget() throws IOException, InvalidIdentityFileException {
162
-        new ConfigFileBackedConfigProvider(getClass().getResourceAsStream("identity2"), false);
163
-    }
164
-
165
-    @Test
166
-    public void testMigrate() throws IOException, InvalidIdentityFileException {
167
-        final ConfigFileBackedConfigProvider id = new ConfigFileBackedConfigProvider(getClass().
168
-                getResourceAsStream("identity3"), false);
169
-
170
-        assertTrue(id.file.isKeyDomain("identity"));
171
-        assertTrue(id.file.isKeyDomain("meep"));
172
-        assertTrue(id.file.isKeyDomain("unit"));
173
-
174
-        assertEquals("unit test", id.file.getKeyDomain("identity").get("name"));
175
-        assertEquals("true", id.file.getKeyDomain("unit").get("test"));
176
-        assertEquals("2", id.file.getKeyDomain("meep").get("moop"));
177
-    }
178
-
179
-    @Test
180
-    public void testSetListener() {
181
-        final ConfigChangeListener listener = mock(ConfigChangeListener.class);
182
-        myIdent.addListener(listener);
183
-
184
-        verify(listener, never()).configChanged(anyString(), anyString());
185
-
186
-        myIdent.setOption("unit", "test", "meep");
187
-
188
-        verify(listener).configChanged("unit", "test");
189
-    }
190
-
191
-    @Test
192
-    public void testUnsetListener() {
193
-        final ConfigChangeListener listener = mock(ConfigChangeListener.class);
194
-        myIdent.setOption("unit", "test", "meep");
195
-        myIdent.addListener(listener);
196
-
197
-        verify(listener, never()).configChanged(anyString(), anyString());
198
-
199
-        myIdent.unsetOption("unit", "test");
200
-
201
-        verify(listener).configChanged("unit", "test");
202
-    }
203
-
204
-}

Loading…
Cancel
Save