View Javadoc
1   /*
2    * Copyright (C) 2009-2010, Google Inc.
3    * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 2007, Shawn O. Pearce <spearce@spearce.org>
5    * and other copyright owners as documented in the project's IP log.
6    *
7    * This program and the accompanying materials are made available
8    * under the terms of the Eclipse Distribution License v1.0 which
9    * accompanies this distribution, is reproduced below, and is
10   * available at http://www.eclipse.org/org/documents/edl-v10.php
11   *
12   * All rights reserved.
13   *
14   * Redistribution and use in source and binary forms, with or
15   * without modification, are permitted provided that the following
16   * conditions are met:
17   *
18   * - Redistributions of source code must retain the above copyright
19   *   notice, this list of conditions and the following disclaimer.
20   *
21   * - Redistributions in binary form must reproduce the above
22   *   copyright notice, this list of conditions and the following
23   *   disclaimer in the documentation and/or other materials provided
24   *   with the distribution.
25   *
26   * - Neither the name of the Eclipse Foundation, Inc. nor the
27   *   names of its contributors may be used to endorse or promote
28   *   products derived from this software without specific prior
29   *   written permission.
30   *
31   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44   */
45  
46  package org.eclipse.jgit.junit;
47  
48  import static org.junit.Assert.assertFalse;
49  import static org.junit.Assert.fail;
50  
51  import java.io.File;
52  import java.io.IOException;
53  import java.util.*;
54  
55  import org.eclipse.jgit.dircache.DirCache;
56  import org.eclipse.jgit.dircache.DirCacheEntry;
57  import org.eclipse.jgit.internal.storage.file.FileRepository;
58  import org.eclipse.jgit.lib.*;
59  import org.eclipse.jgit.storage.file.FileBasedConfig;
60  import org.eclipse.jgit.storage.file.WindowCacheConfig;
61  import org.eclipse.jgit.util.FS;
62  import org.eclipse.jgit.util.FileUtils;
63  import org.eclipse.jgit.util.SystemReader;
64  import org.junit.After;
65  import org.junit.Before;
66  
67  /**
68   * JUnit TestCase with specialized support for temporary local repository.
69   * <p>
70   * A temporary directory is created for each test, allowing each test to use a
71   * fresh environment. The temporary directory is cleaned up after the test ends.
72   * <p>
73   * Callers should not use {@link RepositoryCache} from within these tests as it
74   * may wedge file descriptors open past the end of the test.
75   * <p>
76   * A system property {@code jgit.junit.usemmap} defines whether memory mapping
77   * is used. Memory mapping has an effect on the file system, in that memory
78   * mapped files in Java cannot be deleted as long as the mapped arrays have not
79   * been reclaimed by the garbage collector. The programmer cannot control this
80   * with precision, so temporary files may hang around longer than desired during
81   * a test, or tests may fail altogether if there is insufficient file
82   * descriptors or address space for the test process.
83   */
84  public abstract class LocalDiskRepositoryTestCase {
85  	private static final boolean useMMAP = "true".equals(System
86  			.getProperty("jgit.junit.usemmap"));
87  
88  	/** A fake (but stable) identity for author fields in the test. */
89  	protected PersonIdent author;
90  
91  	/** A fake (but stable) identity for committer fields in the test. */
92  	protected PersonIdent committer;
93  
94  	/**
95  	 * A {@link SystemReader} used to coordinate time, envars, etc.
96  	 * @since 4.2
97  	 */
98  	protected MockSystemReader mockSystemReader;
99  
100 	private final List<Repository> toClose = new ArrayList<Repository>();
101 	private File tmp;
102 
103 	@Before
104 	public void setUp() throws Exception {
105 		tmp = File.createTempFile("jgit_test_", "_tmp");
106 		CleanupThread.deleteOnShutdown(tmp);
107 		if (!tmp.delete() || !tmp.mkdir())
108 			throw new IOException("Cannot create " + tmp);
109 
110 		mockSystemReader = new MockSystemReader();
111 		mockSystemReader.userGitConfig = new FileBasedConfig(new File(tmp,
112 				"usergitconfig"), FS.DETECTED);
113 		ceilTestDirectories(getCeilings());
114 		SystemReader.setInstance(mockSystemReader);
115 
116 		final long now = mockSystemReader.getCurrentTime();
117 		final int tz = mockSystemReader.getTimezone(now);
118 		author = new PersonIdent("J. Author", "jauthor@example.com");
119 		author = new PersonIdent(author, now, tz);
120 
121 		committer = new PersonIdent("J. Committer", "jcommitter@example.com");
122 		committer = new PersonIdent(committer, now, tz);
123 
124 		final WindowCacheConfig c = new WindowCacheConfig();
125 		c.setPackedGitLimit(128 * WindowCacheConfig.KB);
126 		c.setPackedGitWindowSize(8 * WindowCacheConfig.KB);
127 		c.setPackedGitMMAP(useMMAP);
128 		c.setDeltaBaseCacheLimit(8 * WindowCacheConfig.KB);
129 		c.install();
130 	}
131 
132 	protected File getTemporaryDirectory() {
133 		return tmp.getAbsoluteFile();
134 	}
135 
136 	protected List<File> getCeilings() {
137 		return Collections.singletonList(getTemporaryDirectory());
138 	}
139 
140 	private void ceilTestDirectories(List<File> ceilings) {
141 		mockSystemReader.setProperty(Constants.GIT_CEILING_DIRECTORIES_KEY, makePath(ceilings));
142 	}
143 
144 	private static String makePath(List<?> objects) {
145 		final StringBuilder stringBuilder = new StringBuilder();
146 		for (Object object : objects) {
147 			if (stringBuilder.length() > 0)
148 				stringBuilder.append(File.pathSeparatorChar);
149 			stringBuilder.append(object.toString());
150 		}
151 		return stringBuilder.toString();
152 	}
153 
154 	@After
155 	public void tearDown() throws Exception {
156 		RepositoryCache.clear();
157 		for (Repository r : toClose)
158 			r.close();
159 		toClose.clear();
160 
161 		// Since memory mapping is controlled by the GC we need to
162 		// tell it this is a good time to clean up and unlock
163 		// memory mapped files.
164 		//
165 		if (useMMAP)
166 			System.gc();
167 		if (tmp != null)
168 			recursiveDelete(tmp, false, true);
169 		if (tmp != null && !tmp.exists())
170 			CleanupThread.removed(tmp);
171 
172 		SystemReader.setInstance(null);
173 	}
174 
175 	/** Increment the {@link #author} and {@link #committer} times. */
176 	protected void tick() {
177 		mockSystemReader.tick(5 * 60);
178 		final long now = mockSystemReader.getCurrentTime();
179 		final int tz = mockSystemReader.getTimezone(now);
180 
181 		author = new PersonIdent(author, now, tz);
182 		committer = new PersonIdent(committer, now, tz);
183 	}
184 
185 	/**
186 	 * Recursively delete a directory, failing the test if the delete fails.
187 	 *
188 	 * @param dir
189 	 *            the recursively directory to delete, if present.
190 	 */
191 	protected void recursiveDelete(final File dir) {
192 		recursiveDelete(dir, false, true);
193 	}
194 
195 	private static boolean recursiveDelete(final File dir,
196 			boolean silent, boolean failOnError) {
197 		assert !(silent && failOnError);
198 		if (!dir.exists())
199 			return silent;
200 		final File[] ls = dir.listFiles();
201 		if (ls != null)
202 			for (int k = 0; k < ls.length; k++) {
203 				final File e = ls[k];
204 				if (e.isDirectory())
205 					silent = recursiveDelete(e, silent, failOnError);
206 				else if (!e.delete()) {
207 					if (!silent)
208 						reportDeleteFailure(failOnError, e);
209 					silent = !failOnError;
210 				}
211 			}
212 		if (!dir.delete()) {
213 			if (!silent)
214 				reportDeleteFailure(failOnError, dir);
215 			silent = !failOnError;
216 		}
217 		return silent;
218 	}
219 
220 	private static void reportDeleteFailure(boolean failOnError, File e) {
221 		String severity = failOnError ? "ERROR" : "WARNING";
222 		String msg = severity + ": Failed to delete " + e;
223 		if (failOnError)
224 			fail(msg);
225 		else
226 			System.err.println(msg);
227 	}
228 
229 	public static final int MOD_TIME = 1;
230 
231 	public static final int SMUDGE = 2;
232 
233 	public static final int LENGTH = 4;
234 
235 	public static final int CONTENT_ID = 8;
236 
237 	public static final int CONTENT = 16;
238 
239 	public static final int ASSUME_UNCHANGED = 32;
240 
241 	/**
242 	 * Represent the state of the index in one String. This representation is
243 	 * useful when writing tests which do assertions on the state of the index.
244 	 * By default information about path, mode, stage (if different from 0) is
245 	 * included. A bitmask controls which additional info about
246 	 * modificationTimes, smudge state and length is included.
247 	 * <p>
248 	 * The format of the returned string is described with this BNF:
249 	 *
250 	 * <pre>
251 	 * result = ( "[" path mode stage? time? smudge? length? sha1? content? "]" )* .
252 	 * mode = ", mode:" number .
253 	 * stage = ", stage:" number .
254 	 * time = ", time:t" timestamp-index .
255 	 * smudge = "" | ", smudged" .
256 	 * length = ", length:" number .
257 	 * sha1 = ", sha1:" hex-sha1 .
258 	 * content = ", content:" blob-data .
259 	 * </pre>
260 	 *
261 	 * 'stage' is only presented when the stage is different from 0. All
262 	 * reported time stamps are mapped to strings like "t0", "t1", ... "tn". The
263 	 * smallest reported time-stamp will be called "t0". This allows to write
264 	 * assertions against the string although the concrete value of the time
265 	 * stamps is unknown.
266 	 *
267 	 * @param repo
268 	 *            the repository the index state should be determined for
269 	 *
270 	 * @param includedOptions
271 	 *            a bitmask constructed out of the constants {@link #MOD_TIME},
272 	 *            {@link #SMUDGE}, {@link #LENGTH}, {@link #CONTENT_ID} and
273 	 *            {@link #CONTENT} controlling which info is present in the
274 	 *            resulting string.
275 	 * @return a string encoding the index state
276 	 * @throws IllegalStateException
277 	 * @throws IOException
278 	 */
279 	public static String indexState(Repository repo, int includedOptions)
280 			throws IllegalStateException, IOException {
281 		DirCache dc = repo.readDirCache();
282 		StringBuilder sb = new StringBuilder();
283 		TreeSet<Long> timeStamps = new TreeSet<Long>();
284 
285 		// iterate once over the dircache just to collect all time stamps
286 		if (0 != (includedOptions & MOD_TIME)) {
287 			for (int i=0; i<dc.getEntryCount(); ++i)
288 				timeStamps.add(Long.valueOf(dc.getEntry(i).getLastModified()));
289 		}
290 
291 		// iterate again, now produce the result string
292 		for (int i=0; i<dc.getEntryCount(); ++i) {
293 			DirCacheEntry entry = dc.getEntry(i);
294 			sb.append("["+entry.getPathString()+", mode:" + entry.getFileMode());
295 			int stage = entry.getStage();
296 			if (stage != 0)
297 				sb.append(", stage:" + stage);
298 			if (0 != (includedOptions & MOD_TIME)) {
299 				sb.append(", time:t"+
300 						timeStamps.headSet(Long.valueOf(entry.getLastModified())).size());
301 			}
302 			if (0 != (includedOptions & SMUDGE))
303 				if (entry.isSmudged())
304 					sb.append(", smudged");
305 			if (0 != (includedOptions & LENGTH))
306 				sb.append(", length:"
307 						+ Integer.toString(entry.getLength()));
308 			if (0 != (includedOptions & CONTENT_ID))
309 				sb.append(", sha1:" + ObjectId.toString(entry.getObjectId()));
310 			if (0 != (includedOptions & CONTENT)) {
311 				sb.append(", content:"
312 						+ new String(repo.open(entry.getObjectId(),
313 						Constants.OBJ_BLOB).getCachedBytes(), "UTF-8"));
314 			}
315 			if (0 != (includedOptions & ASSUME_UNCHANGED))
316 				sb.append(", assume-unchanged:"
317 						+ Boolean.toString(entry.isAssumeValid()));
318 			sb.append("]");
319 		}
320 		return sb.toString();
321 	}
322 
323 
324 	/**
325 	 * Creates a new empty bare repository.
326 	 *
327 	 * @return the newly created repository, opened for access
328 	 * @throws IOException
329 	 *             the repository could not be created in the temporary area
330 	 */
331 	protected FileRepository createBareRepository() throws IOException {
332 		return createRepository(true /* bare */);
333 	}
334 
335 	/**
336 	 * Creates a new empty repository within a new empty working directory.
337 	 *
338 	 * @return the newly created repository, opened for access
339 	 * @throws IOException
340 	 *             the repository could not be created in the temporary area
341 	 */
342 	protected FileRepository createWorkRepository() throws IOException {
343 		return createRepository(false /* not bare */);
344 	}
345 
346 	/**
347 	 * Creates a new empty repository.
348 	 *
349 	 * @param bare
350 	 *            true to create a bare repository; false to make a repository
351 	 *            within its working directory
352 	 * @return the newly created repository, opened for access
353 	 * @throws IOException
354 	 *             the repository could not be created in the temporary area
355 	 */
356 	private FileRepository createRepository(boolean bare) throws IOException {
357 		File gitdir = createUniqueTestGitDir(bare);
358 		FileRepository db = new FileRepository(gitdir);
359 		assertFalse(gitdir.exists());
360 		db.create(bare);
361 		toClose.add(db);
362 		return db;
363 	}
364 
365 	/**
366 	 * Adds a repository to the list of repositories which is closed at the end
367 	 * of the tests
368 	 *
369 	 * @param r
370 	 *            the repository to be closed
371 	 */
372 	public void addRepoToClose(Repository r) {
373 		toClose.add(r);
374 	}
375 
376 	/**
377 	 * Creates a unique directory for a test
378 	 *
379 	 * @param name
380 	 *            a subdirectory
381 	 * @return a unique directory for a test
382 	 * @throws IOException
383 	 */
384 	protected File createTempDirectory(String name) throws IOException {
385 		File directory = new File(createTempFile(), name);
386 		FileUtils.mkdirs(directory);
387 		return directory.getCanonicalFile();
388 	}
389 
390 	/**
391 	 * Creates a new unique directory for a test repository
392 	 *
393 	 * @param bare
394 	 *            true for a bare repository; false for a repository with a
395 	 *            working directory
396 	 * @return a unique directory for a test repository
397 	 * @throws IOException
398 	 */
399 	protected File createUniqueTestGitDir(boolean bare) throws IOException {
400 		String gitdirName = createTempFile().getPath();
401 		if (!bare)
402 			gitdirName += "/";
403 		return new File(gitdirName + Constants.DOT_GIT);
404 	}
405 
406 	/**
407 	 * Allocates a new unique file path that does not exist.
408 	 * <p>
409 	 * Unlike the standard {@code File.createTempFile} the returned path does
410 	 * not exist, but may be created by another thread in a race with the
411 	 * caller. Good luck.
412 	 * <p>
413 	 * This method is inherently unsafe due to a race condition between creating
414 	 * the name and the first use that reserves it.
415 	 *
416 	 * @return a unique path that does not exist.
417 	 * @throws IOException
418 	 */
419 	protected File createTempFile() throws IOException {
420 		File p = File.createTempFile("tmp_", "", tmp);
421 		if (!p.delete()) {
422 			throw new IOException("Cannot obtain unique path " + tmp);
423 		}
424 		return p;
425 	}
426 
427 	/**
428 	 * Run a hook script in the repository, returning the exit status.
429 	 *
430 	 * @param db
431 	 *            repository the script should see in GIT_DIR environment
432 	 * @param hook
433 	 *            path of the hook script to execute, must be executable file
434 	 *            type on this platform
435 	 * @param args
436 	 *            arguments to pass to the hook script
437 	 * @return exit status code of the invoked hook
438 	 * @throws IOException
439 	 *             the hook could not be executed
440 	 * @throws InterruptedException
441 	 *             the caller was interrupted before the hook completed
442 	 */
443 	protected int runHook(final Repository db, final File hook,
444 			final String... args) throws IOException, InterruptedException {
445 		final String[] argv = new String[1 + args.length];
446 		argv[0] = hook.getAbsolutePath();
447 		System.arraycopy(args, 0, argv, 1, args.length);
448 
449 		final Map<String, String> env = cloneEnv();
450 		env.put("GIT_DIR", db.getDirectory().getAbsolutePath());
451 		putPersonIdent(env, "AUTHOR", author);
452 		putPersonIdent(env, "COMMITTER", committer);
453 
454 		final File cwd = db.getWorkTree();
455 		final Process p = Runtime.getRuntime().exec(argv, toEnvArray(env), cwd);
456 		p.getOutputStream().close();
457 		p.getErrorStream().close();
458 		p.getInputStream().close();
459 		return p.waitFor();
460 	}
461 
462 	private static void putPersonIdent(final Map<String, String> env,
463 			final String type, final PersonIdent who) {
464 		final String ident = who.toExternalString();
465 		final String date = ident.substring(ident.indexOf("> ") + 2);
466 		env.put("GIT_" + type + "_NAME", who.getName());
467 		env.put("GIT_" + type + "_EMAIL", who.getEmailAddress());
468 		env.put("GIT_" + type + "_DATE", date);
469 	}
470 
471 	/**
472 	 * Create a string to a UTF-8 temporary file and return the path.
473 	 *
474 	 * @param body
475 	 *            complete content to write to the file. If the file should end
476 	 *            with a trailing LF, the string should end with an LF.
477 	 * @return path of the temporary file created within the trash area.
478 	 * @throws IOException
479 	 *             the file could not be written.
480 	 */
481 	protected File write(final String body) throws IOException {
482 		final File f = File.createTempFile("temp", "txt", tmp);
483 		try {
484 			write(f, body);
485 			return f;
486 		} catch (Error e) {
487 			f.delete();
488 			throw e;
489 		} catch (RuntimeException e) {
490 			f.delete();
491 			throw e;
492 		} catch (IOException e) {
493 			f.delete();
494 			throw e;
495 		}
496 	}
497 
498 	/**
499 	 * Write a string as a UTF-8 file.
500 	 *
501 	 * @param f
502 	 *            file to write the string to. Caller is responsible for making
503 	 *            sure it is in the trash directory or will otherwise be cleaned
504 	 *            up at the end of the test. If the parent directory does not
505 	 *            exist, the missing parent directories are automatically
506 	 *            created.
507 	 * @param body
508 	 *            content to write to the file.
509 	 * @throws IOException
510 	 *             the file could not be written.
511 	 */
512 	protected void write(final File f, final String body) throws IOException {
513 		JGitTestUtil.write(f, body);
514 	}
515 
516 	protected String read(final File f) throws IOException {
517 		return JGitTestUtil.read(f);
518 	}
519 
520 	private static String[] toEnvArray(final Map<String, String> env) {
521 		final String[] envp = new String[env.size()];
522 		int i = 0;
523 		for (Map.Entry<String, String> e : env.entrySet())
524 			envp[i++] = e.getKey() + "=" + e.getValue();
525 		return envp;
526 	}
527 
528 	private static HashMap<String, String> cloneEnv() {
529 		return new HashMap<String, String>(System.getenv());
530 	}
531 
532 	private static final class CleanupThread extends Thread {
533 		private static final CleanupThread me;
534 		static {
535 			me = new CleanupThread();
536 			Runtime.getRuntime().addShutdownHook(me);
537 		}
538 
539 		static void deleteOnShutdown(File tmp) {
540 			synchronized (me) {
541 				me.toDelete.add(tmp);
542 			}
543 		}
544 
545 		static void removed(File tmp) {
546 			synchronized (me) {
547 				me.toDelete.remove(tmp);
548 			}
549 		}
550 
551 		private final List<File> toDelete = new ArrayList<File>();
552 
553 		@Override
554 		public void run() {
555 			// On windows accidentally open files or memory
556 			// mapped regions may prevent files from being deleted.
557 			// Suggesting a GC increases the likelihood that our
558 			// test repositories actually get removed after the
559 			// tests, even in the case of failure.
560 			System.gc();
561 			synchronized (this) {
562 				boolean silent = false;
563 				boolean failOnError = false;
564 				for (File tmp : toDelete)
565 					recursiveDelete(tmp, silent, failOnError);
566 			}
567 		}
568 	}
569 }