Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ac9e78a7d63e081952874e56cd14cc3f702367d5 (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
/*******************************************************************************
 *  Copyright (c) 2007 Oracle. 
 *  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
 *  
 *  Contributors: 
 *  	Oracle - initial API and implementation
 *******************************************************************************/
package org.eclipse.jpt.core.tests.internal;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.AssertionFailedException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
/**
 * Copied from org.eclipse.wst.common.tests
 */
public class ProjectUtility {
    public static IProject[] getAllProjects() {
    	IProject[] projects = new IProject[0];
    	try {
        projects =  ResourcesPlugin.getWorkspace().getRoot().getProjects();
    	} catch (AssertionFailedException ex) {
    		// Catch Malformed tree exception that occurs from time to time...
    	}
    	return projects;
    }
    public static void deleteAllProjects() throws Exception {
        //closing projects and tread work in here is a hack because of a BeanInfo bug holding
        //onto jars loaded in another VM
        
//        for (int i = 0; i < projects.length; i++) {
//            if (projects[i].exists()) {
//                projects[i].close(null); // This should signal the extra VM to kill itself
//            }
//        }
 //       Thread.yield(); // give the VM a chance to die
        IWorkspaceRunnable runnable = new IWorkspaceRunnable() {

			public void run(IProgressMonitor monitor) {
				IProject[] projects = getAllProjects();
				for (int i = 0; i < projects.length; i++) {
					IProject project = projects[i];
					boolean success = false;
					Exception lastException = null;
					// Don't make 2^12 is about 4 seconds which is the max we
					// will wait for the VM to die
					for (int j = 0; j < 13 && !success; j++) {
						try {
							if (project.exists()) {
								project.delete(true, true, null);
								ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);
							}
							success = true;
						} catch (Exception e) {
							lastException = e;
							if (project.exists()) {
								try {
									project.close(null);
									project.open(null);
								} catch (Exception e2) {
									// do nothing
								}
							}
							try {
								Thread.sleep((int) Math.pow(2, j));
							} catch (InterruptedException e1) {
								// do nothing
							} // if the VM isn't dead, try sleeping
						}
					}
					if (!success && lastException != null) {
						//Logger.getLogger().log("Problem while deleting: " + lastException.getMessage());
//						 Assert.fail("Caught Exception=" +
//						 lastException.getMessage() + " when deleting project=" + project.getName());
					}
				}
			}
		};
		try {
			ResourcesPlugin.getWorkspace().run(runnable, null);
		} catch (CoreException ce) {
			// do nothing
		}
        //verifyNoProjects();
    }
}

Back to the top