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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 static org.junit.Assert.assertTrue;
  49. import java.io.ByteArrayOutputStream;
  50. import java.io.File;
  51. import java.io.FileOutputStream;
  52. import java.io.IOException;
  53. import org.eclipse.jgit.errors.ConfigInvalidException;
  54. import org.eclipse.jgit.util.FS;
  55. import org.eclipse.jgit.util.FileUtils;
  56. import org.eclipse.jgit.util.IO;
  57. import org.junit.After;
  58. import org.junit.Before;
  59. import org.junit.Test;
  60. public class FileBasedConfigTest {
  61. private static final String USER = "user";
  62. private static final String NAME = "name";
  63. private static final String ALICE = "Alice";
  64. private static final String BOB = "Bob";
  65. private static final String CONTENT1 = "[" + USER + "]\n\t" + NAME + " = "
  66. + ALICE + "\n";
  67. private static final String CONTENT2 = "[" + USER + "]\n\t" + NAME + " = "
  68. + BOB + "\n";
  69. private File trash;
  70. @Before
  71. public void setUp() throws Exception {
  72. trash = File.createTempFile("tmp_", "");
  73. trash.delete();
  74. assertTrue("mkdir " + trash, trash.mkdir());
  75. }
  76. @After
  77. public void tearDown() throws Exception {
  78. FileUtils.delete(trash, FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
  79. }
  80. @Test
  81. public void testSystemEncoding() throws IOException, ConfigInvalidException {
  82. final File file = createFile(CONTENT1.getBytes());
  83. final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
  84. config.load();
  85. assertEquals(ALICE, config.getString(USER, null, NAME));
  86. config.setString(USER, null, NAME, BOB);
  87. config.save();
  88. assertArrayEquals(CONTENT2.getBytes(), IO.readFully(file));
  89. }
  90. @Test
  91. public void testUTF8withoutBOM() throws IOException, ConfigInvalidException {
  92. final File file = createFile(CONTENT1.getBytes(UTF_8));
  93. final FileBasedConfig config = new FileBasedConfig(file, 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(), IO.readFully(file));
  99. }
  100. @Test
  101. public void testUTF8withBOM() throws IOException, ConfigInvalidException {
  102. final ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
  103. bos1.write(0xEF);
  104. bos1.write(0xBB);
  105. bos1.write(0xBF);
  106. bos1.write(CONTENT1.getBytes(UTF_8));
  107. final File file = createFile(bos1.toByteArray());
  108. final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
  109. config.load();
  110. assertEquals(ALICE, config.getString(USER, null, NAME));
  111. config.setString(USER, null, NAME, BOB);
  112. config.save();
  113. final ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
  114. bos2.write(0xEF);
  115. bos2.write(0xBB);
  116. bos2.write(0xBF);
  117. bos2.write(CONTENT2.getBytes(UTF_8));
  118. assertArrayEquals(bos2.toByteArray(), IO.readFully(file));
  119. }
  120. @Test
  121. public void testLeadingWhitespaces() throws IOException, ConfigInvalidException {
  122. final ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
  123. bos1.write(" \n\t".getBytes());
  124. bos1.write(CONTENT1.getBytes());
  125. final File file = createFile(bos1.toByteArray());
  126. final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
  127. config.load();
  128. assertEquals(ALICE, config.getString(USER, null, NAME));
  129. config.setString(USER, null, NAME, BOB);
  130. config.save();
  131. final ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
  132. bos2.write(" \n\t".getBytes());
  133. bos2.write(CONTENT2.getBytes());
  134. assertArrayEquals(bos2.toByteArray(), IO.readFully(file));
  135. }
  136. @Test
  137. public void testIncludeAbsolute()
  138. throws IOException, ConfigInvalidException {
  139. final File includedFile = createFile(CONTENT1.getBytes());
  140. final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  141. bos.write("[include]\npath=".getBytes());
  142. bos.write(pathToString(includedFile).getBytes());
  143. final File file = createFile(bos.toByteArray());
  144. final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
  145. config.load();
  146. assertEquals(ALICE, config.getString(USER, null, NAME));
  147. }
  148. @Test
  149. public void testIncludeRelativeDot()
  150. throws IOException, ConfigInvalidException {
  151. final File includedFile = createFile(CONTENT1.getBytes(), "dir1");
  152. final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  153. bos.write("[include]\npath=".getBytes());
  154. bos.write(("./" + includedFile.getName()).getBytes());
  155. final File file = createFile(bos.toByteArray(), "dir1");
  156. final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
  157. config.load();
  158. assertEquals(ALICE, config.getString(USER, null, NAME));
  159. }
  160. @Test
  161. public void testIncludeRelativeDotDot()
  162. throws IOException, ConfigInvalidException {
  163. final File includedFile = createFile(CONTENT1.getBytes(), "dir1");
  164. final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  165. bos.write("[include]\npath=".getBytes());
  166. bos.write(("../" + includedFile.getParentFile().getName() + "/"
  167. + includedFile.getName()).getBytes());
  168. final File file = createFile(bos.toByteArray(), "dir2");
  169. final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
  170. config.load();
  171. assertEquals(ALICE, config.getString(USER, null, NAME));
  172. }
  173. @Test
  174. public void testIncludeRelativeDotDotNotFound()
  175. throws IOException, ConfigInvalidException {
  176. final File includedFile = createFile(CONTENT1.getBytes());
  177. final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  178. bos.write("[include]\npath=".getBytes());
  179. bos.write(("../" + includedFile.getName()).getBytes());
  180. final File file = createFile(bos.toByteArray());
  181. final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
  182. config.load();
  183. assertEquals(null, config.getString(USER, null, NAME));
  184. }
  185. @Test
  186. public void testIncludeWithTilde()
  187. throws IOException, ConfigInvalidException {
  188. final File includedFile = createFile(CONTENT1.getBytes(), "home");
  189. final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  190. bos.write("[include]\npath=".getBytes());
  191. bos.write(("~/" + includedFile.getName()).getBytes());
  192. final File file = createFile(bos.toByteArray(), "repo");
  193. final FS fs = FS.DETECTED.newInstance();
  194. fs.setUserHome(includedFile.getParentFile());
  195. final FileBasedConfig config = new FileBasedConfig(file, fs);
  196. config.load();
  197. assertEquals(ALICE, config.getString(USER, null, NAME));
  198. }
  199. private File createFile(byte[] content) throws IOException {
  200. return createFile(content, null);
  201. }
  202. private File createFile(byte[] content, String subdir) throws IOException {
  203. File dir = subdir != null ? new File(trash, subdir) : trash;
  204. dir.mkdirs();
  205. File f = File.createTempFile(getClass().getName(), null, dir);
  206. try (FileOutputStream os = new FileOutputStream(f, true)) {
  207. os.write(content);
  208. }
  209. return f;
  210. }
  211. }