Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d44ab83b6596d0381ab3eba59bc3389b83f9e2b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*******************************************************************************
 * Copyright (c) 2012 Red Hat, Inc. Distributed under license by Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Andre Dietisheim - initial API and implementation
 *******************************************************************************/
package org.eclipse.egit.core.test;

import static org.hamcrest.Matchers.isIn;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.IOException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.RepositoryCache;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.util.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class RepositoryCacheTest extends GitTestCase {

	private TestRepository testRepository;
	private Repository repository;
	private RepositoryCache cache;

	@Before
	public void setUp() throws Exception {
		super.setUp();
		this.testRepository = new TestRepository(gitDir);
		this.repository = testRepository.getRepository();
		this.cache = Activator.getDefault().getRepositoryCache();
	}

	@After
	public void tearDown() throws Exception {
		testRepository.dispose();
		repository = null;
		super.tearDown();
	}

	@Test
	public void shouldNotContainDeletedRepository() throws IOException {
		cache.lookupRepository(repository.getDirectory());
		assertThat(repository, isIn(cache.getAllRepositories()));
		FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE);
		assertThat(repository, not(isIn(cache.getAllRepositories())));
	}

	@Test
	public void findsRepositoryForOpenProject() throws Exception {
		IFile a = testUtils.addFileToProject(project.getProject(),
				"folder1/a.txt", "a");
		assertEquals(repository, cache.getRepository(a));
	}

	@Test
	public void findsRepositoryForClosedProject() throws Exception {
		IFile a = testUtils.addFileToProject(project.getProject(),
				"folder1/a.txt", "a");
		project.getProject().close(null);
		assertEquals(repository, cache.getRepository(a));
	}

	@Test
	public void findsNestedRepositoryForClosedProject()
			throws Exception {
		IFile a = testUtils.addFileToProject(project.getProject(),
				"folder1/a.txt", "a");

		// now we create a second project2 in a nested repository2
		File workdir = project.createFolder("nested").getLocation().toFile();
		TestRepository repository2 = new TestRepository(new File(workdir,
				Constants.DOT_GIT));

		String projectName = "project2";
		IProject project2 = testUtils.createProjectInLocalFileSystem(workdir,
				projectName);
		IFile b = testUtils.addFileToProject(project2, "folder1/b.txt",
				"Hello world");
		repository2.connect(project2);

		project.getProject().close(null);
		project2.getProject().close(null);

		// assert that we don't get confused by nesting repositories
		assertEquals(repository, cache.getRepository(a));
		assertEquals(repository2.repository, cache.getRepository(b));
	}
}

Back to the top