You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigTest.java 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  3. * Copyright (C) 2009-2010, Google Inc.
  4. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  5. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  6. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  7. * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
  8. * and other copyright owners as documented in the project's IP log.
  9. *
  10. * This program and the accompanying materials are made available
  11. * under the terms of the Eclipse Distribution License v1.0 which
  12. * accompanies this distribution, is reproduced below, and is
  13. * available at http://www.eclipse.org/org/documents/edl-v10.php
  14. *
  15. * All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or
  18. * without modification, are permitted provided that the following
  19. * conditions are met:
  20. *
  21. * - Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. *
  24. * - Redistributions in binary form must reproduce the above
  25. * copyright notice, this list of conditions and the following
  26. * disclaimer in the documentation and/or other materials provided
  27. * with the distribution.
  28. *
  29. * - Neither the name of the Eclipse Foundation, Inc. nor the
  30. * names of its contributors may be used to endorse or promote
  31. * products derived from this software without specific prior
  32. * written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  35. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  36. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  38. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  39. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  43. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  46. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. */
  48. package org.eclipse.jgit.lib;
  49. import static org.junit.Assert.assertArrayEquals;
  50. import static org.junit.Assert.assertEquals;
  51. import static org.junit.Assert.assertFalse;
  52. import static org.junit.Assert.assertNotNull;
  53. import static org.junit.Assert.assertNull;
  54. import static org.junit.Assert.assertSame;
  55. import static org.junit.Assert.assertTrue;
  56. import static org.junit.Assert.fail;
  57. import java.text.MessageFormat;
  58. import java.util.Arrays;
  59. import java.util.Iterator;
  60. import java.util.LinkedList;
  61. import java.util.Set;
  62. import org.eclipse.jgit.api.MergeCommand.FastForwardMode;
  63. import org.eclipse.jgit.errors.ConfigInvalidException;
  64. import org.eclipse.jgit.junit.MockSystemReader;
  65. import org.eclipse.jgit.util.FS;
  66. import org.eclipse.jgit.util.SystemReader;
  67. import org.junit.After;
  68. import org.junit.Test;
  69. /**
  70. * Test reading of git config
  71. */
  72. public class ConfigTest {
  73. @After
  74. public void tearDown() {
  75. SystemReader.setInstance(null);
  76. }
  77. @Test
  78. public void test001_ReadBareKey() throws ConfigInvalidException {
  79. final Config c = parse("[foo]\nbar\n");
  80. assertTrue(c.getBoolean("foo", null, "bar", false));
  81. assertEquals("", c.getString("foo", null, "bar"));
  82. }
  83. @Test
  84. public void test002_ReadWithSubsection() throws ConfigInvalidException {
  85. final Config c = parse("[foo \"zip\"]\nbar\n[foo \"zap\"]\nbar=false\nn=3\n");
  86. assertTrue(c.getBoolean("foo", "zip", "bar", false));
  87. assertEquals("", c.getString("foo","zip", "bar"));
  88. assertFalse(c.getBoolean("foo", "zap", "bar", true));
  89. assertEquals("false", c.getString("foo", "zap", "bar"));
  90. assertEquals(3, c.getInt("foo", "zap", "n", 4));
  91. assertEquals(4, c.getInt("foo", "zap","m", 4));
  92. }
  93. @Test
  94. public void test003_PutRemote() {
  95. final Config c = new Config();
  96. c.setString("sec", "ext", "name", "value");
  97. c.setString("sec", "ext", "name2", "value2");
  98. final String expText = "[sec \"ext\"]\n\tname = value\n\tname2 = value2\n";
  99. assertEquals(expText, c.toText());
  100. }
  101. @Test
  102. public void test004_PutGetSimple() {
  103. Config c = new Config();
  104. c.setString("my", null, "somename", "false");
  105. assertEquals("false", c.getString("my", null, "somename"));
  106. assertEquals("[my]\n\tsomename = false\n", c.toText());
  107. }
  108. @Test
  109. public void test005_PutGetStringList() {
  110. Config c = new Config();
  111. final LinkedList<String> values = new LinkedList<String>();
  112. values.add("value1");
  113. values.add("value2");
  114. c.setStringList("my", null, "somename", values);
  115. final Object[] expArr = values.toArray();
  116. final String[] actArr = c.getStringList("my", null, "somename");
  117. assertArrayEquals(expArr, actArr);
  118. final String expText = "[my]\n\tsomename = value1\n\tsomename = value2\n";
  119. assertEquals(expText, c.toText());
  120. }
  121. @Test
  122. public void test006_readCaseInsensitive() throws ConfigInvalidException {
  123. final Config c = parse("[Foo]\nBar\n");
  124. assertTrue(c.getBoolean("foo", null, "bar", false));
  125. assertEquals("", c.getString("foo", null, "bar"));
  126. }
  127. @Test
  128. public void test007_readUserConfig() {
  129. final MockSystemReader mockSystemReader = new MockSystemReader();
  130. SystemReader.setInstance(mockSystemReader);
  131. final String hostname = mockSystemReader.getHostname();
  132. final Config userGitConfig = mockSystemReader.openUserConfig(null,
  133. FS.DETECTED);
  134. final Config localConfig = new Config(userGitConfig);
  135. mockSystemReader.clearProperties();
  136. String authorName;
  137. String authorEmail;
  138. // no values defined nowhere
  139. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  140. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  141. assertEquals(Constants.UNKNOWN_USER_DEFAULT, authorName);
  142. assertEquals(Constants.UNKNOWN_USER_DEFAULT + "@" + hostname, authorEmail);
  143. assertTrue(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
  144. assertTrue(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
  145. // the system user name is defined
  146. mockSystemReader.setProperty(Constants.OS_USER_NAME_KEY, "os user name");
  147. localConfig.uncache(UserConfig.KEY);
  148. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  149. assertEquals("os user name", authorName);
  150. assertTrue(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
  151. if (hostname != null && hostname.length() != 0) {
  152. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  153. assertEquals("os user name@" + hostname, authorEmail);
  154. }
  155. assertTrue(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
  156. // the git environment variables are defined
  157. mockSystemReader.setProperty(Constants.GIT_AUTHOR_NAME_KEY, "git author name");
  158. mockSystemReader.setProperty(Constants.GIT_AUTHOR_EMAIL_KEY, "author@email");
  159. localConfig.uncache(UserConfig.KEY);
  160. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  161. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  162. assertEquals("git author name", authorName);
  163. assertEquals("author@email", authorEmail);
  164. assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
  165. assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
  166. // the values are defined in the global configuration
  167. userGitConfig.setString("user", null, "name", "global username");
  168. userGitConfig.setString("user", null, "email", "author@globalemail");
  169. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  170. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  171. assertEquals("global username", authorName);
  172. assertEquals("author@globalemail", authorEmail);
  173. assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
  174. assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
  175. // the values are defined in the local configuration
  176. localConfig.setString("user", null, "name", "local username");
  177. localConfig.setString("user", null, "email", "author@localemail");
  178. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  179. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  180. assertEquals("local username", authorName);
  181. assertEquals("author@localemail", authorEmail);
  182. assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
  183. assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
  184. authorName = localConfig.get(UserConfig.KEY).getCommitterName();
  185. authorEmail = localConfig.get(UserConfig.KEY).getCommitterEmail();
  186. assertEquals("local username", authorName);
  187. assertEquals("author@localemail", authorEmail);
  188. assertFalse(localConfig.get(UserConfig.KEY).isCommitterNameImplicit());
  189. assertFalse(localConfig.get(UserConfig.KEY).isCommitterEmailImplicit());
  190. }
  191. @Test
  192. public void testReadBoolean_TrueFalse1() throws ConfigInvalidException {
  193. final Config c = parse("[s]\na = true\nb = false\n");
  194. assertEquals("true", c.getString("s", null, "a"));
  195. assertEquals("false", c.getString("s", null, "b"));
  196. assertTrue(c.getBoolean("s", "a", false));
  197. assertFalse(c.getBoolean("s", "b", true));
  198. }
  199. @Test
  200. public void testReadBoolean_TrueFalse2() throws ConfigInvalidException {
  201. final Config c = parse("[s]\na = TrUe\nb = fAlSe\n");
  202. assertEquals("TrUe", c.getString("s", null, "a"));
  203. assertEquals("fAlSe", c.getString("s", null, "b"));
  204. assertTrue(c.getBoolean("s", "a", false));
  205. assertFalse(c.getBoolean("s", "b", true));
  206. }
  207. @Test
  208. public void testReadBoolean_YesNo1() throws ConfigInvalidException {
  209. final Config c = parse("[s]\na = yes\nb = no\n");
  210. assertEquals("yes", c.getString("s", null, "a"));
  211. assertEquals("no", c.getString("s", null, "b"));
  212. assertTrue(c.getBoolean("s", "a", false));
  213. assertFalse(c.getBoolean("s", "b", true));
  214. }
  215. @Test
  216. public void testReadBoolean_YesNo2() throws ConfigInvalidException {
  217. final Config c = parse("[s]\na = yEs\nb = NO\n");
  218. assertEquals("yEs", c.getString("s", null, "a"));
  219. assertEquals("NO", c.getString("s", null, "b"));
  220. assertTrue(c.getBoolean("s", "a", false));
  221. assertFalse(c.getBoolean("s", "b", true));
  222. }
  223. @Test
  224. public void testReadBoolean_OnOff1() throws ConfigInvalidException {
  225. final Config c = parse("[s]\na = on\nb = off\n");
  226. assertEquals("on", c.getString("s", null, "a"));
  227. assertEquals("off", c.getString("s", null, "b"));
  228. assertTrue(c.getBoolean("s", "a", false));
  229. assertFalse(c.getBoolean("s", "b", true));
  230. }
  231. @Test
  232. public void testReadBoolean_OnOff2() throws ConfigInvalidException {
  233. final Config c = parse("[s]\na = ON\nb = OFF\n");
  234. assertEquals("ON", c.getString("s", null, "a"));
  235. assertEquals("OFF", c.getString("s", null, "b"));
  236. assertTrue(c.getBoolean("s", "a", false));
  237. assertFalse(c.getBoolean("s", "b", true));
  238. }
  239. static enum TestEnum {
  240. ONE_TWO;
  241. }
  242. @Test
  243. public void testGetEnum() throws ConfigInvalidException {
  244. Config c = parse("[s]\na = ON\nb = input\nc = true\nd = off\n");
  245. assertSame(CoreConfig.AutoCRLF.TRUE, c.getEnum("s", null, "a",
  246. CoreConfig.AutoCRLF.FALSE));
  247. assertSame(CoreConfig.AutoCRLF.INPUT, c.getEnum("s", null, "b",
  248. CoreConfig.AutoCRLF.FALSE));
  249. assertSame(CoreConfig.AutoCRLF.TRUE, c.getEnum("s", null, "c",
  250. CoreConfig.AutoCRLF.FALSE));
  251. assertSame(CoreConfig.AutoCRLF.FALSE, c.getEnum("s", null, "d",
  252. CoreConfig.AutoCRLF.TRUE));
  253. c = new Config();
  254. assertSame(CoreConfig.AutoCRLF.FALSE, c.getEnum("s", null, "d",
  255. CoreConfig.AutoCRLF.FALSE));
  256. c = parse("[s \"b\"]\n\tc = one two\n");
  257. assertSame(TestEnum.ONE_TWO, c.getEnum("s", "b", "c", TestEnum.ONE_TWO));
  258. c = parse("[s \"b\"]\n\tc = one-two\n");
  259. assertSame(TestEnum.ONE_TWO, c.getEnum("s", "b", "c", TestEnum.ONE_TWO));
  260. }
  261. @Test
  262. public void testGetInvalidEnum() throws ConfigInvalidException {
  263. Config c = parse("[a]\n\tb = invalid\n");
  264. try {
  265. c.getEnum("a", null, "b", TestEnum.ONE_TWO);
  266. fail();
  267. } catch (IllegalArgumentException e) {
  268. assertEquals("Invalid value: a.b=invalid", e.getMessage());
  269. }
  270. c = parse("[a \"b\"]\n\tc = invalid\n");
  271. try {
  272. c.getEnum("a", "b", "c", TestEnum.ONE_TWO);
  273. fail();
  274. } catch (IllegalArgumentException e) {
  275. assertEquals("Invalid value: a.b.c=invalid", e.getMessage());
  276. }
  277. }
  278. @Test
  279. public void testSetEnum() {
  280. final Config c = new Config();
  281. c.setEnum("s", "b", "c", TestEnum.ONE_TWO);
  282. assertEquals("[s \"b\"]\n\tc = one two\n", c.toText());
  283. }
  284. @Test
  285. public void testGetFastForwardMergeoptions() throws ConfigInvalidException {
  286. Config c = new Config(null); // not set
  287. assertSame(FastForwardMode.FF, c.getEnum(
  288. ConfigConstants.CONFIG_BRANCH_SECTION, "side",
  289. ConfigConstants.CONFIG_KEY_MERGEOPTIONS, FastForwardMode.FF));
  290. c = parse("[branch \"side\"]\n\tmergeoptions = --ff-only\n");
  291. assertSame(FastForwardMode.FF_ONLY, c.getEnum(
  292. ConfigConstants.CONFIG_BRANCH_SECTION, "side",
  293. ConfigConstants.CONFIG_KEY_MERGEOPTIONS,
  294. FastForwardMode.FF_ONLY));
  295. c = parse("[branch \"side\"]\n\tmergeoptions = --ff\n");
  296. assertSame(FastForwardMode.FF, c.getEnum(
  297. ConfigConstants.CONFIG_BRANCH_SECTION, "side",
  298. ConfigConstants.CONFIG_KEY_MERGEOPTIONS, FastForwardMode.FF));
  299. c = parse("[branch \"side\"]\n\tmergeoptions = --no-ff\n");
  300. assertSame(FastForwardMode.NO_FF, c.getEnum(
  301. ConfigConstants.CONFIG_BRANCH_SECTION, "side",
  302. ConfigConstants.CONFIG_KEY_MERGEOPTIONS, FastForwardMode.NO_FF));
  303. }
  304. @Test
  305. public void testSetFastForwardMergeoptions() {
  306. final Config c = new Config();
  307. c.setEnum("branch", "side", "mergeoptions", FastForwardMode.FF);
  308. assertEquals("[branch \"side\"]\n\tmergeoptions = --ff\n", c.toText());
  309. c.setEnum("branch", "side", "mergeoptions", FastForwardMode.FF_ONLY);
  310. assertEquals("[branch \"side\"]\n\tmergeoptions = --ff-only\n",
  311. c.toText());
  312. c.setEnum("branch", "side", "mergeoptions", FastForwardMode.NO_FF);
  313. assertEquals("[branch \"side\"]\n\tmergeoptions = --no-ff\n",
  314. c.toText());
  315. }
  316. @Test
  317. public void testGetFastForwardMerge() throws ConfigInvalidException {
  318. Config c = new Config(null); // not set
  319. assertSame(FastForwardMode.Merge.TRUE, c.getEnum(
  320. ConfigConstants.CONFIG_KEY_MERGE, null,
  321. ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.TRUE));
  322. c = parse("[merge]\n\tff = only\n");
  323. assertSame(FastForwardMode.Merge.ONLY, c.getEnum(
  324. ConfigConstants.CONFIG_KEY_MERGE, null,
  325. ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.ONLY));
  326. c = parse("[merge]\n\tff = true\n");
  327. assertSame(FastForwardMode.Merge.TRUE, c.getEnum(
  328. ConfigConstants.CONFIG_KEY_MERGE, null,
  329. ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.TRUE));
  330. c = parse("[merge]\n\tff = false\n");
  331. assertSame(FastForwardMode.Merge.FALSE, c.getEnum(
  332. ConfigConstants.CONFIG_KEY_MERGE, null,
  333. ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.FALSE));
  334. }
  335. @Test
  336. public void testSetFastForwardMerge() {
  337. final Config c = new Config();
  338. c.setEnum("merge", null, "ff",
  339. FastForwardMode.Merge.valueOf(FastForwardMode.FF));
  340. assertEquals("[merge]\n\tff = true\n", c.toText());
  341. c.setEnum("merge", null, "ff",
  342. FastForwardMode.Merge.valueOf(FastForwardMode.FF_ONLY));
  343. assertEquals("[merge]\n\tff = only\n", c.toText());
  344. c.setEnum("merge", null, "ff",
  345. FastForwardMode.Merge.valueOf(FastForwardMode.NO_FF));
  346. assertEquals("[merge]\n\tff = false\n", c.toText());
  347. }
  348. @Test
  349. public void testReadLong() throws ConfigInvalidException {
  350. assertReadLong(1L);
  351. assertReadLong(-1L);
  352. assertReadLong(Long.MIN_VALUE);
  353. assertReadLong(Long.MAX_VALUE);
  354. assertReadLong(4L * 1024 * 1024 * 1024, "4g");
  355. assertReadLong(3L * 1024 * 1024, "3 m");
  356. assertReadLong(8L * 1024, "8 k");
  357. try {
  358. assertReadLong(-1, "1.5g");
  359. fail("incorrectly accepted 1.5g");
  360. } catch (IllegalArgumentException e) {
  361. assertEquals("Invalid integer value: s.a=1.5g", e.getMessage());
  362. }
  363. }
  364. @Test
  365. public void testBooleanWithNoValue() throws ConfigInvalidException {
  366. Config c = parse("[my]\n\tempty\n");
  367. assertEquals("", c.getString("my", null, "empty"));
  368. assertEquals(1, c.getStringList("my", null, "empty").length);
  369. assertEquals("", c.getStringList("my", null, "empty")[0]);
  370. assertTrue(c.getBoolean("my", "empty", false));
  371. assertEquals("[my]\n\tempty\n", c.toText());
  372. }
  373. @Test
  374. public void testEmptyString() throws ConfigInvalidException {
  375. Config c = parse("[my]\n\tempty =\n");
  376. assertNull(c.getString("my", null, "empty"));
  377. String[] values = c.getStringList("my", null, "empty");
  378. assertNotNull(values);
  379. assertEquals(1, values.length);
  380. assertNull(values[0]);
  381. // always matches the default, because its non-boolean
  382. assertTrue(c.getBoolean("my", "empty", true));
  383. assertFalse(c.getBoolean("my", "empty", false));
  384. assertEquals("[my]\n\tempty =\n", c.toText());
  385. c = new Config();
  386. c.setStringList("my", null, "empty", Arrays.asList(values));
  387. assertEquals("[my]\n\tempty =\n", c.toText());
  388. }
  389. @Test
  390. public void testUnsetBranchSection() throws ConfigInvalidException {
  391. Config c = parse("" //
  392. + "[branch \"keep\"]\n"
  393. + " merge = master.branch.to.keep.in.the.file\n"
  394. + "\n"
  395. + "[branch \"remove\"]\n"
  396. + " merge = this.will.get.deleted\n"
  397. + " remote = origin-for-some-long-gone-place\n"
  398. + "\n"
  399. + "[core-section-not-to-remove-in-test]\n"
  400. + " packedGitLimit = 14\n");
  401. c.unsetSection("branch", "does.not.exist");
  402. c.unsetSection("branch", "remove");
  403. assertEquals("" //
  404. + "[branch \"keep\"]\n"
  405. + " merge = master.branch.to.keep.in.the.file\n"
  406. + "\n"
  407. + "[core-section-not-to-remove-in-test]\n"
  408. + " packedGitLimit = 14\n", c.toText());
  409. }
  410. @Test
  411. public void testUnsetSingleSection() throws ConfigInvalidException {
  412. Config c = parse("" //
  413. + "[branch \"keep\"]\n"
  414. + " merge = master.branch.to.keep.in.the.file\n"
  415. + "\n"
  416. + "[single]\n"
  417. + " merge = this.will.get.deleted\n"
  418. + " remote = origin-for-some-long-gone-place\n"
  419. + "\n"
  420. + "[core-section-not-to-remove-in-test]\n"
  421. + " packedGitLimit = 14\n");
  422. c.unsetSection("single", null);
  423. assertEquals("" //
  424. + "[branch \"keep\"]\n"
  425. + " merge = master.branch.to.keep.in.the.file\n"
  426. + "\n"
  427. + "[core-section-not-to-remove-in-test]\n"
  428. + " packedGitLimit = 14\n", c.toText());
  429. }
  430. @Test
  431. public void test008_readSectionNames() throws ConfigInvalidException {
  432. final Config c = parse("[a]\n [B]\n");
  433. Set<String> sections = c.getSections();
  434. assertTrue("Sections should contain \"a\"", sections.contains("a"));
  435. assertTrue("Sections should contain \"b\"", sections.contains("b"));
  436. }
  437. @Test
  438. public void test009_readNamesInSection() throws ConfigInvalidException {
  439. String configString = "[core]\n" + "repositoryFormatVersion = 0\n"
  440. + "filemode = false\n" + "logAllRefUpdates = true\n";
  441. final Config c = parse(configString);
  442. Set<String> names = c.getNames("core");
  443. assertEquals("Core section size", 3, names.size());
  444. assertTrue("Core section should contain \"filemode\"", names
  445. .contains("filemode"));
  446. assertTrue("Core section should contain \"repositoryFormatVersion\"",
  447. names.contains("repositoryFormatVersion"));
  448. assertTrue("Core section should contain \"repositoryformatversion\"",
  449. names.contains("repositoryformatversion"));
  450. Iterator<String> itr = names.iterator();
  451. assertEquals("filemode", itr.next());
  452. assertEquals("logAllRefUpdates", itr.next());
  453. assertEquals("repositoryFormatVersion", itr.next());
  454. assertFalse(itr.hasNext());
  455. }
  456. @Test
  457. public void test_ReadNamesInSectionRecursive()
  458. throws ConfigInvalidException {
  459. String baseConfigString = "[core]\n" + "logAllRefUpdates = true\n";
  460. String configString = "[core]\n" + "repositoryFormatVersion = 0\n"
  461. + "filemode = false\n";
  462. final Config c = parse(configString, parse(baseConfigString));
  463. Set<String> names = c.getNames("core", true);
  464. assertEquals("Core section size", 3, names.size());
  465. assertTrue("Core section should contain \"filemode\"",
  466. names.contains("filemode"));
  467. assertTrue("Core section should contain \"repositoryFormatVersion\"",
  468. names.contains("repositoryFormatVersion"));
  469. assertTrue("Core section should contain \"logAllRefUpdates\"",
  470. names.contains("logAllRefUpdates"));
  471. assertTrue("Core section should contain \"logallrefupdates\"",
  472. names.contains("logallrefupdates"));
  473. Iterator<String> itr = names.iterator();
  474. assertEquals("filemode", itr.next());
  475. assertEquals("repositoryFormatVersion", itr.next());
  476. assertEquals("logAllRefUpdates", itr.next());
  477. assertFalse(itr.hasNext());
  478. }
  479. @Test
  480. public void test010_readNamesInSubSection() throws ConfigInvalidException {
  481. String configString = "[a \"sub1\"]\n"//
  482. + "x = 0\n" //
  483. + "y = false\n"//
  484. + "z = true\n"//
  485. + "[a \"sub2\"]\n"//
  486. + "a=0\n"//
  487. + "b=1\n";
  488. final Config c = parse(configString);
  489. Set<String> names = c.getNames("a", "sub1");
  490. assertEquals("Subsection size", 3, names.size());
  491. assertTrue("Subsection should contain \"x\"", names.contains("x"));
  492. assertTrue("Subsection should contain \"y\"", names.contains("y"));
  493. assertTrue("Subsection should contain \"z\"", names.contains("z"));
  494. names = c.getNames("a", "sub2");
  495. assertEquals("Subsection size", 2, names.size());
  496. assertTrue("Subsection should contain \"a\"", names.contains("a"));
  497. assertTrue("Subsection should contain \"b\"", names.contains("b"));
  498. }
  499. @Test
  500. public void readNamesInSubSectionRecursive() throws ConfigInvalidException {
  501. String baseConfigString = "[a \"sub1\"]\n"//
  502. + "x = 0\n" //
  503. + "y = false\n"//
  504. + "[a \"sub2\"]\n"//
  505. + "A=0\n";//
  506. String configString = "[a \"sub1\"]\n"//
  507. + "z = true\n"//
  508. + "[a \"sub2\"]\n"//
  509. + "B=1\n";
  510. final Config c = parse(configString, parse(baseConfigString));
  511. Set<String> names = c.getNames("a", "sub1", true);
  512. assertEquals("Subsection size", 3, names.size());
  513. assertTrue("Subsection should contain \"x\"", names.contains("x"));
  514. assertTrue("Subsection should contain \"y\"", names.contains("y"));
  515. assertTrue("Subsection should contain \"z\"", names.contains("z"));
  516. names = c.getNames("a", "sub2", true);
  517. assertEquals("Subsection size", 2, names.size());
  518. assertTrue("Subsection should contain \"A\"", names.contains("A"));
  519. assertTrue("Subsection should contain \"a\"", names.contains("a"));
  520. assertTrue("Subsection should contain \"B\"", names.contains("B"));
  521. }
  522. @Test
  523. public void testQuotingForSubSectionNames() {
  524. String resultPattern = "[testsection \"{0}\"]\n\ttestname = testvalue\n";
  525. String result;
  526. Config config = new Config();
  527. config.setString("testsection", "testsubsection", "testname",
  528. "testvalue");
  529. result = MessageFormat.format(resultPattern, "testsubsection");
  530. assertEquals(result, config.toText());
  531. config.clear();
  532. config.setString("testsection", "#quotable", "testname", "testvalue");
  533. result = MessageFormat.format(resultPattern, "#quotable");
  534. assertEquals(result, config.toText());
  535. config.clear();
  536. config.setString("testsection", "with\"quote", "testname", "testvalue");
  537. result = MessageFormat.format(resultPattern, "with\\\"quote");
  538. assertEquals(result, config.toText());
  539. }
  540. @Test
  541. public void testNoFinalNewline() throws ConfigInvalidException {
  542. Config c = parse("[a]\n"
  543. + "x = 0\n"
  544. + "y = 1");
  545. assertEquals("0", c.getString("a", null, "x"));
  546. assertEquals("1", c.getString("a", null, "y"));
  547. }
  548. private static void assertReadLong(long exp) throws ConfigInvalidException {
  549. assertReadLong(exp, String.valueOf(exp));
  550. }
  551. private static void assertReadLong(long exp, String act)
  552. throws ConfigInvalidException {
  553. final Config c = parse("[s]\na = " + act + "\n");
  554. assertEquals(exp, c.getLong("s", null, "a", 0L));
  555. }
  556. private static Config parse(final String content)
  557. throws ConfigInvalidException {
  558. return parse(content, null);
  559. }
  560. private static Config parse(final String content, Config baseConfig)
  561. throws ConfigInvalidException {
  562. final Config c = new Config(baseConfig);
  563. c.fromText(content);
  564. return c;
  565. }
  566. }