diff options
author | Sebastian Schmidt | 2012-07-05 13:58:22 +0000 |
---|---|---|
committer | Sebastian Schmidt | 2012-07-15 20:45:01 +0000 |
commit | 8f248af021786c1e6f45b077c248f2138a7bae0a (patch) | |
tree | 5e9862a9b41238413e5076926d216ac704f9162e | |
parent | f773342e1c7e33424d9d9adb40d4fa61f3048c07 (diff) | |
download | org.eclipse.mylyn.context-8f248af021786c1e6f45b077c248f2138a7bae0a.tar.gz org.eclipse.mylyn.context-8f248af021786c1e6f45b077c248f2138a7bae0a.tar.xz org.eclipse.mylyn.context-8f248af021786c1e6f45b077c248f2138a7bae0a.zip |
NEW - bug 384365: create api to import/export breakpoints
https://bugs.eclipse.org/bugs/show_bug.cgi?id=384365
Change-Id: I2834f9c7c742397779bc1f4e81f5e4b6091ea2ad
10 files changed, 316 insertions, 2 deletions
diff --git a/org.eclipse.mylyn.debug.tests/META-INF/MANIFEST.MF b/org.eclipse.mylyn.debug.tests/META-INF/MANIFEST.MF index f41a24170..44f7a963e 100644 --- a/org.eclipse.mylyn.debug.tests/META-INF/MANIFEST.MF +++ b/org.eclipse.mylyn.debug.tests/META-INF/MANIFEST.MF @@ -16,5 +16,10 @@ Require-Bundle: org.junit, org.eclipse.mylyn.context.core, org.eclipse.mylyn.context.sdk.util, org.eclipse.mylyn.context.ui, - org.eclipse.mylyn.debug.ui + org.eclipse.mylyn.debug.ui, + org.eclipse.debug.core, + org.eclipse.mylyn.context.sdk.java, + org.apache.commons.io, + org.eclipse.jdt.debug Bundle-RequiredExecutionEnvironment: J2SE-1.5 +Export-Package: org.eclipse.mylyn.internal.debug.ui;x-internal:=true diff --git a/org.eclipse.mylyn.debug.tests/build.properties b/org.eclipse.mylyn.debug.tests/build.properties index d5cc6112d..aef691868 100644 --- a/org.eclipse.mylyn.debug.tests/build.properties +++ b/org.eclipse.mylyn.debug.tests/build.properties @@ -7,7 +7,8 @@ ############################################################################### bin.includes = META-INF/,\ .,\ - about.html + about.html,\ + testdata/ src.includes = about.html source.. = src/ output.. = bin/ diff --git a/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsContextUtilTest.java b/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsContextUtilTest.java new file mode 100644 index 000000000..72de412ff --- /dev/null +++ b/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsContextUtilTest.java @@ -0,0 +1,130 @@ +/******************************************************************************* + * Copyright (c) 2012 Sebastian Schmidt and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Sebastian Schmidt - initial API and implementation + *******************************************************************************/ +package org.eclipse.mylyn.internal.debug.ui; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.debug.core.DebugPlugin; +import org.eclipse.debug.core.IBreakpointManager; +import org.eclipse.debug.core.model.IBreakpoint; +import org.eclipse.jdt.internal.debug.core.breakpoints.JavaLineBreakpoint; +import org.eclipse.mylyn.context.core.ContextCore; +import org.eclipse.mylyn.context.core.IInteractionContext; +import org.eclipse.mylyn.context.core.IInteractionContextManager; +import org.eclipse.mylyn.context.sdk.java.WorkspaceSetupHelper; +import org.eclipse.mylyn.internal.context.core.ContextCorePlugin; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * @author Sebastian Schmidt + */ +public class BreakpointsContextUtilTest { + + private final String contextFileName = "contextWithBreakpoints.xml.zip"; //$NON-NLS-1$ + + private final File contextFile = new File("testdata/" + contextFileName); //$NON-NLS-1$ + + private File tempContextFile; + + private final IInteractionContextManager contextManager = ContextCore.getContextManager(); + + @Before + public void setUp() throws IOException, CoreException { + File contextStore = ContextCorePlugin.getContextStore().getContextDirectory(); + tempContextFile = new File(contextStore, contextFileName); + FileUtils.copyFile(contextFile, tempContextFile); + assertTrue(contextFile.exists()); + } + + @After + public void tearDown() throws Exception { + if (tempContextFile != null && tempContextFile.exists()) { + tempContextFile.delete(); + } + contextManager.deactivateContext("contextWithBreakpoints"); //$NON-NLS-1$ + WorkspaceSetupHelper.clearWorkspace(); + } + + /** + * If the project isn't in the workspace, breakpoints from context should be ignored + */ + @Test + public void testImportBreakpointsWithMissingProject() { + contextManager.activateContext("contextWithBreakpoints"); //$NON-NLS-1$ + IInteractionContext testContext = contextManager.getActiveContext(); + List<IBreakpoint> breakpoints = BreakpointsContextUtil.importBreakpoints(testContext, null); + assertEquals(Collections.emptyList(), breakpoints); + } + + @Test + public void testImportBreakpoints() throws Exception { + BreakpointsTestUtil.createProject(); + contextManager.activateContext("contextWithBreakpoints"); //$NON-NLS-1$ + IInteractionContext testContext = contextManager.getActiveContext(); + List<IBreakpoint> breakpoints = BreakpointsContextUtil.importBreakpoints(testContext, null); + assertTrue(breakpoints.size() == 2); + + assertTrue(breakpoints.get(0) instanceof JavaLineBreakpoint); + IMarker marker = breakpoints.get(0).getMarker(); + assertEquals("test.java", marker.getResource().getName()); + assertEquals(11, marker.getAttribute(IMarker.LINE_NUMBER, 0)); + + assertTrue(breakpoints.get(1) instanceof JavaLineBreakpoint); + marker = breakpoints.get(1).getMarker(); + assertEquals("test.java", marker.getResource().getName()); + assertEquals(10, marker.getAttribute(IMarker.LINE_NUMBER, 0)); + } + + @Test + public void testExportBreakpoints() throws Exception { + BreakpointsTestUtil.createProject(); + InputStream expectedResult = new FileInputStream(new File("testdata/breakpointFile.xml")); //$NON-NLS-1$ + IBreakpoint breakpoint = BreakpointsTestUtil.createTestBreakpoint(); + List<IBreakpoint> breakpoints = new ArrayList<IBreakpoint>(); + breakpoints.add(breakpoint); + InputStream exportedBreakpoints = BreakpointsContextUtil.exportBreakpoints(breakpoints, null); + assertTrue(IOUtils.contentEquals(expectedResult, exportedBreakpoints)); + } + + @Test + public void testRemoveBreakpoints() throws Exception { + BreakpointsTestUtil.createProject(); + IBreakpointManager breakpointManager = DebugPlugin.getDefault().getBreakpointManager(); + IBreakpoint[] breakpoints = breakpointManager.getBreakpoints(); + int currentBreakpoints = breakpoints.length; + + IBreakpoint breakpoint = BreakpointsTestUtil.createTestBreakpoint(); + breakpointManager.addBreakpoint(breakpoint); + List<IBreakpoint> breakpointsToRemove = new ArrayList<IBreakpoint>(); + breakpointsToRemove.add(breakpoint); + + breakpointManager.addBreakpoint(breakpoint); + assertEquals(currentBreakpoints + 1, breakpointManager.getBreakpoints().length); + + BreakpointsContextUtil.removeBreakpoints(breakpointsToRemove); + assertEquals(currentBreakpoints, breakpointManager.getBreakpoints().length); + } +} diff --git a/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsTestUtil.java b/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsTestUtil.java new file mode 100644 index 000000000..1c14a6b85 --- /dev/null +++ b/org.eclipse.mylyn.debug.tests/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsTestUtil.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2012 Sebastian Schmidt and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Sebastian Schmidt - initial API and implementation + *******************************************************************************/ + +package org.eclipse.mylyn.internal.debug.ui; + +import java.io.File; +import java.util.HashMap; +import java.util.zip.ZipFile; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.debug.core.DebugException; +import org.eclipse.debug.core.model.IBreakpoint; +import org.eclipse.jdt.internal.debug.core.breakpoints.JavaLineBreakpoint; +import org.eclipse.mylyn.commons.sdk.util.CommonTestUtil; +import org.eclipse.mylyn.context.sdk.java.WorkspaceSetupHelper; + +/** + * @author Sebastian Schmidt + */ +public class BreakpointsTestUtil { + + public static IBreakpoint createTestBreakpoint() throws DebugException { + IResource testClass = ResourcesPlugin.getWorkspace().getRoot().findMember("/test/src/test.java"); //$NON-NLS-1$ + return new JavaLineBreakpoint(testClass, "test", 5, 1, 5, 0, true, new HashMap<String, Object>()); //$NON-NLS-1$ + } + + public static IProject createProject() throws Exception { + IProject project = WorkspaceSetupHelper.createProject("test"); //$NON-NLS-1$ + ZipFile zip = new ZipFile(new File("testdata/projects/project.zip")); //$NON-NLS-1$ + CommonTestUtil.unzip(zip, project.getLocation().toFile()); + project.refreshLocal(IResource.DEPTH_INFINITE, null); + return project; + } +} diff --git a/org.eclipse.mylyn.debug.tests/testdata/breakpointFile.xml b/org.eclipse.mylyn.debug.tests/testdata/breakpointFile.xml new file mode 100644 index 000000000..6630803e8 --- /dev/null +++ b/org.eclipse.mylyn.debug.tests/testdata/breakpointFile.xml @@ -0,0 +1,16 @@ +<?xml version="1.0" encoding="UTF-8"?> +<breakpoints> +<breakpoint enabled="true" persistant="true" registered="true"> +<resource path="/test/src/test.java" type="1"/> +<marker charStart="1" lineNumber="5" type="org.eclipse.jdt.debug.javaLineBreakpointMarker"> +<attrib name="org.eclipse.debug.core.id" value="org.eclipse.jdt.debug"/> +<attrib name="org.eclipse.jdt.debug.core.suspendPolicy" value="2"/> +<attrib name="charStart" value="1"/> +<attrib name="charEnd" value="5"/> +<attrib name="org.eclipse.debug.core.enabled" value="true"/> +<attrib name="org.eclipse.jdt.debug.core.typeName" value="test"/> +<attrib name="workingset_name" value=""/> +<attrib name="workingset_id" value="org.eclipse.debug.ui.breakpointWorkingSet"/> +</marker> +</breakpoint> +</breakpoints>
\ No newline at end of file diff --git a/org.eclipse.mylyn.debug.tests/testdata/contextWithBreakpoints.xml.zip b/org.eclipse.mylyn.debug.tests/testdata/contextWithBreakpoints.xml.zip Binary files differnew file mode 100644 index 000000000..de583b265 --- /dev/null +++ b/org.eclipse.mylyn.debug.tests/testdata/contextWithBreakpoints.xml.zip diff --git a/org.eclipse.mylyn.debug.tests/testdata/projects/project.zip b/org.eclipse.mylyn.debug.tests/testdata/projects/project.zip Binary files differnew file mode 100644 index 000000000..5aeaff2cf --- /dev/null +++ b/org.eclipse.mylyn.debug.tests/testdata/projects/project.zip diff --git a/org.eclipse.mylyn.debug.ui/META-INF/MANIFEST.MF b/org.eclipse.mylyn.debug.ui/META-INF/MANIFEST.MF index 15654e0f6..83c676e7d 100644 --- a/org.eclipse.mylyn.debug.ui/META-INF/MANIFEST.MF +++ b/org.eclipse.mylyn.debug.ui/META-INF/MANIFEST.MF @@ -3,6 +3,7 @@ Bundle-ManifestVersion: 2 Bundle-Name: %Bundle-Name Bundle-SymbolicName: org.eclipse.mylyn.debug.ui;singleton:=true Bundle-Version: 3.9.0.qualifier +Bundle-Activator: org.eclipse.mylyn.internal.debug.ui.DebugUiPlugin Bundle-Localization: plugin Require-Bundle: org.eclipse.core.runtime, org.eclipse.mylyn.commons.core;bundle-version="[3.8.0,4.0.0)", diff --git a/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsContextUtil.java b/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsContextUtil.java new file mode 100644 index 000000000..916baba57 --- /dev/null +++ b/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/BreakpointsContextUtil.java @@ -0,0 +1,93 @@ +/******************************************************************************* + * Copyright (c) 2012 Sebastian Schmidt and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Sebastian Schmidt - initial API and implementation + *******************************************************************************/ +package org.eclipse.mylyn.internal.debug.ui; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Scanner; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.debug.core.DebugPlugin; +import org.eclipse.debug.core.model.IBreakpoint; +import org.eclipse.debug.ui.actions.ExportBreakpointsOperation; +import org.eclipse.debug.ui.actions.ImportBreakpointsOperation; +import org.eclipse.mylyn.commons.core.StatusHandler; +import org.eclipse.mylyn.context.core.ContextCore; +import org.eclipse.mylyn.context.core.IInteractionContext; + +/** + * @author Sebastian Schmidt + */ +public class BreakpointsContextUtil { + + public static InputStream exportBreakpoints(Collection<IBreakpoint> breakpoints, IProgressMonitor progressMonitor) { + if (breakpoints.size() == 0) { + return null; + } + + ExportBreakpointsOperation exportBreakpointOperation = new ExportBreakpointsOperation( + breakpoints.toArray(new IBreakpoint[0])); + try { + exportBreakpointOperation.run(progressMonitor); + return new ByteArrayInputStream(exportBreakpointOperation.getBuffer().toString().getBytes("UTF-8")); //$NON-NLS-1$ + } catch (Exception e) { + StatusHandler.log(new Status(IStatus.WARNING, DebugUiPlugin.ID_PLUGIN, + "Could not export context breakpoints", e));//$NON-NLS-1$ + } + return null; + } + + public static List<IBreakpoint> importBreakpoints(IInteractionContext context, IProgressMonitor progressMonitor) { + InputStream stream = ContextCore.getContextManager() + .getAdditionalContextData(context, DebugUiPlugin.CONTRIBUTOR_ID); + if (stream == null) { + return new ArrayList<IBreakpoint>(); + } + return importBreakpoints(stream, progressMonitor); + } + + public static List<IBreakpoint> importBreakpoints(InputStream stream, IProgressMonitor progressMonitor) { + Scanner scanner = new Scanner(stream); + scanner.useDelimiter("\\A"); //$NON-NLS-1$ + String breakpoints = scanner.next(); + scanner.close(); + + ImportBreakpointsOperation importBreakpointOperation = new ImportBreakpointsOperation(new StringBuffer( + breakpoints), true, false); + try { + importBreakpointOperation.run(progressMonitor); + return new ArrayList<IBreakpoint>(Arrays.asList(importBreakpointOperation.getImportedBreakpoints())); + } catch (InvocationTargetException e) { + StatusHandler.log(new Status(IStatus.WARNING, DebugUiPlugin.ID_PLUGIN, + "Could not import context breakpoints", e));//$NON-NLS-1$ + } + return new ArrayList<IBreakpoint>(); + } + + public static void removeBreakpoints(Collection<IBreakpoint> breakpoints) { + try { + DebugPlugin.getDefault() + .getBreakpointManager() + .removeBreakpoints(breakpoints.toArray(new IBreakpoint[0]), true); + } catch (CoreException e) { + StatusHandler.log(new Status(IStatus.WARNING, DebugUiPlugin.ID_PLUGIN, + "Could not remove obsolete breakpoints from workspace", e)); //$NON-NLS-1$ + } + } +} diff --git a/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/DebugUiPlugin.java b/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/DebugUiPlugin.java new file mode 100644 index 000000000..9e321b798 --- /dev/null +++ b/org.eclipse.mylyn.debug.ui/src/org/eclipse/mylyn/internal/debug/ui/DebugUiPlugin.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright (c) 2012 Sebastian Schmidt and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Sebastian Schmidt - initial API and implementation + *******************************************************************************/ + +package org.eclipse.mylyn.internal.debug.ui; + +import org.eclipse.ui.plugin.AbstractUIPlugin; + +/** + * @author Sebastian Schmidt + */ +public class DebugUiPlugin extends AbstractUIPlugin { + + public static String ID_PLUGIN = "org.eclipse.mylyn.debug.ui"; //$NON-NLS-1$ + + public static String CONTRIBUTOR_ID = "org.eclipse.mylyn.debug.breakpoints"; //$NON-NLS-1$ +} |