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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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.assertEquals;
  50. import static org.junit.Assert.assertFalse;
  51. import static org.junit.Assert.assertNotNull;
  52. import static org.junit.Assert.assertNull;
  53. import static org.junit.Assert.assertSame;
  54. import static org.junit.Assert.assertTrue;
  55. import static org.junit.Assert.fail;
  56. import java.text.MessageFormat;
  57. import java.util.Arrays;
  58. import java.util.Iterator;
  59. import java.util.LinkedList;
  60. import java.util.Set;
  61. import org.eclipse.jgit.errors.ConfigInvalidException;
  62. import org.eclipse.jgit.junit.MockSystemReader;
  63. import org.eclipse.jgit.util.FS;
  64. import org.eclipse.jgit.util.SystemReader;
  65. import org.junit.Test;
  66. /**
  67. * Test reading of git config
  68. */
  69. public class ConfigTest {
  70. @Test
  71. public void test001_ReadBareKey() throws ConfigInvalidException {
  72. final Config c = parse("[foo]\nbar\n");
  73. assertEquals(true, c.getBoolean("foo", null, "bar", false));
  74. assertEquals("", c.getString("foo", null, "bar"));
  75. }
  76. @Test
  77. public void test002_ReadWithSubsection() throws ConfigInvalidException {
  78. final Config c = parse("[foo \"zip\"]\nbar\n[foo \"zap\"]\nbar=false\nn=3\n");
  79. assertEquals(true, c.getBoolean("foo", "zip", "bar", false));
  80. assertEquals("", c.getString("foo","zip", "bar"));
  81. assertEquals(false, c.getBoolean("foo", "zap", "bar", true));
  82. assertEquals("false", c.getString("foo", "zap", "bar"));
  83. assertEquals(3, c.getInt("foo", "zap", "n", 4));
  84. assertEquals(4, c.getInt("foo", "zap","m", 4));
  85. }
  86. @Test
  87. public void test003_PutRemote() {
  88. final Config c = new Config();
  89. c.setString("sec", "ext", "name", "value");
  90. c.setString("sec", "ext", "name2", "value2");
  91. final String expText = "[sec \"ext\"]\n\tname = value\n\tname2 = value2\n";
  92. assertEquals(expText, c.toText());
  93. }
  94. @Test
  95. public void test004_PutGetSimple() {
  96. Config c = new Config();
  97. c.setString("my", null, "somename", "false");
  98. assertEquals("false", c.getString("my", null, "somename"));
  99. assertEquals("[my]\n\tsomename = false\n", c.toText());
  100. }
  101. @Test
  102. public void test005_PutGetStringList() {
  103. Config c = new Config();
  104. final LinkedList<String> values = new LinkedList<String>();
  105. values.add("value1");
  106. values.add("value2");
  107. c.setStringList("my", null, "somename", values);
  108. final Object[] expArr = values.toArray();
  109. final String[] actArr = c.getStringList("my", null, "somename");
  110. assertTrue(Arrays.equals(expArr, actArr));
  111. final String expText = "[my]\n\tsomename = value1\n\tsomename = value2\n";
  112. assertEquals(expText, c.toText());
  113. }
  114. @Test
  115. public void test006_readCaseInsensitive() throws ConfigInvalidException {
  116. final Config c = parse("[Foo]\nBar\n");
  117. assertEquals(true, c.getBoolean("foo", null, "bar", false));
  118. assertEquals("", c.getString("foo", null, "bar"));
  119. }
  120. @Test
  121. public void test007_readUserConfig() {
  122. final MockSystemReader mockSystemReader = new MockSystemReader();
  123. SystemReader.setInstance(mockSystemReader);
  124. final String hostname = mockSystemReader.getHostname();
  125. final Config userGitConfig = mockSystemReader.openUserConfig(null,
  126. FS.DETECTED);
  127. final Config localConfig = new Config(userGitConfig);
  128. mockSystemReader.clearProperties();
  129. String authorName;
  130. String authorEmail;
  131. // no values defined nowhere
  132. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  133. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  134. assertEquals(Constants.UNKNOWN_USER_DEFAULT, authorName);
  135. assertEquals(Constants.UNKNOWN_USER_DEFAULT + "@" + hostname, authorEmail);
  136. assertTrue(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
  137. assertTrue(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
  138. // the system user name is defined
  139. mockSystemReader.setProperty(Constants.OS_USER_NAME_KEY, "os user name");
  140. localConfig.uncache(UserConfig.KEY);
  141. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  142. assertEquals("os user name", authorName);
  143. assertTrue(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
  144. if (hostname != null && hostname.length() != 0) {
  145. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  146. assertEquals("os user name@" + hostname, authorEmail);
  147. }
  148. assertTrue(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
  149. // the git environment variables are defined
  150. mockSystemReader.setProperty(Constants.GIT_AUTHOR_NAME_KEY, "git author name");
  151. mockSystemReader.setProperty(Constants.GIT_AUTHOR_EMAIL_KEY, "author@email");
  152. localConfig.uncache(UserConfig.KEY);
  153. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  154. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  155. assertEquals("git author name", authorName);
  156. assertEquals("author@email", authorEmail);
  157. assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
  158. assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
  159. // the values are defined in the global configuration
  160. userGitConfig.setString("user", null, "name", "global username");
  161. userGitConfig.setString("user", null, "email", "author@globalemail");
  162. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  163. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  164. assertEquals("global username", authorName);
  165. assertEquals("author@globalemail", authorEmail);
  166. assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
  167. assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
  168. // the values are defined in the local configuration
  169. localConfig.setString("user", null, "name", "local username");
  170. localConfig.setString("user", null, "email", "author@localemail");
  171. authorName = localConfig.get(UserConfig.KEY).getAuthorName();
  172. authorEmail = localConfig.get(UserConfig.KEY).getAuthorEmail();
  173. assertEquals("local username", authorName);
  174. assertEquals("author@localemail", authorEmail);
  175. assertFalse(localConfig.get(UserConfig.KEY).isAuthorNameImplicit());
  176. assertFalse(localConfig.get(UserConfig.KEY).isAuthorEmailImplicit());
  177. authorName = localConfig.get(UserConfig.KEY).getCommitterName();
  178. authorEmail = localConfig.get(UserConfig.KEY).getCommitterEmail();
  179. assertEquals("local username", authorName);
  180. assertEquals("author@localemail", authorEmail);
  181. assertFalse(localConfig.get(UserConfig.KEY).isCommitterNameImplicit());
  182. assertFalse(localConfig.get(UserConfig.KEY).isCommitterEmailImplicit());
  183. }
  184. @Test
  185. public void testReadBoolean_TrueFalse1() throws ConfigInvalidException {
  186. final Config c = parse("[s]\na = true\nb = false\n");
  187. assertEquals("true", c.getString("s", null, "a"));
  188. assertEquals("false", c.getString("s", null, "b"));
  189. assertTrue(c.getBoolean("s", "a", false));
  190. assertFalse(c.getBoolean("s", "b", true));
  191. }
  192. @Test
  193. public void testReadBoolean_TrueFalse2() 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_YesNo1() throws ConfigInvalidException {
  202. final Config c = parse("[s]\na = yes\nb = no\n");
  203. assertEquals("yes", c.getString("s", null, "a"));
  204. assertEquals("no", 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_YesNo2() 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_OnOff1() throws ConfigInvalidException {
  218. final Config c = parse("[s]\na = on\nb = off\n");
  219. assertEquals("on", c.getString("s", null, "a"));
  220. assertEquals("off", 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_OnOff2() 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. static enum TestEnum {
  233. ONE_TWO;
  234. }
  235. @Test
  236. public void testGetEnum() throws ConfigInvalidException {
  237. Config c = parse("[s]\na = ON\nb = input\nc = true\nd = off\n");
  238. assertSame(CoreConfig.AutoCRLF.TRUE, c.getEnum("s", null, "a",
  239. CoreConfig.AutoCRLF.FALSE));
  240. assertSame(CoreConfig.AutoCRLF.INPUT, c.getEnum("s", null, "b",
  241. CoreConfig.AutoCRLF.FALSE));
  242. assertSame(CoreConfig.AutoCRLF.TRUE, c.getEnum("s", null, "c",
  243. CoreConfig.AutoCRLF.FALSE));
  244. assertSame(CoreConfig.AutoCRLF.FALSE, c.getEnum("s", null, "d",
  245. CoreConfig.AutoCRLF.TRUE));
  246. c = new Config();
  247. assertSame(CoreConfig.AutoCRLF.FALSE, c.getEnum("s", null, "d",
  248. CoreConfig.AutoCRLF.FALSE));
  249. c = parse("[s \"b\"]\n\tc = one two\n");
  250. assertSame(TestEnum.ONE_TWO, c.getEnum("s", "b", "c", TestEnum.ONE_TWO));
  251. }
  252. @Test
  253. public void testSetEnum() {
  254. final Config c = new Config();
  255. c.setEnum("s", "b", "c", TestEnum.ONE_TWO);
  256. assertEquals("[s \"b\"]\n\tc = one two\n", c.toText());
  257. }
  258. @Test
  259. public void testReadLong() throws ConfigInvalidException {
  260. assertReadLong(1L);
  261. assertReadLong(-1L);
  262. assertReadLong(Long.MIN_VALUE);
  263. assertReadLong(Long.MAX_VALUE);
  264. assertReadLong(4L * 1024 * 1024 * 1024, "4g");
  265. assertReadLong(3L * 1024 * 1024, "3 m");
  266. assertReadLong(8L * 1024, "8 k");
  267. try {
  268. assertReadLong(-1, "1.5g");
  269. fail("incorrectly accepted 1.5g");
  270. } catch (IllegalArgumentException e) {
  271. assertEquals("Invalid integer value: s.a=1.5g", e.getMessage());
  272. }
  273. }
  274. @Test
  275. public void testBooleanWithNoValue() throws ConfigInvalidException {
  276. Config c = parse("[my]\n\tempty\n");
  277. assertEquals("", c.getString("my", null, "empty"));
  278. assertEquals(1, c.getStringList("my", null, "empty").length);
  279. assertEquals("", c.getStringList("my", null, "empty")[0]);
  280. assertTrue(c.getBoolean("my", "empty", false));
  281. assertEquals("[my]\n\tempty\n", c.toText());
  282. }
  283. @Test
  284. public void testEmptyString() throws ConfigInvalidException {
  285. Config c = parse("[my]\n\tempty =\n");
  286. assertNull(c.getString("my", null, "empty"));
  287. String[] values = c.getStringList("my", null, "empty");
  288. assertNotNull(values);
  289. assertEquals(1, values.length);
  290. assertNull(values[0]);
  291. // always matches the default, because its non-boolean
  292. assertTrue(c.getBoolean("my", "empty", true));
  293. assertFalse(c.getBoolean("my", "empty", false));
  294. assertEquals("[my]\n\tempty =\n", c.toText());
  295. c = new Config();
  296. c.setStringList("my", null, "empty", Arrays.asList(values));
  297. assertEquals("[my]\n\tempty =\n", c.toText());
  298. }
  299. @Test
  300. public void testUnsetBranchSection() throws ConfigInvalidException {
  301. Config c = parse("" //
  302. + "[branch \"keep\"]\n"
  303. + " merge = master.branch.to.keep.in.the.file\n"
  304. + "\n"
  305. + "[branch \"remove\"]\n"
  306. + " merge = this.will.get.deleted\n"
  307. + " remote = origin-for-some-long-gone-place\n"
  308. + "\n"
  309. + "[core-section-not-to-remove-in-test]\n"
  310. + " packedGitLimit = 14\n");
  311. c.unsetSection("branch", "does.not.exist");
  312. c.unsetSection("branch", "remove");
  313. assertEquals("" //
  314. + "[branch \"keep\"]\n"
  315. + " merge = master.branch.to.keep.in.the.file\n"
  316. + "\n"
  317. + "[core-section-not-to-remove-in-test]\n"
  318. + " packedGitLimit = 14\n", c.toText());
  319. }
  320. @Test
  321. public void testUnsetSingleSection() throws ConfigInvalidException {
  322. Config c = parse("" //
  323. + "[branch \"keep\"]\n"
  324. + " merge = master.branch.to.keep.in.the.file\n"
  325. + "\n"
  326. + "[single]\n"
  327. + " merge = this.will.get.deleted\n"
  328. + " remote = origin-for-some-long-gone-place\n"
  329. + "\n"
  330. + "[core-section-not-to-remove-in-test]\n"
  331. + " packedGitLimit = 14\n");
  332. c.unsetSection("single", null);
  333. assertEquals("" //
  334. + "[branch \"keep\"]\n"
  335. + " merge = master.branch.to.keep.in.the.file\n"
  336. + "\n"
  337. + "[core-section-not-to-remove-in-test]\n"
  338. + " packedGitLimit = 14\n", c.toText());
  339. }
  340. @Test
  341. public void test008_readSectionNames() throws ConfigInvalidException {
  342. final Config c = parse("[a]\n [B]\n");
  343. Set<String> sections = c.getSections();
  344. assertTrue("Sections should contain \"a\"", sections.contains("a"));
  345. assertTrue("Sections should contain \"b\"", sections.contains("b"));
  346. }
  347. @Test
  348. public void test009_readNamesInSection() throws ConfigInvalidException {
  349. String configString = "[core]\n" + "repositoryFormatVersion = 0\n"
  350. + "filemode = false\n" + "logAllRefUpdates = true\n";
  351. final Config c = parse(configString);
  352. Set<String> names = c.getNames("core");
  353. assertEquals("Core section size", 3, names.size());
  354. assertTrue("Core section should contain \"filemode\"", names
  355. .contains("filemode"));
  356. assertTrue("Core section should contain \"repositoryFormatVersion\"",
  357. names.contains("repositoryFormatVersion"));
  358. assertTrue("Core section should contain \"repositoryformatversion\"",
  359. names.contains("repositoryformatversion"));
  360. Iterator<String> itr = names.iterator();
  361. assertEquals("filemode", itr.next());
  362. assertEquals("logAllRefUpdates", itr.next());
  363. assertEquals("repositoryFormatVersion", itr.next());
  364. assertFalse(itr.hasNext());
  365. }
  366. @Test
  367. public void test010_readNamesInSubSection() throws ConfigInvalidException {
  368. String configString = "[a \"sub1\"]\n"//
  369. + "x = 0\n" //
  370. + "y = false\n"//
  371. + "z = true\n"//
  372. + "[a \"sub2\"]\n"//
  373. + "a=0\n"//
  374. + "b=1\n";
  375. final Config c = parse(configString);
  376. Set<String> names = c.getNames("a", "sub1");
  377. assertEquals("Subsection size", 3, names.size());
  378. assertTrue("Subsection should contain \"x\"", names.contains("x"));
  379. assertTrue("Subsection should contain \"y\"", names.contains("y"));
  380. assertTrue("Subsection should contain \"z\"", names.contains("z"));
  381. names = c.getNames("a", "sub2");
  382. assertEquals("Subsection size", 2, names.size());
  383. assertTrue("Subsection should contain \"a\"", names.contains("a"));
  384. assertTrue("Subsection should contain \"b\"", names.contains("b"));
  385. }
  386. @Test
  387. public void testQuotingForSubSectionNames() {
  388. String resultPattern = "[testsection \"{0}\"]\n\ttestname = testvalue\n";
  389. String result;
  390. Config config = new Config();
  391. config.setString("testsection", "testsubsection", "testname",
  392. "testvalue");
  393. result = MessageFormat.format(resultPattern, "testsubsection");
  394. assertEquals(result, config.toText());
  395. config.clear();
  396. config.setString("testsection", "#quotable", "testname", "testvalue");
  397. result = MessageFormat.format(resultPattern, "#quotable");
  398. assertEquals(result, config.toText());
  399. config.clear();
  400. config.setString("testsection", "with\"quote", "testname", "testvalue");
  401. result = MessageFormat.format(resultPattern, "with\\\"quote");
  402. assertEquals(result, config.toText());
  403. }
  404. private void assertReadLong(long exp) throws ConfigInvalidException {
  405. assertReadLong(exp, String.valueOf(exp));
  406. }
  407. private void assertReadLong(long exp, String act)
  408. throws ConfigInvalidException {
  409. final Config c = parse("[s]\na = " + act + "\n");
  410. assertEquals(exp, c.getLong("s", null, "a", 0L));
  411. }
  412. private Config parse(final String content) throws ConfigInvalidException {
  413. final Config c = new Config(null);
  414. c.fromText(content);
  415. return c;
  416. }
  417. }