Bug 518699: cleanup platform breakpoints between tests

Ensure no platform breakpoints are left over from previous tests

Change-Id: I3266636e9014d4930ec72f7411c9a4dc737d0d9f
diff --git a/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/tests/dsf/gdb/framework/BaseTestCase.java b/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/tests/dsf/gdb/framework/BaseTestCase.java
index 08bb8d5..4069552 100644
--- a/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/tests/dsf/gdb/framework/BaseTestCase.java
+++ b/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/tests/dsf/gdb/framework/BaseTestCase.java
@@ -54,11 +54,13 @@
 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
 import org.eclipse.core.runtime.preferences.InstanceScope;
 import org.eclipse.debug.core.DebugPlugin;
+import org.eclipse.debug.core.IBreakpointManager;
 import org.eclipse.debug.core.ILaunch;
 import org.eclipse.debug.core.ILaunchConfiguration;
 import org.eclipse.debug.core.ILaunchConfigurationType;
 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
 import org.eclipse.debug.core.ILaunchManager;
+import org.eclipse.debug.core.model.IBreakpoint;
 import org.eclipse.debug.internal.core.IInternalDebugCoreConstants;
 import org.junit.After;
 import org.junit.AfterClass;
@@ -291,6 +293,16 @@
 
 	/**
 	 * Make sure we are starting with a clean/known state. That means no
+	 * platform breakpoints that will be automatically installed.
+	 */
+	public void removeAllPlatformBreakpoints() throws CoreException {
+		IBreakpointManager manager = DebugPlugin.getDefault().getBreakpointManager();
+		IBreakpoint[] breakpoints = manager.getBreakpoints();
+		manager.removeBreakpoints(breakpoints, true);
+	}
+	
+	/**
+	 * Make sure we are starting with a clean/known state. That means no
 	 * existing launches.
 	 */
 	public void removeTeminatedLaunchesBeforeTest() throws CoreException {
@@ -327,6 +339,7 @@
 	@Before
 	public void doBeforeTest() throws Exception {
 		removeTeminatedLaunchesBeforeTest();
+		removeAllPlatformBreakpoints();
 		setLaunchAttributes();
 		doLaunch();
 	}
@@ -591,6 +604,7 @@
 			assertLaunchTerminates();
 			fLaunch = null;
 		}
+		removeAllPlatformBreakpoints();
 	}
 
  	/**
diff --git a/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/tests/dsf/gdb/tests/SourceLookupTest.java b/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/tests/dsf/gdb/tests/SourceLookupTest.java
index 60ed786..cefabaf 100644
--- a/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/tests/dsf/gdb/tests/SourceLookupTest.java
+++ b/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/tests/dsf/gdb/tests/SourceLookupTest.java
@@ -226,10 +226,8 @@
 	public void doAfterTest() throws Exception {
 		super.doAfterTest();
 
+		removeAllPlatformBreakpoints();
 		IBreakpointManager manager = DebugPlugin.getDefault().getBreakpointManager();
-		IBreakpoint[] breakpoints = manager.getBreakpoints();
-		manager.removeBreakpoints(breakpoints, true);
-
 		manager.removeBreakpointListener(fBreakpointListener);
 	}
 
diff --git a/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/tests/dsf/gdb/tests/TraceFileTest.java b/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/tests/dsf/gdb/tests/TraceFileTest.java
index 5304ff7..6c6cb8c 100644
--- a/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/tests/dsf/gdb/tests/TraceFileTest.java
+++ b/dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/tests/dsf/gdb/tests/TraceFileTest.java
@@ -50,6 +50,7 @@
 import org.eclipse.cdt.tests.dsf.gdb.framework.BaseParametrizedTestCase;
 import org.eclipse.cdt.tests.dsf.gdb.framework.SyncUtil;
 import org.eclipse.cdt.tests.dsf.gdb.launching.TestsPlugin;
+import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.debug.core.DebugPlugin;
 import org.eclipse.debug.core.IBreakpointManager;
@@ -81,6 +82,7 @@
 	private IGDBTraceControl fTraceService;
 	private IBreakpointsTargetDMContext fBreakpointsDmc;
 	private ITraceTargetDMContext fTraceTargetDmc;
+	private boolean suppressRemoveAllPlatformBreakpoints;
 
 
     @Override
@@ -102,6 +104,18 @@
 			System.out.println("ERROR: Failed to delete all breakpoints");
 		}
     }
+    
+	/**
+	 * Some tests call doBefore/After in the middle of their test and rely on
+	 * platform breakpoints to survive that step. So override with the ability
+	 * to disable.
+	 */
+	@Override
+    public void removeAllPlatformBreakpoints() throws CoreException {
+    	if (!suppressRemoveAllPlatformBreakpoints) {
+    		super.removeAllPlatformBreakpoints();
+    	}
+    }
 
 	@Override
 	@After
@@ -176,10 +190,15 @@
 
 		try {
     		createTraceFile();
-    		// Cleanup the interim launch that we just caused
-    		doAfterTest();
-    		// Setup for the upcoming launch
-    		doBeforeTest();
+    		suppressRemoveAllPlatformBreakpoints = true;
+    		try {
+	    		// Cleanup the interim launch that we just caused
+	    		doAfterTest();
+	    		// Setup for the upcoming launch
+	    		doBeforeTest();
+    		} finally {
+    			suppressRemoveAllPlatformBreakpoints = false;
+    		}
     	} catch (Throwable t) {
     		// If we cannot create the trace file, ignore the test using the
     		// assume check below.  The reason for the failure could be a missing
@@ -214,10 +233,15 @@
     	// the required test ourselves.
     	try {
     		testTraceFile();
-    		// Cleanup the interim launch that we just caused
-    		doAfterTest();
-    		// Setup for the upcoming launch
-    		doBeforeTest();
+    		suppressRemoveAllPlatformBreakpoints = true;
+    		try {
+	    		// Cleanup the interim launch that we just caused
+	    		doAfterTest();
+	    		// Setup for the upcoming launch
+	    		doBeforeTest();
+    		} finally {
+    			suppressRemoveAllPlatformBreakpoints = false;
+    		}
     	} catch (Throwable t) {
     		// If we cannot setup properly, ignore the test using the
     		// assume check below.  The reason for the failure could be a missing