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.

FileBasedConfigTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (C) 2012, Marc Strapetz
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.storage.file;
  44. import static java.nio.charset.StandardCharsets.UTF_8;
  45. import static org.eclipse.jgit.util.FileUtils.pathToString;
  46. import static org.junit.Assert.assertArrayEquals;
  47. import static org.junit.Assert.assertEquals;
  48. import java.io.ByteArrayOutputStream;
  49. import java.io.IOException;
  50. import java.io.OutputStream;
  51. import java.nio.file.Files;
  52. import java.nio.file.Path;
  53. import java.nio.file.StandardOpenOption;
  54. import java.util.StringTokenizer;
  55. import org.eclipse.jgit.errors.ConfigInvalidException;
  56. import org.eclipse.jgit.junit.MockSystemReader;
  57. import org.eclipse.jgit.util.FS;
  58. import org.eclipse.jgit.util.FileUtils;
  59. import org.eclipse.jgit.util.IO;
  60. import org.eclipse.jgit.util.SystemReader;
  61. import org.junit.After;
  62. import org.junit.Before;
  63. import org.junit.Test;
  64. public class FileBasedConfigTest {
  65. private static final String USER = "user";
  66. private static final String NAME = "name";
  67. private static final String EMAIL = "email";
  68. private static final String ALICE = "Alice";
  69. private static final String BOB = "Bob";
  70. private static final String ALICE_EMAIL = "alice@home";
  71. private static final String CONTENT1 = "[" + USER + "]\n\t" + NAME + " = "
  72. + ALICE + "\n";
  73. private static final String CONTENT2 = "[" + USER + "]\n\t" + NAME + " = "
  74. + BOB + "\n";
  75. private static final String CONTENT3 = "[" + USER + "]\n\t" + NAME + " = "
  76. + ALICE + "\n" + "[" + USER + "]\n\t" + EMAIL + " = " + ALICE_EMAIL;
  77. private Path trash;
  78. @Before
  79. public void setUp() throws Exception {
  80. SystemReader.setInstance(new MockSystemReader());
  81. trash = Files.createTempDirectory("tmp_");
  82. FS.getFileStoreAttributes(trash.getParent());
  83. }
  84. @After
  85. public void tearDown() throws Exception {
  86. FileUtils.delete(trash.toFile(),
  87. FileUtils.RECURSIVE | FileUtils.SKIP_MISSING | FileUtils.RETRY);
  88. }
  89. @Test
  90. public void testSystemEncoding() throws IOException, ConfigInvalidException {
  91. final Path file = createFile(CONTENT1.getBytes(UTF_8));
  92. final FileBasedConfig config = new FileBasedConfig(file.toFile(),
  93. FS.DETECTED);
  94. config.load();
  95. assertEquals(ALICE, config.getString(USER, null, NAME));
  96. config.setString(USER, null, NAME, BOB);
  97. config.save();
  98. assertArrayEquals(CONTENT2.getBytes(UTF_8), IO.readFully(file.toFile()));
  99. }
  100. @Test
  101. public void testUTF8withoutBOM() throws IOException, ConfigInvalidException {
  102. final Path file = createFile(CONTENT1.getBytes(UTF_8));
  103. final FileBasedConfig config = new FileBasedConfig(file.toFile(),
  104. FS.DETECTED);
  105. config.load();
  106. assertEquals(ALICE, config.getString(USER, null, NAME));
  107. config.setString(USER, null, NAME, BOB);
  108. config.save();
  109. assertArrayEquals(CONTENT2.getBytes(UTF_8), IO.readFully(file.toFile()));
  110. }
  111. @Test
  112. public void testUTF8withBOM() throws IOException, ConfigInvalidException {
  113. final ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
  114. bos1.write(0xEF);
  115. bos1.write(0xBB);
  116. bos1.write(0xBF);
  117. bos1.write(CONTENT1.getBytes(UTF_8));
  118. final Path file = createFile(bos1.toByteArray());
  119. final FileBasedConfig config = new FileBasedConfig(file.toFile(),
  120. FS.DETECTED);
  121. config.load();
  122. assertEquals(ALICE, config.getString(USER, null, NAME));
  123. config.setString(USER, null, NAME, BOB);
  124. config.save();
  125. final ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
  126. bos2.write(0xEF);
  127. bos2.write(0xBB);
  128. bos2.write(0xBF);
  129. bos2.write(CONTENT2.getBytes(UTF_8));
  130. assertArrayEquals(bos2.toByteArray(), IO.readFully(file.toFile()));
  131. }
  132. @Test
  133. public void testLeadingWhitespaces() throws IOException, ConfigInvalidException {
  134. final ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
  135. bos1.write(" \n\t".getBytes(UTF_8));
  136. bos1.write(CONTENT1.getBytes(UTF_8));
  137. final Path file = createFile(bos1.toByteArray());
  138. final FileBasedConfig config = new FileBasedConfig(file.toFile(),
  139. FS.DETECTED);
  140. config.load();
  141. assertEquals(ALICE, config.getString(USER, null, NAME));
  142. config.setString(USER, null, NAME, BOB);
  143. config.save();
  144. final ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
  145. bos2.write(" \n\t".getBytes(UTF_8));
  146. bos2.write(CONTENT2.getBytes(UTF_8));
  147. assertArrayEquals(bos2.toByteArray(), IO.readFully(file.toFile()));
  148. }
  149. @Test
  150. public void testIncludeAbsolute()
  151. throws IOException, ConfigInvalidException {
  152. final Path includedFile = createFile(CONTENT1.getBytes(UTF_8));
  153. final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  154. bos.write("[include]\npath=".getBytes(UTF_8));
  155. bos.write(pathToString(includedFile.toFile()).getBytes(UTF_8));
  156. final Path file = createFile(bos.toByteArray());
  157. final FileBasedConfig config = new FileBasedConfig(file.toFile(),
  158. FS.DETECTED);
  159. config.load();
  160. assertEquals(ALICE, config.getString(USER, null, NAME));
  161. }
  162. @Test
  163. public void testIncludeRelativeDot()
  164. throws IOException, ConfigInvalidException {
  165. final Path includedFile = createFile(CONTENT1.getBytes(UTF_8), "dir1");
  166. final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  167. bos.write("[include]\npath=".getBytes(UTF_8));
  168. bos.write(("./" + includedFile.getFileName()).getBytes(UTF_8));
  169. final Path file = createFile(bos.toByteArray(), "dir1");
  170. final FileBasedConfig config = new FileBasedConfig(file.toFile(),
  171. FS.DETECTED);
  172. config.load();
  173. assertEquals(ALICE, config.getString(USER, null, NAME));
  174. }
  175. @Test
  176. public void testIncludeRelativeDotDot()
  177. throws IOException, ConfigInvalidException {
  178. final Path includedFile = createFile(CONTENT1.getBytes(UTF_8), "dir1");
  179. final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  180. bos.write("[include]\npath=".getBytes(UTF_8));
  181. bos.write(("../" + includedFile.getParent().getFileName() + "/"
  182. + includedFile.getFileName()).getBytes(UTF_8));
  183. final Path file = createFile(bos.toByteArray(), "dir2");
  184. final FileBasedConfig config = new FileBasedConfig(file.toFile(),
  185. FS.DETECTED);
  186. config.load();
  187. assertEquals(ALICE, config.getString(USER, null, NAME));
  188. }
  189. @Test
  190. public void testIncludeRelativeDotDotNotFound()
  191. throws IOException, ConfigInvalidException {
  192. final Path includedFile = createFile(CONTENT1.getBytes(UTF_8));
  193. final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  194. bos.write("[include]\npath=".getBytes(UTF_8));
  195. bos.write(("../" + includedFile.getFileName()).getBytes(UTF_8));
  196. final Path file = createFile(bos.toByteArray());
  197. final FileBasedConfig config = new FileBasedConfig(file.toFile(),
  198. FS.DETECTED);
  199. config.load();
  200. assertEquals(null, config.getString(USER, null, NAME));
  201. }
  202. @Test
  203. public void testIncludeWithTilde()
  204. throws IOException, ConfigInvalidException {
  205. final Path includedFile = createFile(CONTENT1.getBytes(UTF_8), "home");
  206. final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  207. bos.write("[include]\npath=".getBytes(UTF_8));
  208. bos.write(("~/" + includedFile.getFileName()).getBytes(UTF_8));
  209. final Path file = createFile(bos.toByteArray(), "repo");
  210. final FS fs = FS.DETECTED.newInstance();
  211. fs.setUserHome(includedFile.getParent().toFile());
  212. final FileBasedConfig config = new FileBasedConfig(file.toFile(), fs);
  213. config.load();
  214. assertEquals(ALICE, config.getString(USER, null, NAME));
  215. }
  216. @Test
  217. public void testIncludeDontInlineIncludedLinesOnSave()
  218. throws IOException, ConfigInvalidException {
  219. // use a content with multiple sections and multiple key/value pairs
  220. // because code for first line works different than for subsequent lines
  221. final Path includedFile = createFile(CONTENT3.getBytes(UTF_8), "dir1");
  222. final Path file = createFile(new byte[0], "dir2");
  223. FileBasedConfig config = new FileBasedConfig(file.toFile(),
  224. FS.DETECTED);
  225. config.setString("include", null, "path",
  226. ("../" + includedFile.getParent().getFileName() + "/"
  227. + includedFile.getFileName()));
  228. // just by setting the include.path, it won't be included
  229. assertEquals(null, config.getString(USER, null, NAME));
  230. assertEquals(null, config.getString(USER, null, EMAIL));
  231. config.save();
  232. // and it won't be included after saving
  233. assertEquals(null, config.getString(USER, null, NAME));
  234. assertEquals(null, config.getString(USER, null, EMAIL));
  235. final String expectedText = config.toText();
  236. assertEquals(2,
  237. new StringTokenizer(expectedText, "\n", false).countTokens());
  238. config = new FileBasedConfig(file.toFile(), FS.DETECTED);
  239. config.load();
  240. String actualText = config.toText();
  241. assertEquals(expectedText, actualText);
  242. // but it will be included after (re)loading
  243. assertEquals(ALICE, config.getString(USER, null, NAME));
  244. assertEquals(ALICE_EMAIL, config.getString(USER, null, EMAIL));
  245. config.save();
  246. actualText = config.toText();
  247. assertEquals(expectedText, actualText);
  248. // and of course preserved after saving
  249. assertEquals(ALICE, config.getString(USER, null, NAME));
  250. assertEquals(ALICE_EMAIL, config.getString(USER, null, EMAIL));
  251. }
  252. private Path createFile(byte[] content) throws IOException {
  253. return createFile(content, null);
  254. }
  255. private Path createFile(byte[] content, String subdir) throws IOException {
  256. Path dir = subdir != null ? trash.resolve(subdir) : trash;
  257. Files.createDirectories(dir);
  258. Path f = Files.createTempFile(dir, getClass().getName(), null);
  259. try (OutputStream os = Files.newOutputStream(f,
  260. StandardOpenOption.APPEND)) {
  261. os.write(content);
  262. }
  263. return f;
  264. }
  265. }