| author | Steffen Pingel | 2012-02-24 07:38:43 (EST) |
|---|---|---|
| committer | Steffen Pingel | 2012-02-24 07:38:43 (EST) |
| commit | b4988ce002db49e0e83bfa52dcfb8a2da8c84a78 (patch) (side-by-side diff) | |
| tree | a5b65db0690a214a5f1f33c49f30d9203aee5509 | |
| parent | 30202b47ce1066c566409cf120d8568a35e92592 (diff) | |
| download | org.eclipse.mylyn.commons-b4988ce002db49e0e83bfa52dcfb8a2da8c84a78.zip org.eclipse.mylyn.commons-b4988ce002db49e0e83bfa52dcfb8a2da8c84a78.tar.gz org.eclipse.mylyn.commons-b4988ce002db49e0e83bfa52dcfb8a2da8c84a78.tar.bz2 | |
RESOLVED - bug 371984: support moving paths
https://bugs.eclipse.org/bugs/show_bug.cgi?id=371984
Change-Id: I4308fb2cbfc4d8cadd40f42fd515ac73c5c46857
2 files changed, 59 insertions, 13 deletions
diff --git a/org.eclipse.mylyn.commons.core/src/org/eclipse/mylyn/commons/core/storage/CommonStore.java b/org.eclipse.mylyn.commons.core/src/org/eclipse/mylyn/commons/core/storage/CommonStore.java index 3b7f09f..6f10f2a 100644 --- a/org.eclipse.mylyn.commons.core/src/org/eclipse/mylyn/commons/core/storage/CommonStore.java +++ b/org.eclipse.mylyn.commons.core/src/org/eclipse/mylyn/commons/core/storage/CommonStore.java @@ -16,6 +16,7 @@ import java.util.HashMap; import java.util.Map; import org.eclipse.core.runtime.Assert; +import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; @@ -24,6 +25,7 @@ import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.mylyn.commons.core.StatusHandler; import org.eclipse.mylyn.internal.commons.core.CommonsCorePlugin; +import org.eclipse.osgi.util.NLS; /** * @author Steffen Pingel @@ -75,10 +77,6 @@ public class CommonStore { return storable; } - public synchronized void copy(IPath source, IPath target, boolean overwrite) { - // FIXME - } - public File getLocation() { return location; } @@ -99,8 +97,12 @@ public class CommonStore { } private File getFile(IPath path) { + return getFile(path, true); + } + + private File getFile(IPath path, boolean create) { File file = new File(location, path.toOSString()); - if (!file.getParentFile().exists()) { + if (create && !file.getParentFile().exists()) { file.getParentFile().mkdirs(); } return file; @@ -132,4 +134,17 @@ public class CommonStore { storableByLocation.remove(storable.getPath()); } + public void move(IPath oldPath, IPath newPath) throws CoreException { + File oldFile = getFile(oldPath, false); + // TODO lock hierarchy and throw an exception if oldFile is in use + if (oldFile.exists()) { + File newFile = getFile(newPath, false); + newFile.getParentFile().mkdirs(); + if (!oldFile.renameTo(newFile)) { + throw new CoreException(new Status(IStatus.ERROR, CommonsCorePlugin.ID_PLUGIN, NLS.bind( + "The target path ''{0}'' already exists", newPath))); + } + } + } + } diff --git a/org.eclipse.mylyn.commons.tests/src/org/eclipse/mylyn/commons/tests/core/storage/CommonStoreTest.java b/org.eclipse.mylyn.commons.tests/src/org/eclipse/mylyn/commons/tests/core/storage/CommonStoreTest.java index bec122f..6795f3f 100644 --- a/org.eclipse.mylyn.commons.tests/src/org/eclipse/mylyn/commons/tests/core/storage/CommonStoreTest.java +++ b/org.eclipse.mylyn.commons.tests/src/org/eclipse/mylyn/commons/tests/core/storage/CommonStoreTest.java @@ -98,14 +98,6 @@ public class CommonStoreTest extends TestCase { assertTrue(storable.exists("handle")); } - public void testGetPathWrite() throws Exception { - ICommonStorable storable = store.get(new Path("sub")); - writeHello(storable, "handle"); - File subFile = new File(location, "sub"); - assertEquals(Collections.singletonList(subFile), Arrays.asList(location.listFiles())); - assertEquals(Collections.singletonList(new File(subFile, "handle")), Arrays.asList(subFile.listFiles())); - } - public void testGet() throws Exception { ICommonStorable storable = store.get(Path.EMPTY); ICommonStorable storable2 = store.get(Path.EMPTY); @@ -127,6 +119,45 @@ public class CommonStoreTest extends TestCase { assertEquals(Collections.emptyList(), Arrays.asList(location.listFiles())); } + public void testGetPathWrite() throws Exception { + ICommonStorable storable = store.get(new Path("sub")); + writeHello(storable, "handle"); + File subFile = new File(location, "sub"); + assertEquals(Collections.singletonList(subFile), Arrays.asList(location.listFiles())); + assertEquals(Collections.singletonList(new File(subFile, "handle")), Arrays.asList(subFile.listFiles())); + } + + public void testMove() throws Exception { + ICommonStorable storable = store.get(new Path("source")); + writeHello(storable, "handle"); + store.move(new Path("source"), new Path("target")); + File targetFile = new File(location, "target"); + assertEquals(Collections.singletonList(targetFile), Arrays.asList(location.listFiles())); + assertEquals(Collections.singletonList(new File(targetFile, "handle")), Arrays.asList(targetFile.listFiles())); + } + + public void testMoveExistant() throws Exception { + ICommonStorable storable = store.get(new Path("source")); + writeHello(storable, "handle"); + ICommonStorable storable2 = store.get(new Path("target")); + writeHello(storable2, "handle2"); + try { + store.move(new Path("source"), new Path("target")); + fail("Expected CoreException"); + } catch (CoreException expected) { + File sourceFile = new File(location, "source"); + File targetFile = new File(location, "target"); + assertEquals(Arrays.asList(sourceFile, targetFile), Arrays.asList(location.listFiles())); + assertEquals(Collections.singletonList(new File(targetFile, "handle2")), + Arrays.asList(targetFile.listFiles())); + } + } + + public void testMoveNonExistant() throws Exception { + store.move(new Path("source"), new Path("target")); + assertEquals(Collections.emptyList(), Arrays.asList(location.listFiles())); + } + public void testRelease() throws Exception { ICommonStorable storable = store.get(Path.EMPTY); storable.release(); |

