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 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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.merge.MergeConfig;
  66. import org.eclipse.jgit.util.FS;
  67. import org.eclipse.jgit.util.SystemReader;
  68. import org.junit.After;
  69. import org.junit.Test;
  70. /**
  71. * Test reading of git config
  72. */
  73. public class ConfigTest {
  74. @After
  75. public void tearDown() {
  76. SystemReader.setInstance(null);
  77. }
  78. @Test
  79. public void test001_ReadBareKey() throws ConfigInvalidException {
  80. final Config c = parse("[foo]\nbar\n");
  81. assertTrue(c.getBoolean("foo", null, "bar", false));
  82. assertEquals("", c.getString("foo", null, "bar"));
  83. }
  84. @Test
  85. public void test002_ReadWithSubsection() throws ConfigInvalidException {
  86. final Config c = parse("[foo \"zip\"]\nbar\n[foo \"zap\"]\nbar=false\nn=3\n");
  87. assertTrue(c.getBoolean("foo", "zip", "bar", false));
  88. assertEquals("", c.getString("foo","zip", "bar"));
  89. assertFalse(c.getBoolean("foo", "zap", "bar", true));
  90. assertEquals("false", c.getString("foo", "zap", "bar"));
  91. assertEquals(3, c.getInt("foo", "zap", "n", 4));
  92. assertEquals(4, c.getInt("foo", "zap","m", 4));
  93. }
  94. @Test
  95. public void test003_PutRemote() {
  96. final Config c = new Config();
  97. c.setString("sec", "ext", "name", "value");
  98. c.setString("sec", "ext", "name2", "value2");
  99. final String expText = "[sec \"ext\"]\n\tname = value\n\tname2 = value2\n";
  100. assertEquals(expText, c.toText());
  101. }
  102. @Test
  103. public void test004_PutGetSimple() {
  104. Config c = new Config();
  105. c.setString("my", null, "somename", "false");
  106. assertEquals("false", c.getString("my", null, "somename"));
  107. assertEquals("[my]\n\tsomename = false\n", c.toText());
  108. }
  109. @Test
  110. public void test005_PutGetStringList() {
  111. Config c = new Config();
  112. final LinkedList<String> values = new LinkedList<String>();
  113. values.add("value1");
  114. values.add("value2");
  115. c.setStringList("my", null, "somename", values);
  116. final Object[] expArr = values.toArray();
  117. final String[] actArr = c.getStringList("my", null, "somename");
  118. assertArrayEquals(expArr, actArr);
  119. final String expText = "[my]\n\tsomename = value1\n\tsomename = value2\n";
  120. assertEquals(expText, c.toText());
  121. }
  122. @Test
  123. public void test006_readCaseInsensitive() throws ConfigInvalidException {
  124. final Config c = parse("[Foo]\nBar\n");
  125. assertTrue(c.getBoolean("foo", null, "bar", false));
  126. assertEquals("", c.getString("foo", null, "bar"));
  127. }
  128. @Test
  129. public void test007_readUserConfig() {
  130. final MockSystemReader mockSystemReader = new MockSystemReader();
  131. SystemReader.setInstance(mockSystemReader);
  132. final String hostname = mockSystemReader.getHostname();
  133. final Config userGitConfig = mockSystemReader.openUserConfig(null,
  134. FS.DETECTED);
  135. final Config localConfig = new Config(userGitConfig);
  136. mockSystemReader.clearProperties();
  137. String authorName;
  138. String authorEmail;
  139. // no values defined nowhere
  140. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  141. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  142. assertEquals(Constants.UNKNOWN_USER_DEFAULT, authorName);
  143. assertEquals(Constants.UNKNOWN_USER_DEFAULT + "@" + hostname, authorEmail);
  144. assertTrue(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
  145. assertTrue(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
  146. // the system user name is defined
  147. mockSystemReader.setProperty(Constants.OS_USER_NAME_KEY, "os user name");
  148. localConfig.uncache(UserConfig.KEY);
  149. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  150. assertEquals("os user name", authorName);
  151. assertTrue(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
  152. if (hostname != null && hostname.length() != 0) {
  153. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  154. assertEquals("os user name@" + hostname, authorEmail);
  155. }
  156. assertTrue(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
  157. // the git environment variables are defined
  158. mockSystemReader.setProperty(Constants.GIT_AUTHOR_NAME_KEY, "git author name");
  159. mockSystemReader.setProperty(Constants.GIT_AUTHOR_EMAIL_KEY, "author@email");
  160. localConfig.uncache(UserConfig.KEY);
  161. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  162. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  163. assertEquals("git author name", authorName);
  164. assertEquals("author@email", authorEmail);
  165. assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
  166. assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
  167. // the values are defined in the global configuration
  168. userGitConfig.setString("user", null, "name", "global username");
  169. userGitConfig.setString("user", null, "email", "author@globalemail");
  170. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  171. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  172. assertEquals("global username", authorName);
  173. assertEquals("author@globalemail", authorEmail);
  174. assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
  175. assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
  176. // the values are defined in the local configuration
  177. localConfig.setString("user", null, "name", "local username");
  178. localConfig.setString("user", null, "email", "author@localemail");
  179. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  180. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  181. assertEquals("local username", authorName);
  182. assertEquals("author@localemail", authorEmail);
  183. assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
  184. assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
  185. authorName = localConfig.get(UserConfig.KEY).getCommitterName();
  186. authorEmail = localConfig.get(UserConfig.KEY).getCommitterEmail();
  187. assertEquals("local username", authorName);
  188. assertEquals("author@localemail", authorEmail);
  189. assertFalse(localConfig.get(UserConfig.KEY).isCommitterNameImplicit());
  190. assertFalse(localConfig.get(UserConfig.KEY).isCommitterEmailImplicit());
  191. }
  192. @Test
  193. public void testReadBoolean_TrueFalse1() throws ConfigInvalidException {
  194. final Config c = parse("[s]\na = true\nb = false\n");
  195. assertEquals("true", c.getString("s", null, "a"));
  196. assertEquals("false", c.getString("s", null, "b"));
  197. assertTrue(c.getBoolean("s", "a", false));
  198. assertFalse(c.getBoolean("s", "b", true));
  199. }
  200. @Test
  201. public void testReadBoolean_TrueFalse2() throws ConfigInvalidException {
  202. final Config c = parse("[s]\na = TrUe\nb = fAlSe\n");
  203. assertEquals("TrUe", c.getString("s", null, "a"));
  204. assertEquals("fAlSe", c.getString("s", null, "b"));
  205. assertTrue(c.getBoolean("s", "a", false));
  206. assertFalse(c.getBoolean("s", "b", true));
  207. }
  208. @Test
  209. public void testReadBoolean_YesNo1() throws ConfigInvalidException {
  210. final Config c = parse("[s]\na = yes\nb = no\n");
  211. assertEquals("yes", c.getString("s", null, "a"));
  212. assertEquals("no", c.getString("s", null, "b"));
  213. assertTrue(c.getBoolean("s", "a", false));
  214. assertFalse(c.getBoolean("s", "b", true));
  215. }
  216. @Test
  217. public void testReadBoolean_YesNo2() throws ConfigInvalidException {
  218. final Config c = parse("[s]\na = yEs\nb = NO\n");
  219. assertEquals("yEs", c.getString("s", null, "a"));
  220. assertEquals("NO", c.getString("s", null, "b"));
  221. assertTrue(c.getBoolean("s", "a", false));
  222. assertFalse(c.getBoolean("s", "b", true));
  223. }
  224. @Test
  225. public void testReadBoolean_OnOff1() throws ConfigInvalidException {
  226. final Config c = parse("[s]\na = on\nb = off\n");
  227. assertEquals("on", c.getString("s", null, "a"));
  228. assertEquals("off", c.getString("s", null, "b"));
  229. assertTrue(c.getBoolean("s", "a", false));
  230. assertFalse(c.getBoolean("s", "b", true));
  231. }
  232. @Test
  233. public void testReadBoolean_OnOff2() throws ConfigInvalidException {
  234. final Config c = parse("[s]\na = ON\nb = OFF\n");
  235. assertEquals("ON", c.getString("s", null, "a"));
  236. assertEquals("OFF", c.getString("s", null, "b"));
  237. assertTrue(c.getBoolean("s", "a", false));
  238. assertFalse(c.getBoolean("s", "b", true));
  239. }
  240. static enum TestEnum {
  241. ONE_TWO;
  242. }
  243. @Test
  244. public void testGetEnum() throws ConfigInvalidException {
  245. Config c = parse("[s]\na = ON\nb = input\nc = true\nd = off\n");
  246. assertSame(CoreConfig.AutoCRLF.TRUE, c.getEnum("s", null, "a",
  247. CoreConfig.AutoCRLF.FALSE));
  248. assertSame(CoreConfig.AutoCRLF.INPUT, c.getEnum("s", null, "b",
  249. CoreConfig.AutoCRLF.FALSE));
  250. assertSame(CoreConfig.AutoCRLF.TRUE, c.getEnum("s", null, "c",
  251. CoreConfig.AutoCRLF.FALSE));
  252. assertSame(CoreConfig.AutoCRLF.FALSE, c.getEnum("s", null, "d",
  253. CoreConfig.AutoCRLF.TRUE));
  254. c = new Config();
  255. assertSame(CoreConfig.AutoCRLF.FALSE, c.getEnum("s", null, "d",
  256. CoreConfig.AutoCRLF.FALSE));
  257. c = parse("[s \"b\"]\n\tc = one two\n");
  258. assertSame(TestEnum.ONE_TWO, c.getEnum("s", "b", "c", TestEnum.ONE_TWO));
  259. c = parse("[s \"b\"]\n\tc = one-two\n");
  260. assertSame(TestEnum.ONE_TWO, c.getEnum("s", "b", "c", TestEnum.ONE_TWO));
  261. }
  262. @Test
  263. public void testGetInvalidEnum() throws ConfigInvalidException {
  264. Config c = parse("[a]\n\tb = invalid\n");
  265. try {
  266. c.getEnum("a", null, "b", TestEnum.ONE_TWO);
  267. fail();
  268. } catch (IllegalArgumentException e) {
  269. assertEquals("Invalid value: a.b=invalid", e.getMessage());
  270. }
  271. c = parse("[a \"b\"]\n\tc = invalid\n");
  272. try {
  273. c.getEnum("a", "b", "c", TestEnum.ONE_TWO);
  274. fail();
  275. } catch (IllegalArgumentException e) {
  276. assertEquals("Invalid value: a.b.c=invalid", e.getMessage());
  277. }
  278. }
  279. @Test
  280. public void testSetEnum() {
  281. final Config c = new Config();
  282. c.setEnum("s", "b", "c", TestEnum.ONE_TWO);
  283. assertEquals("[s \"b\"]\n\tc = one two\n", c.toText());
  284. }
  285. @Test
  286. public void testGetFastForwardMergeoptions() throws ConfigInvalidException {
  287. Config c = new Config(null); // not set
  288. assertSame(FastForwardMode.FF, c.getEnum(
  289. ConfigConstants.CONFIG_BRANCH_SECTION, "side",
  290. ConfigConstants.CONFIG_KEY_MERGEOPTIONS, FastForwardMode.FF));
  291. MergeConfig mergeConfig = c.get(MergeConfig.getParser("side"));
  292. assertSame(FastForwardMode.FF, mergeConfig.getFastForwardMode());
  293. c = parse("[branch \"side\"]\n\tmergeoptions = --ff-only\n");
  294. assertSame(FastForwardMode.FF_ONLY, c.getEnum(
  295. ConfigConstants.CONFIG_BRANCH_SECTION, "side",
  296. ConfigConstants.CONFIG_KEY_MERGEOPTIONS,
  297. FastForwardMode.FF_ONLY));
  298. mergeConfig = c.get(MergeConfig.getParser("side"));
  299. assertSame(FastForwardMode.FF_ONLY, mergeConfig.getFastForwardMode());
  300. c = parse("[branch \"side\"]\n\tmergeoptions = --ff\n");
  301. assertSame(FastForwardMode.FF, c.getEnum(
  302. ConfigConstants.CONFIG_BRANCH_SECTION, "side",
  303. ConfigConstants.CONFIG_KEY_MERGEOPTIONS, FastForwardMode.FF));
  304. mergeConfig = c.get(MergeConfig.getParser("side"));
  305. assertSame(FastForwardMode.FF, mergeConfig.getFastForwardMode());
  306. c = parse("[branch \"side\"]\n\tmergeoptions = --no-ff\n");
  307. assertSame(FastForwardMode.NO_FF, c.getEnum(
  308. ConfigConstants.CONFIG_BRANCH_SECTION, "side",
  309. ConfigConstants.CONFIG_KEY_MERGEOPTIONS, FastForwardMode.NO_FF));
  310. mergeConfig = c.get(MergeConfig.getParser("side"));
  311. assertSame(FastForwardMode.NO_FF, mergeConfig.getFastForwardMode());
  312. }
  313. @Test
  314. public void testSetFastForwardMergeoptions() {
  315. final Config c = new Config();
  316. c.setEnum("branch", "side", "mergeoptions", FastForwardMode.FF);
  317. assertEquals("[branch \"side\"]\n\tmergeoptions = --ff\n", c.toText());
  318. c.setEnum("branch", "side", "mergeoptions", FastForwardMode.FF_ONLY);
  319. assertEquals("[branch \"side\"]\n\tmergeoptions = --ff-only\n",
  320. c.toText());
  321. c.setEnum("branch", "side", "mergeoptions", FastForwardMode.NO_FF);
  322. assertEquals("[branch \"side\"]\n\tmergeoptions = --no-ff\n",
  323. c.toText());
  324. }
  325. @Test
  326. public void testGetFastForwardMerge() throws ConfigInvalidException {
  327. Config c = new Config(null); // not set
  328. assertSame(FastForwardMode.Merge.TRUE, c.getEnum(
  329. ConfigConstants.CONFIG_KEY_MERGE, null,
  330. ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.TRUE));
  331. MergeConfig mergeConfig = c.get(MergeConfig.getParser("side"));
  332. assertSame(FastForwardMode.FF, mergeConfig.getFastForwardMode());
  333. c = parse("[merge]\n\tff = only\n");
  334. assertSame(FastForwardMode.Merge.ONLY, c.getEnum(
  335. ConfigConstants.CONFIG_KEY_MERGE, null,
  336. ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.ONLY));
  337. mergeConfig = c.get(MergeConfig.getParser("side"));
  338. assertSame(FastForwardMode.FF_ONLY, mergeConfig.getFastForwardMode());
  339. c = parse("[merge]\n\tff = true\n");
  340. assertSame(FastForwardMode.Merge.TRUE, c.getEnum(
  341. ConfigConstants.CONFIG_KEY_MERGE, null,
  342. ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.TRUE));
  343. mergeConfig = c.get(MergeConfig.getParser("side"));
  344. assertSame(FastForwardMode.FF, mergeConfig.getFastForwardMode());
  345. c = parse("[merge]\n\tff = false\n");
  346. assertSame(FastForwardMode.Merge.FALSE, c.getEnum(
  347. ConfigConstants.CONFIG_KEY_MERGE, null,
  348. ConfigConstants.CONFIG_KEY_FF, FastForwardMode.Merge.FALSE));
  349. mergeConfig = c.get(MergeConfig.getParser("side"));
  350. assertSame(FastForwardMode.NO_FF, mergeConfig.getFastForwardMode());
  351. }
  352. @Test
  353. public void testCombinedMergeOptions() throws ConfigInvalidException {
  354. Config c = new Config(null); // not set
  355. MergeConfig mergeConfig = c.get(MergeConfig.getParser("side"));
  356. assertSame(FastForwardMode.FF, mergeConfig.getFastForwardMode());
  357. assertTrue(mergeConfig.isCommit());
  358. assertFalse(mergeConfig.isSquash());
  359. // branch..mergeoptions should win over merge.ff
  360. c = parse("[merge]\n\tff = false\n"
  361. + "[branch \"side\"]\n\tmergeoptions = --ff-only\n");
  362. mergeConfig = c.get(MergeConfig.getParser("side"));
  363. assertSame(FastForwardMode.FF_ONLY, mergeConfig.getFastForwardMode());
  364. assertTrue(mergeConfig.isCommit());
  365. assertFalse(mergeConfig.isSquash());
  366. // merge.ff used for ff setting if not set via mergeoptions
  367. c = parse("[merge]\n\tff = only\n"
  368. + "[branch \"side\"]\n\tmergeoptions = --squash\n");
  369. mergeConfig = c.get(MergeConfig.getParser("side"));
  370. assertSame(FastForwardMode.FF_ONLY, mergeConfig.getFastForwardMode());
  371. assertTrue(mergeConfig.isCommit());
  372. assertTrue(mergeConfig.isSquash());
  373. // mergeoptions wins if it has ff options amongst other options
  374. c = parse("[merge]\n\tff = false\n"
  375. + "[branch \"side\"]\n\tmergeoptions = --ff-only --no-commit\n");
  376. mergeConfig = c.get(MergeConfig.getParser("side"));
  377. assertSame(FastForwardMode.FF_ONLY, mergeConfig.getFastForwardMode());
  378. assertFalse(mergeConfig.isCommit());
  379. assertFalse(mergeConfig.isSquash());
  380. }
  381. @Test
  382. public void testSetFastForwardMerge() {
  383. final Config c = new Config();
  384. c.setEnum("merge", null, "ff",
  385. FastForwardMode.Merge.valueOf(FastForwardMode.FF));
  386. assertEquals("[merge]\n\tff = true\n", c.toText());
  387. c.setEnum("merge", null, "ff",
  388. FastForwardMode.Merge.valueOf(FastForwardMode.FF_ONLY));
  389. assertEquals("[merge]\n\tff = only\n", c.toText());
  390. c.setEnum("merge", null, "ff",
  391. FastForwardMode.Merge.valueOf(FastForwardMode.NO_FF));
  392. assertEquals("[merge]\n\tff = false\n", c.toText());
  393. }
  394. @Test
  395. public void testReadLong() throws ConfigInvalidException {
  396. assertReadLong(1L);
  397. assertReadLong(-1L);
  398. assertReadLong(Long.MIN_VALUE);
  399. assertReadLong(Long.MAX_VALUE);
  400. assertReadLong(4L * 1024 * 1024 * 1024, "4g");
  401. assertReadLong(3L * 1024 * 1024, "3 m");
  402. assertReadLong(8L * 1024, "8 k");
  403. try {
  404. assertReadLong(-1, "1.5g");
  405. fail("incorrectly accepted 1.5g");
  406. } catch (IllegalArgumentException e) {
  407. assertEquals("Invalid integer value: s.a=1.5g", e.getMessage());
  408. }
  409. }
  410. @Test
  411. public void testBooleanWithNoValue() throws ConfigInvalidException {
  412. Config c = parse("[my]\n\tempty\n");
  413. assertEquals("", c.getString("my", null, "empty"));
  414. assertEquals(1, c.getStringList("my", null, "empty").length);
  415. assertEquals("", c.getStringList("my", null, "empty")[0]);
  416. assertTrue(c.getBoolean("my", "empty", false));
  417. assertEquals("[my]\n\tempty\n", c.toText());
  418. }
  419. @Test
  420. public void testEmptyString() throws ConfigInvalidException {
  421. Config c = parse("[my]\n\tempty =\n");
  422. assertNull(c.getString("my", null, "empty"));
  423. String[] values = c.getStringList("my", null, "empty");
  424. assertNotNull(values);
  425. assertEquals(1, values.length);
  426. assertNull(values[0]);
  427. // always matches the default, because its non-boolean
  428. assertTrue(c.getBoolean("my", "empty", true));
  429. assertFalse(c.getBoolean("my", "empty", false));
  430. assertEquals("[my]\n\tempty =\n", c.toText());
  431. c = new Config();
  432. c.setStringList("my", null, "empty", Arrays.asList(values));
  433. assertEquals("[my]\n\tempty =\n", c.toText());
  434. }
  435. @Test
  436. public void testUnsetBranchSection() throws ConfigInvalidException {
  437. Config c = parse("" //
  438. + "[branch \"keep\"]\n"
  439. + " merge = master.branch.to.keep.in.the.file\n"
  440. + "\n"
  441. + "[branch \"remove\"]\n"
  442. + " merge = this.will.get.deleted\n"
  443. + " remote = origin-for-some-long-gone-place\n"
  444. + "\n"
  445. + "[core-section-not-to-remove-in-test]\n"
  446. + " packedGitLimit = 14\n");
  447. c.unsetSection("branch", "does.not.exist");
  448. c.unsetSection("branch", "remove");
  449. assertEquals("" //
  450. + "[branch \"keep\"]\n"
  451. + " merge = master.branch.to.keep.in.the.file\n"
  452. + "\n"
  453. + "[core-section-not-to-remove-in-test]\n"
  454. + " packedGitLimit = 14\n", c.toText());
  455. }
  456. @Test
  457. public void testUnsetSingleSection() throws ConfigInvalidException {
  458. Config c = parse("" //
  459. + "[branch \"keep\"]\n"
  460. + " merge = master.branch.to.keep.in.the.file\n"
  461. + "\n"
  462. + "[single]\n"
  463. + " merge = this.will.get.deleted\n"
  464. + " remote = origin-for-some-long-gone-place\n"
  465. + "\n"
  466. + "[core-section-not-to-remove-in-test]\n"
  467. + " packedGitLimit = 14\n");
  468. c.unsetSection("single", null);
  469. assertEquals("" //
  470. + "[branch \"keep\"]\n"
  471. + " merge = master.branch.to.keep.in.the.file\n"
  472. + "\n"
  473. + "[core-section-not-to-remove-in-test]\n"
  474. + " packedGitLimit = 14\n", c.toText());
  475. }
  476. @Test
  477. public void test008_readSectionNames() throws ConfigInvalidException {
  478. final Config c = parse("[a]\n [B]\n");
  479. Set<String> sections = c.getSections();
  480. assertTrue("Sections should contain \"a\"", sections.contains("a"));
  481. assertTrue("Sections should contain \"b\"", sections.contains("b"));
  482. }
  483. @Test
  484. public void test009_readNamesInSection() throws ConfigInvalidException {
  485. String configString = "[core]\n" + "repositoryFormatVersion = 0\n"
  486. + "filemode = false\n" + "logAllRefUpdates = true\n";
  487. final Config c = parse(configString);
  488. Set<String> names = c.getNames("core");
  489. assertEquals("Core section size", 3, names.size());
  490. assertTrue("Core section should contain \"filemode\"", names
  491. .contains("filemode"));
  492. assertTrue("Core section should contain \"repositoryFormatVersion\"",
  493. names.contains("repositoryFormatVersion"));
  494. assertTrue("Core section should contain \"repositoryformatversion\"",
  495. names.contains("repositoryformatversion"));
  496. Iterator<String> itr = names.iterator();
  497. assertEquals("filemode", itr.next());
  498. assertEquals("logAllRefUpdates", itr.next());
  499. assertEquals("repositoryFormatVersion", itr.next());
  500. assertFalse(itr.hasNext());
  501. }
  502. @Test
  503. public void test_ReadNamesInSectionRecursive()
  504. throws ConfigInvalidException {
  505. String baseConfigString = "[core]\n" + "logAllRefUpdates = true\n";
  506. String configString = "[core]\n" + "repositoryFormatVersion = 0\n"
  507. + "filemode = false\n";
  508. final Config c = parse(configString, parse(baseConfigString));
  509. Set<String> names = c.getNames("core", true);
  510. assertEquals("Core section size", 3, names.size());
  511. assertTrue("Core section should contain \"filemode\"",
  512. names.contains("filemode"));
  513. assertTrue("Core section should contain \"repositoryFormatVersion\"",
  514. names.contains("repositoryFormatVersion"));
  515. assertTrue("Core section should contain \"logAllRefUpdates\"",
  516. names.contains("logAllRefUpdates"));
  517. assertTrue("Core section should contain \"logallrefupdates\"",
  518. names.contains("logallrefupdates"));
  519. Iterator<String> itr = names.iterator();
  520. assertEquals("filemode", itr.next());
  521. assertEquals("repositoryFormatVersion", itr.next());
  522. assertEquals("logAllRefUpdates", itr.next());
  523. assertFalse(itr.hasNext());
  524. }
  525. @Test
  526. public void test010_readNamesInSubSection() throws ConfigInvalidException {
  527. String configString = "[a \"sub1\"]\n"//
  528. + "x = 0\n" //
  529. + "y = false\n"//
  530. + "z = true\n"//
  531. + "[a \"sub2\"]\n"//
  532. + "a=0\n"//
  533. + "b=1\n";
  534. final Config c = parse(configString);
  535. Set<String> names = c.getNames("a", "sub1");
  536. assertEquals("Subsection size", 3, names.size());
  537. assertTrue("Subsection should contain \"x\"", names.contains("x"));
  538. assertTrue("Subsection should contain \"y\"", names.contains("y"));
  539. assertTrue("Subsection should contain \"z\"", names.contains("z"));
  540. names = c.getNames("a", "sub2");
  541. assertEquals("Subsection size", 2, names.size());
  542. assertTrue("Subsection should contain \"a\"", names.contains("a"));
  543. assertTrue("Subsection should contain \"b\"", names.contains("b"));
  544. }
  545. @Test
  546. public void readNamesInSubSectionRecursive() throws ConfigInvalidException {
  547. String baseConfigString = "[a \"sub1\"]\n"//
  548. + "x = 0\n" //
  549. + "y = false\n"//
  550. + "[a \"sub2\"]\n"//
  551. + "A=0\n";//
  552. String configString = "[a \"sub1\"]\n"//
  553. + "z = true\n"//
  554. + "[a \"sub2\"]\n"//
  555. + "B=1\n";
  556. final Config c = parse(configString, parse(baseConfigString));
  557. Set<String> names = c.getNames("a", "sub1", true);
  558. assertEquals("Subsection size", 3, names.size());
  559. assertTrue("Subsection should contain \"x\"", names.contains("x"));
  560. assertTrue("Subsection should contain \"y\"", names.contains("y"));
  561. assertTrue("Subsection should contain \"z\"", names.contains("z"));
  562. names = c.getNames("a", "sub2", true);
  563. assertEquals("Subsection size", 2, names.size());
  564. assertTrue("Subsection should contain \"A\"", names.contains("A"));
  565. assertTrue("Subsection should contain \"a\"", names.contains("a"));
  566. assertTrue("Subsection should contain \"B\"", names.contains("B"));
  567. }
  568. @Test
  569. public void testQuotingForSubSectionNames() {
  570. String resultPattern = "[testsection \"{0}\"]\n\ttestname = testvalue\n";
  571. String result;
  572. Config config = new Config();
  573. config.setString("testsection", "testsubsection", "testname",
  574. "testvalue");
  575. result = MessageFormat.format(resultPattern, "testsubsection");
  576. assertEquals(result, config.toText());
  577. config.clear();
  578. config.setString("testsection", "#quotable", "testname", "testvalue");
  579. result = MessageFormat.format(resultPattern, "#quotable");
  580. assertEquals(result, config.toText());
  581. config.clear();
  582. config.setString("testsection", "with\"quote", "testname", "testvalue");
  583. result = MessageFormat.format(resultPattern, "with\\\"quote");
  584. assertEquals(result, config.toText());
  585. }
  586. @Test
  587. public void testNoFinalNewline() throws ConfigInvalidException {
  588. Config c = parse("[a]\n"
  589. + "x = 0\n"
  590. + "y = 1");
  591. assertEquals("0", c.getString("a", null, "x"));
  592. assertEquals("1", c.getString("a", null, "y"));
  593. }
  594. private static void assertReadLong(long exp) throws ConfigInvalidException {
  595. assertReadLong(exp, String.valueOf(exp));
  596. }
  597. private static void assertReadLong(long exp, String act)
  598. throws ConfigInvalidException {
  599. final Config c = parse("[s]\na = " + act + "\n");
  600. assertEquals(exp, c.getLong("s", null, "a", 0L));
  601. }
  602. private static Config parse(final String content)
  603. throws ConfigInvalidException {
  604. return parse(content, null);
  605. }
  606. private static Config parse(final String content, Config baseConfig)
  607. throws ConfigInvalidException {
  608. final Config c = new Config(baseConfig);
  609. c.fromText(content);
  610. return c;
  611. }
  612. }