Bug 506401 - testTranslateContainers diagnostics

Reduced wait time for jobs, added logging on setup and teardown and
added cleanup on tearDown().

Change-Id: If3ed133344b6063f5ae4e4db069cabd115392dda
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AbstractDebugTest.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AbstractDebugTest.java
index 31aa4ef..db64de8 100644
--- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AbstractDebugTest.java
+++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/AbstractDebugTest.java
@@ -26,7 +26,6 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.TimeUnit;
 
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IFolder;
@@ -58,6 +57,7 @@
 import org.eclipse.debug.core.ILaunchDelegate;
 import org.eclipse.debug.core.ILaunchManager;
 import org.eclipse.debug.core.model.IBreakpoint;
+import org.eclipse.debug.core.model.IDebugTarget;
 import org.eclipse.debug.core.model.ILineBreakpoint;
 import org.eclipse.debug.core.model.IProcess;
 import org.eclipse.debug.core.model.IThread;
@@ -126,6 +126,7 @@
 import org.eclipse.jdt.debug.tests.core.LiteralTests17;
 import org.eclipse.jdt.debug.tests.refactoring.MemberParser;
 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants;
+import org.eclipse.jdt.internal.debug.core.model.JDIDebugTarget;
 import org.eclipse.jdt.internal.debug.eval.ast.engine.ASTEvaluationEngine;
 import org.eclipse.jdt.internal.debug.ui.BreakpointUtils;
 import org.eclipse.jdt.internal.debug.ui.IJDIPreferencesConstants;
@@ -248,12 +249,9 @@
 		SafeRunnable.setIgnoreErrors(true);
 	}
 	
-	
-	/* (non-Javadoc)
-	 * @see junit.framework.TestCase#setUp()
-	 */
 	@Override
 	protected void setUp() throws Exception {
+		TestUtil.log(IStatus.INFO, getName(), "setUp");
 		super.setUp();
 		setPreferences();
 		IProject pro = ResourcesPlugin.getWorkspace().getRoot().getProject(ONE_FOUR_PROJECT_NAME);
@@ -856,24 +854,25 @@
 	 */
 	protected IJavaProject createJavaProjectClone(String name, String contentpath, String ee, boolean delete) throws Exception {
 		IProject pro = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
+		String owner = "Creating project: " + name;
         if (pro.exists() && delete) {
 			pro.delete(true, true, null);
-			TestUtil.waitForJobs(300, TimeUnit.MINUTES.toMillis(3));
+			TestUtil.waitForJobs(owner, 100, 10000);
 			TestUtil.runEventLoop();
         }
         // create project and import source
         IJavaProject jp = JavaProjectHelper.createJavaProject(name, JavaProjectHelper.BIN_DIR);
-		TestUtil.waitForJobs(100, TimeUnit.MINUTES.toMillis(3));
+		TestUtil.waitForJobs(owner, 100, 10000);
 		TestUtil.runEventLoop();
 
         JavaProjectHelper.addSourceContainer(jp, JavaProjectHelper.SRC_DIR);
-		TestUtil.waitForJobs(100, TimeUnit.MINUTES.toMillis(3));
+		TestUtil.waitForJobs(owner, 100, 10000);
 		TestUtil.runEventLoop();
 
         File root = JavaTestPlugin.getDefault().getFileInPlugin(new Path(contentpath));
         JavaProjectHelper.importFilesFromDirectory(root, jp.getPath(), null);
 
-		TestUtil.waitForJobs(100, TimeUnit.MINUTES.toMillis(3));
+		TestUtil.waitForJobs(owner, 100, 10000);
 		TestUtil.runEventLoop();
 
         // add the EE library
@@ -890,7 +889,7 @@
         if (!folder.exists()) {
         	folder.create(true, true, null);
         }
-		TestUtil.waitForJobs(300, TimeUnit.MINUTES.toMillis(3));
+		TestUtil.waitForJobs(owner, 100, 10000);
 		TestUtil.runEventLoop();
         return jp;
 	}
@@ -907,19 +906,20 @@
 	 */
 	protected IProject createProjectClone(String name, String contentpath, boolean delete) throws Exception {
 		IProject pro = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
+		String owner = "Creating project: " + name;
         if (pro.exists() && delete) {
 			pro.delete(true, true, null);
-			TestUtil.waitForJobs(100, TimeUnit.MINUTES.toMillis(3));
+			TestUtil.waitForJobs(owner, 100, 10000);
 			TestUtil.runEventLoop();
         }
         // create project and import source
         IProject pj = JavaProjectHelper.createProject(name);
-		TestUtil.waitForJobs(100, TimeUnit.MINUTES.toMillis(3));
+		TestUtil.waitForJobs(owner, 100, 10000);
 		TestUtil.runEventLoop();
 
         File root = JavaTestPlugin.getDefault().getFileInPlugin(new Path(contentpath));
         JavaProjectHelper.importFilesFromDirectory(root, pj.getFullPath(), null);
-		TestUtil.waitForJobs(100, TimeUnit.MINUTES.toMillis(3));
+		TestUtil.waitForJobs(owner, 100, 10000);
 		TestUtil.runEventLoop();
         return pj;
 	}
@@ -2497,15 +2497,32 @@
 				super.runBare();
 				tryAgain = false;
 			} catch (TestAgainException e) {
-				Status status = new Status(IStatus.ERROR, "org.eclipse.jdt.debug.tests", "Test failed attempt " + attempts + ". Re-testing: " + this.getName(), e); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
-				TestUtil.cleanUp();
-				DebugPlugin.log(status);
+				TestUtil.log(IStatus.ERROR, getName(), "Test failed attempt " + attempts + ". Re-testing.", e);
+				TestUtil.cleanUp(getName());
 				if (attempts > 5) {
 					tryAgain = false;
 				}
 			}
 		}
 	}
