1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.eclipse.jgit.lib;
17
18 import static java.nio.charset.StandardCharsets.UTF_8;
19 import static java.util.concurrent.TimeUnit.DAYS;
20 import static java.util.concurrent.TimeUnit.HOURS;
21 import static java.util.concurrent.TimeUnit.MICROSECONDS;
22 import static java.util.concurrent.TimeUnit.MILLISECONDS;
23 import static java.util.concurrent.TimeUnit.MINUTES;
24 import static java.util.concurrent.TimeUnit.NANOSECONDS;
25 import static java.util.concurrent.TimeUnit.SECONDS;
26 import static org.eclipse.jgit.util.FileUtils.pathToString;
27 import static org.junit.Assert.assertArrayEquals;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertNull;
31 import static org.junit.Assert.assertSame;
32 import static org.junit.Assert.assertThrows;
33 import static org.junit.Assert.assertTrue;
34 import static org.junit.Assert.fail;
35
36 import java.io.File;
37 import java.io.FileNotFoundException;
38 import java.io.IOException;
39 import java.nio.file.Files;
40 import java.text.MessageFormat;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Collections;
44 import java.util.Iterator;
45 import java.util.LinkedList;
46 import java.util.List;
47 import java.util.Set;
48 import java.util.concurrent.TimeUnit;
49 import java.util.function.Consumer;
50
51 import org.eclipse.jgit.api.MergeCommand.FastForwardMode;
52 import org.eclipse.jgit.errors.ConfigInvalidException;
53 import org.eclipse.jgit.internal.JGitText;
54 import org.eclipse.jgit.junit.JGitTestUtil;
55 import org.eclipse.jgit.junit.MockSystemReader;
56 import org.eclipse.jgit.merge.MergeConfig;
57 import org.eclipse.jgit.storage.file.FileBasedConfig;
58 import org.eclipse.jgit.transport.RefSpec;
59 import org.eclipse.jgit.util.FS;
60 import org.eclipse.jgit.util.SystemReader;
61 import org.junit.After;
62 import org.junit.Rule;
63 import org.junit.Test;
64 import org.junit.rules.TemporaryFolder;
65
66
67
68
69 @SuppressWarnings("boxing")
70 public class ConfigTest {
71
72 private static final char WS = '\u2002';
73
74 private static final String REFS_ORIGIN = "+refs/heads/*:refs/remotes/origin/*";
75
76 private static final String REFS_UPSTREAM = "+refs/heads/*:refs/remotes/upstream/*";
77
78 private static final String REFS_BACKUP = "+refs/heads/*:refs/remotes/backup/*";
79
80 @Rule
81 public TemporaryFolder tmp = new TemporaryFolder();
82
83 @After
84 public void tearDown() {
85 SystemReader.setInstance(null);
86 }
87
88 @Test
89 public void test001_ReadBareKey() throws ConfigInvalidException {
90 final Config c = parse("[foo]\nbar\n");
91 assertTrue(c.getBoolean("foo", null, "bar", false));
92 assertEquals("", c.getString("foo", null, "bar"));
93 }
94
95 @Test
96 public void test002_ReadWithSubsection() throws ConfigInvalidException {
97 final Config c = parse("[foo \"zip\"]\nbar\n[foo \"zap\"]\nbar=false\nn=3\n");
98 assertTrue(c.getBoolean("foo", "zip", "bar", false));
99 assertEquals("", c.getString("foo","zip", "bar"));
100 assertFalse(c.getBoolean("foo", "zap", "bar", true));
101 assertEquals("false", c.getString("foo", "zap", "bar"));
102 assertEquals(3, c.getInt("foo", "zap", "n", 4));
103 assertEquals(4, c.getInt("foo", "zap","m", 4));
104 }
105
106 @Test
107 public void test003_PutRemote() {
108 final Config c = new Config();
109 c.setString("sec", "ext", "name", "value");
110 c.setString("sec", "ext", "name2", "value2");
111 final String expText = "[sec \"ext\"]\n\tname = value\n\tname2 = value2\n";
112 assertEquals(expText, c.toText());
113 }
114
115 @Test
116 public void test004_PutGetSimple() {
117 Config c = new Config();
118 c.setString("my", null, "somename", "false");
119 assertEquals("false", c.getString("my", null, "somename"));
120 assertEquals("[my]\n\tsomename = false\n", c.toText());
121 }
122
123 @Test
124 public void test005_PutGetStringList() {
125 Config c = new Config();
126 final LinkedList<String> values = new LinkedList<>();
127 values.add("value1");
128 values.add("value2");
129 c.setStringList("my", null, "somename", values);
130
131 final Object[] expArr = values.toArray();
132 final String[] actArr = c.getStringList("my", null, "somename");
133 assertArrayEquals(expArr, actArr);
134
135 final String expText = "[my]\n\tsomename = value1\n\tsomename = value2\n";
136 assertEquals(expText, c.toText());
137 }
138
139 @Test
140 public void test006_readCaseInsensitive() throws ConfigInvalidException {
141 final Config c = parse("[Foo]\nBar\n");
142 assertTrue(c.getBoolean("foo", null, "bar", false));
143 assertEquals("", c.getString("foo", null, "bar"));
144 }
145
146 @Test
147 public void test007_readUserConfig() {
148 final MockSystemReader mockSystemReader = new MockSystemReader();
149 SystemReader.setInstance(mockSystemReader);
150 final String hostname = mockSystemReader.getHostname();
151 final Config userGitConfig = mockSystemReader.openUserConfig(null,
152 FS.DETECTED);
153 final Config localConfig = new Config(userGitConfig);
154 mockSystemReader.clearProperties();
155
156 String authorName;
157 String authorEmail;
158
159
160 authorName = localConfig.get(UserConfig.KEY).getAuthorName();
161 authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
162 assertEquals(Constants.UNKNOWN_USER_DEFAULT, authorName);
163 assertEquals(Constants.UNKNOWN_USER_DEFAULT + "@" + hostname, authorEmail);
164 assertTrue(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
165 assertTrue(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
166
167
168 mockSystemReader.setProperty(Constants.OS_USER_NAME_KEY, "os user name");
169 localConfig.uncache(UserConfig.KEY);
170 authorName = localConfig.get(UserConfig.KEY).getAuthorName();
171 assertEquals("os user name", authorName);
172 assertTrue(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
173
174 if (hostname != null && hostname.length() != 0) {
175 authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
176 assertEquals("os user name@" + hostname, authorEmail);
177 }
178 assertTrue(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
179
180
181 mockSystemReader.setProperty(Constants.GIT_AUTHOR_NAME_KEY, "git author name");
182 mockSystemReader.setProperty(Constants.GIT_AUTHOR_EMAIL_KEY, "author@email");
183 localConfig.uncache(UserConfig.KEY);
184 authorName = localConfig.get(UserConfig.KEY).getAuthorName();
185 authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
186 assertEquals("git author name", authorName);
187 assertEquals("author@email", authorEmail);
188 assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
189 assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
190
191
192
193
194 mockSystemReader.clearProperties();
195 userGitConfig.setString("user", null, "name", "global username");
196 userGitConfig.setString("user", null, "email", "author@globalemail");
197 authorName = localConfig.get(UserConfig.KEY).getAuthorName();
198 authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
199 assertEquals("global username", authorName);
200 assertEquals("author@globalemail", authorEmail);
201 assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
202 assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
203
204
205 localConfig.setString("user", null, "name", "local username");
206 localConfig.setString("user", null, "email", "author@localemail");
207 authorName = localConfig.get(UserConfig.KEY).getAuthorName();
208 authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
209 assertEquals("local username", authorName);
210 assertEquals("author@localemail", authorEmail);
211 assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
212 assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
213
214 authorName = localConfig.get(UserConfig.KEY).getCommitterName();
215 authorEmail = localConfig.get(UserConfig.KEY).getCommitterEmail();
216 assertEquals("local username", authorName);
217 assertEquals("author@localemail", authorEmail);
218 assertFalse(localConfig.get(UserConfig.KEY).isCommitterNameImplicit());
219 assertFalse(localConfig.get(UserConfig.KEY).isCommitterEmailImplicit());
220
221
222 mockSystemReader.setProperty(Constants.GIT_AUTHOR_NAME_KEY,
223 "git author name");
224 mockSystemReader.setProperty(Constants.GIT_AUTHOR_EMAIL_KEY,
225 "author@email");
226 localConfig.setString("user", null, "name", "local username");
227 localConfig.setString("user", null, "email", "author@localemail");
228 authorName = localConfig.get(UserConfig.KEY).getAuthorName();
229 authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
230 assertEquals("git author name", authorName);
231 assertEquals("author@email", authorEmail);
232 assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
233 assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
234 }
235
236 @Test
237 public void testReadUserConfigWithInvalidCharactersStripped() {
238 final MockSystemReader mockSystemReader = new MockSystemReader();
239 final Config localConfig = new Config(mockSystemReader.openUserConfig(
240 null, FS.DETECTED));
241
242 localConfig.setString("user", null, "name", "foo<bar");
243 localConfig.setString("user", null, "email", "baz>\nqux@example.com");
244
245 UserConfig userConfig = localConfig.get(UserConfig.KEY);
246 assertEquals("foobar", userConfig.getAuthorName());
247 assertEquals("bazqux@example.com", userConfig.getAuthorEmail());
248 }
249
250 @Test
251 public void testReadBoolean_TrueFalse1() throws ConfigInvalidException {
252 final Config c = parse("[s]\na = true\nb = false\n");
253 assertEquals("true", c.getString("s", null, "a"));
254 assertEquals("false", c.getString("s", null, "b"));
255
256 assertTrue(c.getBoolean("s", "a", false));
257 assertFalse(c.getBoolean("s", "b", true));
258 }
259
260 @Test
261 public void testReadBoolean_TrueFalse2() throws ConfigInvalidException {
262 final Config c = parse("[s]\na = TrUe\nb = fAlSe\n");
263 assertEquals("TrUe", c.getString("s", null, "a"));
264 assertEquals("fAlSe", c.getString("s", null, "b"));
265
266 assertTrue(c.getBoolean("s", "a", false));
267 assertFalse(c.getBoolean("s", "b", true));
268 }
269
270 @Test
271 public void testReadBoolean_YesNo1() throws ConfigInvalidException {
272 final Config c = parse("[s]\na = yes\nb = no\n");
273 assertEquals("yes", c.getString("s", null, "a"));
274 assertEquals("no", c.getString("s", null, "b"));
275
276 assertTrue(c.getBoolean("s", "a", false));
277 assertFalse(c.getBoolean("s", "b", true));
278 }
279
280 @Test
281 public void testReadBoolean_YesNo2() throws ConfigInvalidException {
282 final Config c = parse("[s]\na = yEs\nb = NO\n");
283 assertEquals("yEs", c.getString("s", null, "a"));
284 assertEquals("NO", c.getString("s", null, "b"));
285
286 assertTrue(c.getBoolean("s", "a", false));
287 assertFalse(c.getBoolean("s", "b", true));
288 }
289
290 @Test
291 public void testReadBoolean_OnOff1() throws ConfigInvalidException {
292 final Config c = parse("[s]\na = on\nb = off\n");
293 assertEquals("on", c.getString("s", null, "a"));
294 assertEquals("off", c.getString("s", null, "b"));
295
296 assertTrue(c.getBoolean("s", "a", false));
297 assertFalse(c.getBoolean("s", "b", true));
298 }
299
300 @Test
301 public void testReadBoolean_OnOff2() throws ConfigInvalidException {
302 final Config c = parse("[s]\na = ON\nb = OFF\n");
303 assertEquals("ON", c.getString("s", null, "a"));
304 assertEquals("OFF", c.getString("s", null, "b"));
305
306 assertTrue(c.getBoolean("s", "a", false));
307 assertFalse(c.getBoolean("s", "b", true));
308 }
309
310 enum TestEnum {
311 ONE_TWO;
312 }
313
314 @Test
315 public void testGetEnum() throws ConfigInvalidException {
316 Config c = parse("[s]\na = ON\nb = input\nc = true\nd = off\n");
317 assertSame(CoreConfig.AutoCRLF.TRUE, c.getEnum("s", null, "a",
318 CoreConfig.AutoCRLF.FALSE));
319
320 assertSame(CoreConfig.AutoCRLF.INPUT, c.getEnum("s", null, "b",
321 CoreConfig.AutoCRLF.FALSE));
322
323 assertSame(CoreConfig.AutoCRLF.TRUE, c.getEnum("s", null, "c",
324 CoreConfig.AutoCRLF.FALSE));
325
326 assertSame(CoreConfig.AutoCRLF.FALSE, c.getEnum("s", null, "d",
327 CoreConfig.AutoCRLF.TRUE));
328
329 c = new Config();
330 assertSame(CoreConfig.AutoCRLF.FALSE, c.getEnum("s", null, "d",
331 CoreConfig.AutoCRLF.FALSE));
332
333 c = parse("[s \"b\"]\n\tc = one two\n");
334 assertSame(TestEnum.ONE_TWO, c.getEnum("s", "b", "c", TestEnum.ONE_TWO));
335
336 c = parse("[s \"b\"]\n\tc = one-two\n");
337 assertSame(TestEnum.ONE_TWO, c.getEnum("s", "b", "c", TestEnum.ONE_TWO));
338 }
339
340 @Test
341 public void testGetInvalidEnum() throws ConfigInvalidException {
342 Config c = parse("[a]\n\tb = invalid\n");
343 try {
344 c.getEnum("a", null, "b", TestEnum.ONE_TWO);
345 fail();
346 } catch (IllegalArgumentException e) {
347 assertEquals("Invalid value: a.b=invalid", e.getMessage());
348 }
349
350 c = parse("[a \"b\"]\n\tc = invalid\n");
351 try {
352 c.getEnum("a", "b", "c", TestEnum.ONE_TWO);
353 fail();
354 } catch (IllegalArgumentException e) {
355 assertEquals("Invalid value: a.b.c=invalid", e.getMessage());
356 }
357 }
358
359 @Test
360 public void testSetEnum() {
361 final Config c = new Config();
362 c.setEnum("s", "b", "c", TestEnum.ONE_TWO);
363 assertEquals("[s \"b\"]\n\tc = one two\n", c.toText());
364 }
365
366 @Test
367 public void testGetFastForwardMergeoptions() throws ConfigInvalidException {
368 Config c = new Config(null);
369 assertSame(FastForwardMode.FF, c.getEnum(
370 ConfigConstants.CONFIG_BRANCH_SECTION, "side",
371 ConfigConstants.CONFIG_KEY_MERGEOPTIONS, FastForwardMode.FF));
372 MergeConfig mergeConfig = c.get(MergeConfig.getParser("side"));
373 assertSame(FastForwardMode.FF, mergeConfig.getFastForwardMode());
374 c = parse("[branch \"side\"]\n\tmergeoptions = --ff-only\n");
375 assertSame(FastForwardMode.FF_ONLY, c.getEnum(
376 ConfigConstants.CONFIG_BRANCH_SECTION, "side",
377 ConfigConstants.CONFIG_KEY_MERGEOPTIONS,
378 FastForwardMode.FF_ONLY));
379 mergeConfig = c.get(MergeConfig.getParser("side"));
380 assertSame(FastForwardMode.FF_ONLY, mergeConfig.getFastForwardMode());
381 c = parse("[branch \"side\"]\n\tmergeoptions = --ff\n");
382 assertSame(FastForwardMode.FF, c.getEnum(
383 ConfigConstants.CONFIG_BRANCH_SECTION, "side",
384 ConfigConstants.CONFIG_KEY_MERGEOPTIONS, FastForwardMode.FF));
385 mergeConfig = c.get(MergeConfig.getParser("side"));
386 assertSame(FastForwardMode.FF, mergeConfig.getFastForwardMode());
387 c = parse("[branch \"side\"]\n\tmergeoptions = --no-ff\n");
388 assertSame(FastForwardMode.NO_FF, c.getEnum(
389 ConfigConstants.CONFIG_BRANCH_SECTION, "side",
390 ConfigConstants.CONFIG_KEY_MERGEOPTIONS, FastForwardMode.NO_FF));
391 mergeConfig = c.get(MergeConfig.getParser("side"));
392 assertSame(FastForwardMode.NO_FF, mergeConfig.getFastForwardMode());
393 }
394
395 @Test
396 public void testSetFastForwardMergeoptions() {
397 final Config c = new Config();
398 c.setEnum("branch", "side", "mergeoptions", FastForwardMode.FF);
399 assertEquals("[branch \"side\"]\n\tmergeoptions = --ff\n", c.toText());
400 c.setEnum("branch", "side", "mergeoptions", FastForwardMode.FF_ONLY);
401 assertEquals("[branch \"side\"]\n\tmergeoptions = --ff-only\n",
402 c.toText());
403 c.setEnum("branch", "side", "mergeoptions", FastForwardMode.NO_FF);
404 assertEquals("[branch \"side\"]\n\tmergeoptions = --no-ff\n",
405 c.toText());
406 }
407
408 @Test
409 public void testGetFastForwardMerge() throws ConfigInvalidException {
410 Config c = new Config(null);
411 assertSame(FastForwardMode.Merge.TRUE, c.getEnum(
412 ConfigConstants.CONFIG_KEY_MERGE, null,
413 ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.TRUE));
414 MergeConfig mergeConfig = c.get(MergeConfig.getParser("side"));
415 assertSame(FastForwardMode.FF, mergeConfig.getFastForwardMode());
416 c = parse("[merge]\n\tff = only\n");
417 assertSame(FastForwardMode.Merge.ONLY, c.getEnum(
418 ConfigConstants.CONFIG_KEY_MERGE, null,
419 ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.ONLY));
420 mergeConfig = c.get(MergeConfig.getParser("side"));
421 assertSame(FastForwardMode.FF_ONLY, mergeConfig.getFastForwardMode());
422 c = parse("[merge]\n\tff = true\n");
423 assertSame(FastForwardMode.Merge.TRUE, c.getEnum(
424 ConfigConstants.CONFIG_KEY_MERGE, null,
425 ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.TRUE));
426 mergeConfig = c.get(MergeConfig.getParser("side"));
427 assertSame(FastForwardMode.FF, mergeConfig.getFastForwardMode());
428 c = parse("[merge]\n\tff = false\n");
429 assertSame(FastForwardMode.Merge.FALSE, c.getEnum(
430 ConfigConstants.CONFIG_KEY_MERGE, null,
431 ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.FALSE));
432 mergeConfig = c.get(MergeConfig.getParser("side"));
433 assertSame(FastForwardMode.NO_FF, mergeConfig.getFastForwardMode());
434 }
435
436 @Test
437 public void testCombinedMergeOptions() throws ConfigInvalidException {
438 Config c = new Config(null);
439 MergeConfig mergeConfig = c.get(MergeConfig.getParser("side"));
440 assertSame(FastForwardMode.FF, mergeConfig.getFastForwardMode());
441 assertTrue(mergeConfig.isCommit());
442 assertFalse(mergeConfig.isSquash());
443
444 c = parse("[merge]\n\tff = false\n"
445 + "[branch \"side\"]\n\tmergeoptions = --ff-only\n");
446 mergeConfig = c.get(MergeConfig.getParser("side"));
447 assertSame(FastForwardMode.FF_ONLY, mergeConfig.getFastForwardMode());
448 assertTrue(mergeConfig.isCommit());
449 assertFalse(mergeConfig.isSquash());
450
451 c = parse("[merge]\n\tff = only\n"
452 + "[branch \"side\"]\n\tmergeoptions = --squash\n");
453 mergeConfig = c.get(MergeConfig.getParser("side"));
454 assertSame(FastForwardMode.FF_ONLY, mergeConfig.getFastForwardMode());
455 assertTrue(mergeConfig.isCommit());
456 assertTrue(mergeConfig.isSquash());
457
458 c = parse("[merge]\n\tff = false\n"
459 + "[branch \"side\"]\n\tmergeoptions = --ff-only --no-commit\n");
460 mergeConfig = c.get(MergeConfig.getParser("side"));
461 assertSame(FastForwardMode.FF_ONLY, mergeConfig.getFastForwardMode());
462 assertFalse(mergeConfig.isCommit());
463 assertFalse(mergeConfig.isSquash());
464 }
465
466 @Test
467 public void testSetFastForwardMerge() {
468 final Config c = new Config();
469 c.setEnum("merge", null, "ff",
470 FastForwardMode.Merge.valueOf(FastForwardMode.FF));
471 assertEquals("[merge]\n\tff = true\n", c.toText());
472 c.setEnum("merge", null, "ff",
473 FastForwardMode.Merge.valueOf(FastForwardMode.FF_ONLY));
474 assertEquals("[merge]\n\tff = only\n", c.toText());
475 c.setEnum("merge", null, "ff",
476 FastForwardMode.Merge.valueOf(FastForwardMode.NO_FF));
477 assertEquals("[merge]\n\tff = false\n", c.toText());
478 }
479
480 @Test
481 public void testReadLong() throws ConfigInvalidException {
482 assertReadLong(1L);
483 assertReadLong(-1L);
484 assertReadLong(Long.MIN_VALUE);
485 assertReadLong(Long.MAX_VALUE);
486 assertReadLong(4L * 1024 * 1024 * 1024, "4g");
487 assertReadLong(3L * 1024 * 1024, "3 m");
488 assertReadLong(8L * 1024, "8 k");
489
490 try {
491 assertReadLong(-1, "1.5g");
492 fail("incorrectly accepted 1.5g");
493 } catch (IllegalArgumentException e) {
494 assertEquals("Invalid integer value: s.a=1.5g", e.getMessage());
495 }
496 }
497
498 @Test
499 public void testBooleanWithNoValue() throws ConfigInvalidException {
500 Config c = parse("[my]\n\tempty\n");
501 assertEquals("", c.getString("my", null, "empty"));
502 assertEquals(1, c.getStringList("my", null, "empty").length);
503 assertEquals("", c.getStringList("my", null, "empty")[0]);
504 assertTrue(c.getBoolean("my", "empty", false));
505 assertEquals("[my]\n\tempty\n", c.toText());
506 }
507
508 @Test
509 public void testUnsetBranchSection() throws ConfigInvalidException {
510 Config c = parse(""
511 + "[branch \"keep\"]\n"
512 + " merge = master.branch.to.keep.in.the.file\n"
513 + "\n"
514 + "[branch \"remove\"]\n"
515 + " merge = this.will.get.deleted\n"
516 + " remote = origin-for-some-long-gone-place\n"
517 + "\n"
518 + "[core-section-not-to-remove-in-test]\n"
519 + " packedGitLimit = 14\n");
520 c.unsetSection("branch", "does.not.exist");
521 c.unsetSection("branch", "remove");
522 assertEquals(""
523 + "[branch \"keep\"]\n"
524 + " merge = master.branch.to.keep.in.the.file\n"
525 + "\n"
526 + "[core-section-not-to-remove-in-test]\n"
527 + " packedGitLimit = 14\n", c.toText());
528 }
529
530 @Test
531 public void testUnsetSingleSection() throws ConfigInvalidException {
532 Config c = parse(""
533 + "[branch \"keep\"]\n"
534 + " merge = master.branch.to.keep.in.the.file\n"
535 + "\n"
536 + "[single]\n"
537 + " merge = this.will.get.deleted\n"
538 + " remote = origin-for-some-long-gone-place\n"
539 + "\n"
540 + "[core-section-not-to-remove-in-test]\n"
541 + " packedGitLimit = 14\n");
542 c.unsetSection("single", null);
543 assertEquals(""
544 + "[branch \"keep\"]\n"
545 + " merge = master.branch.to.keep.in.the.file\n"
546 + "\n"
547 + "[core-section-not-to-remove-in-test]\n"
548 + " packedGitLimit = 14\n", c.toText());
549 }
550
551 @Test
552 public void test008_readSectionNames() throws ConfigInvalidException {
553 final Config c = parse("[a]\n [B]\n");
554 Set<String> sections = c.getSections();
555 assertTrue("Sections should contain \"a\"", sections.contains("a"));
556 assertTrue("Sections should contain \"b\"", sections.contains("b"));
557 }
558
559 @Test
560 public void test009_readNamesInSection() throws ConfigInvalidException {
561 String configString = "[core]\n" + "repositoryFormatVersion = 0\n"
562 + "filemode = false\n" + "logAllRefUpdates = true\n";
563 final Config c = parse(configString);
564 Set<String> names = c.getNames("core");
565 assertEquals("Core section size", 3, names.size());
566 assertTrue("Core section should contain \"filemode\"", names
567 .contains("filemode"));
568
569 assertTrue("Core section should contain \"repositoryFormatVersion\"",
570 names.contains("repositoryFormatVersion"));
571
572 assertTrue("Core section should contain \"repositoryformatversion\"",
573 names.contains("repositoryformatversion"));
574
575 Iterator<String> itr = names.iterator();
576 assertEquals("filemode", itr.next());
577 assertEquals("logAllRefUpdates", itr.next());
578 assertEquals("repositoryFormatVersion", itr.next());
579 assertFalse(itr.hasNext());
580 }
581
582 @Test
583 public void test_ReadNamesInSectionRecursive()
584 throws ConfigInvalidException {
585 String baseConfigString = "[core]\n" + "logAllRefUpdates = true\n";
586 String configString = "[core]\n" + "repositoryFormatVersion = 0\n"
587 + "filemode = false\n";
588 final Config c = parse(configString, parse(baseConfigString));
589 Set<String> names = c.getNames("core", true);
590 assertEquals("Core section size", 3, names.size());
591 assertTrue("Core section should contain \"filemode\"",
592 names.contains("filemode"));
593 assertTrue("Core section should contain \"repositoryFormatVersion\"",
594 names.contains("repositoryFormatVersion"));
595 assertTrue("Core section should contain \"logAllRefUpdates\"",
596 names.contains("logAllRefUpdates"));
597 assertTrue("Core section should contain \"logallrefupdates\"",
598 names.contains("logallrefupdates"));
599
600 Iterator<String> itr = names.iterator();
601 assertEquals("filemode", itr.next());
602 assertEquals("repositoryFormatVersion", itr.next());
603 assertEquals("logAllRefUpdates", itr.next());
604 assertFalse(itr.hasNext());
605 }
606
607 @Test
608 public void test010_readNamesInSubSection() throws ConfigInvalidException {
609 String configString = "[a \"sub1\"]\n"
610 + "x = 0\n"
611 + "y = false\n"
612 + "z = true\n"
613 + "[a \"sub2\"]\n"
614 + "a=0\n"
615 + "b=1\n";
616 final Config c = parse(configString);
617 Set<String> names = c.getNames("a", "sub1");
618 assertEquals("Subsection size", 3, names.size());
619 assertTrue("Subsection should contain \"x\"", names.contains("x"));
620 assertTrue("Subsection should contain \"y\"", names.contains("y"));
621 assertTrue("Subsection should contain \"z\"", names.contains("z"));
622 names = c.getNames("a", "sub2");
623 assertEquals("Subsection size", 2, names.size());
624 assertTrue("Subsection should contain \"a\"", names.contains("a"));
625 assertTrue("Subsection should contain \"b\"", names.contains("b"));
626 }
627
628 @Test
629 public void readNamesInSubSectionRecursive() throws ConfigInvalidException {
630 String baseConfigString = "[a \"sub1\"]\n"
631 + "x = 0\n"
632 + "y = false\n"
633 + "[a \"sub2\"]\n"
634 + "A=0\n";
635 String configString = "[a \"sub1\"]\n"
636 + "z = true\n"
637 + "[a \"sub2\"]\n"
638 + "B=1\n";
639 final Config c = parse(configString, parse(baseConfigString));
640 Set<String> names = c.getNames("a", "sub1", true);
641 assertEquals("Subsection size", 3, names.size());
642 assertTrue("Subsection should contain \"x\"", names.contains("x"));
643 assertTrue("Subsection should contain \"y\"", names.contains("y"));
644 assertTrue("Subsection should contain \"z\"", names.contains("z"));
645 names = c.getNames("a", "sub2", true);
646 assertEquals("Subsection size", 2, names.size());
647 assertTrue("Subsection should contain \"A\"", names.contains("A"));
648 assertTrue("Subsection should contain \"a\"", names.contains("a"));
649 assertTrue("Subsection should contain \"B\"", names.contains("B"));
650 }
651
652
653 @Test
654 public void testNoFinalNewline() throws ConfigInvalidException {
655 Config c = parse("[a]\n"
656 + "x = 0\n"
657 + "y = 1");
658 assertEquals("0", c.getString("a", null, "x"));
659 assertEquals("1", c.getString("a", null, "y"));
660 }
661
662 @Test
663 public void testExplicitlySetEmptyString() throws Exception {
664 Config c = new Config();
665 c.setString("a", null, "x", "0");
666 c.setString("a", null, "y", "");
667
668 assertEquals("0", c.getString("a", null, "x"));
669 assertEquals(0, c.getInt("a", null, "x", 1));
670
671 assertEquals("", c.getString("a", null, "y"));
672 assertArrayEquals(new String[]{""}, c.getStringList("a", null, "y"));
673 assertEquals(1, c.getInt("a", null, "y", 1));
674
675 assertNull(c.getString("a", null, "z"));
676 assertArrayEquals(new String[]{}, c.getStringList("a", null, "z"));
677 }
678
679 @Test
680 public void testParsedEmptyString() throws Exception {
681 Config c = parse("[a]\n"
682 + "x = 0\n"
683 + "y =\n");
684
685 assertEquals("0", c.getString("a", null, "x"));
686 assertEquals(0, c.getInt("a", null, "x", 1));
687
688 assertNull(c.getString("a", null, "y"));
689 assertArrayEquals(new String[]{null}, c.getStringList("a", null, "y"));
690 assertEquals(1, c.getInt("a", null, "y", 1));
691
692 assertNull(c.getString("a", null, "z"));
693 assertArrayEquals(new String[]{}, c.getStringList("a", null, "z"));
694 }
695
696 @Test
697 public void testSetStringListWithEmptyValue() throws Exception {
698 Config c = new Config();
699 c.setStringList("a", null, "x", Arrays.asList(""));
700 assertArrayEquals(new String[]{""}, c.getStringList("a", null, "x"));
701 }
702
703 @Test
704 public void testEmptyValueAtEof() throws Exception {
705 String text = "[a]\nx =";
706 Config c = parse(text);
707 assertNull(c.getString("a", null, "x"));
708 assertArrayEquals(new String[]{null},
709 c.getStringList("a", null, "x"));
710 c = parse(text + "\n");
711 assertNull(c.getString("a", null, "x"));
712 assertArrayEquals(new String[]{null},
713 c.getStringList("a", null, "x"));
714 }
715
716 @Test
717 public void testReadMultipleValuesForName() throws ConfigInvalidException {
718 Config c = parse("[foo]\nbar=false\nbar=true\n");
719 assertTrue(c.getBoolean("foo", "bar", false));
720 }
721
722 @Test
723 public void testIncludeInvalidName() {
724 assertThrows(JGitText.get().invalidLineInConfigFile,
725 ConfigInvalidException.class, () -> parse("[include]\nbar\n"));
726 }
727
728 @Test
729 public void testIncludeNoValue() {
730 assertThrows(JGitText.get().invalidLineInConfigFile,
731 ConfigInvalidException.class, () -> parse("[include]\npath\n"));
732 }
733
734 @Test
735 public void testIncludeEmptyValue() {
736 assertThrows(JGitText.get().invalidLineInConfigFile,
737 ConfigInvalidException.class,
738 () -> parse("[include]\npath=\n"));
739 }
740
741 @Test
742 public void testIncludeValuePathNotFound() throws ConfigInvalidException {
743
744 String notFound = "/not/found";
745 Config parsed = parse("[include]\npath=" + notFound + "\n");
746 assertEquals(1, parsed.getSections().size());
747 assertEquals(notFound, parsed.getString("include", null, "path"));
748 }
749
750 @Test
751 public void testIncludeValuePathWithTilde() throws ConfigInvalidException {
752
753
754 String notSupported = "~/someFile";
755 Config parsed = parse("[include]\npath=" + notSupported + "\n");
756 assertEquals(1, parsed.getSections().size());
757 assertEquals(notSupported, parsed.getString("include", null, "path"));
758 }
759
760 @Test
761 public void testIncludeValuePathRelative() throws ConfigInvalidException {
762
763
764 String notSupported = "someRelativeFile";
765 Config parsed = parse("[include]\npath=" + notSupported + "\n");
766 assertEquals(1, parsed.getSections().size());
767 assertEquals(notSupported, parsed.getString("include", null, "path"));
768 }
769
770 @Test
771 public void testIncludeTooManyRecursions() throws IOException {
772 File config = tmp.newFile("config");
773 String include = "[include]\npath=" + pathToString(config) + "\n";
774 Files.write(config.toPath(), include.getBytes(UTF_8));
775 try {
776 loadConfig(config);
777 fail();
778 } catch (ConfigInvalidException cie) {
779 for (Throwable t = cie; t != null; t = t.getCause()) {
780 if (t.getMessage()
781 .equals(JGitText.get().tooManyIncludeRecursions)) {
782 return;
783 }
784 }
785 fail("Expected to find expected exception message: "
786 + JGitText.get().tooManyIncludeRecursions);
787 }
788 }
789
790 @Test
791 public void testIncludeIsNoop() throws IOException, ConfigInvalidException {
792 File config = tmp.newFile("config");
793
794 String fooBar = "[foo]\nbar=true\n";
795 Files.write(config.toPath(), fooBar.getBytes(UTF_8));
796
797 Config parsed = parse("[include]\npath=" + pathToString(config) + "\n");
798 assertFalse(parsed.getBoolean("foo", "bar", false));
799 }
800
801 @Test
802 public void testIncludeCaseInsensitiveSection()
803 throws IOException, ConfigInvalidException {
804 File included = tmp.newFile("included");
805 String content = "[foo]\nbar=true\n";
806 Files.write(included.toPath(), content.getBytes(UTF_8));
807
808 File config = tmp.newFile("config");
809 content = "[Include]\npath=" + pathToString(included) + "\n";
810 Files.write(config.toPath(), content.getBytes(UTF_8));
811
812 FileBasedConfig fbConfig = loadConfig(config);
813 assertTrue(fbConfig.getBoolean("foo", "bar", false));
814 }
815
816 @Test
817 public void testIncludeCaseInsensitiveKey()
818 throws IOException, ConfigInvalidException {
819 File included = tmp.newFile("included");
820 String content = "[foo]\nbar=true\n";
821 Files.write(included.toPath(), content.getBytes(UTF_8));
822
823 File config = tmp.newFile("config");
824 content = "[include]\nPath=" + pathToString(included) + "\n";
825 Files.write(config.toPath(), content.getBytes(UTF_8));
826
827 FileBasedConfig fbConfig = loadConfig(config);
828 assertTrue(fbConfig.getBoolean("foo", "bar", false));
829 }
830
831 @Test
832 public void testIncludeExceptionContainsLine() {
833 try {
834 parse("[include]\npath=\n");
835 fail("Expected ConfigInvalidException");
836 } catch (ConfigInvalidException e) {
837 assertTrue(
838 "Expected to find the problem line in the exception message",
839 e.getMessage().contains("include.path"));
840 }
841 }
842
843 @Test
844 public void testIncludeExceptionContainsFile() throws IOException {
845 File included = tmp.newFile("included");
846 String includedPath = pathToString(included);
847 String content = "[include]\npath=\n";
848 Files.write(included.toPath(), content.getBytes(UTF_8));
849
850 File config = tmp.newFile("config");
851 String include = "[include]\npath=" + includedPath + "\n";
852 Files.write(config.toPath(), include.getBytes(UTF_8));
853 try {
854 loadConfig(config);
855 fail("Expected ConfigInvalidException");
856 } catch (ConfigInvalidException e) {
857
858
859 for (Throwable t = e; t != null; t = t.getCause()) {
860 if (t.getMessage().contains(includedPath)) {
861 return;
862 }
863 }
864 fail("Expected to find the path in the exception message: "
865 + includedPath);
866 }
867 }
868
869 @Test
870 public void testIncludeSetValueMustNotTouchIncludedLines1()
871 throws IOException, ConfigInvalidException {
872 File includedFile = createAllTypesIncludedContent();
873
874 File configFile = tmp.newFile("config");
875 String content = createAllTypesSampleContent("Alice Parker", false, 11,
876 21, 31, CoreConfig.AutoCRLF.FALSE,
877 "+refs/heads/*:refs/remotes/origin/*") + "\n[include]\npath="
878 + pathToString(includedFile);
879 Files.write(configFile.toPath(), content.getBytes(UTF_8));
880
881 FileBasedConfig fbConfig = loadConfig(configFile);
882 assertValuesAsIncluded(fbConfig, REFS_ORIGIN, REFS_UPSTREAM);
883 assertSections(fbConfig, "user", "core", "remote", "include");
884
885 setAllValuesNew(fbConfig);
886 assertValuesAsIsSaveLoad(fbConfig, config -> {
887 assertValuesAsIncluded(config, REFS_BACKUP, REFS_UPSTREAM);
888 assertSections(fbConfig, "user", "core", "remote", "include");
889 });
890 }
891
892 @Test
893 public void testIncludeSetValueMustNotTouchIncludedLines2()
894 throws IOException, ConfigInvalidException {
895 File includedFile = createAllTypesIncludedContent();
896
897 File configFile = tmp.newFile("config");
898 String content = "[include]\npath=" + pathToString(includedFile) + "\n"
899 + createAllTypesSampleContent("Alice Parker", false, 11, 21, 31,
900 CoreConfig.AutoCRLF.FALSE,
901 "+refs/heads/*:refs/remotes/origin/*");
902 Files.write(configFile.toPath(), content.getBytes(UTF_8));
903
904 FileBasedConfig fbConfig = loadConfig(configFile);
905 assertValuesAsConfig(fbConfig, REFS_UPSTREAM, REFS_ORIGIN);
906 assertSections(fbConfig, "include", "user", "core", "remote");
907
908 setAllValuesNew(fbConfig);
909 assertValuesAsIsSaveLoad(fbConfig, config -> {
910 assertValuesAsNew(config, REFS_UPSTREAM, REFS_BACKUP);
911 assertSections(fbConfig, "include", "user", "core", "remote");
912 });
913 }
914
915 @Test
916 public void testIncludeSetValueOnFileWithJustContainsInclude()
917 throws IOException, ConfigInvalidException {
918 File includedFile = createAllTypesIncludedContent();
919
920 File configFile = tmp.newFile("config");
921 String content = "[include]\npath=" + pathToString(includedFile);
922 Files.write(configFile.toPath(), content.getBytes(UTF_8));
923
924 FileBasedConfig fbConfig = loadConfig(configFile);
925 assertValuesAsIncluded(fbConfig, REFS_UPSTREAM);
926 assertSections(fbConfig, "include", "user", "core", "remote");
927
928 setAllValuesNew(fbConfig);
929 assertValuesAsIsSaveLoad(fbConfig, config -> {
930 assertValuesAsNew(config, REFS_UPSTREAM, REFS_BACKUP);
931 assertSections(fbConfig, "include", "user", "core", "remote");
932 });
933 }
934
935 @Test
936 public void testIncludeSetValueOnFileWithJustEmptySection1()
937 throws IOException, ConfigInvalidException {
938 File includedFile = createAllTypesIncludedContent();
939
940 File configFile = tmp.newFile("config");
941 String content = "[user]\n[include]\npath="
942 + pathToString(includedFile);
943 Files.write(configFile.toPath(), content.getBytes(UTF_8));
944
945 FileBasedConfig fbConfig = loadConfig(configFile);
946 assertValuesAsIncluded(fbConfig, REFS_UPSTREAM);
947 assertSections(fbConfig, "user", "include", "core", "remote");
948
949 setAllValuesNew(fbConfig);
950 assertValuesAsIsSaveLoad(fbConfig, config -> {
951 assertValuesAsNewWithName(config, "Alice Muller", REFS_UPSTREAM,
952 REFS_BACKUP);
953 assertSections(fbConfig, "user", "include", "core", "remote");
954 });
955 }
956
957 @Test
958 public void testIncludeSetValueOnFileWithJustEmptySection2()
959 throws IOException, ConfigInvalidException {
960 File includedFile = createAllTypesIncludedContent();
961
962 File configFile = tmp.newFile("config");
963 String content = "[include]\npath=" + pathToString(includedFile)
964 + "\n[user]";
965 Files.write(configFile.toPath(), content.getBytes(UTF_8));
966
967 FileBasedConfig fbConfig = loadConfig(configFile);
968 assertValuesAsIncluded(fbConfig, REFS_UPSTREAM);
969 assertSections(fbConfig, "include", "user", "core", "remote");
970
971 setAllValuesNew(fbConfig);
972 assertValuesAsIsSaveLoad(fbConfig, config -> {
973 assertValuesAsNew(config, REFS_UPSTREAM, REFS_BACKUP);
974 assertSections(fbConfig, "include", "user", "core", "remote");
975 });
976 }
977
978 @Test
979 public void testIncludeSetValueOnFileWithJustExistingSection1()
980 throws IOException, ConfigInvalidException {
981 File includedFile = createAllTypesIncludedContent();
982
983 File configFile = tmp.newFile("config");
984 String content = "[user]\nemail=alice@home\n[include]\npath="
985 + pathToString(includedFile);
986 Files.write(configFile.toPath(), content.getBytes(UTF_8));
987
988 FileBasedConfig fbConfig = loadConfig(configFile);
989 assertValuesAsIncluded(fbConfig, REFS_UPSTREAM);
990 assertSections(fbConfig, "user", "include", "core", "remote");
991
992 setAllValuesNew(fbConfig);
993 assertValuesAsIsSaveLoad(fbConfig, config -> {
994 assertValuesAsNewWithName(config, "Alice Muller", REFS_UPSTREAM,
995 REFS_BACKUP);
996 assertSections(fbConfig, "user", "include", "core", "remote");
997 });
998 }
999
1000 @Test
1001 public void testIncludeSetValueOnFileWithJustExistingSection2()
1002 throws IOException, ConfigInvalidException {
1003 File includedFile = createAllTypesIncludedContent();
1004
1005 File configFile = tmp.newFile("config");
1006 String content = "[include]\npath=" + pathToString(includedFile)
1007 + "\n[user]\nemail=alice@home\n";
1008 Files.write(configFile.toPath(), content.getBytes(UTF_8));
1009
1010 FileBasedConfig fbConfig = loadConfig(configFile);
1011 assertValuesAsIncluded(fbConfig, REFS_UPSTREAM);
1012 assertSections(fbConfig, "include", "user", "core", "remote");
1013
1014 setAllValuesNew(fbConfig);
1015 assertValuesAsIsSaveLoad(fbConfig, config -> {
1016 assertValuesAsNew(config, REFS_UPSTREAM, REFS_BACKUP);
1017 assertSections(fbConfig, "include", "user", "core", "remote");
1018 });
1019 }
1020
1021 @Test
1022 public void testIncludeUnsetSectionMustNotTouchIncludedLines()
1023 throws IOException, ConfigInvalidException {
1024 File includedFile = tmp.newFile("included");
1025 RefSpec includedRefSpec = new RefSpec(REFS_UPSTREAM);
1026 String includedContent = "[remote \"origin\"]\n" + "fetch="
1027 + includedRefSpec;
1028 Files.write(includedFile.toPath(), includedContent.getBytes(UTF_8));
1029
1030 File configFile = tmp.newFile("config");
1031 RefSpec refSpec = new RefSpec(REFS_ORIGIN);
1032 String content = "[include]\npath=" + pathToString(includedFile) + "\n"
1033 + "[remote \"origin\"]\n" + "fetch=" + refSpec;
1034 Files.write(configFile.toPath(), content.getBytes(UTF_8));
1035
1036 FileBasedConfig fbConfig = loadConfig(configFile);
1037
1038 Consumer<FileBasedConfig> assertion = config -> {
1039 assertEquals(Arrays.asList(includedRefSpec, refSpec),
1040 config.getRefSpecs("remote", "origin", "fetch"));
1041 };
1042 assertion.accept(fbConfig);
1043
1044 fbConfig.unsetSection("remote", "origin");
1045 assertValuesAsIsSaveLoad(fbConfig, config -> {
1046 assertEquals(Collections.singletonList(includedRefSpec),
1047 config.getRefSpecs("remote", "origin", "fetch"));
1048 });
1049 }
1050
1051 private File createAllTypesIncludedContent() throws IOException {
1052 File includedFile = tmp.newFile("included");
1053 String includedContent = createAllTypesSampleContent("Alice Muller",
1054 true, 10, 20, 30, CoreConfig.AutoCRLF.TRUE,
1055 "+refs/heads/*:refs/remotes/upstream/*");
1056 Files.write(includedFile.toPath(), includedContent.getBytes(UTF_8));
1057 return includedFile;
1058 }
1059
1060 private static void assertValuesAsIsSaveLoad(FileBasedConfig fbConfig,
1061 Consumer<FileBasedConfig> assertion)
1062 throws IOException, ConfigInvalidException {
1063 assertion.accept(fbConfig);
1064
1065 fbConfig.save();
1066 assertion.accept(fbConfig);
1067
1068 fbConfig = loadConfig(fbConfig.getFile());
1069 assertion.accept(fbConfig);
1070 }
1071
1072 private static void setAllValuesNew(Config config) {
1073 config.setString("user", null, "name", "Alice Bauer");
1074 config.setBoolean("core", null, "fileMode", false);
1075 config.setInt("core", null, "deltaBaseCacheLimit", 12);
1076 config.setLong("core", null, "packedGitLimit", 22);
1077 config.setLong("core", null, "repositoryCacheExpireAfter", 32);
1078 config.setEnum("core", null, "autocrlf", CoreConfig.AutoCRLF.FALSE);
1079 config.setString("remote", "origin", "fetch",
1080 "+refs/heads/*:refs/remotes/backup/*");
1081 }
1082
1083 private static void assertValuesAsIncluded(Config config, String... refs) {
1084 assertAllTypesSampleContent("Alice Muller", true, 10, 20, 30,
1085 CoreConfig.AutoCRLF.TRUE, config, refs);
1086 }
1087
1088 private static void assertValuesAsConfig(Config config, String... refs) {
1089 assertAllTypesSampleContent("Alice Parker", false, 11, 21, 31,
1090 CoreConfig.AutoCRLF.FALSE, config, refs);
1091 }
1092
1093 private static void assertValuesAsNew(Config config, String... refs) {
1094 assertValuesAsNewWithName(config, "Alice Bauer", refs);
1095 }
1096
1097 private static void assertValuesAsNewWithName(Config config, String name,
1098 String... refs) {
1099 assertAllTypesSampleContent(name, false, 12, 22, 32,
1100 CoreConfig.AutoCRLF.FALSE, config, refs);
1101 }
1102
1103 private static void assertSections(Config config, String... sections) {
1104 assertEquals(Arrays.asList(sections),
1105 new ArrayList<>(config.getSections()));
1106 }
1107
1108 private static String createAllTypesSampleContent(String name,
1109 boolean fileMode, int deltaBaseCacheLimit, long packedGitLimit,
1110 long repositoryCacheExpireAfter, CoreConfig.AutoCRLF autoCRLF,
1111 String fetchRefSpec) {
1112 final StringBuilder builder = new StringBuilder();
1113 builder.append("[user]\n");
1114 builder.append("name=");
1115 builder.append(name);
1116 builder.append("\n");
1117
1118 builder.append("[core]\n");
1119 builder.append("fileMode=");
1120 builder.append(fileMode);
1121 builder.append("\n");
1122
1123 builder.append("deltaBaseCacheLimit=");
1124 builder.append(deltaBaseCacheLimit);
1125 builder.append("\n");
1126
1127 builder.append("packedGitLimit=");
1128 builder.append(packedGitLimit);
1129 builder.append("\n");
1130
1131 builder.append("repositoryCacheExpireAfter=");
1132 builder.append(repositoryCacheExpireAfter);
1133 builder.append("\n");
1134
1135 builder.append("autocrlf=");
1136 builder.append(autoCRLF.name());
1137 builder.append("\n");
1138
1139 builder.append("[remote \"origin\"]\n");
1140 builder.append("fetch=");
1141 builder.append(fetchRefSpec);
1142 builder.append("\n");
1143 return builder.toString();
1144 }
1145
1146 private static void assertAllTypesSampleContent(String name,
1147 boolean fileMode, int deltaBaseCacheLimit, long packedGitLimit,
1148 long repositoryCacheExpireAfter, CoreConfig.AutoCRLF autoCRLF,
1149 Config config, String... fetchRefSpecs) {
1150 assertEquals(name, config.getString("user", null, "name"));
1151 assertEquals(fileMode,
1152 config.getBoolean("core", "fileMode", !fileMode));
1153 assertEquals(deltaBaseCacheLimit,
1154 config.getInt("core", "deltaBaseCacheLimit", -1));
1155 assertEquals(packedGitLimit,
1156 config.getLong("core", "packedGitLimit", -1));
1157 assertEquals(repositoryCacheExpireAfter, config.getTimeUnit("core",
1158 null, "repositoryCacheExpireAfter", -1, MILLISECONDS));
1159 assertEquals(autoCRLF, config.getEnum("core", null, "autocrlf",
1160 CoreConfig.AutoCRLF.INPUT));
1161 final List<RefSpec> refspecs = new ArrayList<>();
1162 for (String fetchRefSpec : fetchRefSpecs) {
1163 refspecs.add(new RefSpec(fetchRefSpec));
1164 }
1165
1166 assertEquals(refspecs, config.getRefSpecs("remote", "origin", "fetch"));
1167 }
1168
1169 private static void assertReadLong(long exp) throws ConfigInvalidException {
1170 assertReadLong(exp, String.valueOf(exp));
1171 }
1172
1173 private static void assertReadLong(long exp, String act)
1174 throws ConfigInvalidException {
1175 final Config c = parse("[s]\na = " + act + "\n");
1176 assertEquals(exp, c.getLong("s", null, "a", 0L));
1177 }
1178
1179 private static Config parse(String content)
1180 throws ConfigInvalidException {
1181 return parse(content, null);
1182 }
1183
1184 private static Config parse(String content, Config baseConfig)
1185 throws ConfigInvalidException {
1186 final Config c = new Config(baseConfig);
1187 c.fromText(content);
1188 return c;
1189 }
1190
1191 @Test
1192 public void testTimeUnit() throws ConfigInvalidException {
1193 assertEquals(0, parseTime("0", NANOSECONDS));
1194 assertEquals(2, parseTime("2ns", NANOSECONDS));
1195 assertEquals(200, parseTime("200 nanoseconds", NANOSECONDS));
1196
1197 assertEquals(0, parseTime("0", MICROSECONDS));
1198 assertEquals(2, parseTime("2us", MICROSECONDS));
1199 assertEquals(2, parseTime("2000 nanoseconds", MICROSECONDS));
1200 assertEquals(200, parseTime("200 microseconds", MICROSECONDS));
1201
1202 assertEquals(0, parseTime("0", MILLISECONDS));
1203 assertEquals(2, parseTime("2ms", MILLISECONDS));
1204 assertEquals(2, parseTime("2000microseconds", MILLISECONDS));
1205 assertEquals(200, parseTime("200 milliseconds", MILLISECONDS));
1206
1207 assertEquals(0, parseTime("0s", SECONDS));
1208 assertEquals(2, parseTime("2s", SECONDS));
1209 assertEquals(231, parseTime("231sec", SECONDS));
1210 assertEquals(1, parseTime("1second", SECONDS));
1211 assertEquals(300, parseTime("300 seconds", SECONDS));
1212
1213 assertEquals(2, parseTime("2m", MINUTES));
1214 assertEquals(2, parseTime("2min", MINUTES));
1215 assertEquals(1, parseTime("1 minute", MINUTES));
1216 assertEquals(10, parseTime("10 minutes", MINUTES));
1217
1218 assertEquals(5, parseTime("5h", HOURS));
1219 assertEquals(5, parseTime("5hr", HOURS));
1220 assertEquals(1, parseTime("1hour", HOURS));
1221 assertEquals(48, parseTime("48hours", HOURS));
1222
1223 assertEquals(5, parseTime("5 h", HOURS));
1224 assertEquals(5, parseTime("5 hr", HOURS));
1225 assertEquals(1, parseTime("1 hour", HOURS));
1226 assertEquals(48, parseTime("48 hours", HOURS));
1227 assertEquals(48, parseTime("48 \t \r hours", HOURS));
1228
1229 assertEquals(4, parseTime("4d", DAYS));
1230 assertEquals(1, parseTime("1day", DAYS));
1231 assertEquals(14, parseTime("14days", DAYS));
1232
1233 assertEquals(7, parseTime("1w", DAYS));
1234 assertEquals(7, parseTime("1week", DAYS));
1235 assertEquals(14, parseTime("2w", DAYS));
1236 assertEquals(14, parseTime("2weeks", DAYS));
1237
1238 assertEquals(30, parseTime("1mon", DAYS));
1239 assertEquals(30, parseTime("1month", DAYS));
1240 assertEquals(60, parseTime("2mon", DAYS));
1241 assertEquals(60, parseTime("2months", DAYS));
1242
1243 assertEquals(365, parseTime("1y", DAYS));
1244 assertEquals(365, parseTime("1year", DAYS));
1245 assertEquals(365 * 2, parseTime("2years", DAYS));
1246 }
1247
1248 private long parseTime(String value, TimeUnit unit)
1249 throws ConfigInvalidException {
1250 Config c = parse("[a]\na=" + value + "\n");
1251 return c.getTimeUnit("a", null, "a", 0, unit);
1252 }
1253
1254 @Test
1255 public void testTimeUnitDefaultValue() throws ConfigInvalidException {
1256
1257 assertEquals(20, parse("[a]\na=0\n").getTimeUnit("a", null, "b", 20,
1258 MILLISECONDS));
1259
1260 assertEquals(20, parse("[a]\na=\" \"\n").getTimeUnit("a", null, "a", 20,
1261 MILLISECONDS));
1262
1263
1264 assertEquals(20, parse("[a]\na=test\n").getTimeUnit("a", null, "a", 20,
1265 MILLISECONDS));
1266 }
1267
1268 @Test
1269 public void testTimeUnitInvalid() {
1270 assertThrows("Invalid time unit value: a.a=1 monttthhh",
1271 IllegalArgumentException.class,
1272 () -> parseTime("1 monttthhh", DAYS));
1273 }
1274
1275 @Test
1276 public void testTimeUnitInvalidWithSection() throws ConfigInvalidException {
1277 Config c = parse("[a \"b\"]\na=1 monttthhh\n");
1278 assertThrows("Invalid time unit value: a.b.a=1 monttthhh",
1279 IllegalArgumentException.class,
1280 () -> c.getTimeUnit("a", "b", "a", 0, DAYS));
1281 }
1282
1283 @Test
1284 public void testTimeUnitNegative() {
1285 assertThrows(IllegalArgumentException.class,
1286 () -> parseTime("-1", MILLISECONDS));
1287 }
1288
1289 @Test
1290 public void testEscapeSpacesOnly() throws ConfigInvalidException {
1291
1292 assertEquals("", Config.escapeValue(""));
1293
1294 assertValueRoundTrip(" ", "\" \"");
1295 assertValueRoundTrip(" ", "\" \"");
1296 }
1297
1298 @Test
1299 public void testEscapeLeadingSpace() throws ConfigInvalidException {
1300 assertValueRoundTrip("x", "x");
1301 assertValueRoundTrip(" x", "\" x\"");
1302 assertValueRoundTrip(" x", "\" x\"");
1303 }
1304
1305 @Test
1306 public void testEscapeTrailingSpace() throws ConfigInvalidException {
1307 assertValueRoundTrip("x", "x");
1308 assertValueRoundTrip("x ","\"x \"");
1309 assertValueRoundTrip("x ","\"x \"");
1310 }
1311
1312 @Test
1313 public void testEscapeLeadingAndTrailingSpace()
1314 throws ConfigInvalidException {
1315 assertValueRoundTrip(" x ", "\" x \"");
1316 assertValueRoundTrip(" x ", "\" x \"");
1317 assertValueRoundTrip(" x ", "\" x \"");
1318 assertValueRoundTrip(" x ", "\" x \"");
1319 }
1320
1321 @Test
1322 public void testNoEscapeInternalSpaces() throws ConfigInvalidException {
1323 assertValueRoundTrip("x y");
1324 assertValueRoundTrip("x y");
1325 assertValueRoundTrip("x y");
1326 assertValueRoundTrip("x y z");
1327 assertValueRoundTrip("x " + WS + " y");
1328 }
1329
1330 @Test
1331 public void testNoEscapeSpecialCharacters() throws ConfigInvalidException {
1332 assertValueRoundTrip("x\\y", "x\\\\y");
1333 assertValueRoundTrip("x\"y", "x\\\"y");
1334 assertValueRoundTrip("x\ny", "x\\ny");
1335 assertValueRoundTrip("x\ty", "x\\ty");
1336 assertValueRoundTrip("x\by", "x\\by");
1337 }
1338
1339 @Test
1340 public void testParseLiteralBackspace() throws ConfigInvalidException {
1341
1342
1343 assertEquals("x\by", parseEscapedValue("x\by"));
1344 }
1345
1346 @Test
1347 public void testEscapeCommentCharacters() throws ConfigInvalidException {
1348 assertValueRoundTrip("x#y", "\"x#y\"");
1349 assertValueRoundTrip("x;y", "\"x;y\"");
1350 }
1351
1352 @Test
1353 public void testEscapeValueInvalidCharacters() {
1354 assertIllegalArgumentException(() -> Config.escapeSubsection("x\0y"));
1355 }
1356
1357 @Test
1358 public void testEscapeSubsectionInvalidCharacters() {
1359 assertIllegalArgumentException(() -> Config.escapeSubsection("x\ny"));
1360 assertIllegalArgumentException(() -> Config.escapeSubsection("x\0y"));
1361 }
1362
1363 @Test
1364 public void testParseMultipleQuotedRegions() throws ConfigInvalidException {
1365 assertEquals("b a z; \n", parseEscapedValue("b\" a\"\" z; \\n\""));
1366 }
1367
1368 @Test
1369 public void testParseComments() throws ConfigInvalidException {
1370 assertEquals("baz", parseEscapedValue("baz; comment"));
1371 assertEquals("baz", parseEscapedValue("baz# comment"));
1372 assertEquals("baz", parseEscapedValue("baz ; comment"));
1373 assertEquals("baz", parseEscapedValue("baz # comment"));
1374
1375 assertEquals("baz", parseEscapedValue("baz ; comment"));
1376 assertEquals("baz", parseEscapedValue("baz # comment"));
1377 assertEquals("baz", parseEscapedValue("baz " + WS + " ; comment"));
1378 assertEquals("baz", parseEscapedValue("baz " + WS + " # comment"));
1379
1380 assertEquals("baz ", parseEscapedValue("\"baz \"; comment"));
1381 assertEquals("baz ", parseEscapedValue("\"baz \"# comment"));
1382 assertEquals("baz ", parseEscapedValue("\"baz \" ; comment"));
1383 assertEquals("baz ", parseEscapedValue("\"baz \" # comment"));
1384 }
1385
1386 @Test
1387 public void testEscapeSubsection() throws ConfigInvalidException {
1388 assertSubsectionRoundTrip("", "\"\"");
1389 assertSubsectionRoundTrip("x", "\"x\"");
1390 assertSubsectionRoundTrip(" x", "\" x\"");
1391 assertSubsectionRoundTrip("x ", "\"x \"");
1392 assertSubsectionRoundTrip(" x ", "\" x \"");
1393 assertSubsectionRoundTrip("x y", "\"x y\"");
1394 assertSubsectionRoundTrip("x y", "\"x y\"");
1395 assertSubsectionRoundTrip("x\\y", "\"x\\\\y\"");
1396 assertSubsectionRoundTrip("x\"y", "\"x\\\"y\"");
1397
1398
1399 assertSubsectionRoundTrip("x\by", "\"x\by\"");
1400 assertSubsectionRoundTrip("x\ty", "\"x\ty\"");
1401 }
1402
1403 @Test
1404 public void testParseInvalidValues() {
1405 assertInvalidValue(JGitText.get().newlineInQuotesNotAllowed, "x\"\n\"y");
1406 assertInvalidValue(JGitText.get().endOfFileInEscape, "x\\");
1407 assertInvalidValue(
1408 MessageFormat.format(JGitText.get().badEscape, 'q'), "x\\q");
1409 }
1410
1411 @Test
1412 public void testParseInvalidSubsections() {
1413 assertInvalidSubsection(
1414 JGitText.get().newlineInQuotesNotAllowed, "\"x\ny\"");
1415 }
1416
1417 @Test
1418 public void testDropBackslashFromInvalidEscapeSequenceInSubsectionName()
1419 throws ConfigInvalidException {
1420 assertEquals("x0", parseEscapedSubsection("\"x\\0\""));
1421 assertEquals("xq", parseEscapedSubsection("\"x\\q\""));
1422
1423 assertEquals("xb", parseEscapedSubsection("\"x\\b\""));
1424 assertEquals("xn", parseEscapedSubsection("\"x\\n\""));
1425 assertEquals("xt", parseEscapedSubsection("\"x\\t\""));
1426 }
1427
1428 @Test
1429 public void testInvalidGroupHeader() {
1430 assertThrows(JGitText.get().badGroupHeader,
1431 ConfigInvalidException.class,
1432 () -> parse("[foo \"bar\" ]\nfoo=bar\n"));
1433 }
1434
1435 @Test
1436 public void testCrLf() throws ConfigInvalidException {
1437 assertEquals("true", parseEscapedValue("true\r\n"));
1438 }
1439
1440 @Test
1441 public void testLfContinuation() throws ConfigInvalidException {
1442 assertEquals("true", parseEscapedValue("tr\\\nue"));
1443 }
1444
1445 @Test
1446 public void testCrCharContinuation() {
1447 assertThrows("Bad escape: \\u000d", ConfigInvalidException.class,
1448 () -> parseEscapedValue("tr\\\rue"));
1449 }
1450
1451 @Test
1452 public void testCrEOFContinuation() {
1453 assertThrows("Bad escape: \\u000d", ConfigInvalidException.class,
1454 () -> parseEscapedValue("tr\\\r"));
1455 }
1456
1457 @Test
1458 public void testCrLfContinuation() throws ConfigInvalidException {
1459 assertEquals("true", parseEscapedValue("tr\\\r\nue"));
1460 }
1461
1462 @Test
1463 public void testWhitespaceContinuation() throws ConfigInvalidException {
1464 assertEquals("tr ue", parseEscapedValue("tr \\\n ue"));
1465 assertEquals("tr ue", parseEscapedValue("tr \\\r\n ue"));
1466 }
1467
1468 @Test
1469 public void testCommitTemplateEmptyConfig()
1470 throws ConfigInvalidException, IOException {
1471
1472 Config config = new Config(null);
1473 assertNull(config.get(CommitConfig.KEY).getCommitTemplatePath());
1474 assertNull(config.get(CommitConfig.KEY).getCommitTemplateContent());
1475 }
1476
1477 @Test
1478 public void testCommitTemplateConfig()
1479 throws ConfigInvalidException, IOException {
1480
1481 File tempFile = tmp.newFile("testCommitTemplate-");
1482 String templateContent = "content of the template";
1483 JGitTestUtil.write(tempFile, templateContent);
1484 String expectedTemplatePath = tempFile.getPath();
1485
1486 Config config = parse(
1487 "[commit]\n\ttemplate = " + expectedTemplatePath + "\n");
1488
1489 String templatePath = config.get(CommitConfig.KEY)
1490 .getCommitTemplatePath();
1491 String commitEncoding = config.get(CommitConfig.KEY)
1492 .getCommitEncoding();
1493 assertEquals(expectedTemplatePath, templatePath);
1494 assertEquals(templateContent,
1495 config.get(CommitConfig.KEY).getCommitTemplateContent());
1496 assertNull("no commitEncoding has been set so it must be null",
1497 commitEncoding);
1498 }
1499
1500 @Test
1501 public void testCommitTemplateEncoding()
1502 throws ConfigInvalidException, IOException {
1503 Config config = new Config(null);
1504 File tempFile = tmp.newFile("testCommitTemplate-");
1505 String templateContent = "content of the template";
1506 JGitTestUtil.write(tempFile, templateContent);
1507 String expectedTemplatePath = tempFile.getPath();
1508 config = parse("[i18n]\n\tcommitEncoding = utf-8\n"
1509 + "[commit]\n\ttemplate = " + expectedTemplatePath + "\n");
1510 assertEquals(templateContent,
1511 config.get(CommitConfig.KEY).getCommitTemplateContent());
1512 String commitEncoding = config.get(CommitConfig.KEY)
1513 .getCommitEncoding();
1514 assertEquals("commitEncoding has been set to utf-8 it must be utf-8",
1515 "utf-8", commitEncoding);
1516 }
1517
1518 @Test
1519 public void testCommitTemplatePathInHomeDirecory()
1520 throws ConfigInvalidException, IOException {
1521 Config config = new Config(null);
1522 File tempFile = tmp.newFile("testCommitTemplate-");
1523 String templateContent = "content of the template";
1524 JGitTestUtil.write(tempFile, templateContent);
1525
1526 String homeDir = System.getProperty("user.home");
1527 File tempFileInHomeDirectory = File.createTempFile("fileInHomeFolder",
1528 ".tmp", new File(homeDir));
1529 tempFileInHomeDirectory.deleteOnExit();
1530 JGitTestUtil.write(tempFileInHomeDirectory, templateContent);
1531 String expectedTemplatePath = tempFileInHomeDirectory.getPath()
1532 .replace(homeDir, "~");
1533 config = parse("[commit]\n\ttemplate = " + expectedTemplatePath + "\n");
1534 String templatePath = config.get(CommitConfig.KEY)
1535 .getCommitTemplatePath();
1536 assertEquals(expectedTemplatePath, templatePath);
1537 assertEquals(templateContent,
1538 config.get(CommitConfig.KEY).getCommitTemplateContent());
1539 }
1540
1541 @Test(expected = ConfigInvalidException.class)
1542 public void testCommitTemplateWithInvalidEncoding()
1543 throws ConfigInvalidException, IOException {
1544 Config config = new Config(null);
1545 File tempFile = tmp.newFile("testCommitTemplate-");
1546 String templateContent = "content of the template";
1547 JGitTestUtil.write(tempFile, templateContent);
1548 config = parse("[i18n]\n\tcommitEncoding = invalidEcoding\n"
1549 + "[commit]\n\ttemplate = " + tempFile.getPath() + "\n");
1550 config.get(CommitConfig.KEY).getCommitTemplateContent();
1551 }
1552
1553 @Test(expected = FileNotFoundException.class)
1554 public void testCommitTemplateWithInvalidPath()
1555 throws ConfigInvalidException, IOException {
1556 Config config = new Config(null);
1557 File tempFile = tmp.newFile("testCommitTemplate-");
1558 String templateContent = "content of the template";
1559 JGitTestUtil.write(tempFile, templateContent);
1560
1561 String expectedTemplatePath = "nonExistingTemplate";
1562 config = parse("[commit]\n\ttemplate = " + expectedTemplatePath + "\n");
1563 String templatePath = config.get(CommitConfig.KEY)
1564 .getCommitTemplatePath();
1565 assertEquals(expectedTemplatePath, templatePath);
1566 config.get(CommitConfig.KEY).getCommitTemplateContent();
1567 }
1568
1569 private static void assertValueRoundTrip(String value)
1570 throws ConfigInvalidException {
1571 assertValueRoundTrip(value, value);
1572 }
1573
1574 private static void assertValueRoundTrip(String value, String expectedEscaped)
1575 throws ConfigInvalidException {
1576 String escaped = Config.escapeValue(value);
1577 assertEquals("escape failed;", expectedEscaped, escaped);
1578 assertEquals("parse failed;", value, parseEscapedValue(escaped));
1579 }
1580
1581 private static String parseEscapedValue(String escapedValue)
1582 throws ConfigInvalidException {
1583 String text = "[foo]\nbar=" + escapedValue;
1584 Config c = parse(text);
1585 return c.getString("foo", null, "bar");
1586 }
1587
1588 private static void assertInvalidValue(String expectedMessage,
1589 String escapedValue) {
1590 try {
1591 parseEscapedValue(escapedValue);
1592 fail("expected ConfigInvalidException");
1593 } catch (ConfigInvalidException e) {
1594 assertEquals(expectedMessage, e.getMessage());
1595 }
1596 }
1597
1598 private static void assertSubsectionRoundTrip(String subsection,
1599 String expectedEscaped) throws ConfigInvalidException {
1600 String escaped = Config.escapeSubsection(subsection);
1601 assertEquals("escape failed;", expectedEscaped, escaped);
1602 assertEquals("parse failed;", subsection, parseEscapedSubsection(escaped));
1603 }
1604
1605 private static String parseEscapedSubsection(String escapedSubsection)
1606 throws ConfigInvalidException {
1607 String text = "[foo " + escapedSubsection + "]\nbar = value";
1608 Config c = parse(text);
1609 Set<String> subsections = c.getSubsections("foo");
1610 assertEquals("only one section", 1, subsections.size());
1611 return subsections.iterator().next();
1612 }
1613
1614 private static void assertIllegalArgumentException(Runnable r) {
1615 try {
1616 r.run();
1617 fail("expected IllegalArgumentException");
1618 } catch (IllegalArgumentException e) {
1619
1620 }
1621 }
1622
1623 private static void assertInvalidSubsection(String expectedMessage,
1624 String escapedSubsection) {
1625 try {
1626 parseEscapedSubsection(escapedSubsection);
1627 fail("expected ConfigInvalidException");
1628 } catch (ConfigInvalidException e) {
1629 assertEquals(expectedMessage, e.getMessage());
1630 }
1631 }
1632
1633 private static FileBasedConfig loadConfig(File file)
1634 throws IOException, ConfigInvalidException {
1635 final FileBasedConfig config = new FileBasedConfig(null, file,
1636 FS.DETECTED);
1637 config.load();
1638 return config;
1639 }
1640 }