Bug 464369 - Fixing tests in IncrementalTests and MultiProjectTests

Calling waitForManualRefresh and waitForAutoBuild to ensure delta
processor and build has enough time to complete.

Change-Id: I2f03af2ebbe2bf38acce849138bfb64c2d97b7d8
diff --git a/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/BuildpathTests.java b/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/BuildpathTests.java
index 9f6716f..a19de48 100644
--- a/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/BuildpathTests.java
+++ b/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/BuildpathTests.java
@@ -39,8 +39,7 @@
 }
 
 public static Test suite() {
-//	return buildTestSuite(BuildpathTests.class);
-	return buildTestSuite(BuildpathTests.class, BYTECODE_DECLARATION_ORDER);
+	return buildTestSuite(BuildpathTests.class);
 }
 @Override
 protected void setUp() throws Exception {
@@ -179,8 +178,9 @@
 	options.put(JavaCore.CORE_JAVA_BUILD_INVALID_CLASSPATH, JavaCore.IGNORE);
 	JavaCore.setOptions(options);
 	env.closeProject(project1Path);
-
+	env.waitForManualRefresh();
 	incrementalBuild();
+	env.waitForAutoBuild();
 	expectingOnlyProblemsFor(new IPath[] {project2Path, project3Path});
 	expectingOnlySpecificProblemFor(project2Path,
 		new Problem("Build path", "Project 'CP2' is missing required Java project: 'CP1'", project2Path, -1, -1, CategorizedProblem.CAT_BUILDPATH, IMarker.SEVERITY_ERROR) //$NON-NLS-1$ //$NON-NLS-2$
@@ -230,8 +230,9 @@
 	JavaCore.setOptions(options);
 
 	env.removeBinaryClass(outputFolderPackage, "Test"); //$NON-NLS-1$
-
+	env.waitForManualRefresh();
 	incrementalBuild();
+	env.waitForAutoBuild();
 	expectingNoProblems();
 	env.removeProject(project1Path);
 }
@@ -249,6 +250,7 @@
 	);
 
 	fullBuild();
+	env.waitForAutoBuild();
 	expectingNoProblems();
 
 	IPath outputFolderPackage = bin.append("p"); //$NON-NLS-1$
@@ -260,6 +262,7 @@
 	);
 
 	incrementalBuild();
+	env.waitForAutoBuild();
 	expectingOnlySpecificProblemFor(subTest, new Problem("", "p.Test cannot be resolved to a type", subTest, 40, 46, CategorizedProblem.CAT_TYPE, IMarker.SEVERITY_ERROR)); //$NON-NLS-1$ //$NON-NLS-2$)
 
 	env.addClass(src, "p", "Test", //$NON-NLS-1$ //$NON-NLS-2$
@@ -268,6 +271,7 @@
 	);
 
 	fullBuild();
+	env.waitForAutoBuild();
 	expectingNoProblems();
 
 	Hashtable options = JavaCore.getOptions();
@@ -275,7 +279,7 @@
 	JavaCore.setOptions(options);
 
 	env.removeBinaryClass(outputFolderPackage, "Test"); //$NON-NLS-1$
-
+	env.waitForManualRefresh();
 	incrementalBuild();
 	expectingNoProblems();
 	env.removeProject(project1Path);
@@ -318,6 +322,7 @@
 		);
 
 		fullBuild(projectPath);
+		env.waitForAutoBuild();
 		expectingNoProblems();
 
 		String externalClassFile = externalLib + File.separator + "p" + File.separator + "X.class";
@@ -342,6 +347,7 @@
 		env.waitForManualRefresh();
 
 		incrementalBuild(projectPath);
+		env.waitForAutoBuild();
 		expectingProblemsFor(
 			classY,
 			"Problem : The method foo() is undefined for the type X [ resource : </Project/q/Y.java> range : <54,57> category : <50> severity : <2>]"
@@ -665,6 +671,7 @@
 	);
 
 	fullBuild();
+	env.waitForAutoBuild();
 	expectingSpecificProblemFor(
 		projectPath,
 		new Problem("", "The project was not built since its build path is incomplete. Cannot find the class file for java.lang.Object. Fix the build path then try building this project", projectPath, -1, -1, CategorizedProblem.CAT_BUILDPATH, IMarker.SEVERITY_ERROR)); //$NON-NLS-1$ //$NON-NLS-2$
@@ -687,6 +694,7 @@
 	env.addExternalJars(projectPath, Util.getJavaClassLibs());
 
 	incrementalBuild();
