View Javadoc
1   /*
2    * Copyright (C) 2012, GitHub Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.internal.storage.file;
11  
12  import static org.junit.Assert.assertNotNull;
13  import static org.junit.Assert.assertTrue;
14  import static org.junit.Assert.fail;
15  
16  import org.eclipse.jgit.api.Git;
17  import org.eclipse.jgit.api.errors.JGitInternalException;
18  import org.eclipse.jgit.errors.LockFailedException;
19  import org.eclipse.jgit.junit.RepositoryTestCase;
20  import org.eclipse.jgit.revwalk.RevCommit;
21  import org.junit.Test;
22  
23  /**
24   * Unit tests of {@link LockFile}
25   */
26  public class LockFileTest extends RepositoryTestCase {
27  
28  	@Test
29  	public void lockFailedExceptionRecovery() throws Exception {
30  		try (Git git = new Git(db)) {
31  			writeTrashFile("file.txt", "content");
32  			git.add().addFilepattern("file.txt").call();
33  			RevCommit commit1 = git.commit().setMessage("create file").call();
34  
35  			assertNotNull(commit1);
36  			writeTrashFile("file.txt", "content2");
37  			git.add().addFilepattern("file.txt").call();
38  			assertNotNull(git.commit().setMessage("edit file").call());
39  
40  			LockFile lf = new LockFile(db.getIndexFile());
41  			assertTrue(lf.lock());
42  			try {
43  				git.checkout().setName(commit1.name()).call();
44  				fail("JGitInternalException not thrown");
45  			} catch (JGitInternalException e) {
46  				assertTrue(e.getCause() instanceof LockFailedException);
47  				lf.unlock();
48  				git.checkout().setName(commit1.name()).call();
49  			}
50  		}
51  	}
52  }