|
@@ -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
|
+}
|