Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7112ddb7a6fe78cafc81d50f088fbd6538e304bd (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*******************************************************************************
 * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.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.op;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.egit.core.op.TrackOperation;
import org.eclipse.egit.core.op.UntrackOperation;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.egit.core.test.DualRepositoryTestCase;
import org.eclipse.egit.core.test.TestRepository;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.lib.Constants;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TrackUntrackOperationTest extends DualRepositoryTestCase {

	File workdir;

	IProject project;

	String projectName = "TrackTest";

	@Before
	public void setUp() throws Exception {

		workdir = testUtils.getTempDir("Repository1");

		repository1 = new TestRepository(new File(workdir, Constants.DOT_GIT));

		// now we create a project in repo1
		project = testUtils
				.createProjectInLocalFileSystem(workdir, projectName);
		testUtils.addFileToProject(project, "folder1/file1.txt", "Hello world");

		repository1.connect(project);
	}

	@After
	public void tearDown() throws Exception {
		project.close(null);
		project.delete(false, false, null);
		repository1.dispose();
		repository1 = null;
		testUtils.deleteRecursive(workdir);
	}

	@Test
	public void testTrackFiles() throws Exception {

		final ArrayList<IFile> files = new ArrayList<IFile>();

		project.accept(new IResourceVisitor() {

			public boolean visit(IResource resource) throws CoreException {
				if (resource instanceof IFile)
					files.add((IFile) resource);
				return true;
			}
		});

		IFile[] fileArr = files.toArray(new IFile[files.size()]);

		assertTrackedState(fileArr, false);

		TrackOperation trop = new TrackOperation(fileArr);
		trop.execute(new NullProgressMonitor());

		assertTrackedState(fileArr, true);

		UntrackOperation utop = new UntrackOperation(Arrays.asList(fileArr));

		utop.execute(new NullProgressMonitor());

		assertTrackedState(fileArr, false);
	}

	private void assertTrackedState(IFile[] fileArr, boolean expectedState)
			throws IOException {
		DirCache cache = DirCache.read(repository1.getRepository());
		for (IFile file : fileArr) {
			RepositoryMapping rm = RepositoryMapping.getMapping(file);
			String fileDir = rm.getRepoRelativePath(file);
			boolean tracked = cache.findEntry(fileDir) > -1;
			assertTrue("Wrong tracking state", tracked == expectedState);
		}
	}

	@Test
	public void testTrackProject() throws Exception {

		final ArrayList<IContainer> containers = new ArrayList<IContainer>();
		containers.add(project);

		IContainer[] projectArr = containers.toArray(new IContainer[containers
				.size()]);

		final ArrayList<IFile> files = new ArrayList<IFile>();

		project.accept(new IResourceVisitor() {

			public boolean visit(IResource resource) throws CoreException {
				if (resource instanceof IFile)
					files.add((IFile) resource);
				return true;
			}
		});

		IFile[] fileArr = files.toArray(new IFile[files.size()]);

		assertTrackedState(fileArr, false);

		TrackOperation trop = new TrackOperation(projectArr);
		trop.execute(new NullProgressMonitor());

		assertTrackedState(fileArr, true);

		UntrackOperation utrop = new UntrackOperation(containers);
		utrop.execute(new NullProgressMonitor());

		assertTrackedState(fileArr, false);
	}

}

Back to the top