Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0389a70c64f8dff3ff20a43edf7e45b70fde4c85 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*******************************************************************************
 * Copyright (C) 2012, Robin Stocker <robin@nibor.org>
 *
 * 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.eclipse.core.resources.IResourceFilterDescription.EXCLUDE_ALL;
import static org.eclipse.core.resources.IResourceFilterDescription.FILES;
import static org.eclipse.core.resources.IResourceFilterDescription.FOLDERS;
import static org.eclipse.core.resources.IResourceFilterDescription.INHERITABLE;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.FileInfoMatcherDescription;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.egit.core.AdaptableFileTreeIterator;
import org.eclipse.egit.core.ContainerTreeIterator;
import org.eclipse.egit.core.op.ConnectProviderOperation;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.treewalk.AbstractTreeIterator;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.junit.Before;
import org.junit.Test;

/**
 * Test for how {@link ContainerTreeIterator} handles filtered resources.
 * <p>
 * The tricky thing is that they are not returned from API like
 * {@link IContainer#members()}. So we have to fall back to using an
 * {@link AdaptableFileTreeIterator} if there may be resource filters active.
 * <p>
 * In case of nested projects where the subproject is filtered in the parent
 * project with resource filters, we want the nested project to be walked with
 * {@link ContainerTreeIterator} again. That is, it should "recover" from
 * falling back to {@link AdaptableFileTreeIterator}.
 */
public class ContainerTreeIteratorResourceFilterTest extends GitTestCase {

	private Repository repository;

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

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

