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
|
/*******************************************************************************
* 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 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.egit.core.test.op;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.JobFamilies;
import org.eclipse.egit.core.internal.indexdiff.IndexDiffCache;
import org.eclipse.egit.core.internal.indexdiff.IndexDiffCacheEntry;
import org.eclipse.egit.core.op.DeletePathsOperation;
import org.eclipse.egit.core.test.DualRepositoryTestCase;
import org.eclipse.egit.core.test.JobSchedulingAssert;
import org.eclipse.egit.core.test.TestRepository;
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 DeletePathsOperationTest extends DualRepositoryTestCase {
File workdir;
IProject project;
String projectName = "DeletePathsOperationTest";
@Before
public void setUp() throws Exception {
workdir = testUtils.createTempDir("Repository1");
repository1 = new TestRepository(new File(workdir, Constants.DOT_GIT));
project = testUtils.createProjectInLocalFileSystem(workdir, projectName);
repository1.connect(project);
}
@After
public void tearDown() throws Exception {
Job.getJobManager().join(JobFamilies.INDEX_DIFF_CACHE_UPDATE, null);
project.close(null);
project.delete(false, false, null);
repository1.dispose();
repository1 = null;
testUtils.deleteTempDirs();
}
@Test
public void testDeleteResourceOfProject() throws Exception {
IResource resource = testUtils.addFileToProject(project, "file.txt", "Hello world 1");
deletePaths(Arrays.asList(resource.getLocation()));
File file = resource.getFullPath().toFile();
assertFalse("File should have been deleted", file.exists());
}
@Test
public void testDeleteResourceOutsideOfProject() throws Exception {
File outsideOfProject = new File(workdir, "outside-of-project.txt");
FileUtils.createNewFile(outsideOfProject);
IPath path = new Path(outsideOfProject.getAbsolutePath());
// Make sure the cache has at least be refreshed once, otherwise the
// assertion at the end is not effective
initIndexDiffCache(repository1.getRepository());
JobSchedulingAssert jobSchedulingAssertion = JobSchedulingAssert
.forFamily(JobFamilies.INDEX_DIFF_CACHE_UPDATE);
deletePaths(Arrays.asList(path));
assertFalse("File should have been deleted", outsideOfProject.exists());
jobSchedulingAssertion
.assertScheduled("Delete of file outside of workspace should have cause an index diff cache job.");
}
private static void initIndexDiffCache(Repository repository)
throws Exception {
IndexDiffCache cache = Activator.getDefault().getIndexDiffCache();
IndexDiffCacheEntry cacheEntry = cache.getIndexDiffCacheEntry(repository);
assertNotNull(cacheEntry);
Job.getJobManager().join(JobFamilies.INDEX_DIFF_CACHE_UPDATE, null);
}
private void deletePaths(Collection<IPath> paths) throws CoreException {
DeletePathsOperation operation = new DeletePathsOperation(paths);
operation.execute(null);
}
}
|