View Javadoc
1   /*
2    * Copyright (C) 2011, Christian Halstrick <christian.halstrick@sap.com> 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.api;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.fail;
14  
15  import java.io.File;
16  import java.io.IOException;
17  
18  import org.eclipse.jgit.api.ListBranchCommand.ListMode;
19  import org.eclipse.jgit.api.errors.GitAPIException;
20  import org.eclipse.jgit.api.errors.JGitInternalException;
21  import org.eclipse.jgit.errors.RepositoryNotFoundException;
22  import org.eclipse.jgit.junit.RepositoryTestCase;
23  import org.eclipse.jgit.lib.Repository;
24  import org.eclipse.jgit.util.FileUtils;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  public class GitConstructionTest extends RepositoryTestCase {
29  	private Repository bareRepo;
30  
31  	@Override
32  	@Before
33  	public void setUp() throws Exception {
34  		super.setUp();
35  		try (Git git = new Git(db)) {
36  			git.commit().setMessage("initial commit").call();
37  			writeTrashFile("Test.txt", "Hello world");
38  			git.add().addFilepattern("Test.txt").call();
39  			git.commit().setMessage("Initial commit").call();
40  		}
41  
42  		bareRepo = Git.cloneRepository().setBare(true)
43  				.setURI(db.getDirectory().toURI().toString())
44  				.setDirectory(createUniqueTestGitDir(true)).call()
45  				.getRepository();
46  		addRepoToClose(bareRepo);
47  	}
48  
49  	@Test
50  	public void testWrap() throws JGitInternalException, GitAPIException {
51  		Git git = Git.wrap(db);
52  		assertEquals(1, git.branchList().call().size());
53  
54  		git = Git.wrap(bareRepo);
55  		assertEquals(1, git.branchList().setListMode(ListMode.ALL).call()
56  				.size());
57  
58  		try {
59  			Git.wrap(null);
60  			fail("Expected exception has not been thrown");
61  		} catch (NullPointerException e) {
62  			// should not get here
63  		}
64  	}
65  
66  	@Test
67  	public void testOpen() throws IOException, JGitInternalException,
68  			GitAPIException {
69  		Git git = Git.open(db.getDirectory());
70  		assertEquals(1, git.branchList().call().size());
71  
72  		git = Git.open(bareRepo.getDirectory());
73  		assertEquals(1, git.branchList().setListMode(ListMode.ALL).call()
74  				.size());
75  
76  		git = Git.open(db.getWorkTree());
77  		assertEquals(1, git.branchList().setListMode(ListMode.ALL).call()
78  				.size());
79  
80  		try {
81  			Git.open(db.getObjectsDirectory());
82  			fail("Expected exception has not been thrown");
83  		} catch (RepositoryNotFoundException e) {
84  			// should not get here
85  		}
86  	}
87  
88  	@Test
89  	/**
90  	 * Tests that a repository with packfiles can be deleted after calling
91  	 * Git.close(). On Windows the first try to delete the worktree will fail
92  	 * (because file handles on packfiles are still open) but the second
93  	 * attempt after a close will succeed.
94  	 *
95  	 * @throws IOException
96  	 * @throws JGitInternalException
97  	 * @throws GitAPIException
98  	 */
99  	public void testClose() throws IOException, JGitInternalException,
100 			GitAPIException {
101 		File workTree = db.getWorkTree();
102 		Git git = Git.open(workTree);
103 		git.gc().setExpire(null).call();
104 		git.checkout().setName(git.getRepository().resolve("HEAD^").getName())
105 				.call();
106 		try {
107 			FileUtils.delete(workTree, FileUtils.RECURSIVE);
108 		} catch (IOException e) {
109 			git.close();
110 			FileUtils.delete(workTree, FileUtils.RECURSIVE);
111 		}
112 	}
113 }