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