1 /*******************************************************************************
2  * Copyright (c) 2000, 2012 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.core.tests.internal.localstore;
15 
16 import org.eclipse.core.internal.resources.Workspace;
17 import org.eclipse.core.resources.*;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 
21 public class DeleteTest extends LocalStoreTest {
22 
testDeleteOpenProject()23 	public void testDeleteOpenProject() {
24 		IProject project = projects[0];
25 		IFolder folder = project.getFolder("folder");
26 		IFile file = folder.getFile("file");
27 
28 		/* ===========================================================
29 		 * project is OPEN, deleteContents=FALSE, force=TRUE
30 		 * =========================================================== */
31 
32 		/* create some resources */
33 		ensureExistsInWorkspace(new IResource[] {project, folder, file}, true);
34 		IPath folderPath = folder.getLocation();
35 		IPath filePath = file.getLocation();
36 		IPath projectLocation = project.getLocation();
37 
38 		/* delete */
39 		try {
40 			project.delete(false, true, getMonitor());
41 		} catch (CoreException e) {
42 			fail("1.0", e);
43 		}
44 
45 		/* assert project does not exist anymore in the workspace*/
46 		assertTrue("1.1", !project.exists());
47 		assertTrue("1.2", !((Workspace) getWorkspace()).getMetaArea().locationFor(project).toFile().exists());
48 		assertNull("1.3", project.getLocation());
49 
50 		/* assert resources still exist */
51 		assertTrue("1.4", folderPath.toFile().isDirectory());
52 		assertTrue("1.5", filePath.toFile().isFile());
53 
54 		/* remove trash */
55 		Workspace.clear(projectLocation.toFile());
56 
57 		/* ===========================================================
58 		 * project is OPEN, deleteContents=TRUE, force=TRUE
59 		 * 	- uses default default mapping
60 		 * ========================================================== */
61 
62 		/* initialize common objects */
63 		ensureExistsInWorkspace(new IResource[] {project, folder, file}, true);
64 		folderPath = folder.getLocation();
65 		filePath = file.getLocation();
66 		projectLocation = project.getLocation();
67 
68 		/* delete */
69 		try {
70 			project.delete(true, true, getMonitor());
71 		} catch (CoreException e) {
72 			fail("2.0", e);
73 		}
74 
75 		/* assert project does not exist anymore in the workspace */
76 		assertTrue("2.1", !project.exists());
77 		assertTrue("2.2", !((Workspace) getWorkspace()).getMetaArea().locationFor(project).toFile().exists());
78 		assertNull("2.3", project.getLocation());
79 
80 		/* assert resources do not exist anymore */
81 		assertTrue("2.4", !projectLocation.toFile().exists());
82 		assertTrue("2.5", !folderPath.toFile().exists());
83 		assertTrue("2.6", !filePath.toFile().exists());
84 
85 		/* ===========================================================
86 		 * project is OPEN, deleteContents=TRUE, force=TRUE
87 		 * 	- defines default mapping
88 		 * 	- does not create resources on disk
89 		 * =========================================================== */
90 
91 		/* initialize common objects */
92 		ensureExistsInWorkspace(project, true);
93 		ensureExistsInWorkspace(new IResource[] {folder, file}, false);
94 		folderPath = folder.getLocation();
95 		filePath = file.getLocation();
96 		projectLocation = project.getLocation();
97 
98 		/* delete */
99 		try {
100 			project.delete(true, true, getMonitor());
101 		} catch (CoreException e) {
102 			fail("3.0", e);
103 		}
104 
105 		/* assert project does not exist anymore */
106 		assertTrue("3.1", !project.exists());
107 		assertTrue("3.2", !((Workspace) getWorkspace()).getMetaArea().locationFor(project).toFile().exists());
108 		assertNull("3.3", project.getLocation());
109 
110 		/* assert resources do not exist anymore */
111 		assertTrue("3.4", !folderPath.toFile().isDirectory());
112 		assertTrue("3.5", !filePath.toFile().isFile());
113 
114 		/* ===========================================================
115 		 * project is OPEN, deleteContents=TRUE, force=true
116 		 * 	- create resources at default default area
117 		 * 	- defines default mapping
118 		 * =========================================================== */
119 
120 		/* initialize common objects */
121 		ensureExistsInWorkspace(new IResource[] {project, folder, file}, true);
122 		folderPath = folder.getLocation();
123 		filePath = file.getLocation();
124 
125 		/* delete */
126 		try {
127 			project.delete(true, true, getMonitor());
128 		} catch (CoreException e) {
129 			fail("4.0", e);
130 		}
131 
132 		/* assert project does not exist anymore */
133 		assertTrue("6.1", !project.exists());
134 		assertTrue("6.2", !((Workspace) getWorkspace()).getMetaArea().locationFor(project).toFile().exists());
135 		assertNull("6.3", project.getLocation());
136 
137 		/* assert resources do not still exist at default default area */
138 		assertTrue("6.4", !folderPath.toFile().exists());
139 		assertTrue("6.5", !filePath.toFile().exists());
140 
141 		/* remove trash */
142 		Workspace.clear(folderPath.toFile());
143 
144 		/* ===========================================================
145 		 * project is OPEN, deleteContents=TRUE, force=TRUE
146 		 * 	- defines default mapping
147 		 * 	- creates resources only on disk
148 		 * =========================================================== */
149 
150 		/* initialize common objects */
151 		ensureExistsInWorkspace(project, true);
152 		ensureExistsInFileSystem(new IResource[] {folder, file});
153 		folderPath = folder.getLocation();
154 		filePath = file.getLocation();
155 		projectLocation = project.getLocation();
156 
157 		/* delete */
158 		try {
159 			project.delete(true, true, getMonitor());
160 		} catch (CoreException e) {
161 			fail("7.0", e);
162 		}
163 
164 		/* assert project does not exist anymore */
165 		assertTrue("7.1", !project.exists());
166 		assertTrue("7.2", !((Workspace) getWorkspace()).getMetaArea().locationFor(project).toFile().exists());
167 		assertNull("7.3", project.getLocation());
168 
169 		/* assert resources do not exist anymore */
170 		assertTrue("7.4", !folderPath.toFile().isDirectory());
171 		assertTrue("7.5", !filePath.toFile().isFile());
172 
173 	}
174 
testDeleteClosedProject()175 	public void testDeleteClosedProject() throws Throwable {
176 
177 		IProject project = projects[0];
178 		IFolder folder = project.getFolder("folder");
179 		IFile file = folder.getFile("file");
180 
181 		/* ===========================================================
182 		 * project is CLOSED, deleteContents=FALSE, force=TRUE
183 		 * 	- resources exist in workspace but not on disk
184 		 * =========================================================== */
185 
186 		/* initialize common objects */
187 		ensureExistsInWorkspace(new IResource[] {project, folder}, true);
188 		ensureExistsInWorkspace(file, false);
189 		IPath folderPath = folder.getLocation();
190 		IPath filePath = file.getLocation();
191 		IPath projectLocation = project.getLocation();
192 
193 		/* close and delete */
194 		try {
195 			project.close(getMonitor());
196 		} catch (CoreException e) {
197 			fail("1.0", e);
198 		}
199 		try {
200 			project.delete(false, true, getMonitor());
201 		} catch (CoreException e) {
202 			fail("1.1", e);
203 		}
204 
205 		/* assert project does not exist anymore in the workspace */
206 		assertTrue("1.2", !project.exists());
207 		assertTrue("1.3", !((Workspace) getWorkspace()).getMetaArea().locationFor(project).toFile().exists());
208 		assertNull("1.4", project.getLocation());
209 
210 		/* assert resources still exist (if appropriate) */
211 		assertTrue("1.5", folderPath.toFile().exists());
212 		assertTrue("1.6", !filePath.toFile().exists());
213 
214 		/* remove trash */
215 		Workspace.clear(projectLocation.toFile());
216 
217 		/* ===========================================================
218 		 * project is CLOSED, deleteContents=TRUE, force=TRUE
219 		 * 	- uses default default mapping
220 		 * =========================================================== */
221 
222 		/* initialize common objects */
223 		ensureExistsInWorkspace(new IResource[] {project, folder, file}, true);
224 		folderPath = folder.getLocation();
225 		filePath = file.getLocation();
226 
227 		/* close and delete */
228 		try {
229 			project.close(getMonitor());
230 		} catch (CoreException e) {
231 			fail("2.0", e);
232 		}
233 		try {
234 			project.delete(true, true, getMonitor());
235 		} catch (CoreException e) {
236 			fail("2.1", e);
237 		}
238 
239 		/* assert project does not exist anymore */
240 		assertTrue("2.2", !project.exists());
241 		assertTrue("2.3", !((Workspace) getWorkspace()).getMetaArea().locationFor(project).toFile().exists());
242 		assertNull("2.4", project.getLocation());
243 
244 		/* assert resources do not exist anymore */
245 		assertTrue("2.5", !folderPath.toFile().isDirectory());
246 		assertTrue("2.6", !filePath.toFile().isFile());
247 
248 		/* ===========================================================
249 		 * project is CLOSED, deleteContents=TRUE, force = FALSE
250 		 * 	- uses default default mapping
251 		 * =========================================================== */
252 
253 		/* initialize common objects */
254 		ensureExistsInWorkspace(new IResource[] {project, folder}, true);
255 		ensureExistsInWorkspace(file, false);
256 		folderPath = folder.getLocation();
257 		filePath = file.getLocation();
258 		projectLocation = project.getLocation();
259 
260 		/* close and delete */
261 		try {
262 			projects[0].close(getMonitor());
263 		} catch (CoreException e) {
264 			fail("3.0", e);
265 		}
266 		try {
267 			projects[0].delete(true, false, getMonitor());
268 		} catch (CoreException e) {
269 			fail("3.1", e);
270 		}
271 
272 		/* assert project was deleted */
273 		assertTrue("3.2", !project.exists());
274 		IPath metaAreaLocation = ((Workspace) getWorkspace()).getMetaArea().locationFor(project);
275 		assertTrue("3.3", !metaAreaLocation.toFile().exists());
276 		assertTrue("3.4", !metaAreaLocation.append(".properties").toFile().exists());
277 		assertTrue("3.5", !projectLocation.append(IProjectDescription.DESCRIPTION_FILE_NAME).toFile().exists());
278 		assertNull("3.6", project.getLocation());
279 
280 		/* assert resources do not exist anymore */
281 		assertTrue("3.7", !folderPath.toFile().exists());
282 		assertTrue("3.8", !filePath.toFile().exists());
283 	}
284 
testDeleteResource()285 	public void testDeleteResource() throws Throwable {
286 		/* test's hierarchy
287 
288 		 P0
289 		 |
290 		 |-- folder
291 		 |
292 		 |-- fileSync
293 		 |
294 		 |-- fileUnsync
295 		 |
296 		 |-- fileCreated
297 		 |
298 		 |-- subfolderSync
299 		 |	|
300 		 |	|-- deletedfolderSync
301 		 |
302 		 |-- subfolderUnsync
303 		 |	|
304 		 |	|-- subsubfolderUnsync
305 		 |		|
306 		 |		|-- susubfileSync
307 		 |		|
308 		 |		|-- susubfileUnsync
309 
310 		 */
311 
312 		/* =================== */
313 		/* (1) force = TRUE    */
314 		/* =================== */
315 
316 		/* create some resources */
317 		IFolder folder = projects[0].getFolder("folder");
318 		ensureExistsInWorkspace(folder, true);
319 		IFile fileSync = folder.getFile("fileSync");
320 		ensureExistsInWorkspace(fileSync, true);
321 		IFile fileUnsync = folder.getFile("fileUnsync");
322 		ensureExistsInWorkspace(fileUnsync, true);
323 		IFile fileCreated = folder.getFile("fileCreated");
324 		ensureExistsInFileSystem(fileCreated); // create only in file system
325 		IFolder subfolderSync = folder.getFolder("subfolderSync");
326 		ensureExistsInWorkspace(subfolderSync, true);
327 		IFolder deletedfolderSync = subfolderSync.getFolder("deletedfolderSync");
328 		ensureExistsInWorkspace(deletedfolderSync, true);
329 		IFolder subfolderUnsync = folder.getFolder("subfolderUnsync");
330 		ensureExistsInWorkspace(subfolderUnsync, true);
331 		IFolder subsubfolderUnsync = subfolderUnsync.getFolder("subsubfolderUnsync");
332 		ensureExistsInWorkspace(subsubfolderUnsync, true);
333 		IFile subsubfileSync = subsubfolderUnsync.getFile("subsubfileSync");
334 		ensureExistsInWorkspace(subsubfileSync, true);
335 		IFile subsubfileUnsync = subsubfolderUnsync.getFile("subsubfileUnsync");
336 		ensureExistsInWorkspace(subsubfileUnsync, true);
337 
338 		/* make some resources "unsync" with the workspace */
339 		ensureOutOfSync(fileUnsync);
340 		ensureDoesNotExistInFileSystem(deletedfolderSync);
341 		ensureOutOfSync(subsubfileUnsync);
342 
343 		/* delete */
344 		folder.delete(true, null);
345 
346 		/* assert resources do not exist anymore */
347 		assertTrue("1.1", !folder.getLocation().toFile().exists());
348 
349 		/* =================== */
350 		/* (2) force = FALSE   */
351 		/* =================== */
352 
353 		/* create some resources */
354 		folder = projects[0].getFolder("folder");
355 		ensureExistsInWorkspace(folder, true);
356 		//
357 		fileSync = folder.getFile("fileSync");
358 		ensureExistsInWorkspace(fileSync, true);
359 		//
360 		fileUnsync = folder.getFile("fileUnsync");
361 		ensureExistsInWorkspace(fileUnsync, true);
362 		//
363 		fileCreated = folder.getFile("fileCreated");
364 		ensureExistsInFileSystem(fileCreated); // create only in file system
365 		//
366 		subfolderSync = folder.getFolder("subfolderSync");
367 		ensureExistsInWorkspace(subfolderSync, true);
368 		//
369 		deletedfolderSync = subfolderSync.getFolder("deletedfolderSync");
370 		ensureExistsInWorkspace(deletedfolderSync, true);
371 		//
372 		subfolderUnsync = folder.getFolder("subfolderUnsync");
373 		ensureExistsInWorkspace(subfolderUnsync, true);
374 		//
375 		subsubfolderUnsync = subfolderUnsync.getFolder("subsubfolderUnsync");
376 		ensureExistsInWorkspace(subsubfolderUnsync, true);
377 		//
378 		subsubfileSync = subsubfolderUnsync.getFile("subsubfileSync");
379 		ensureExistsInWorkspace(subsubfileSync, true);
380 		//
381 		subsubfileUnsync = subsubfolderUnsync.getFile("subsubfileUnsync");
382 		ensureExistsInWorkspace(subsubfileUnsync, true);
383 
384 		/* make some resources "unsync" with the workspace */
385 		ensureOutOfSync(fileUnsync);
386 		ensureDoesNotExistInFileSystem(deletedfolderSync);
387 		ensureOutOfSync(subsubfileUnsync);
388 
389 		/* delete */
390 		try {
391 			folder.delete(false, null);
392 			fail("2.0");
393 		} catch (CoreException e) {
394 			// expected
395 		}
396 
397 		/* assert resources do not exist anymore in the file system */
398 		assertTrue("2.1", folder.getLocation().toFile().exists());
399 		assertTrue("2.2", !fileSync.getLocation().toFile().exists());
400 		assertTrue("2.3", fileUnsync.getLocation().toFile().exists());
401 		assertTrue("2.4", !subfolderSync.getLocation().toFile().exists());
402 		assertTrue("2.5", subfolderUnsync.getLocation().toFile().exists());
403 		assertTrue("2.6", !deletedfolderSync.getLocation().toFile().exists());
404 		assertTrue("2.7", subsubfolderUnsync.getLocation().toFile().exists());
405 		assertTrue("2.8", subsubfileUnsync.getLocation().toFile().exists());
406 		assertTrue("2.9", !subsubfileSync.getLocation().toFile().exists());
407 		assertTrue("2.10", fileCreated.getLocation().toFile().exists());
408 	}
409 }
410