+	env.waitForAutoBuild();
 	expectingNoProblems();
 	expectingPresenceOf(new IPath[]{
 		bin.append("p2").append("Test1.class"), //$NON-NLS-1$ //$NON-NLS-2$
@@ -704,19 +712,23 @@
 	fullBuild();
 	expectingNoProblems();
 	project.setOption(JavaCore.CORE_INCOMPLETE_CLASSPATH, CompilerOptions.WARNING);
+	env.waitForManualRefresh();
 	env.addLibrary(projectPath, projectPath.append("/lib/dummy.jar"), null, null);
 	fullBuild();
+	env.waitForAutoBuild();
 	expectingSpecificProblemFor(
 		projectPath,
 		new Problem("Build path", "Project 'Project' is missing required library: 'lib/dummy.jar'", projectPath, -1, -1, CategorizedProblem.CAT_BUILDPATH,
 				IMarker.SEVERITY_WARNING));
 	project.setOption(JavaCore.CORE_INCOMPLETE_CLASSPATH, CompilerOptions.ERROR);
+	env.waitForManualRefresh();
 	// force classpath change delta - should not have to do this
 	IClasspathEntry[] classpath = project.getRawClasspath();
 	IPath outputLocation;
 	project.setRawClasspath(null, outputLocation = project.getOutputLocation(), false, null);
 	project.setRawClasspath(classpath, outputLocation, false, null);
 	fullBuild();
+	env.waitForAutoBuild();
 	expectingSpecificProblemFor(
 		projectPath,
 		new Problem("", "The project cannot be built until build path errors are resolved", projectPath, -1, -1, CategorizedProblem.CAT_BUILDPATH, IMarker.SEVERITY_ERROR));
@@ -734,7 +746,9 @@
 	fullBuild();
 	expectingNoProblems();
 	env.addLibrary(projectPath, projectPath.append("/lib/dummy.jar"), null, null);
+	env.waitForManualRefresh();
 	fullBuild();
+	env.waitForAutoBuild();
 	expectingSpecificProblemFor(
 		projectPath,
 		new Problem("", "The project cannot be built until build path errors are resolved", projectPath, -1, -1, CategorizedProblem.CAT_BUILDPATH, IMarker.SEVERITY_ERROR));
@@ -742,7 +756,9 @@
 		projectPath,
 		new Problem("Build path", "Project 'Project' is missing required library: 'lib/dummy.jar'", projectPath, -1, -1, CategorizedProblem.CAT_BUILDPATH, IMarker.SEVERITY_ERROR));
 	project.setOption(JavaCore.CORE_INCOMPLETE_CLASSPATH, CompilerOptions.WARNING);
+	env.waitForManualRefresh();
 	incrementalBuild();
+	env.waitForManualRefresh();
 	expectingSpecificProblemFor(
 		projectPath,
 		new Problem("Build path", "Project 'Project' is missing required library: 'lib/dummy.jar'", projectPath, -1, -1, CategorizedProblem.CAT_BUILDPATH,
@@ -768,7 +784,9 @@
 
 	// Change project incompatible jdk level preferences to warning, perform incremental build and expect 1 problem
 	project.setOption(JavaCore.CORE_INCOMPATIBLE_JDK_LEVEL, CompilerOptions.WARNING);
+	env.waitForManualRefresh();
 	incrementalBuild();
+	env.waitForAutoBuild();
 	long projectRuntimeJDKLevel = CompilerOptions.versionToJdkLevel(projectRuntime);
 	int max = classlibs.length;
 	List expectedProblems = new ArrayList();
@@ -784,7 +802,9 @@
 
 	// Change project incompatible jdk level preferences to error, perform incremental build and expect 2 problems
 	project.setOption(JavaCore.CORE_INCOMPATIBLE_JDK_LEVEL, CompilerOptions.ERROR);
+	env.waitForManualRefresh();
 	incrementalBuild();
+	env.waitForAutoBuild();
 
 	expectedProblems = new ArrayList();
 	for (int i = 0; i < max; i++) {
@@ -819,6 +839,7 @@
 
 		// Build it expecting no problem
 		fullBuild();
+		env.waitForAutoBuild();
 		expectingNoProblems();
 
 		// Build incompatible jdk level problem string
@@ -828,8 +849,9 @@
 		Arrays.sort(classlibs);
 		// Change workspace  incompatible jdk level preferences to warning, perform incremental build and expect 1 problem
 		preferences.put(JavaCore.CORE_INCOMPATIBLE_JDK_LEVEL, JavaCore.WARNING);
+		env.waitForManualRefresh();
 		incrementalBuild();
-
+		env.waitForAutoBuild();
 		List expectedProblems = new ArrayList();
 		int max = classlibs.length;
 		for (int i = 0; i < max; i++) {
@@ -844,8 +866,9 @@
 
 		// Change workspace incompatible jdk level preferences to error, perform incremental build and expect 2 problems
 		preferences.put(JavaCore.CORE_INCOMPATIBLE_JDK_LEVEL, JavaCore.ERROR);
+		env.waitForManualRefresh();
 		incrementalBuild();
-
+		env.waitForAutoBuild();
 		expectedProblems = new ArrayList();
 		for (int i = 0; i < max; i++) {
 			String path = project.getPackageFragmentRoot(classlibs[i]).getPath().makeRelative().toString();
@@ -887,6 +910,7 @@
 	env.removeProject(project1Path);
 
 	incrementalBuild();
+	env.waitForAutoBuild();
 	expectingOnlyProblemsFor(project2Path);
 	expectingOnlySpecificProblemsFor(project2Path,
 		new Problem[] {
@@ -899,6 +923,7 @@
 	env.addExternalJars(project1Path, Util.getJavaClassLibs());
 
 	incrementalBuild();
+	env.waitForAutoBuild();
 	expectingNoProblems();
 
 	//----------------------------
@@ -907,9 +932,11 @@
 	Hashtable options = JavaCore.getOptions();
 	options.put(JavaCore.CORE_JAVA_BUILD_INVALID_CLASSPATH, JavaCore.IGNORE);
 	JavaCore.setOptions(options);
+	env.waitForManualRefresh();
 	env.removeProject(project1Path);
 
 	incrementalBuild();
+	env.waitForAutoBuild();
 	expectingOnlyProblemsFor(project2Path);
 	expectingOnlySpecificProblemFor(project2Path,
 		new Problem("Build path", "Project 'MP2' is missing required Java project: 'MP1'", project2Path, -1, -1, CategorizedProblem.CAT_BUILDPATH, IMarker.SEVERITY_ERROR) //$NON-NLS-1$ //$NON-NLS-2$
@@ -919,6 +946,7 @@
 	env.addExternalJars(project1Path, Util.getJavaClassLibs());
 
 	incrementalBuild();
+	env.waitForAutoBuild();
 	expectingNoProblems();
 	env.removeProject(project1Path);
 	env.removeProject(project2Path);
@@ -955,9 +983,11 @@
 	Hashtable options = JavaCore.getOptions();
 	options.put(JavaCore.CORE_JAVA_BUILD_INVALID_CLASSPATH, JavaCore.IGNORE);
 	JavaCore.setOptions(options);
+	env.waitForManualRefresh();
 	env.removeProject(project1Path);
 
 	incrementalBuild();
+	env.waitForAutoBuild();
 	expectingNoProblems();
 
 	project1Path = env.addProject("MP1"); //$NON-NLS-1$
diff --git a/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/IncrementalTests.java b/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/IncrementalTests.java
index ce19400..a16c359 100644
--- a/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/IncrementalTests.java
+++ b/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/IncrementalTests.java
@@ -69,6 +69,7 @@
 			"Problem : The type CC is already defined [ resource : </Project/src/p/D.java> range : <37,39> category : <-1> severity : <2>]"
 		);
 		expectingSpecificProblemsFor(pathToD, new Problem[] {new Problem("", "The type CC is already defined", pathToD, 37, 39, -1, IMarker.SEVERITY_ERROR)});
+		env.removeProject(projectPath);
 	}
 
 	public void testDefaultPackage() throws JavaModelException {
@@ -90,6 +91,7 @@
 
 		incrementalBuild(projectPath);
 		expectingNoProblems();
+		env.removeProject(projectPath);
 	}
 
 	public void testDefaultPackage2() throws JavaModelException {
@@ -111,6 +113,7 @@
 
 		incrementalBuild(projectPath);
 		expectingNoProblems();
+		env.removeProject(projectPath);
 	}
 
 	public void testNewJCL() {
@@ -148,6 +151,7 @@
 
 		incrementalBuild();
 		expectingNoProblems();
+		env.removeProject(projectPath);
 	}
 
 	/*
@@ -208,6 +212,7 @@
 
 		incrementalBuild(projectPath);
 		expectingNoProblems();
+		env.removeProject(projectPath);
 	}
 
 	/*
@@ -278,6 +283,7 @@
 
 		incrementalBuild(projectPath);
 		expectingNoProblems();
+		env.removeProject(projectPath);
 	}
 
 	/*
@@ -348,6 +354,7 @@
 
 		incrementalBuild(projectPath);
 		expectingNoProblems();
+		env.removeProject(projectPath);
 	}
 
 	public void testMoveSecondaryType() throws JavaModelException {
@@ -401,6 +408,7 @@
 
 		incrementalBuild(projectPath);
 		expectingNoProblems();
+		env.removeProject(projectPath);
 	}
 
 	public void testMoveMemberType() throws JavaModelException {
@@ -471,6 +479,7 @@
 			new Problem[]{
 				new Problem("", "The import p.ZA is never used", new Path("/Project/src/p/AB.java"), 35, 39, CategorizedProblem.CAT_UNNECESSARY_CODE, IMarker.SEVERITY_WARNING), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 			});
+		env.removeProject(projectPath);
 	}
 
 	public void testMovePackage() throws JavaModelException {
@@ -496,6 +505,7 @@
 
 		incrementalBuild(projectPath);
 		expectingNoProblems();
+		env.removeProject(projectPath);
 	}
 
 	public void testMovePackage2() throws JavaModelException {
@@ -533,6 +543,7 @@
 
 		incrementalBuild(projectPath);
 		expectingNoProblems();
+		env.removeProject(projectPath);
 	}
 
 	public void testMemberTypeFromClassFile() throws JavaModelException {
@@ -585,6 +596,7 @@
 		incrementalBuild(projectPath);
 		org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.MAX_AT_ONCE = previous;
 		expectingNoProblems();
+		env.removeProject(projectPath);
 	}
 
 	//https://bugs.eclipse.org/bugs/show_bug.cgi?id=372418
@@ -645,6 +657,7 @@
 		fullBuild(projectPath1);
 		org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.MAX_AT_ONCE = previous;
 		expectingNoProblems();
+		env.removeProject(projectPath1);
 	}
 
 	//https://bugs.eclipse.org/bugs/show_bug.cgi?id=377401
@@ -701,6 +714,7 @@
 		fullBuild(projectPath1);
 		org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.MAX_AT_ONCE = previous;
 		expectingNoProblems();
+		env.removeProject(projectPath1);
 	}
 
 	// http://dev.eclipse.org/bugs/show_bug.cgi?id=27658
@@ -751,6 +765,7 @@
 				new Problem[]{
 					new Problem("", "The type java.lang.Object cannot have a superclass or superinterfaces", new Path("/Project/src/java/lang/Object.java"), 33, 39, CategorizedProblem.CAT_INTERNAL, IMarker.SEVERITY_ERROR), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 				});
+			env.removeProject(projectPath);
 
 		} catch(StackOverflowError e){
 			assertTrue("Infinite loop in cycle detection", false); //$NON-NLS-1$
@@ -793,6 +808,7 @@
 		incrementalBuild();
 		expectingProblemsFor(x, "???");
 		expectingNoPresenceOf(bin.append("X.class")); //$NON-NLS-1$
+		env.removeProject(projectPath);
 	}
 
 	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=100631
@@ -837,6 +853,7 @@
 
 			incrementalBuild(projectPath);
 			expectingNoProblems();
+			env.removeProject(projectPath);
 		} finally {
 			org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.MAX_AT_ONCE = max;
 		}
@@ -898,6 +915,7 @@
 
 			incrementalBuild(projectPath);
 			expectingNoProblems();
+			env.removeProject(projectPath);
 		} finally {
 			org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.MAX_AT_ONCE = max;
 		}
@@ -945,6 +963,7 @@
 		incrementalBuild(projectPath);
 		expectingSpecificProblemFor(yPath, new Problem("Y", "Zork cannot be resolved to a type", yPath, 34, 38, CategorizedProblem.CAT_TYPE, IMarker.SEVERITY_ERROR)); //$NON-NLS-1$ //$NON-NLS-2$
 		expectingNoProblemsFor(xPath);
+		env.removeProject(projectPath);
 	}
 
 	public void testSecondaryType() throws JavaModelException {
@@ -972,6 +991,7 @@
 			org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.MAX_AT_ONCE = max;
 		}
 		expectingNoProblems();
+		env.removeProject(projectPath);
 	}
 
 	// http://dev.eclipse.org/bugs/show_bug.cgi?id=196200 - variation
@@ -1013,6 +1033,7 @@
 			);
 		incrementalBuild(projectPath);
 		expectingSpecificProblemFor(xPath, new Problem("X", "This method has a constructor name", xPath, 73, 76, CategorizedProblem.CAT_CODE_STYLE, IMarker.SEVERITY_WARNING)); //$NON-NLS-1$ //$NON-NLS-2$
+		env.removeProject(projectPath);
 	}
 
 	// http://dev.eclipse.org/bugs/show_bug.cgi?id=196200 - variation
@@ -1057,6 +1078,7 @@
 			);
 		incrementalBuild(projectPath);
 		expectingSpecificProblemFor(xPath, new Problem("X", "This method has a constructor name", xPath, 73, 76, CategorizedProblem.CAT_CODE_STYLE, IMarker.SEVERITY_WARNING)); //$NON-NLS-1$ //$NON-NLS-2$
+		env.removeProject(projectPath);
 	}
 
 	// http://dev.eclipse.org/bugs/show_bug.cgi?id=196200 - variation
@@ -1101,6 +1123,7 @@
 			);
 		incrementalBuild(projectPath);
 		expectingSpecificProblemFor(xPath, new Problem("X", "This method has a constructor name", xPath, 73, 76, CategorizedProblem.CAT_CODE_STYLE, IMarker.SEVERITY_WARNING)); //$NON-NLS-1$ //$NON-NLS-2$
+		env.removeProject(projectPath);
 	}
 	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=334377
 	public void testBug334377() throws JavaModelException {
@@ -1165,6 +1188,7 @@
 
 			incrementalBuild(projectPath);
 			expectingNoProblems();
+			env.removeProject(projectPath);
 		} finally {
 			org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.MAX_AT_ONCE = max;
 			JavaCore.setOptions(options);
@@ -1189,8 +1213,9 @@
 			"import w.I;\n" +
 			"import w.W;\n" +
 			"public class A {}"); //$NON-NLS-1$
-
+		env.waitForManualRefresh();
 		fullBuild(projectPath);
+		env.waitForAutoBuild();
 		expectingSpecificProblemFor(wPath, new Problem("W", "w.I cannot be resolved to a type", wPath, 37, 40, CategorizedProblem.CAT_TYPE, IMarker.SEVERITY_ERROR)); //$NON-NLS-1$ //$NON-NLS-2$
 		expectingSpecificProblemFor(aPath, new Problem("A", "The import w.I cannot be resolved", aPath, 18, 21, CategorizedProblem.CAT_IMPORT, IMarker.SEVERITY_ERROR)); //$NON-NLS-1$ //$NON-NLS-2$
 
@@ -1200,7 +1225,10 @@
 				"import w.W;\n" +
 				"public class A {}"); //$NON-NLS-1$
 
+		env.waitForManualRefresh();
 		incrementalBuild(projectPath);
+		env.waitForAutoBuild();
 		expectingSpecificProblemFor(aPath, new Problem("A", "The import w.I cannot be resolved", aPath, 18, 21, CategorizedProblem.CAT_IMPORT, IMarker.SEVERITY_ERROR)); //$NON-NLS-1$ //$NON-NLS-2$
+		env.removeProject(projectPath);
 	}
 }
diff --git a/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/MultiProjectTests.java b/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/MultiProjectTests.java
index 504bd1a..3ca5817 100644
--- a/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/MultiProjectTests.java
+++ b/org.eclipse.jdt.core.tests.builder/src/org/eclipse/jdt/core/tests/builder/MultiProjectTests.java
@@ -75,8 +75,9 @@
 			"public class C {\n"+ //$NON-NLS-1$
 			"}\n" //$NON-NLS-1$
 			);
-
+		env.waitForManualRefresh();
 		fullBuild();
+		env.waitForAutoBuild();
 		expectingNoProblems();
 
 		//----------------------------
@@ -87,9 +88,13 @@
 			"   int x;\n"+ //$NON-NLS-1$
 			"}\n" //$NON-NLS-1$
 			);
-
+		env.waitForManualRefresh();
 		incrementalBuild();
+		env.waitForAutoBuild();
 		expectingCompiledClasses(new String[]{"A", "B"}); //$NON-NLS-1$ //$NON-NLS-2$
+		env.removeProject(project1Path);
+		env.removeProject(project2Path);
+		env.removeProject(project3Path);
 	}
 
 	// 14103 - avoid recompiling unaffected sources in dependent projects
@@ -135,7 +140,9 @@
 			"}\n" //$NON-NLS-1$
 			);
 
+		env.waitForManualRefresh();
 		fullBuild();
+		env.waitForAutoBuild();
 		expectingNoProblems();
 
 		//----------------------------
@@ -153,9 +160,13 @@
 			"   int x; //structural change\n"+ //$NON-NLS-1$
 			"}\n" //$NON-NLS-1$
 			);
-
+		env.waitForManualRefresh();
 		incrementalBuild();
+		env.waitForAutoBuild();
 		expectingCompiledClasses(new String[]{"A", "Unreferenced"}); //$NON-NLS-1$ //$NON-NLS-2$
+		env.removeProject(project1Path);
+		env.removeProject(project2Path);
+		env.removeProject(project3Path);
 	}
 
 	public void testRemoveField() throws JavaModelException {
@@ -192,8 +203,9 @@
 			"   }\n"+ //$NON-NLS-1$
 			"}\n" //$NON-NLS-1$
 			);
-
+		env.waitForManualRefresh();
 		fullBuild();
+		env.waitForAutoBuild();
 		expectingNoProblems();
 
 		//----------------------------
@@ -203,9 +215,12 @@
 			"public class A {\n"+ //$NON-NLS-1$
 			"}\n" //$NON-NLS-1$
 			);
-
+		env.waitForManualRefresh();
 		incrementalBuild();
+		env.waitForAutoBuild();
 		expectingSpecificProblemFor(b, new Problem("B.foo()", "x cannot be resolved or is not a field", b, 61, 62, CategorizedProblem.CAT_MEMBER, IMarker.SEVERITY_ERROR)); //$NON-NLS-1$ //$NON-NLS-2$
+		env.removeProject(project1Path);
+		env.removeProject(project2Path);
 	}
 
 	public void testCompileOrder() throws JavaModelException {
@@ -267,7 +282,9 @@
 			);
 
 		env.setBuildOrder(new String[]{"P1", "P3", "P2"});//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
+		env.waitForManualRefresh();
 		fullBuild();
+		env.waitForAutoBuild();
 
 		expectingCompilingOrder(new String[]{"p1.X", "p3.Z", "p2.Y"}); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
 		IPath workspaceRootPath = env.getWorkspaceRootPath();
@@ -277,6 +294,9 @@
 				new Problem("p1", "W cannot be resolved to a type", c1, 31, 32, CategorizedProblem.CAT_TYPE, IMarker.SEVERITY_ERROR)//$NON-NLS-1$ //$NON-NLS-2$
 		});
 		JavaCore.setOptions(options);
+		env.removeProject(p1);
+		env.removeProject(p2);
+		env.removeProject(p3);
 	}
 
 	public void testCycle1() throws JavaModelException {
@@ -360,7 +380,9 @@
 
 		try {
 			env.setBuildOrder(new String[]{"P1", "P2", "P3"});//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
+			env.waitForManualRefresh();
 			fullBuild();
+			env.waitForAutoBuild();
 
 			expectingCompilingOrder(new String[]{"p1.X", "p2.Y", "p3.Z", "p1.X", "p2.Y", "p3.Z", "p1.X"});//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$//$NON-NLS-5$//$NON-NLS-6$//$NON-NLS-7$
 			expectingOnlySpecificProblemFor(p1, new Problem("p1", "A cycle was detected in the build path of project 'P1'. The cycle consists of projects {P1, P2, P3}", p1, -1, -1, CategorizedProblem.CAT_BUILDPATH, IMarker.SEVERITY_WARNING));//$NON-NLS-1$ //$NON-NLS-2$
@@ -370,6 +392,9 @@
 			JavaCore.setOptions(options);
 		} finally {
 			env.setBuildOrder(null);
+			env.removeProject(p1);
+			env.removeProject(p2);
+			env.removeProject(p3);
 		}
 	}
 
@@ -454,8 +479,9 @@
 
 		try {
 			env.setBuildOrder(new String[]{"P1", "P2", "P3"});//$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
+			env.waitForManualRefresh();
 			fullBuild();
-
+			env.waitForAutoBuild();
 			expectingCompilingOrder(new String[]{"p1.X", "p2.Y", "p3.Z", "p1.X", "p2.Y", "p3.Z", "p1.X"});//$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$//$NON-NLS-5$ //$NON-NLS-6$//$NON-NLS-7$
 			expectingOnlySpecificProblemFor(p1,new Problem("p1", "A cycle was detected in the build path of project 'P1'. The cycle consists of projects {P1, P2, P3}", p1, -1, -1, CategorizedProblem.CAT_BUILDPATH, IMarker.SEVERITY_WARNING));//$NON-NLS-1$ //$NON-NLS-2$
 			expectingOnlySpecificProblemsFor(p2,new Problem[]{
@@ -467,6 +493,9 @@
 			JavaCore.setOptions(options);
 		} finally {
 			env.setBuildOrder(null);
+			env.removeProject(p1);
+			env.removeProject(p2);
+			env.removeProject(p3);
 		}
 	}
 
@@ -551,7 +580,9 @@
 
 		try {
 			env.setBuildOrder(new String[]{"P1", "P2", "P3"});//$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
+			env.waitForManualRefresh();
 			fullBuild();
+			env.waitForAutoBuild();
 
 			expectingCompilingOrder(new String[]{"p1.X", "p2.Y", "p3.Z", "p1.X", "p2.Y", "p3.Z", "p1.X"});//$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$//$NON-NLS-5$ //$NON-NLS-6$//$NON-NLS-7$
 			expectingOnlySpecificProblemFor(p1,new Problem("p1", "A cycle was detected in the build path of project 'P1'. The cycle consists of projects {P1, P2, P3}", p1, -1, -1, CategorizedProblem.CAT_BUILDPATH, IMarker.SEVERITY_WARNING));//$NON-NLS-1$ //$NON-NLS-2$
@@ -568,7 +599,7 @@
 				"}\n" //$NON-NLS-1$
 				);
 			incrementalBuild();
-
+			env.waitForAutoBuild();
 			expectingCompilingOrder(new String[]{"p1.X", "p2.Y", "p3.Z"}); //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
 			expectingOnlySpecificProblemFor(p1,new Problem("p1", "A cycle was detected in the build path of project 'P1'. The cycle consists of projects {P1, P2, P3}", p1, -1, -1, CategorizedProblem.CAT_BUILDPATH, IMarker.SEVERITY_WARNING));//$NON-NLS-1$ //$NON-NLS-2$
 			expectingOnlySpecificProblemsFor(p2,new Problem[]{
@@ -580,6 +611,9 @@
 			JavaCore.setOptions(options);
 		} finally {
 			env.setBuildOrder(null);
+			env.removeProject(p1);
+			env.removeProject(p2);
+			env.removeProject(p3);
 		}
 	}
 	public void testCycle4() throws JavaModelException {
@@ -653,7 +687,9 @@
 
 		try {
 			env.setBuildOrder(new String[]{"P1", "P2", "P3"});//$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
+			env.waitForManualRefresh();
 			fullBuild();
+			env.waitForAutoBuild();
 
 			expectingCompilingOrder(new String[]{"p2.Y", "p3.Z", "p2.Y"});//$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
 			expectingOnlySpecificProblemFor(p1,new Problem("p1", "A cycle was detected in the build path of project 'P1'. The cycle consists of projects {P1, P2, P3}", p1, -1, -1, CategorizedProblem.CAT_BUILDPATH, IMarker.SEVERITY_WARNING));//$NON-NLS-1$ //$NON-NLS-2$
@@ -680,6 +716,7 @@
 				"}\n" //$NON-NLS-1$
 				);
 			incrementalBuild();
+			env.waitForAutoBuild();
 			expectingCompilingOrder(new String[]{"p1.X", "p2.Y", "p3.Z", "p1.X", "p2.Y"}); //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$//$NON-NLS-5$
 			expectingOnlySpecificProblemFor(p1,new Problem("p1", "A cycle was detected in the build path of project 'P1'. The cycle consists of projects {P1, P2, P3}", p1, -1, -1, CategorizedProblem.CAT_BUILDPATH, IMarker.SEVERITY_WARNING));//$NON-NLS-1$ //$NON-NLS-2$
 			expectingOnlySpecificProblemFor(p2,new Problem("p2", "A cycle was detected in the build path of project 'P2'. The cycle consists of projects {P1, P2, P3}", p2, -1, -1, CategorizedProblem.CAT_BUILDPATH, IMarker.SEVERITY_WARNING));//$NON-NLS-1$ //$NON-NLS-2$
@@ -688,6 +725,9 @@
 			JavaCore.setOptions(options);
 		} finally {
 			env.setBuildOrder(null);
+			env.removeProject(p1);
+			env.removeProject(p2);
+			env.removeProject(p3);
 		}
 	}
 
@@ -744,7 +784,9 @@
 
 		try {
 			env.setBuildOrder(new String[]{"P1", "P2"});//$NON-NLS-1$ //$NON-NLS-2$
+			env.waitForManualRefresh();
 			fullBuild();
+			env.waitForAutoBuild();
 
 			expectingCompilingOrder(new String[]{"p1.X", "p2.Y", "p1.X", "p2.Y"});//$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$
 			expectingOnlySpecificProblemsFor(p1,new Problem[]{
@@ -768,7 +810,7 @@
 				);
 
 			incrementalBuild();
-
+			env.waitForAutoBuild();
 			expectingCompilingOrder(new String[]{"p11.XX", "p22.YY", "p2.Y", "p1.X"});//$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$
 			expectingOnlySpecificProblemsFor(p1,new Problem[]{
 				new Problem("p1", "The import p22 is never used", c1, 32, 35, CategorizedProblem.CAT_UNNECESSARY_CODE, IMarker.SEVERITY_WARNING),//$NON-NLS-1$ //$NON-NLS-2$
@@ -782,6 +824,8 @@
 			JavaCore.setOptions(options);
 		} finally {
 			env.setBuildOrder(null);
+			env.removeProject(p1);
+			env.removeProject(p2);
 		}
 	}
 
@@ -854,8 +898,9 @@
 	env.addRequiredProject(p3, p2, accessiblePaths, forbiddenPaths, false);
 
 	try {
+		env.waitForManualRefresh();
 		fullBuild();
-
+		env.waitForAutoBuild();
 		expectingOnlySpecificProblemsFor(p1,new Problem[]{
 			new Problem("p1", "A cycle was detected in the build path of project 'P1'. The cycle consists of projects {P1, P2, P3}", p1, -1, -1, CategorizedProblem.CAT_BUILDPATH, IMarker.SEVERITY_WARNING)//$NON-NLS-1$ //$NON-NLS-2$
 		});
@@ -868,6 +913,9 @@
 
 	} finally {
 		JavaCore.setOptions(options);
+		env.removeProject(p1);
+		env.removeProject(p2);
+		env.removeProject(p3);
 	}
 }
 
@@ -940,8 +988,9 @@
 	env.addRequiredProject(p3, p2, accessiblePaths, forbiddenPaths, false);
 
 	try {
+		env.waitForManualRefresh();
 		fullBuild();
-
+		env.waitForAutoBuild();
 		expectingOnlySpecificProblemsFor(p1,new Problem[]{
 			new Problem("p1", "A cycle was detected in the build path of project 'P1'. The cycle consists of projects {P1, P2, P3}", p1, -1, -1, CategorizedProblem.CAT_BUILDPATH, IMarker.SEVERITY_WARNING)//$NON-NLS-1$ //$NON-NLS-2$
 		});
@@ -954,6 +1003,9 @@
 
 	} finally {
 		JavaCore.setOptions(options);
+		env.removeProject(p1);
+		env.removeProject(p2);
+		env.removeProject(p3);
 	}
 }
 
@@ -993,9 +1045,12 @@
 			"public class D extends p.internal.B {\n"+ //$NON-NLS-1$
 			"}\n" //$NON-NLS-1$
 			);
-
+		env.waitForManualRefresh();
 		fullBuild();
+		env.waitForAutoBuild();
 		expectingSpecificProblemFor(project2Path, new Problem("", "Access restriction: The type 'B' is not API (restriction on required project 'Project1')", d, 23, 35, CategorizedProblem.CAT_RESTRICTION, IMarker.SEVERITY_ERROR)); //$NON-NLS-1$ //$NON-NLS-2$
+		env.removeProject(project1Path);
+		env.removeProject(project2Path);
 	}
 
 	/*
@@ -1033,8 +1088,9 @@
 			"public class C extends p.api.A {\n"+ //$NON-NLS-1$
 			"}\n" //$NON-NLS-1$
 			);
-
+		env.waitForManualRefresh();
 		fullBuild();
+		env.waitForAutoBuild();
 		expectingNoProblems();
 
 		//----------------------------
@@ -1046,7 +1102,10 @@
 			);
 
 		incrementalBuild();
+		env.waitForAutoBuild();
 		expectingSpecificProblemFor(project2Path, new Problem("", "Access restriction: The type 'B' is not API (restriction on required project 'Project1')", d, 23, 35, CategorizedProblem.CAT_RESTRICTION, IMarker.SEVERITY_ERROR)); //$NON-NLS-1$ //$NON-NLS-2$
+		env.removeProject(project1Path);
+		env.removeProject(project2Path);
 	}
 
 	/*
@@ -1088,8 +1147,9 @@
 			"public class D extends p.internal.B {\n"+ //$NON-NLS-1$
 			"}\n" //$NON-NLS-1$
 			);
-
+		env.waitForManualRefresh();
 		fullBuild();
+		env.waitForAutoBuild();
 		expectingSpecificProblemFor(project2Path, new Problem("", "Access restriction: The type 'B' is not API (restriction on required project 'Project1')", d, 23, 35, CategorizedProblem.CAT_RESTRICTION, IMarker.SEVERITY_ERROR)); //$NON-NLS-1$ //$NON-NLS-2$
 
 		//----------------------------
@@ -1099,7 +1159,10 @@
 		env.addRequiredProject(project2Path, project1Path, new IPath[] {}, new IPath[] {}, false);
 
 		incrementalBuild();
+		env.waitForAutoBuild();
 		expectingNoProblems();
+		env.removeProject(project1Path);
+		env.removeProject(project2Path);
 	}
 
 	/*
@@ -1140,7 +1203,10 @@
 			);
 
 		fullBuild();
+		env.waitForAutoBuild();
 		expectingSpecificProblemFor(project2Path, new Problem("", "Access restriction: The type 'B' is not API (restriction on required project 'Project1')", d, 23, 35, CategorizedProblem.CAT_RESTRICTION, IMarker.SEVERITY_ERROR)); //$NON-NLS-1$ //$NON-NLS-2$
+		env.removeProject(project1Path);
+		env.removeProject(project2Path);
 	}
 
 	/*
@@ -1178,8 +1244,9 @@
 			"public class C extends p.api.A {\n"+ //$NON-NLS-1$
 			"}\n" //$NON-NLS-1$
 			);
-
+		env.waitForManualRefresh();
 		fullBuild();
+		env.waitForAutoBuild();
 		expectingNoProblems();
 
 		//----------------------------
@@ -1191,7 +1258,10 @@
 			);
 
 		incrementalBuild();
+		env.waitForAutoBuild();
 		expectingSpecificProblemFor(project2Path, new Problem("", "Access restriction: The type 'B' is not API (restriction on required project 'Project1')", d, 23, 35, CategorizedProblem.CAT_RESTRICTION, IMarker.SEVERITY_ERROR)); //$NON-NLS-1$ //$NON-NLS-2$
+		env.removeProject(project1Path);
+		env.removeProject(project2Path);
 	}
 
 	/*
@@ -1233,8 +1303,9 @@
 			"public class D extends p.internal.B {\n"+ //$NON-NLS-1$
 			"}\n" //$NON-NLS-1$
 			);
-
+		env.waitForManualRefresh();
 		fullBuild();
+		env.waitForAutoBuild();
 		expectingSpecificProblemFor(project2Path, new Problem("", "Access restriction: The type 'B' is not API (restriction on required project 'Project1')", d, 23, 35, CategorizedProblem.CAT_RESTRICTION, IMarker.SEVERITY_ERROR)); //$NON-NLS-1$ //$NON-NLS-2$
 
 		//----------------------------
@@ -1244,7 +1315,10 @@
 		env.addRequiredProject(project2Path, project1Path, new IPath[] {}, new IPath[] {}, false);
 
 		incrementalBuild();
+		env.waitForAutoBuild();
 		expectingNoProblems();
+		env.removeProject(project1Path);
+		env.removeProject(project2Path);
 	}
 
 	/*
@@ -1290,9 +1364,13 @@
 			"public class B extends p.A {\n"+ //$NON-NLS-1$
 			"}\n" //$NON-NLS-1$
 			);
-
+		env.waitForManualRefresh();
 		fullBuild();
+		env.waitForAutoBuild();
 		expectingNoProblems();
+		env.removeProject(project1Path);
+		env.removeProject(project2Path);
+		env.removeProject(project3Path);
 	}
 
 	/*
@@ -1338,9 +1416,13 @@
 			"public class B extends p.A {\n"+ //$NON-NLS-1$
 			"}\n" //$NON-NLS-1$
 			);
-
+		env.waitForManualRefresh();
 		fullBuild();
+		env.waitForAutoBuild();
 		expectingSpecificProblemFor(project3Path, new Problem("", "Discouraged access: The type 'A' is not API (restriction on required project 'Project2')", b, 35, 38, CategorizedProblem.CAT_RESTRICTION, IMarker.SEVERITY_WARNING)); //$NON-NLS-1$ //$NON-NLS-2$
+		env.removeProject(project1Path);
+		env.removeProject(project2Path);
+		env.removeProject(project3Path);
 	}
 
 	public void testMissingRequiredBinaries() throws JavaModelException {
@@ -1390,7 +1472,9 @@
 			);
 
 		try {
+			env.waitForManualRefresh();
 			fullBuild();
+			env.waitForAutoBuild();
 
 			expectingOnlySpecificProblemsFor(p1,new Problem[]{
 				new Problem("p1", "The type p3.Z cannot be resolved. It is indirectly referenced from required .class files", x, 48, 49, CategorizedProblem.CAT_BUILDPATH, IMarker.SEVERITY_ERROR),//$NON-NLS-1$ //$NON-NLS-2$
@@ -1398,6 +1482,9 @@
 			});
 		} finally {
 			env.setBuildOrder(null);
+			env.removeProject(p1);
+			env.removeProject(p1);
+			env.removeProject(p3);
 		}
 	}
 
@@ -1416,7 +1503,9 @@
 		"public class A {\n" +
 		"}\n"
 		);
+	env.waitForManualRefresh();
 	fullBuild();
+	env.waitForAutoBuild();
 	expectingNoProblems();
 	env.removePackageFragmentRoot(P1, "");
 	env.addClassFolder(P1, P1.append("bin"), true);
@@ -1430,8 +1519,12 @@
 		"public class X {\n" +
 		"  A f;\n" +
 		"}");
+	env.waitForManualRefresh();
 	fullBuild();
+	env.waitForAutoBuild();
 	expectingNoProblems();
+	env.removeProject(P1);
+	env.removeProject(P2);
 }
 
 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=159118
@@ -1463,13 +1556,17 @@
 		"public class X {\n" +
 		"  A f;\n" +
 		"}");
+	env.waitForManualRefresh();
 	fullBuild();
+	env.waitForAutoBuild();
 	expectingSpecificProblemsFor(P2,
 		new Problem[] {
 			new Problem("", "The import p cannot be resolved",
 					c, 7 , 8, CategorizedProblem.CAT_IMPORT, IMarker.SEVERITY_ERROR),
 			new Problem("", "A cannot be resolved to a type",
 					c, 31 , 32, CategorizedProblem.CAT_TYPE, IMarker.SEVERITY_ERROR)});
+	env.removeProject(P1);
+	env.removeProject(P2);
 }
 
 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=164622
@@ -1514,7 +1611,9 @@
 		);
 
 	try {
+		env.waitForManualRefresh();
 		fullBuild();
+		env.waitForAutoBuild();
 		expectingOnlySpecificProblemsFor(p3, new Problem[]{
 			new Problem("p3",
 				"The project was not built since its build path is incomplete. Cannot find the class file for I. Fix the build path then try building this project",
@@ -1525,6 +1624,9 @@
 		});
 	} finally {
 		env.setBuildOrder(null);
+		env.removeProject(p1);
+		env.removeProject(p2);
+		env.removeProject(p3);
 	}
 }
 
@@ -1571,7 +1673,9 @@
 		);
 
 	try {
+		env.waitForManualRefresh();
 		fullBuild();
+		env.waitForAutoBuild();
 		expectingOnlySpecificProblemsFor(p3, new Problem[]{
 				new Problem("p3",
 					"The project was not built since its build path is incomplete. Cannot find the class file for I. Fix the build path then try building this project",
@@ -1582,6 +1686,10 @@
 		});
 	} finally {
 		env.setBuildOrder(null);
+		env.setBuildOrder(null);
+		env.removeProject(p1);
+		env.removeProject(p2);
+		env.removeProject(p3);
 	}
 }
 
@@ -1644,16 +1752,24 @@
 			);
 	
 	try {
+		env.waitForManualRefresh();
 		fullBuild();
+		env.waitForAutoBuild();
 		expectingNoProblems();
 		
 		IFile gcaFile = (IFile) env.getWorkspace().getRoot().findMember(gca);
 		gcaFile.touch(null);
+		env.waitForManualRefresh();
 		incrementalBuild(p2);
+		env.waitForAutoBuild();
 		expectingNoProblems();
 		
 	} finally {
 		env.setBuildOrder(null);
+		env.setBuildOrder(null);
+		env.removeProject(p1);
+		env.removeProject(p2);
+		env.removeProject(p0);
 	}
 }
 
@@ -1715,8 +1831,13 @@
 	// for Project1
 	env.addRequiredProject(p2, p1);
 	env.addRequiredProject(p3, p2);
-
+	env.waitForManualRefresh();
 	fullBuild();
+	env.waitForAutoBuild();
 	expectingNoProblems();
+	env.setBuildOrder(null);
+	env.removeProject(p1);
+	env.removeProject(p2);
+	env.removeProject(p3);
 }
 }