Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3740339e9d6f41a301c06151dc9b5ab556f32fcd (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
/*******************************************************************************
 * Copyright (C) 2009, Tor Arne Vestbø <torarnv@gmail.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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Collections;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.egit.core.AdaptableFileTreeIterator;
import org.eclipse.egit.core.ContainerTreeIterator;
import org.eclipse.egit.core.op.ConnectProviderOperation;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.WorkingTreeIterator;
import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
import org.junit.Before;
import org.junit.Test;

public class AdaptableFileTreeIteratorTest extends GitTestCase {

	private Repository repository;

	private File file;

	@Override
	@Before
	public void setUp() throws Exception {
		super.setUp();

		repository = FileRepositoryBuilder.create(gitDir);
		repository.create();

		file = new File(project.getProject().getLocation().toFile(), "a.txt");
		final Writer fileWriter = new OutputStreamWriter(new FileOutputStream(
				file), "UTF-8");
		fileWriter.write("aaaaaaaaaaa");
		fileWriter.close();

		final ConnectProviderOperation operation = new ConnectProviderOperation(
				project.getProject(), gitDir);
		operation.execute(null);
	}

	@Test
	public void testFileTreeToContainerAdaptation() throws IOException {
		final IWorkspaceRoot root = project.getProject().getWorkspace()
				.getRoot();

		try (final TreeWalk treeWalk = new TreeWalk(repository)) {
			treeWalk.addTree(new AdaptableFileTreeIterator(repository, root));
			treeWalk.setRecursive(true);

			final IFile eclipseFile = project.getProject()
					.getFile(file.getName());
			final RepositoryMapping mapping = RepositoryMapping
					.getMapping(eclipseFile);
			final Set<String> repositoryPaths = Collections
					.singleton(mapping.getRepoRelativePath(eclipseFile));

			assertEquals(1, repositoryPaths.size());
			treeWalk.setFilter(
					PathFilterGroup.createFromStrings(repositoryPaths));

			assertTrue(treeWalk.next());

			final WorkingTreeIterator iterator = treeWalk.getTree(0,
					WorkingTreeIterator.class);
			assertTrue(iterator instanceof ContainerTreeIterator);
		}
	}
}

Back to the top