Bug 564849 - [Tests] Fix unstable JUnit tests

OutputStreamMonitorTests
  Two of the tests are explicit written for UTF-8 but the monitor is
initialized with default charset. Since many modern systems use UTF-8 as
default the test still works most of the times.

InputStreamMonitorTests
  The closing test stops the input monitoring thread and immediately
checks if the thread is stopped. I saw a few random test failures
locally from this race condition.

BreakpointTests
  Calling dispose on a view is not the right way to close the view.

Change-Id: I4e01475032dc5dd69810eedc467ba1ff3efb2ef3
Signed-off-by: Paul Pazderski <paul-eclipse@ppazderski.de>
diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/breakpoint/BreakpointTests.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/breakpoint/BreakpointTests.java
index 60a62d5..39c127b 100644
--- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/breakpoint/BreakpointTests.java
+++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/breakpoint/BreakpointTests.java
@@ -127,7 +127,7 @@
 			}, this, testTimeout, c -> "Breakpoint not restored in view");
 		} finally {
 			if (!viewVisible) {
-				view.dispose();
+				DebugUIPlugin.getActiveWorkbenchWindow().getActivePage().hideView(view);
 			}
 		}
 	}
diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/InputStreamMonitorTests.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/InputStreamMonitorTests.java
index b6a3b3c..361995c 100644
--- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/InputStreamMonitorTests.java
+++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/InputStreamMonitorTests.java
@@ -22,6 +22,7 @@
 import java.io.PipedOutputStream;
 import java.nio.charset.Charset;
 import java.util.Set;
+import java.util.function.Supplier;
 
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.debug.internal.core.DebugCoreMessages;
@@ -95,8 +96,12 @@
 	@Test
 	@SuppressWarnings("resource")
 	public void testClose() throws Exception {
-		Set<Thread> allThreads = Thread.getAllStackTraces().keySet();
-		long alreadyLeakedThreads = allThreads.stream().filter(t -> DebugCoreMessages.InputStreamMonitor_label.equals(t.getName())).count();
+		Supplier<Long> getInputStreamMonitorThreads = () -> {
+			Set<Thread> allThreads = Thread.getAllStackTraces().keySet();
+			long numMonitorThreads = allThreads.stream().filter(t -> DebugCoreMessages.InputStreamMonitor_label.equals(t.getName())).count();
+			return numMonitorThreads;
+		};
+		long alreadyLeakedThreads = getInputStreamMonitorThreads.get();
 		if (alreadyLeakedThreads > 0) {
 			Platform.getLog(TestsPlugin.class).warn("Test started with " + alreadyLeakedThreads + " leaked monitor threads.");
 		}
@@ -130,9 +135,8 @@
 			assertEquals("Stream not closed or to often.", 1, testStream.numClosed);
 		}
 
-		allThreads = Thread.getAllStackTraces().keySet();
-		long numMonitorThreads = allThreads.stream().filter(t -> DebugCoreMessages.InputStreamMonitor_label.equals(t.getName())).count();
-		assertEquals("Leaked monitor threads.", 0, numMonitorThreads);
+		TestUtil.waitWhile(() -> getInputStreamMonitorThreads.get() > 0, 500);
+		assertEquals("Leaked monitor threads.", 0, (long) getInputStreamMonitorThreads.get());
 	}
 
 	/**
diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/OutputStreamMonitorTests.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/OutputStreamMonitorTests.java
index 239aee3..0d36102 100644
--- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/OutputStreamMonitorTests.java
+++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/OutputStreamMonitorTests.java
@@ -73,7 +73,7 @@
 	@Before
 	@SuppressWarnings("resource")
 	public void setUp() throws IOException {
-		monitor = new TestOutputStreamMonitor(new PipedInputStream(sysout), null);
+		monitor = new TestOutputStreamMonitor(new PipedInputStream(sysout), StandardCharsets.UTF_8);
 	}
 
 	/**
@@ -161,18 +161,28 @@
 	 * Test that passing <code>null</code> as charset does not raise exceptions.
 	 */
 	@Test
+	@SuppressWarnings("resource")
 	public void testNullCharset() throws Exception {
 		String input = "o\u00F6O\u00EFiI\u00D6\u00D8\u00F8";
 
-		monitor.addListener(fStreamListener);
-		monitor.startMonitoring();
-		try (PrintStream out = new PrintStream(sysout)) {
-			out.print(input);
-		}
-		sysout.flush();
+		sysout.close();
+		sysout = new PipedOutputStream();
+		monitor.close();
+		monitor = new TestOutputStreamMonitor(new PipedInputStream(sysout), null);
+		try {
+			monitor.addListener(fStreamListener);
+			monitor.startMonitoring();
+			try (PrintStream out = new PrintStream(sysout)) {
+				out.print(input);
+			}
+			sysout.flush();
 
-		TestUtil.waitWhile(() -> notifiedChars.length() < input.length(), 500);
-		assertEquals("Monitor read wrong content.", input, notifiedChars.toString());
+			TestUtil.waitWhile(() -> notifiedChars.length() < input.length(), 500);
+			assertEquals("Monitor read wrong content.", input, notifiedChars.toString());
+		} finally {
+			sysout.close();
+			monitor.close();
+		}
 	}
 
 	/**