View Javadoc
1   /*
2    * Copyright (C) 2011, Christian Halstrick <christian.halstrick@sap.com>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  package org.eclipse.jgit.api;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.fail;
47  
48  import java.io.File;
49  import java.io.IOException;
50  
51  import org.eclipse.jgit.api.ListBranchCommand.ListMode;
52  import org.eclipse.jgit.api.errors.GitAPIException;
53  import org.eclipse.jgit.api.errors.JGitInternalException;
54  import org.eclipse.jgit.errors.RepositoryNotFoundException;
55  import org.eclipse.jgit.junit.RepositoryTestCase;
56  import org.eclipse.jgit.lib.Repository;
57  import org.eclipse.jgit.util.FileUtils;
58  import org.junit.After;
59  import org.junit.Before;
60  import org.junit.Test;
61  
62  public class GitConstructionTest extends RepositoryTestCase {
63  	private Repository bareRepo;
64  
65  	@Override
66  	@Before
67  	public void setUp() throws Exception {
68  		super.setUp();
69  		try (Git git = new Git(db)) {
70  			git.commit().setMessage("initial commit").call();
71  			writeTrashFile("Test.txt", "Hello world");
72  			git.add().addFilepattern("Test.txt").call();
73  			git.commit().setMessage("Initial commit").call();
74  		}
75  
76  		bareRepo = Git.cloneRepository().setBare(true)
77  				.setURI(db.getDirectory().toURI().toString())
78  				.setDirectory(createUniqueTestGitDir(true)).call()
79  				.getRepository();
80  		addRepoToClose(bareRepo);
81  	}
82  
83  	@Override
84  	@After
85  	public void tearDown() throws Exception {
86  		db.close();
87  		bareRepo.close();
88  		super.tearDown();
89  	}
90  
91  	@Test
92  	public void testWrap() throws JGitInternalException, GitAPIException {
93  		Git git = Git.wrap(db);
94  		assertEquals(1, git.branchList().call().size());
95  
96  		git = Git.wrap(bareRepo);
97  		assertEquals(1, git.branchList().setListMode(ListMode.ALL).call()
98  				.size());
99  
100 		try {
101 			Git.wrap(null);
102 			fail("Expected exception has not been thrown");
103 		} catch (NullPointerException e) {
104 			// should not get here
105 		}
106 	}
107 
108 	@Test
109 	public void testOpen() throws IOException, JGitInternalException,
110 			GitAPIException {
111 		Git git = Git.open(db.getDirectory());
112 		assertEquals(1, git.branchList().call().size());
113 
114 		git = Git.open(bareRepo.getDirectory());
115 		assertEquals(1, git.branchList().setListMode(ListMode.ALL).call()
116 				.size());
117 
118 		git = Git.open(db.getWorkTree());
119 		assertEquals(1, git.branchList().setListMode(ListMode.ALL).call()
120 				.size());
121 
122 		try {
123 			Git.open(db.getObjectsDirectory());
124 			fail("Expected exception has not been thrown");
125 		} catch (RepositoryNotFoundException e) {
126 			// should not get here
127 		}
128 	}
129 
130 	@Test
131 	/**
132 	 * Tests that a repository with packfiles can be deleted after calling
133 	 * Git.close(). On Windows the first try to delete the worktree will fail
134 	 * (because file handles on packfiles are still open) but the second
135 	 * attempt after a close will succeed.
136 	 *
137 	 * @throws IOException
138 	 * @throws JGitInternalException
139 	 * @throws GitAPIException
140 	 */
141 	public void testClose() throws IOException, JGitInternalException,
142 			GitAPIException {
143 		File workTree = db.getWorkTree();
144 		db.close();
145 		Git git = Git.open(workTree);
146 		git.gc().setExpire(null).call();
147 		git.checkout().setName(git.getRepository().resolve("HEAD^").getName())
148 				.call();
149 		try {
150 			FileUtils.delete(workTree, FileUtils.RECURSIVE);
151 		} catch (IOException e) {
152 			git.close();
153 			FileUtils.delete(workTree, FileUtils.RECURSIVE);
154 		}
155 	}
156 }