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