View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * Copyright (C) 2007-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 2006-2007, Shawn O. Pearce <spearce@spearce.org>
5    * Copyright (C) 2009, Yann Simon <yann.simon.fr@gmail.com> and others
6    *
7    * This program and the accompanying materials are made available under the
8    * terms of the Eclipse Distribution License v. 1.0 which is available at
9    * https://www.eclipse.org/org/documents/edl-v10.php.
10   *
11   * SPDX-License-Identifier: BSD-3-Clause
12   */
13  
14  package org.eclipse.jgit.junit;
15  
16  import static java.nio.charset.StandardCharsets.UTF_8;
17  import static org.junit.Assert.assertEquals;
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.FileNotFoundException;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.InputStreamReader;
25  import java.io.Reader;
26  import java.nio.file.Path;
27  import java.time.Instant;
28  import java.util.Map;
29  import java.util.concurrent.TimeUnit;
30  
31  import org.eclipse.jgit.api.Git;
32  import org.eclipse.jgit.api.errors.GitAPIException;
33  import org.eclipse.jgit.dircache.DirCacheBuilder;
34  import org.eclipse.jgit.dircache.DirCacheCheckout;
35  import org.eclipse.jgit.dircache.DirCacheEntry;
36  import org.eclipse.jgit.internal.storage.file.FileRepository;
37  import org.eclipse.jgit.lib.Constants;
38  import org.eclipse.jgit.lib.FileMode;
39  import org.eclipse.jgit.lib.ObjectId;
40  import org.eclipse.jgit.lib.ObjectInserter;
41  import org.eclipse.jgit.lib.RefUpdate;
42  import org.eclipse.jgit.lib.Repository;
43  import org.eclipse.jgit.revwalk.RevCommit;
44  import org.eclipse.jgit.revwalk.RevWalk;
45  import org.eclipse.jgit.treewalk.FileTreeIterator;
46  import org.eclipse.jgit.util.FS;
47  import org.eclipse.jgit.util.FileUtils;
48  import org.junit.Before;
49  
50  /**
51   * Base class for most JGit unit tests.
52   *
53   * Sets up a predefined test repository and has support for creating additional
54   * repositories and destroying them when the tests are finished.
55   */
56  public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase {
57  	/**
58  	 * Copy a file
59  	 *
60  	 * @param src
61  	 * @param dst
62  	 * @throws IOException
63  	 */
64  	protected static void copyFile(File src, File dst)
65  			throws IOException {
66  		try (FileInputStream fis = new FileInputStream(src);
67  				FileOutputStream fos = new FileOutputStream(dst)) {
68  			final byte[] buf = new byte[4096];
69  			int r;
70  			while ((r = fis.read(buf)) > 0) {
71  				fos.write(buf, 0, r);
72  			}
73  		}
74  	}
75  
76  	/**
77  	 * Write a trash file
78  	 *
79  	 * @param name
80  	 * @param data
81  	 * @return the trash file
82  	 * @throws IOException
83  	 */
84  	protected File writeTrashFile(String name, String data)
85  			throws IOException {
86  		return JGitTestUtil.writeTrashFile(db, name, data);
87  	}
88  
89  	/**
90  	 * Create a symbolic link
91  	 *
92  	 * @param link
93  	 *            the path of the symbolic link to create
94  	 * @param target
95  	 *            the target of the symbolic link
96  	 * @return the path to the symbolic link
97  	 * @throws Exception
98  	 * @since 4.2
99  	 */
100 	protected Path writeLink(String link, String target)
101 			throws Exception {
102 		return JGitTestUtil.writeLink(db, link, target);
103 	}
104 
105 	/**
106 	 * Write a trash file
107 	 *
108 	 * @param subdir
109 	 * @param name
110 	 * @param data
111 	 * @return the trash file
112 	 * @throws IOException
113 	 */
114 	protected File writeTrashFile(final String subdir, final String name,
115 			final String data)
116 			throws IOException {
117 		return JGitTestUtil.writeTrashFile(db, subdir, name, data);
118 	}
119 
120 	/**
121 	 * Read content of a file
122 	 *
123 	 * @param name
124 	 * @return the file's content
125 	 * @throws IOException
126 	 */
127 	protected String read(String name) throws IOException {
128 		return JGitTestUtil.read(db, name);
129 	}
130 
131 	/**
132 	 * Check if file exists
133 	 *
134 	 * @param name
135 	 *            file name
136 	 * @return if the file exists
137 	 */
138 	protected boolean check(String name) {
139 		return JGitTestUtil.check(db, name);
140 	}
141 
142 	/**
143 	 * Delete a trash file
144 	 *
145 	 * @param name
146 	 *            file name
147 	 * @throws IOException
148 	 */
149 	protected void deleteTrashFile(String name) throws IOException {
150 		JGitTestUtil.deleteTrashFile(db, name);
151 	}
152 
153 	/**
154 	 * Check content of a file.
155 	 *
156 	 * @param f
157 	 * @param checkData
158 	 *            expected content
159 	 * @throws IOException
160 	 */
161 	protected static void checkFile(File f, String checkData)
162 			throws IOException {
163 		try (Reader r = new InputStreamReader(new FileInputStream(f),
164 				UTF_8)) {
165 			if (checkData.length() > 0) {
166 				char[] data = new char[checkData.length()];
167 				assertEquals(data.length, r.read(data));
168 				assertEquals(checkData, new String(data));
169 			}
170 			assertEquals(-1, r.read());
171 		}
172 	}
173 
174 	/** Test repository, initialized for this test case. */
175 	protected FileRepository db;
176 
177 	/** Working directory of {@link #db}. */
178 	protected File trash;
179 
180 	/** {@inheritDoc} */
181 	@Override
182 	@Before
183 	public void setUp() throws Exception {
184 		super.setUp();
185 		db = createWorkRepository();
186 		trash = db.getWorkTree();
187 	}
188 
189 	/**
190 	 * Represent the state of the index in one String. This representation is
191 	 * useful when writing tests which do assertions on the state of the index.
192 	 * By default information about path, mode, stage (if different from 0) is
193 	 * included. A bitmask controls which additional info about
194 	 * modificationTimes, smudge state and length is included.
195 	 * <p>
196 	 * The format of the returned string is described with this BNF:
197 	 *
198 	 * <pre>
199 	 * result = ( "[" path mode stage? time? smudge? length? sha1? content? "]" )* .
200 	 * mode = ", mode:" number .
201 	 * stage = ", stage:" number .
202 	 * time = ", time:t" timestamp-index .
203 	 * smudge = "" | ", smudged" .
204 	 * length = ", length:" number .
205 	 * sha1 = ", sha1:" hex-sha1 .
206 	 * content = ", content:" blob-data .
207 	 * </pre>
208 	 *
209 	 * 'stage' is only presented when the stage is different from 0. All
210 	 * reported time stamps are mapped to strings like "t0", "t1", ... "tn". The
211 	 * smallest reported time-stamp will be called "t0". This allows to write
212 	 * assertions against the string although the concrete value of the time
213 	 * stamps is unknown.
214 	 *
215 	 * @param includedOptions
216 	 *            a bitmask constructed out of the constants {@link #MOD_TIME},
217 	 *            {@link #SMUDGE}, {@link #LENGTH}, {@link #CONTENT_ID} and
218 	 *            {@link #CONTENT} controlling which info is present in the
219 	 *            resulting string.
220 	 * @return a string encoding the index state
221 	 * @throws IllegalStateException
222 	 * @throws IOException
223 	 */
224 	public String indexState(int includedOptions)
225 			throws IllegalStateException, IOException {
226 		return indexState(db, includedOptions);
227 	}
228 
229 	/**
230 	 * Resets the index to represent exactly some filesystem content. E.g. the
231 	 * following call will replace the index with the working tree content:
232 	 * <p>
233 	 * <code>resetIndex(new FileSystemIterator(db))</code>
234 	 * <p>
235 	 * This method can be used by testcases which first prepare a new commit
236 	 * somewhere in the filesystem (e.g. in the working-tree) and then want to
237 	 * have an index which matches their prepared content.
238 	 *
239 	 * @param treeItr
240 	 *            a {@link org.eclipse.jgit.treewalk.FileTreeIterator} which
241 	 *            determines which files should go into the new index
242 	 * @throws FileNotFoundException
243 	 * @throws IOException
244 	 */
245 	protected void resetIndex(FileTreeIterator treeItr)
246 			throws FileNotFoundException, IOException {
247 		try (ObjectInserter inserter = db.newObjectInserter()) {
248 			DirCacheBuilder builder = db.lockDirCache().builder();
249 			DirCacheEntry dce;
250 
251 			while (!treeItr.eof()) {
252 				long len = treeItr.getEntryLength();
253 
254 				dce = new DirCacheEntry(treeItr.getEntryPathString());
255 				dce.setFileMode(treeItr.getEntryFileMode());
256 				dce.setLastModified(treeItr.getEntryLastModifiedInstant());
257 				dce.setLength((int) len);
258 				try (FileInputStream in = new FileInputStream(
259 						treeItr.getEntryFile())) {
260 					dce.setObjectId(
261 							inserter.insert(Constants.OBJ_BLOB, len, in));
262 				}
263 				builder.add(dce);
264 				treeItr.next(1);
265 			}
266 			builder.commit();
267 			inserter.flush();
268 		}
269 	}
270 
271 	/**
272 	 * Helper method to map arbitrary objects to user-defined names. This can be
273 	 * used create short names for objects to produce small and stable debug
274 	 * output. It is guaranteed that when you lookup the same object multiple
275 	 * times even with different nameTemplates this method will always return
276 	 * the same name which was derived from the first nameTemplate.
277 	 * nameTemplates can contain "%n" which will be replaced by a running number
278 	 * before used as a name.
279 	 *
280 	 * @param l
281 	 *            the object to lookup
282 	 * @param lookupTable
283 	 *            a table storing object-name mappings.
284 	 * @param nameTemplate
285 	 *            the name for that object. Can contain "%n" which will be
286 	 *            replaced by a running number before used as a name. If the
287 	 *            lookup table already contains the object this parameter will
288 	 *            be ignored
289 	 * @return a name of that object. Is not guaranteed to be unique. Use
290 	 *         nameTemplates containing "%n" to always have unique names
291 	 */
292 	public static String lookup(Object l, String nameTemplate,
293 			Map<Object, String> lookupTable) {
294 		String name = lookupTable.get(l);
295 		if (name == null) {
296 			name = nameTemplate.replaceAll("%n",
297 					Integer.toString(lookupTable.size()));
298 			lookupTable.put(l, name);
299 		}
300 		return name;
301 	}
302 
303 	/**
304 	 * Replaces '\' by '/'
305 	 *
306 	 * @param str
307 	 *            the string in which backslashes should be replaced
308 	 * @return the resulting string with slashes
309 	 * @since 4.2
310 	 */
311 	public static String slashify(String str) {
312 		str = str.replace('\\', '/');
313 		return str;
314 	}
315 
316 	/**
317 	 * Waits until it is guaranteed that a subsequent file modification has a
318 	 * younger modification timestamp than the modification timestamp of the
319 	 * given file. This is done by touching a temporary file, reading the
320 	 * lastmodified attribute and, if needed, sleeping. After sleeping this loop
321 	 * starts again until the filesystem timer has advanced enough. The
322 	 * temporary file will be created as a sibling of lastFile.
323 	 *
324 	 * @param lastFile
325 	 *            the file on which we want to wait until the filesystem timer
326 	 *            has advanced more than the lastmodification timestamp of this
327 	 *            file
328 	 * @return return the last measured value of the filesystem timer which is
329 	 *         greater than then the lastmodification time of lastfile.
330 	 * @throws InterruptedException
331 	 * @throws IOException
332 	 */
333 	public static Instant fsTick(File lastFile)
334 			throws InterruptedException,
335 			IOException {
336 		File tmp;
337 		FS fs = FS.DETECTED;
338 		if (lastFile == null) {
339 			lastFile = tmp = File
340 					.createTempFile("fsTickTmpFile", null);
341 		} else {
342 			if (!fs.exists(lastFile)) {
343 				throw new FileNotFoundException(lastFile.getPath());
344 			}
345 			tmp = File.createTempFile("fsTickTmpFile", null,
346 					lastFile.getParentFile());
347 		}
348 		long res = FS.getFileStoreAttributes(tmp.toPath())
349 				.getFsTimestampResolution().toNanos();
350 		long sleepTime = res / 10;
351 		try {
352 			Instant startTime = fs.lastModifiedInstant(lastFile);
353 			Instant actTime = fs.lastModifiedInstant(tmp);
354 			while (actTime.compareTo(startTime) <= 0) {
355 				TimeUnit.NANOSECONDS.sleep(sleepTime);
356 				FileUtils.touch(tmp.toPath());
357 				actTime = fs.lastModifiedInstant(tmp);
358 			}
359 			return actTime;
360 		} finally {
361 			FileUtils.delete(tmp);
362 		}
363 	}
364 
365 	/**
366 	 * Create a branch
367 	 *
368 	 * @param objectId
369 	 * @param branchName
370 	 * @throws IOException
371 	 */
372 	protected void createBranch(ObjectId objectId, String branchName)
373 			throws IOException {
374 		RefUpdate updateRef = db.updateRef(branchName);
375 		updateRef.setNewObjectId(objectId);
376 		updateRef.update();
377 	}
378 
379 	/**
380 	 * Checkout a branch
381 	 *
382 	 * @param branchName
383 	 * @throws IllegalStateException
384 	 * @throws IOException
385 	 */
386 	protected void checkoutBranch(String branchName)
387 			throws IllegalStateException, IOException {
388 		try (RevWalk walk = new RevWalk(db)) {
389 			RevCommit head = walk.parseCommit(db.resolve(Constants.HEAD));
390 			RevCommit branch = walk.parseCommit(db.resolve(branchName));
391 			DirCacheCheckout dco = new DirCacheCheckout(db,
392 					head.getTree().getId(), db.lockDirCache(),
393 					branch.getTree().getId());
394 			dco.setFailOnConflict(true);
395 			dco.checkout();
396 		}
397 		// update the HEAD
398 		RefUpdate refUpdate = db.updateRef(Constants.HEAD);
399 		refUpdate.setRefLogMessage("checkout: moving to " + branchName, false);
400 		refUpdate.link(branchName);
401 	}
402 
403 	/**
404 	 * Writes a number of files in the working tree. The first content specified
405 	 * will be written into a file named '0', the second into a file named "1"
406 	 * and so on. If <code>null</code> is specified as content then this file is
407 	 * skipped.
408 	 *
409 	 * @param ensureDistinctTimestamps
410 	 *            if set to <code>true</code> then between two write operations
411 	 *            this method will wait to ensure that the second file will get
412 	 *            a different lastmodification timestamp than the first file.
413 	 * @param contents
414 	 *            the contents which should be written into the files
415 	 * @return the File object associated to the last written file.
416 	 * @throws IOException
417 	 * @throws InterruptedException
418 	 */
419 	protected File writeTrashFiles(boolean ensureDistinctTimestamps,
420 			String... contents)
421 			throws IOException, InterruptedException {
422 				File f = null;
423 				for (int i = 0; i < contents.length; i++)
424 					if (contents[i] != null) {
425 						if (ensureDistinctTimestamps && (f != null))
426 							fsTick(f);
427 						f = writeTrashFile(Integer.toString(i), contents[i]);
428 					}
429 				return f;
430 			}
431 
432 	/**
433 	 * Commit a file with the specified contents on the specified branch,
434 	 * creating the branch if it didn't exist before.
435 	 * <p>
436 	 * It switches back to the original branch after the commit if there was
437 	 * one.
438 	 *
439 	 * @param filename
440 	 * @param contents
441 	 * @param branch
442 	 * @return the created commit
443 	 */
444 	protected RevCommit commitFile(String filename, String contents, String branch) {
445 		try (Git git = new Git(db)) {
446 			Repository repo = git.getRepository();
447 			String originalBranch = repo.getFullBranch();
448 			boolean empty = repo.resolve(Constants.HEAD) == null;
449 			if (!empty) {
450 				if (repo.findRef(branch) == null)
451 					git.branchCreate().setName(branch).call();
452 				git.checkout().setName(branch).call();
453 			}
454 
455 			writeTrashFile(filename, contents);
456 			git.add().addFilepattern(filename).call();
457 			RevCommit commit = git.commit()
458 					.setMessage(branch + ": " + filename).call();
459 
460 			if (originalBranch != null)
461 				git.checkout().setName(originalBranch).call();
462 			else if (empty)
463 				git.branchCreate().setName(branch).setStartPoint(commit).call();
464 
465 			return commit;
466 		} catch (IOException | GitAPIException e) {
467 			throw new RuntimeException(e);
468 		}
469 	}
470 
471 	/**
472 	 * Create <code>DirCacheEntry</code>
473 	 *
474 	 * @param path
475 	 * @param mode
476 	 * @return the DirCacheEntry
477 	 */
478 	protected DirCacheEntry createEntry(String path, FileMode mode) {
479 		return createEntry(path, mode, DirCacheEntry.STAGE_0, path);
480 	}
481 
482 	/**
483 	 * Create <code>DirCacheEntry</code>
484 	 *
485 	 * @param path
486 	 * @param mode
487 	 * @param content
488 	 * @return the DirCacheEntry
489 	 */
490 	protected DirCacheEntry createEntry(final String path, final FileMode mode,
491 			final String content) {
492 		return createEntry(path, mode, DirCacheEntry.STAGE_0, content);
493 	}
494 
495 	/**
496 	 * Create <code>DirCacheEntry</code>
497 	 *
498 	 * @param path
499 	 * @param mode
500 	 * @param stage
501 	 * @param content
502 	 * @return the DirCacheEntry
503 	 */
504 	protected DirCacheEntry createEntry(final String path, final FileMode mode,
505 			final int stage, final String content) {
506 		final DirCacheEntry entry = new DirCacheEntry(path, stage);
507 		entry.setFileMode(mode);
508 		try (ObjectInserter.Formatter formatter = new ObjectInserter.Formatter()) {
509 			entry.setObjectId(formatter.idFor(
510 					Constants.OBJ_BLOB, Constants.encode(content)));
511 		}
512 		return entry;
513 	}
514 
515 	/**
516 	 * Assert files are equal
517 	 *
518 	 * @param expected
519 	 * @param actual
520 	 * @throws IOException
521 	 */
522 	public static void assertEqualsFile(File expected, File actual)
523 			throws IOException {
524 		assertEquals(expected.getCanonicalFile(), actual.getCanonicalFile());
525 	}
526 }