You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LockFileTest.java 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. import static org.junit.Assert.assertNotNull;
  12. import static org.junit.Assert.assertTrue;
  13. import static org.junit.Assert.fail;
  14. import org.eclipse.jgit.api.Git;
  15. import org.eclipse.jgit.api.errors.JGitInternalException;
  16. import org.eclipse.jgit.errors.LockFailedException;
  17. import org.eclipse.jgit.junit.RepositoryTestCase;
  18. import org.eclipse.jgit.revwalk.RevCommit;
  19. import org.junit.Test;
  20. /**
  21. * Unit tests of {@link LockFile}
  22. */
  23. public class LockFileTest extends RepositoryTestCase {
  24. @Test
  25. public void lockFailedExceptionRecovery() throws Exception {
  26. try (Git git = new Git(db)) {
  27. writeTrashFile("file.txt", "content");
  28. git.add().addFilepattern("file.txt").call();
  29. RevCommit commit1 = git.commit().setMessage("create file").call();
  30. assertNotNull(commit1);
  31. writeTrashFile("file.txt", "content2");
  32. git.add().addFilepattern("file.txt").call();
  33. assertNotNull(git.commit().setMessage("edit file").call());
  34. LockFile lf = new LockFile(db.getIndexFile());
  35. assertTrue(lf.lock());
  36. try {
  37. git.checkout().setName(commit1.name()).call();
  38. fail("JGitInternalException not thrown");
  39. } catch (JGitInternalException e) {
  40. assertTrue(e.getCause() instanceof LockFailedException);
  41. lf.unlock();
  42. git.checkout().setName(commit1.name()).call();
  43. }
  44. }
  45. }
  46. }