Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4ed4bdc324720aafeb1b9eafb95f07f8c128c218 (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
/*******************************************************************************
 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
 *
 * 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
 *******************************************************************************/
package org.eclipse.egit.core.test;

import java.io.File;
import java.io.IOException;
import java.util.Collection;

import junit.framework.TestCase;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.egit.core.project.RepositoryFinder;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.util.SystemReader;

public abstract class GitTestCase extends TestCase {

	protected TestProject project;

	protected File gitDir;

	protected void setUp() throws Exception {
		super.setUp();
		((MockSystemReader) SystemReader.getInstance()).setProperty(
				Constants.GIT_CEILING_DIRECTORIES_KEY, ResourcesPlugin
						.getWorkspace().getRoot().getLocation().toFile()
						.getAbsoluteFile().toString());
		project = new TestProject(true);
		checkNotNested();
		gitDir = new File(project.getProject().getWorkspace().getRoot()
				.getRawLocation().toFile(), ".git");
		rmrf(gitDir);
	}

	protected void tearDown() throws Exception {
		super.tearDown();
		project.dispose();
		rmrf(gitDir);
	}

	private void rmrf(File d) throws IOException {
		if (!d.exists())
			return;

		File[] files = d.listFiles();
		if (files != null) {
			for (int i = 0; i < files.length; ++i) {
				if (files[i].isDirectory())
					rmrf(files[i]);
				else if (!files[i].delete())
					throw new IOException(files[i] + " in use or undeletable");
			}
		}
		if (!d.delete())
			throw new IOException(d + " in use or undeletable");
		assert !d.exists();
	}

	protected void checkNotNested() throws CoreException {
		final Collection<RepositoryMapping> parentRepositories = new RepositoryFinder(
				project.getProject()).find(null);
		final int numOfRepositories = parentRepositories.size();
		assertTrue("parent repository found: " + parentRepositories.toString(),
				numOfRepositories == 0);
	}

}

Back to the top