Bug 547417 - trying to fix 180 test fails, take 2

Applied same changes as on EvaluationSetup on DebugEvaluationSetup:

- doubled timeout time for opening connection
- fail the test if the connection is timed out
- added printStackTrace() in all cases where it was missing

Also added a bit more info to the printed messages.

Additionally, added assertions in setUp() about evaluation context and
vm.

Change-Id: I6f939760fc428283ed2a6b8bab79a7cfaded0661
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/eval/DebugEvaluationSetup.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/eval/DebugEvaluationSetup.java
index 0bbdda1..1e4651e 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/eval/DebugEvaluationSetup.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/eval/DebugEvaluationSetup.java
@@ -13,6 +13,8 @@
  *******************************************************************************/
 package org.eclipse.jdt.core.tests.eval;
 
+import static org.junit.Assert.assertTrue;
+
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.ServerSocket;
@@ -57,19 +59,22 @@
 					launcher.setDebugPort(debugPort);
 					this.launchedVM = launcher.launch();
 				} catch (TargetException e) {
-					throw new Error(e.getMessage());
+					e.printStackTrace();
+					throw new Error(e.getMessage(), e);
 				}
 	
 				// Thread that read the stout of the VM so that the VM doesn't block
 				try {
 					startReader("VM's stdout reader", this.launchedVM.getInputStream(), System.out);
 				} catch (TargetException e) {
+					e.printStackTrace();
 				}
 	
 				// Thread that read the sterr of the VM so that the VM doesn't block
 				try {
 					startReader("VM's sterr reader", this.launchedVM.getErrorStream(), System.err);
 				} catch (TargetException e) {
+					e.printStackTrace();
 				}
 	
 				// Start JDI connection (try 10 times)
@@ -77,8 +82,10 @@
 					try {
 						VirtualMachineManager manager = org.eclipse.jdi.Bootstrap.virtualMachineManager();
 						List connectors = manager.attachingConnectors();
-						if (connectors.size() == 0)
+						if (connectors.size() == 0) {
+							System.err.println(getName() + ": could not get attachingConnectors() from VM");
 							break;
+						}
 						AttachingConnector connector = (AttachingConnector)connectors.get(0);
 						Map args = connector.defaultArguments();
 						Connector.Argument argument = (Connector.Argument)args.get("port");
@@ -91,27 +98,22 @@
 						}
 						argument = (Connector.Argument)args.get("timeout");
 						if (argument != null) {
-							argument.setValue("10000");
+							argument.setValue("20000");
 						}
 						this.vm = connector.attach(args);
-	
+						System.out.println(getName() + ": connected to VM using port " + debugPort);
+						
 						// workaround pb with some VMs
 						this.vm.resume();
 	
 						break;
-					} catch (IllegalConnectorArgumentsException e) {
+					} catch (IllegalConnectorArgumentsException | IOException e) {
 						e.printStackTrace();
 						try {
-							System.out.println("Could not contact the VM at " + launcher.getTargetAddress() + ":" + debugPort + ". Retrying...");
+							System.out.println(getName() + ": could not contact the VM at " + launcher.getTargetAddress() + ":" + debugPort + ". Retrying...");
 							Thread.sleep(100);
 						} catch (InterruptedException e2) {
-						}
-					} catch (IOException e) {
-						e.printStackTrace();
-						try {
-							System.out.println("Could not contact the VM at " + launcher.getTargetAddress() + ":" + debugPort + ". Retrying...");
-							Thread.sleep(100);
-						} catch (InterruptedException e2) {
+							e2.printStackTrace();
 						}
 					}
 				}
@@ -128,8 +130,8 @@
 										System.out.print((char)read);
 								} while (read != -1);
 							}
-						} catch (TargetException e) {
-						} catch (IOException e) {
+						} catch (TargetException | IOException e) {
+							e.printStackTrace();
 						}
 	
 						// Shut it down
@@ -142,15 +144,17 @@
 								try {
 									Thread.sleep(retry * 100);
 								} catch (InterruptedException e) {
+									e.printStackTrace();
 								}
 							}
 							if (this.launchedVM.isRunning()) {
 								this.launchedVM.shutDown();
 							}
 						} catch (TargetException e) {
+							e.printStackTrace();
 						}
 					}
-					System.err.println("Could not contact the VM");
+					System.err.println(getName() + ": could not contact the VM");
 					return;
 				}
 	
@@ -159,12 +163,19 @@
 	
 				// Create target
 				this.target = new TargetInterface();
-				this.target.connect(evalServer, 30000); // allow 30s max to connect (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=188127)
+				
+				// allow 30s max to connect (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=188127)
+				// Increased to 60 s for https://bugs.eclipse.org/bugs/show_bug.cgi?id=547417
+				this.target.connect(evalServer, 60000); 
 	
+				assertTrue(getName() + ": failed to connect VM server", this.target.isConnected());
+				
+				System.out.println(getName() + ": connected to target using port " + debugPort);
+				
 				// Create name environment
 				this.env = new FileSystem(Util.getJavaClassLibs(), new String[0], null);
 			} catch (IOException e1) {
-				throw new Error("Failed to open socket", e1);
+				throw new Error(getName() + ": Failed to open socket", e1);
 			}
 		}
 		super.setUp();
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/eval/DebugEvaluationTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/eval/DebugEvaluationTest.java
index 75cdc67..a309002 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/eval/DebugEvaluationTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/eval/DebugEvaluationTest.java
@@ -175,10 +175,15 @@
 	}
 	public void initialize(CompilerTestSetup setUp) {
 		super.initialize(setUp);
-		if (setUp instanceof DebugEvaluationSetup) {
-			this.jdiVM = ((DebugEvaluationSetup)setUp).vm;
-		}
+		this.jdiVM = ((DebugEvaluationSetup)setUp).vm;
 	}
+
+	@Override
+	protected void setUp() throws Exception {
+		super.setUp();
+		assertNotNull("VM is null, probably VM connection error", this.jdiVM);
+	}
+
 	public void removeTempClass(String className) {
 		resetEnv(); // needed to reinitialize the caches
 		Util.delete(SOURCE_DIRECTORY + File.separator + className + ".java");
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/eval/EvaluationSetup.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/eval/EvaluationSetup.java
index 8153472..cdf0ea6 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/eval/EvaluationSetup.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/eval/EvaluationSetup.java
@@ -87,6 +87,8 @@
 				
 				assertTrue("Failed to connect VM server", this.target.isConnected());
 
+				System.out.println(getName() + ": connected to target");
+
 				// Create name environment
 				this.env = new FileSystem(Util.getJavaClassLibs(), new String[0], null);
 			} catch (IOException e1) {
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/eval/EvaluationTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/eval/EvaluationTest.java
index 2f9a68a..79c9a27 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/eval/EvaluationTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/eval/EvaluationTest.java
@@ -505,6 +505,12 @@
 		this.env = evalSetUp.env;
 	}
 
+	@Override
+	protected void setUp() throws Exception {
+		super.setUp();
+		assertNotNull("Evaluation context is null, probably VM connection error", this.context);
+	}
+
 	/**
 	 * Installs all the variables and check that the number of installed variables is the given number.
 	 */