View Javadoc
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  
45  import static java.nio.charset.StandardCharsets.UTF_8;
46  import static org.eclipse.jgit.util.FileUtils.pathToString;
47  import static org.junit.Assert.assertArrayEquals;
48  import static org.junit.Assert.assertEquals;
49  import static org.junit.Assert.assertTrue;
50  
51  import java.io.ByteArrayOutputStream;
52  import java.io.File;
53  import java.io.FileOutputStream;
54  import java.io.IOException;
55  import java.util.StringTokenizer;
56  
57  import org.eclipse.jgit.errors.ConfigInvalidException;
58  import org.eclipse.jgit.util.FS;
59  import org.eclipse.jgit.util.FileUtils;
60  import org.eclipse.jgit.util.IO;
61  import org.junit.After;
62  import org.junit.Before;
63  import org.junit.Test;
64  
65  public class FileBasedConfigTest {
66  
67  	private static final String USER = "user";
68  
69  	private static final String NAME = "name";
70  
71  	private static final String EMAIL = "email";
72  
73  	private static final String ALICE = "Alice";
74  
75  	private static final String BOB = "Bob";
76  
77  	private static final String ALICE_EMAIL = "alice@home";
78  
79  	private static final String CONTENT1 = "[" + USER + "]\n\t" + NAME + " = "
80  			+ ALICE + "\n";
81  
82  	private static final String CONTENT2 = "[" + USER + "]\n\t" + NAME + " = "
83  			+ BOB + "\n";
84  
85  	private static final String CONTENT3 = "[" + USER + "]\n\t" + NAME + " = "
86  			+ ALICE + "\n" + "[" + USER + "]\n\t" + EMAIL + " = " + ALICE_EMAIL;
87  
88  	private File trash;
89  
90  	@Before
91  	public void setUp() throws Exception {
92  		trash = File.createTempFile("tmp_", "");
93  		trash.delete();
94  		assertTrue("mkdir " + trash, trash.mkdir());
95  	}
96  
97  	@After
98  	public void tearDown() throws Exception {
99  		FileUtils.delete(trash, FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
100 	}
101 
102 	@Test
103 	public void testSystemEncoding() throws IOException, ConfigInvalidException {
104 		final File file = createFile(CONTENT1.getBytes(UTF_8));
105 		final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
106 		config.load();
107 		assertEquals(ALICE, config.getString(USER, null, NAME));
108 
109 		config.setString(USER, null, NAME, BOB);
110 		config.save();
111 		assertArrayEquals(CONTENT2.getBytes(UTF_8), IO.readFully(file));
112 	}
113 
114 	@Test
115 	public void testUTF8withoutBOM() throws IOException, ConfigInvalidException {
116 		final File file = createFile(CONTENT1.getBytes(UTF_8));
117 		final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
118 		config.load();
119 		assertEquals(ALICE, config.getString(USER, null, NAME));
120 
121 		config.setString(USER, null, NAME, BOB);
122 		config.save();
123 		assertArrayEquals(CONTENT2.getBytes(UTF_8), IO.readFully(file));
124 	}
125 
126 	@Test
127 	public void testUTF8withBOM() throws IOException, ConfigInvalidException {
128 		final ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
129 		bos1.write(0xEF);
130 		bos1.write(0xBB);
131 		bos1.write(0xBF);
132 		bos1.write(CONTENT1.getBytes(UTF_8));
133 
134 		final File file = createFile(bos1.toByteArray());
135 		final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
136 		config.load();
137 		assertEquals(ALICE, config.getString(USER, null, NAME));
138 
139 		config.setString(USER, null, NAME, BOB);
140 		config.save();
141 
142 		final ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
143 		bos2.write(0xEF);
144 		bos2.write(0xBB);
145 		bos2.write(0xBF);
146 		bos2.write(CONTENT2.getBytes(UTF_8));
147 		assertArrayEquals(bos2.toByteArray(), IO.readFully(file));
148 	}
149 
150 	@Test
151 	public void testLeadingWhitespaces() throws IOException, ConfigInvalidException {
152 		final ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
153 		bos1.write(" \n\t".getBytes(UTF_8));
154 		bos1.write(CONTENT1.getBytes(UTF_8));
155 
156 		final File file = createFile(bos1.toByteArray());
157 		final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
158 		config.load();
159 		assertEquals(ALICE, config.getString(USER, null, NAME));
160 
161 		config.setString(USER, null, NAME, BOB);
162 		config.save();
163 
164 		final ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
165 		bos2.write(" \n\t".getBytes(UTF_8));
166 		bos2.write(CONTENT2.getBytes(UTF_8));
167 		assertArrayEquals(bos2.toByteArray(), IO.readFully(file));
168 	}
169 
170 	@Test
171 	public void testIncludeAbsolute()
172 			throws IOException, ConfigInvalidException {
173 		final File includedFile = createFile(CONTENT1.getBytes(UTF_8));
174 		final ByteArrayOutputStream bos = new ByteArrayOutputStream();
175 		bos.write("[include]\npath=".getBytes(UTF_8));
176 		bos.write(pathToString(includedFile).getBytes(UTF_8));
177 
178 		final File file = createFile(bos.toByteArray());
179 		final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
180 		config.load();
181 		assertEquals(ALICE, config.getString(USER, null, NAME));
182 	}
183 
184 	@Test
185 	public void testIncludeRelativeDot()
186 			throws IOException, ConfigInvalidException {
187 		final File includedFile = createFile(CONTENT1.getBytes(UTF_8), "dir1");
188 		final ByteArrayOutputStream bos = new ByteArrayOutputStream();
189 		bos.write("[include]\npath=".getBytes(UTF_8));
190 		bos.write(("./" + includedFile.getName()).getBytes(UTF_8));
191 
192 		final File file = createFile(bos.toByteArray(), "dir1");
193 		final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
194 		config.load();
195 		assertEquals(ALICE, config.getString(USER, null, NAME));
196 	}
197 
198 	@Test
199 	public void testIncludeRelativeDotDot()
200 			throws IOException, ConfigInvalidException {
201 		final File includedFile = createFile(CONTENT1.getBytes(UTF_8), "dir1");
202 		final ByteArrayOutputStream bos = new ByteArrayOutputStream();
203 		bos.write("[include]\npath=".getBytes(UTF_8));
204 		bos.write(("../" + includedFile.getParentFile().getName() + "/"
205 				+ includedFile.getName()).getBytes(UTF_8));
206 
207 		final File file = createFile(bos.toByteArray(), "dir2");
208 		final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
209 		config.load();
210 		assertEquals(ALICE, config.getString(USER, null, NAME));
211 	}
212 
213 	@Test
214 	public void testIncludeRelativeDotDotNotFound()
215 			throws IOException, ConfigInvalidException {
216 		final File includedFile = createFile(CONTENT1.getBytes(UTF_8));
217 		final ByteArrayOutputStream bos = new ByteArrayOutputStream();
218 		bos.write("[include]\npath=".getBytes(UTF_8));
219 		bos.write(("../" + includedFile.getName()).getBytes(UTF_8));
220 
221 		final File file = createFile(bos.toByteArray());
222 		final FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
223 		config.load();
224 		assertEquals(null, config.getString(USER, null, NAME));
225 	}
226 
227 	@Test
228 	public void testIncludeWithTilde()
229 			throws IOException, ConfigInvalidException {
230 		final File includedFile = createFile(CONTENT1.getBytes(UTF_8), "home");
231 		final ByteArrayOutputStream bos = new ByteArrayOutputStream();
232 		bos.write("[include]\npath=".getBytes(UTF_8));
233 		bos.write(("~/" + includedFile.getName()).getBytes(UTF_8));
234 
235 		final File file = createFile(bos.toByteArray(), "repo");
236 		final FS fs = FS.DETECTED.newInstance();
237 		fs.setUserHome(includedFile.getParentFile());
238 
239 		final FileBasedConfig config = new FileBasedConfig(file, fs);
240 		config.load();
241 		assertEquals(ALICE, config.getString(USER, null, NAME));
242 	}
243 
244 	@Test
245 	public void testIncludeDontInlineIncludedLinesOnSave()
246 			throws IOException, ConfigInvalidException {
247 		// use a content with multiple sections and multiple key/value pairs
248 		// because code for first line works different than for subsequent lines
249 		final File includedFile = createFile(CONTENT3.getBytes(UTF_8), "dir1");
250 
251 		final File file = createFile(new byte[0], "dir2");
252 		FileBasedConfig config = new FileBasedConfig(file, FS.DETECTED);
253 		config.setString("include", null, "path",
254 				("../" + includedFile.getParentFile().getName() + "/"
255 						+ includedFile.getName()));
256 
257 		// just by setting the include.path, it won't be included
258 		assertEquals(null, config.getString(USER, null, NAME));
259 		assertEquals(null, config.getString(USER, null, EMAIL));
260 		config.save();
261 
262 		// and it won't be included after saving
263 		assertEquals(null, config.getString(USER, null, NAME));
264 		assertEquals(null, config.getString(USER, null, EMAIL));
265 
266 		final String expectedText = config.toText();
267 		assertEquals(2,
268 				new StringTokenizer(expectedText, "\n", false).countTokens());
269 
270 		config = new FileBasedConfig(file, FS.DETECTED);
271 		config.load();
272 
273 		String actualText = config.toText();
274 		assertEquals(expectedText, actualText);
275 		// but it will be included after (re)loading
276 		assertEquals(ALICE, config.getString(USER, null, NAME));
277 		assertEquals(ALICE_EMAIL, config.getString(USER, null, EMAIL));
278 
279 		config.save();
280 
281 		actualText = config.toText();
282 		assertEquals(expectedText, actualText);
283 		// and of course preserved after saving
284 		assertEquals(ALICE, config.getString(USER, null, NAME));
285 		assertEquals(ALICE_EMAIL, config.getString(USER, null, EMAIL));
286 	}
287 
288 	private File createFile(byte[] content) throws IOException {
289 		return createFile(content, null);
290 	}
291 
292 	private File createFile(byte[] content, String subdir) throws IOException {
293 		File dir = subdir != null ? new File(trash, subdir) : trash;
294 		dir.mkdirs();
295 
296 		File f = File.createTempFile(getClass().getName(), null, dir);
297 		try (FileOutputStream os = new FileOutputStream(f, true)) {
298 			os.write(content);
299 		}
300 		return f;
301 	}
302 }