		connectProject(project.getProject());
	}

	@Test
	public void simpleNonInheritableFilter() throws Exception {
		IProject p = project.getProject();
		IFile filtered = testUtils.addFileToProject(p, "filtered.txt", "");
		IFile unfiltered = testUtils.addFileToProject(p, "unfiltered.txt", "");
		assertTrue("IFile should exist before filtering.", filtered.exists());
		assertTrue("IFile should exist before filtering.", unfiltered.exists());

		createFilter(p, EXCLUDE_ALL | FILES, "filtered.txt");
		assertFalse("IFile should no longer exist after filtering.", filtered.exists());
		assertTrue("IFile should exist after filtering.", unfiltered.exists());

		List<Entry> entries = walkTree();

		assertThat(entries, hasItem(containerTreeEntry("Project-1/filtered.txt")));
		assertThat(entries, hasItem(containerTreeEntry("Project-1/unfiltered.txt")));
	}

	@Test
	public void simpleNonInheritableFolderFilter() throws Exception {
		IProject p = project.getProject();
		IFile filtered = testUtils.addFileToProject(p, "folder/filtered.txt", "");
		IFile unfiltered = testUtils.addFileToProject(p, "folder2/unfiltered.txt", "");

		createFilter(p, EXCLUDE_ALL | FOLDERS, "folder");
		assertFalse("IFile should no longer exist after filtering.", filtered.exists());
		assertTrue("IFile should exist after filtering.", unfiltered.exists());

		List<Entry> entries = walkTree();

		assertThat(entries, hasItem(adaptableFileTreeEntry("Project-1/folder/filtered.txt")));
		assertThat(entries, hasItem(containerTreeEntry("Project-1/folder2/unfiltered.txt")));
	}

	@Test
	public void inheritableFilter() throws Exception {
		IProject p = project.getProject();
		IFile filtered1 = testUtils.addFileToProject(p, "folder1/filtered.txt", "");
		IFile filtered2 = testUtils.addFileToProject(p, "folder1/folder2/filtered.txt", "");
		IFile unfiltered = testUtils.addFileToProject(p, "folder1/folder2/unfiltered.txt", "");

		createFilter(p, EXCLUDE_ALL | FILES | INHERITABLE, "filtered.txt");
		assertFalse("IFile should no longer exist after filtering.", filtered1.exists());
		assertFalse("IFile should no longer exist after filtering.", filtered2.exists());
		assertTrue("IFile should exist after filtering.", unfiltered.exists());

		List<Entry> entries = walkTree();

		assertThat(entries, hasItem(containerTreeEntry("Project-1/folder1/filtered.txt")));
		assertThat(entries, hasItem(containerTreeEntry("Project-1/folder1/folder2/filtered.txt")));
		assertThat(entries, hasItem(containerTreeEntry("Project-1/folder1/folder2/unfiltered.txt")));
	}

	@Test
	public void directlyNestedProject() throws Exception {
		IProject p = project.getProject();
		testUtils.addFileToProject(p, "file.txt", "");

		TestProject testProject2 = new TestProject(true, "Project-1/Project-2");
		testUtils.addFileToProject(testProject2.getProject(), "project2.txt", "");

		createFilter(p, EXCLUDE_ALL | FOLDERS, "Project-2");

		List<Entry> entries = walkTree();

		assertThat(entries, hasItem(containerTreeEntry("Project-1/file.txt")));
		// Should be handled by container tree iterator because it exists, even
		// when it's not returned by members() of Project-1.
		assertThat(entries, hasItem(containerTreeEntry("Project-1/Project-2/project2.txt")));
	}

	@Test
	public void nestedProject() throws Exception {
		IProject p = project.getProject();
		testUtils.addFileToProject(p, "folder1/file.txt", "");
		testUtils.addFileToProject(p, "folder1/subfolder/filtered.txt", "");

		TestProject testProject2 = new TestProject(true, "Project-1/folder1/subfolder/Project-2");
		connectProject(testProject2.getProject());
		IFile project2File = testUtils.addFileToProject(testProject2.getProject(), "project2.txt", "");
		assertThat(project2File.getProject(), is(testProject2.getProject()));

		createFilter(p.getFolder("folder1"), EXCLUDE_ALL | FOLDERS, "subfolder");
		assertFalse("IFolder should be filtered",
				p.getFolder(new Path("folder1/subfolder")).exists());

		List<Entry> entries = walkTree();

		assertThat(entries, hasItem(containerTreeEntry("Project-1/folder1/file.txt")));
		assertThat(entries, hasItem(adaptableFileTreeEntry("Project-1/folder1/subfolder/filtered.txt")));
		// Should be handled by container tree iterator again, because the project exists.
		assertThat(entries, hasItem(containerTreeEntry("Project-1/folder1/subfolder/Project-2/project2.txt")));

		testProject2.dispose();
	}

	private static void createFilter(IContainer container, int type, String regexFilterArguments) throws CoreException {
		FileInfoMatcherDescription matcherDescription = new FileInfoMatcherDescription(
				"org.eclipse.core.resources.regexFilterMatcher",
				regexFilterArguments);
		container.createFilter(type, matcherDescription, 0, null);
	}

	private void connectProject(IProject p) throws CoreException {
		final ConnectProviderOperation operation = new ConnectProviderOperation(
				p, gitDir);
		operation.execute(null);
	}

	private List<Entry> walkTree() throws IOException {
		try (TreeWalk treeWalk = new TreeWalk(repository)) {
			ContainerTreeIterator tree = new ContainerTreeIterator(repository,
					project.getProject());
			int treeIndex = treeWalk.addTree(tree);
			treeWalk.setRecursive(true);
			List<Entry> entries = new ArrayList<Entry>();
			while (treeWalk.next()) {
				AbstractTreeIterator it = treeWalk.getTree(treeIndex,
						AbstractTreeIterator.class);
				Entry entry = new Entry(treeWalk.getPathString(),
						it.getClass());
				entries.add(entry);
			}
			return entries;
		}
	}

	private static Entry containerTreeEntry(String path) {
		return new Entry(path, ContainerTreeIterator.class);
	}

	private static Entry adaptableFileTreeEntry(String path) {
		return new Entry(path, AdaptableFileTreeIterator.class);
	}

	// Value object (case class).
	private static class Entry {
		private final String path;
		private Class<? extends AbstractTreeIterator> iteratorClass;

		public Entry(String path, Class<? extends AbstractTreeIterator> iteratorClass) {
			this.path = path;
			this.iteratorClass = iteratorClass;
		}

		@Override
		public String toString() {
			return path + " (" + iteratorClass.getSimpleName() + ")";
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result
					+ ((iteratorClass == null) ? 0 : iteratorClass.hashCode());
			result = prime * result + ((path == null) ? 0 : path.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Entry other = (Entry) obj;
			if (iteratorClass == null) {
				if (other.iteratorClass != null)
					return false;
			} else if (!iteratorClass.equals(other.iteratorClass))
				return false;
			if (path == null) {
				if (other.path != null)
					return false;
			} else if (!path.equals(other.path))
				return false;
			return true;
		}
	}
}

Back to the top