+
+	@Override
+	protected void tearDown() throws Exception {
+		TestUtil.log(IStatus.INFO, getName(), "tearDown");
+		shutdownDebugTargets();
+		TestUtil.cleanUp(getName());
+		super.tearDown();
+	}
+
+	protected void shutdownDebugTargets() {
+		ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
+		IDebugTarget[] targets = launchManager.getDebugTargets();
+		for (IDebugTarget target : targets) {
+			if (target instanceof JDIDebugTarget) {
+				((JDIDebugTarget) target).shutdown();
+			}
+		}
+	}
     
 	/**
 	 * Opens and returns an editor on the given file or <code>null</code>
diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/TestUtil.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/TestUtil.java
index 91e9bf4..20bae6d 100644
--- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/TestUtil.java
+++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/TestUtil.java
@@ -12,7 +12,6 @@
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.concurrent.TimeUnit;
 
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
@@ -27,7 +26,7 @@
 	 * Call this in the tearDown method of every test to clean up state that can
 	 * otherwise leak through SWT between tests.
 	 */
-	public static void cleanUp() {
+	public static void cleanUp(String owner) {
 		// Ensure that the Thread.interrupted() flag didn't leak.
 		Assert.assertFalse("The main thread should not be interrupted at the end of a test", Thread.interrupted());
 
@@ -36,18 +35,12 @@
 
 		// Wait for any outstanding jobs to finish. Protect against deadlock by
 		// terminating the wait after a timeout.
-		boolean timedOut = waitForJobs(0, TimeUnit.MINUTES.toMillis(2));
+		boolean timedOut = waitForJobs(owner, 10, 10000);
 		if (timedOut) {
 			// We don't expect any extra jobs run during the test: try to cancel them
+			log(IStatus.INFO, owner, "Trying to cancel running jobs: " + getRunningOrWaitingJobs(null));
 			getRunningOrWaitingJobs(null).forEach(job -> job.cancel());
-		}
-		timedOut = waitForJobs(0, TimeUnit.MINUTES.toMillis(1));
-
-		if (timedOut) {
-			// Don't fail here, just log. See 506401.
-			String message = "Some job is still running or waiting to run: " + dumpRunningOrWaitingJobs();
-			Status status = new Status(IStatus.ERROR, JavaTestPlugin.getDefault().getBundle().getSymbolicName(), message);
-			JavaTestPlugin.getDefault().getLog().log(status);
+			waitForJobs(owner, 10, 1000);
 		}
 
 		// Wait for any pending *syncExec calls to finish
@@ -57,6 +50,16 @@
 		Assert.assertFalse("The main thread should not be interrupted at the end of a test", Thread.interrupted());
 	}
 
+	public static void log(int severity, String owner, String message, Throwable... optionalError) {
+		message = "[" + owner + "] " + message;
+		Throwable error = null;
+		if (optionalError != null && optionalError.length > 0) {
+			error = optionalError[0];
+		}
+		Status status = new Status(severity, JavaTestPlugin.getDefault().getBundle().getSymbolicName(), message, error);
+		JavaTestPlugin.getDefault().getLog().log(status);
+	}
+
 	/**
 	 * Process all queued UI events. If called from background thread, does
 	 * nothing.
@@ -71,19 +74,18 @@
 	}
 
 	/**
-	 * Utility for waiting until the execution of jobs of any family has
-	 * finished or timeout is reached. If no jobs are running, the method waits
-	 * given minimum wait time. While this method is waiting for jobs, UI events
-	 * are processed.
+	 * Utility for waiting until the execution of jobs of any family has finished or timeout is reached. If no jobs are running, the method waits
+	 * given minimum wait time. While this method is waiting for jobs, UI events are processed.
 	 *
+	 * @param owner
+	 *            name of the caller which will be logged as prefix if the wait times out
 	 * @param minTimeMs
 	 *            minimum wait time in milliseconds
 	 * @param maxTimeMs
 	 *            maximum wait time in milliseconds
-	 * @return true if the method timed out, false if all the jobs terminated
-	 *         before the timeout
+	 * @return true if the method timed out, false if all the jobs terminated before the timeout
 	 */
-	public static boolean waitForJobs(long minTimeMs, long maxTimeMs) {
+	public static boolean waitForJobs(String owner, long minTimeMs, long maxTimeMs) {
 		if (maxTimeMs < minTimeMs) {
 			throw new IllegalArgumentException("Max time is smaller as min time!");
 		}
@@ -98,6 +100,8 @@
 		}
 		while (!Job.getJobManager().isIdle()) {
 			if (System.currentTimeMillis() - start >= maxTimeMs) {
+				String message = "Some job is still running or waiting to run: " + dumpRunningOrWaitingJobs();
+				log(IStatus.ERROR, owner, message);
 				return true;
 			}
 			runEventLoop();
diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/HcrTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/HcrTests.java
index 82c4ecd..1f5d26f 100644
--- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/HcrTests.java
+++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/HcrTests.java
@@ -116,6 +116,7 @@
 			cu.commitWorkingCopy(true, null);
 			waitForBuild();
 		}
+		super.tearDown();
 	}	
 
 	public void testSimpleHcr() throws Exception {
diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/JavaLibraryPathTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/JavaLibraryPathTests.java
index 3460a65..6cfa822 100644
--- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/JavaLibraryPathTests.java
+++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/JavaLibraryPathTests.java
@@ -62,9 +62,9 @@
 	
 	@Override
 	protected void tearDown() throws Exception {
-		super.tearDown();
 		deleteProject("PathTests1");
 		deleteProject("PathTests2");
+		super.tearDown();
 	}
 	
 	private IJavaProject createProject(String name) throws Exception {
diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/LineTrackerTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/LineTrackerTests.java
index d0f3693..f275f30 100644
--- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/LineTrackerTests.java
+++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/LineTrackerTests.java
@@ -68,7 +68,6 @@
     
     @Override
 	protected void tearDown() throws Exception {
-        super.tearDown();
         // delete references and gc to free memory.
         fConsole = null;
         fLines = null;
@@ -76,6 +75,7 @@
         fLinesRead = null;
         
         System.gc();
+		super.tearDown();
     }   
     
     
diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/StringSubstitutionTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/StringSubstitutionTests.java
index e6be05d..889651b 100644
--- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/StringSubstitutionTests.java
+++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/core/StringSubstitutionTests.java
@@ -479,12 +479,12 @@
 	 */
 	@Override
 	protected void tearDown() throws Exception {
-		super.tearDown();
 		IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager();
 		manager.removeValueVariableListener(this);
 		fAdded = null;
 		fChanged = null;
 		fRemoved = null;
+		super.tearDown();
 	}
 
 	/* (non-Javadoc)
diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/sourcelookup/JarSourceLookupTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/sourcelookup/JarSourceLookupTests.java
index 697e6ec..0476ded 100644
--- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/sourcelookup/JarSourceLookupTests.java
+++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/sourcelookup/JarSourceLookupTests.java
@@ -17,6 +17,7 @@
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.debug.core.ILaunch;
 import org.eclipse.debug.core.ILaunchConfiguration;
@@ -75,8 +76,9 @@
 
 	@Override
 	protected void setUp() throws Exception {
+		TestUtil.log(IStatus.INFO, getName(), "setUp");
 		assertWelcomeScreenClosed();
-		TestUtil.cleanUp();
+		TestUtil.cleanUp(getName());
 		IPath testrpath = new Path("testresources");
 		IProject jarProject = createProjectClone(fJarProject, testrpath.append(fJarProject).toString(), true);
 
@@ -110,7 +112,6 @@
 		if (fgJarProject.exists()) {
 			fgJarProject.getProject().delete(true, null);
 		}
-		TestUtil.cleanUp();
 		super.tearDown();
 	}
 
diff --git a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/ViewManagementTests.java b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/ViewManagementTests.java
index d90cda4..4f4e60b 100644
--- a/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/ViewManagementTests.java
+++ b/org.eclipse.jdt.debug.tests/tests/org/eclipse/jdt/debug/tests/ui/ViewManagementTests.java
@@ -189,13 +189,13 @@
 	 */
 	@Override
 	protected void tearDown() throws Exception {
-		super.tearDown();
 		IPreferenceStore preferenceStore = DebugUITools.getPreferenceStore();
 		preferenceStore.setValue(IInternalDebugUIConstants.PREF_SWITCH_PERSPECTIVE_ON_SUSPEND, switch_on_suspend);
 		preferenceStore.setValue(IInternalDebugUIConstants.PREF_SWITCH_TO_PERSPECTIVE, switch_on_launch);
 		preferenceStore.setValue(IDebugUIConstants.PREF_MANAGE_VIEW_PERSPECTIVES, debug_perspectives);
 		preferenceStore.setValue(IInternalDebugUIConstants.PREF_USER_VIEW_BINDINGS, user_view_bindings); 	
 		preferenceStore.setValue(IInternalDebugUIConstants.PREF_ACTIVATE_DEBUG_VIEW, activate_debug_view);
+		super.tearDown();
 	}
 
 	/**