From 38fe01398dfc4ffa306f2b37c289fda41ff6c9a7 Mon Sep 17 00:00:00 2001 From: S1eth Date: Tue, 17 Oct 2017 15:15:09 +0200 Subject: [PATCH 1/2] migration to Dropbox API v2 --- build.gradle | 171 +++---- .../DropBoxBasicFileAttributesProvider.java | 36 +- .../attr/DropBoxFileAttributesFactory.java | 4 +- .../driver/DropBoxFileSystemDriver.java | 212 ++++---- .../dropbox/filestore/DropBoxFileStore.java | 27 +- .../fs/dropbox/misc/DropBoxIOException.java | 6 +- .../fs/dropbox/misc/DropBoxInputStream.java | 14 +- .../fs/dropbox/misc/DropBoxOutputStream.java | 14 +- .../provider/DropBoxFileSystemRepository.java | 6 +- .../github/fge/fs/dropbox/FileSystemTest.java | 451 ++++++++++++++++++ ...ropBoxBasicFileAttributesProviderTest.java | 114 +++++ 11 files changed, 827 insertions(+), 228 deletions(-) create mode 100644 src/test/java/com/github/fge/fs/dropbox/FileSystemTest.java create mode 100644 src/test/java/com/github/fge/fs/dropbox/attr/DropBoxBasicFileAttributesProviderTest.java diff --git a/build.gradle b/build.gradle index d65e031..74747a1 100644 --- a/build.gradle +++ b/build.gradle @@ -20,42 +20,42 @@ buildscript { repositories { maven { - url "http://repo.springsource.org/plugins-release"; - mavenCentral(); + url "http://repo.springsource.org/plugins-release" + mavenCentral() } } dependencies { classpath(group: "org.springframework.build.gradle", - name: "propdeps-plugin", version: "0.0.7"); + name: "propdeps-plugin", version: "0.0.7") classpath(group: "info.solidsoft.gradle.pitest", - name: "gradle-pitest-plugin", version: "1.1.1"); + name: "gradle-pitest-plugin", version: "1.1.1") } -}; - -apply(plugin: "java"); -apply(plugin: "maven"); -apply(plugin: "signing"); -apply(plugin: "osgi"); -apply(plugin: "idea"); -apply(plugin: "eclipse"); -apply(plugin: "propdeps"); -apply(plugin: "propdeps-maven"); -apply(plugin: "propdeps-idea"); -apply(plugin: "propdeps-eclipse"); -apply(plugin: "info.solidsoft.pitest"); - -group = "com.github.fge"; -version = "0.0.2-SNAPSHOT"; -description = "FileSystem implementation over DropBox"; -sourceCompatibility = "1.7"; -targetCompatibility = "1.7"; // defaults to sourceCompatibility +} + +apply(plugin: "java") +apply(plugin: "maven") +apply(plugin: "signing") +apply(plugin: "osgi") +apply(plugin: "idea") +apply(plugin: "eclipse") +apply(plugin: "propdeps") +apply(plugin: "propdeps-maven") +apply(plugin: "propdeps-idea") +apply(plugin: "propdeps-eclipse") +apply(plugin: "info.solidsoft.pitest") + +group = "com.github.fge" +version = "0.0.2-SNAPSHOT" +description = "FileSystem implementation over DropBox" +sourceCompatibility = "1.7" +targetCompatibility = "1.7" // defaults to sourceCompatibility /* * Repositories to use */ repositories { - mavenCentral(); - mavenLocal(); + mavenCentral() + mavenLocal() } /* @@ -63,63 +63,66 @@ repositories { */ dependencies { compile(group: "com.github.fge", name: "java7-fs-base", - version: "0.0.2-SNAPSHOT"); + version: "0.0.2-SNAPSHOT") compile(group: "com.dropbox.core", name: "dropbox-core-sdk", - version: "1.7.7"); + version: "3.0.4") provided(group: "com.google.code.findbugs", name: "jsr305", - version: "3.0.0"); + version: "3.0.0") testCompile(group: "org.testng", name: "testng", version: "6.8.8") { - exclude(group: "junit", module: "junit"); - exclude(group: "org.beanshell", module: "bsh"); - exclude(group: "org.yaml", module: "snakeyaml"); - }; - testCompile(group: "org.mockito", name: "mockito-core", version: "1.10.8"); - testCompile(group: "org.assertj", name: "assertj-core", version: "1.7.0"); + exclude(group: "junit", module: "junit") + exclude(group: "org.beanshell", module: "bsh") + exclude(group: "org.yaml", module: "snakeyaml") + } + testCompile(group: "org.mockito", name: "mockito-core", version: "1.10.8") + testCompile(group: "org.assertj", name: "assertj-core", version: "1.7.0") + + testCompile(group: "com.github.marschall", name: "memoryfilesystem", version: "1.0.1") + } -javadoc.options.links("http://docs.oracle.com/javase/7/docs/api/"); -javadoc.options.links("http://jsr-305.googlecode.com/svn/trunk/javadoc/"); -javadoc.options.links("http://dropbox.github.io/dropbox-sdk-java/api-docs/v1.7.x/"); +javadoc.options.links("http://docs.oracle.com/javase/7/docs/api/") +javadoc.options.links("http://jsr-305.googlecode.com/svn/trunk/javadoc/") +javadoc.options.links("http://dropbox.github.io/dropbox-sdk-java/api-docs/v1.7.x/") /* * Necessary! Otherwise TestNG will not be used... */ test { useTestNG() { - useDefaultListeners = true; - }; + useDefaultListeners = true + } } pitest { - pitestVersion = "1.1.2"; // see https://github.com/hcoles/pitest/issues/150 + pitestVersion = "1.1.2" // see https://github.com/hcoles/pitest/issues/150 } /* * Necessary to generate the source and javadoc jars */ task sourcesJar(type: Jar, dependsOn: classes) { - classifier = "sources"; - from sourceSets.main.allSource; + classifier = "sources" + from sourceSets.main.allSource } task javadocJar(type: Jar, dependsOn: javadoc) { - classifier = "javadoc"; - from javadoc.destinationDir; + classifier = "javadoc" + from javadoc.destinationDir } artifacts { - archives jar; - archives sourcesJar; - archives javadocJar; + archives jar + archives sourcesJar + archives javadocJar } task wrapper(type: Wrapper) { - gradleVersion = "2.2"; - distributionUrl = "http://services.gradle.org/distributions/gradle-${gradleVersion}-all.zip"; + gradleVersion = "2.2" + distributionUrl = "http://services.gradle.org/distributions/gradle-${gradleVersion}-all.zip" } task pom << { - pom {}.writeTo("${projectDir}/pom.xml"); + pom {}.writeTo("${projectDir}/pom.xml") } /* @@ -127,47 +130,47 @@ task pom << { */ project.ext { - gitrwscm = sprintf("git@github.com:fge/%s", name); - gitroscm = sprintf("https://github.com/fge/%s.git", name); - projectURL = sprintf("https://github.com/fge/%s", name); - sonatypeStaging = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"; - sonatypeSnapshots = "https://oss.sonatype.org/content/repositories/snapshots/"; -}; + gitrwscm = sprintf("git@github.com:fge/%s", name) + gitroscm = sprintf("https://github.com/fge/%s.git", name) + projectURL = sprintf("https://github.com/fge/%s", name) + sonatypeStaging = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" + sonatypeSnapshots = "https://oss.sonatype.org/content/repositories/snapshots/" +} task checkSigningRequirements << { - def requiredProperties = [ "sonatypeUsername", "sonatypePassword" ]; - def noDice = false; + def requiredProperties = [ "sonatypeUsername", "sonatypePassword" ] + def noDice = false requiredProperties.each { if (project.properties[it] == null) { - noDice = true; + noDice = true System.err.printf("property \"%s\" is not defined!") } } if (noDice) throw new IllegalStateException("missing required properties for " + - "upload"); + "upload") } uploadArchives { - dependsOn(checkSigningRequirements); + dependsOn(checkSigningRequirements) repositories { mavenDeployer { beforeDeployment { - MavenDeployment deployment -> signing.signPom(deployment); + MavenDeployment deployment -> signing.signPom(deployment) } repository(url: "${sonatypeStaging}") { authentication( userName: project.properties["sonatypeUsername"], password: project.properties["sonatypePassword"] - ); + ) } snapshotRepository(url: "${sonatypeSnapshots}") { authentication( userName: project.properties["sonatypeUsername"], password: project.properties["sonatypePassword"] - ); + ) } } } @@ -181,43 +184,43 @@ uploadArchives { uploadArchives.repositories.mavenDeployer ]*.pom*.whenConfigured { pom -> pom.project { - name "${project.name}"; - packaging "jar"; - description "${project.description}"; - url "${projectURL}"; + name "${project.name}" + packaging "jar" + description "${project.description}" + url "${projectURL}" scm { - url "${gitrwscm}"; - connection "${gitrwscm}"; - developerConnection "${gitroscm}"; + url "${gitrwscm}" + connection "${gitrwscm}" + developerConnection "${gitroscm}" } licenses { license { - name "Lesser General Public License, version 3 or greater"; - url "http://www.gnu.org/licenses/lgpl.html"; - distribution "repo"; - }; + name "Lesser General Public License, version 3 or greater" + url "http://www.gnu.org/licenses/lgpl.html" + distribution "repo" + } license { - name "Apache Software License, version 2.0"; - url "http://www.apache.org/licenses/LICENSE-2.0"; - distribution "repo"; + name "Apache Software License, version 2.0" + url "http://www.apache.org/licenses/LICENSE-2.0" + distribution "repo" } } developers { developer { - id "fge"; - name "Francis Galiegue"; - email "fgaliegue@gmail.com"; + id "fge" + name "Francis Galiegue" + email "fgaliegue@gmail.com" } } } } -ext.forRelease = !version.endsWith("-SNAPSHOT"); +ext.forRelease = !version.endsWith("-SNAPSHOT") signing { - required { forRelease && gradle.taskGraph.hasTask("uploadArchives") }; - sign configurations.archives; + required { forRelease && gradle.taskGraph.hasTask("uploadArchives") } + sign configurations.archives } diff --git a/src/main/java/com/github/fge/fs/dropbox/attr/DropBoxBasicFileAttributesProvider.java b/src/main/java/com/github/fge/fs/dropbox/attr/DropBoxBasicFileAttributesProvider.java index 1a66858..286b10a 100644 --- a/src/main/java/com/github/fge/fs/dropbox/attr/DropBoxBasicFileAttributesProvider.java +++ b/src/main/java/com/github/fge/fs/dropbox/attr/DropBoxBasicFileAttributesProvider.java @@ -1,13 +1,14 @@ package com.github.fge.fs.dropbox.attr; -import com.dropbox.core.DbxEntry; +import com.dropbox.core.v2.files.FileMetadata; +import com.dropbox.core.v2.files.FolderMetadata; +import com.dropbox.core.v2.files.Metadata; import com.github.fge.filesystem.attributes.provider.BasicFileAttributesProvider; import javax.annotation.Nonnull; import java.io.IOException; import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.FileTime; -import java.util.Objects; /** * {@link BasicFileAttributes} implementation for DropBox @@ -20,13 +21,11 @@ public final class DropBoxBasicFileAttributesProvider extends BasicFileAttributesProvider { - private final DbxEntry.File fileEntry; + private final Metadata metadata; - public DropBoxBasicFileAttributesProvider(@Nonnull final DbxEntry entry) - throws IOException + public DropBoxBasicFileAttributesProvider(@Nonnull final Metadata metadata) throws IOException { - fileEntry = Objects.requireNonNull(entry).isFolder() - ? entry.asFile() : null; + this.metadata = metadata; } /** @@ -42,8 +41,14 @@ public DropBoxBasicFileAttributesProvider(@Nonnull final DbxEntry entry) @Override public FileTime lastModifiedTime() { - return fileEntry == null ? UNIX_EPOCH - : FileTime.fromMillis(fileEntry.lastModified.getTime()); + if (metadata instanceof FileMetadata) + { + return FileTime.fromMillis(((FileMetadata) metadata).getServerModified().getTime()); + } + else + { + return UNIX_EPOCH; + } } /** @@ -52,7 +57,7 @@ public FileTime lastModifiedTime() @Override public boolean isRegularFile() { - return fileEntry != null; + return metadata instanceof FileMetadata; } /** @@ -61,7 +66,7 @@ public boolean isRegularFile() @Override public boolean isDirectory() { - return fileEntry == null; + return metadata instanceof FolderMetadata; } /** @@ -76,6 +81,13 @@ public boolean isDirectory() @Override public long size() { - return fileEntry == null ? 0L : fileEntry.numBytes; + if (metadata instanceof FileMetadata) + { + return ((FileMetadata) metadata).getSize(); + } + else + { + return 0L; + } } } diff --git a/src/main/java/com/github/fge/fs/dropbox/attr/DropBoxFileAttributesFactory.java b/src/main/java/com/github/fge/fs/dropbox/attr/DropBoxFileAttributesFactory.java index b13c319..8d44bbb 100644 --- a/src/main/java/com/github/fge/fs/dropbox/attr/DropBoxFileAttributesFactory.java +++ b/src/main/java/com/github/fge/fs/dropbox/attr/DropBoxFileAttributesFactory.java @@ -1,6 +1,6 @@ package com.github.fge.fs.dropbox.attr; -import com.dropbox.core.DbxEntry; +import com.dropbox.core.v2.files.Metadata; import com.github.fge.filesystem.attributes.FileAttributesFactory; public final class DropBoxFileAttributesFactory @@ -8,7 +8,7 @@ public final class DropBoxFileAttributesFactory { public DropBoxFileAttributesFactory() { - setMetadataClass(DbxEntry.class); + setMetadataClass(Metadata.class); addImplementation("basic", DropBoxBasicFileAttributesProvider.class); } } diff --git a/src/main/java/com/github/fge/fs/dropbox/driver/DropBoxFileSystemDriver.java b/src/main/java/com/github/fge/fs/dropbox/driver/DropBoxFileSystemDriver.java index acb6ba5..a9801cb 100644 --- a/src/main/java/com/github/fge/fs/dropbox/driver/DropBoxFileSystemDriver.java +++ b/src/main/java/com/github/fge/fs/dropbox/driver/DropBoxFileSystemDriver.java @@ -1,9 +1,17 @@ package com.github.fge.fs.dropbox.driver; -import com.dropbox.core.DbxClient; -import com.dropbox.core.DbxEntry; +import com.dropbox.core.DbxDownloader; import com.dropbox.core.DbxException; -import com.dropbox.core.DbxWriteMode; +import com.dropbox.core.v2.DbxClientV2; +import com.dropbox.core.v2.files.CreateFolderErrorException; +import com.dropbox.core.v2.files.CreateFolderResult; +import com.dropbox.core.v2.files.FileMetadata; +import com.dropbox.core.v2.files.FolderMetadata; +import com.dropbox.core.v2.files.GetMetadataErrorException; +import com.dropbox.core.v2.files.Metadata; +import com.dropbox.core.v2.files.RelocationResult; +import com.dropbox.core.v2.files.UploadUploader; +import com.dropbox.core.v2.files.WriteMode; import com.github.fge.filesystem.driver.UnixLikeFileSystemDriverBase; import com.github.fge.filesystem.exceptions.IsDirectoryException; import com.github.fge.filesystem.provider.FileSystemFactoryProvider; @@ -21,11 +29,13 @@ import java.nio.file.CopyOption; import java.nio.file.DirectoryNotEmptyException; import java.nio.file.DirectoryStream; +import java.nio.file.FileAlreadyExistsException; import java.nio.file.FileStore; import java.nio.file.NoSuchFileException; import java.nio.file.NotDirectoryException; import java.nio.file.OpenOption; import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.nio.file.attribute.FileAttribute; import java.nio.file.spi.FileSystemProvider; import java.util.ArrayList; @@ -39,10 +49,10 @@ public final class DropBoxFileSystemDriver extends UnixLikeFileSystemDriverBase { - private final DbxClient client; + private final DbxClientV2 client; public DropBoxFileSystemDriver(final FileStore fileStore, - final FileSystemFactoryProvider provider, final DbxClient client) + final FileSystemFactoryProvider provider, final DbxClientV2 client) { super(fileStore, provider); this.client = client; @@ -54,24 +64,17 @@ public InputStream newInputStream(final Path path, final Set options) throws IOException { - // TODO: need a "shortcut" way for that; it's quite common final String target = path.toRealPath().toString(); - final DbxEntry entry; - - try { - entry = client.getMetadata(target); - } catch (DbxException e) { - throw DropBoxIOException.wrap(e); - } + final Metadata metadata = getMetadata(target); // TODO: metadata driver - if (entry.isFolder()) + if (metadata instanceof FolderMetadata) throw new IsDirectoryException(target); - final DbxClient.Downloader downloader; + final DbxDownloader downloader; try { - downloader = client.startGetFile(target, null); + downloader = client.files().download(target); } catch (DbxException e) { throw new DropBoxIOException(e); } @@ -85,23 +88,24 @@ public OutputStream newOutputStream(final Path path, final Set options) throws IOException { - // TODO: need a "shortcut" way for that; it's quite common final String target = path.toRealPath().toString(); - final DbxEntry entry; - + Metadata metadata = null; try { - entry = client.getMetadata(target); - } catch (DbxException e) { - throw new DropBoxIOException(e); + metadata = getMetadata(target); + } catch (NoSuchFileException e) { + //ignore } // TODO: metadata - if (entry != null) - if (entry.isFolder()) - throw new IsDirectoryException(target); + if (metadata instanceof FolderMetadata) + throw new IsDirectoryException(target); - final DbxClient.Uploader uploader - = client.startUploadFileChunked(target, DbxWriteMode.force(), -1L); + final UploadUploader uploader; + try { + uploader = client.files().uploadBuilder(target).withMode(WriteMode.OVERWRITE).start(); + } catch (DbxException e) { + throw new DropBoxIOException(e); + } return new DropBoxOutputStream(uploader); } @@ -112,23 +116,22 @@ public DirectoryStream newDirectoryStream(final Path dir, final DirectoryStream.Filter filter) throws IOException { - // TODO: need a "shortcut" way for that; it's quite common final String target = dir.toRealPath().toString(); - final DbxEntry.WithChildren dirent; + final Metadata dirMetadata = getMetadata(target); + + if (!(dirMetadata instanceof FolderMetadata)) + throw new NotDirectoryException(target); + + final List children; try { - dirent = client.getMetadataWithChildren(target); + children = client.files().listFolder(target).getEntries(); } catch (DbxException e) { throw new DropBoxIOException(e); } - - if (!dirent.entry.isFolder()) - throw new NotDirectoryException(target); - - final List children = dirent.children; final List list = new ArrayList<>(children.size()); - for (final DbxEntry child: children) - list.add(dir.resolve(child.name)); + for (final Metadata child: children) + list.add(dir.resolve(child.getName())); //noinspection AnonymousInnerClassWithTooManyMethods return new DirectoryStream() @@ -159,10 +162,16 @@ public void createDirectory(final Path dir, final FileAttribute... attrs) // TODO: need a "shortcut" way for that; it's quite common final String target = dir.toRealPath().toString(); - try { + try + { // TODO: how to diagnose? - if (client.createFolder(target) == null) + CreateFolderResult folderV2 = client.files().createFolderV2(target); + if (folderV2.getMetadata() == null) throw new DropBoxIOException("cannot create directory??"); + } catch (CreateFolderErrorException e) { + if (e.errorValue.getPathValue().isNoWritePermission()) + throw new AccessDeniedException(target); + //TODO: test for other errors } catch (DbxException e) { throw DropBoxIOException.wrap(e); } @@ -172,94 +181,88 @@ public void createDirectory(final Path dir, final FileAttribute... attrs) public void delete(final Path path) throws IOException { - // TODO: need a "shortcut" way for that; it's quite common final String target = path.toRealPath().toString(); + final Metadata metadata = getMetadata(target); - final DbxEntry.WithChildren entry; - + // TODO: metadata! try { - entry = client.getMetadataWithChildren(target); + if (metadata instanceof FolderMetadata && client.files().listFolder(target).getEntries().size() > 0) + throw new DirectoryNotEmptyException(target); } catch (DbxException e) { throw DropBoxIOException.wrap(e); } - // TODO: metadata! - if (entry.entry.isFolder() && !entry.children.isEmpty()) - throw new DirectoryNotEmptyException(target); - try { - client.delete(target); + client.files().deleteV2(target); } catch (DbxException e) { throw DropBoxIOException.wrap(e); } } @Override - public void copy(final Path source, final Path target, - final Set options) + public void copy(final Path source, final Path target, final Set options) throws IOException { - final String srcpath = source.toRealPath().toString(); - final String dstpath = source.toRealPath().toString(); - - final DbxEntry.WithChildren dstentry; + final String srcPath = source.toRealPath().toString(); + final String dstPath = target.toRealPath().toString(); + Metadata dstMetadata = null; try { - dstentry = client.getMetadataWithChildren(dstpath); - } catch (DbxException e) { - throw DropBoxIOException.wrap(e); + dstMetadata = getMetadata(dstPath); + } catch (NoSuchFileException e) { + //ignore } - if (dstentry != null) { - if (dstentry.entry.isFolder() && !dstentry.children.isEmpty()) - throw new DirectoryNotEmptyException(dstpath); - // TODO: unknown what happens when a copy operation is performed - try { - client.delete(dstpath); - } catch (DbxException e) { - throw DropBoxIOException.wrap(e); - } - } + replaceExisting(dstPath, options, dstMetadata); try { // TODO: how to diagnose? - if (client.copy(srcpath, dstpath) == null) + RelocationResult relocationResult = client.files().copyV2(srcPath, dstPath); + + if (relocationResult == null) throw new DropBoxIOException("cannot copy??"); } catch (DbxException e) { throw new DropBoxIOException(e); } } + private void replaceExisting(String destination, Set options, Metadata dstMetadata) + throws DirectoryNotEmptyException, DropBoxIOException, FileAlreadyExistsException + { + if (dstMetadata != null) { + if (options.contains(StandardCopyOption.REPLACE_EXISTING)) { + try { + if (dstMetadata instanceof FolderMetadata && client.files().listFolder(destination).getEntries().size() > 0) + throw new DirectoryNotEmptyException(destination); + + client.files().deleteV2(destination); + } catch (DbxException e) { + throw DropBoxIOException.wrap(e); + } + } else { + throw new FileAlreadyExistsException(destination); + } + } + } + @Override - public void move(final Path source, final Path target, - final Set options) + public void move(final Path source, final Path target, final Set options) throws IOException { - final String srcpath = source.toRealPath().toString(); - final String dstpath = source.toRealPath().toString(); - - final DbxEntry.WithChildren dstentry; + final String srcPath = source.toRealPath().toString(); + final String dstPath = target.toRealPath().toString(); + Metadata dstMetadata = null; try { - dstentry = client.getMetadataWithChildren(dstpath); - } catch (DbxException e) { - throw DropBoxIOException.wrap(e); + dstMetadata = getMetadata(dstPath); + } catch (NoSuchFileException e) { + //ignore } - if (dstentry != null) { - if (dstentry.entry.isFolder() && !dstentry.children.isEmpty()) - throw new DirectoryNotEmptyException(dstpath); - // TODO: unknown what happens when a move operation is performed - // and the target already exists - try { - client.delete(dstpath); - } catch (DbxException e) { - throw DropBoxIOException.wrap(e); - } - } + replaceExisting(dstPath, options, dstMetadata); try { - client.move(srcpath, dstpath); + client.files().moveV2(srcPath, dstPath); } catch (DbxException e) { throw DropBoxIOException.wrap(e); } @@ -281,18 +284,12 @@ public void checkAccess(final Path path, final AccessMode... modes) { final String target = path.toRealPath().toString(); - final DbxEntry entry; - - try { - entry = client.getMetadata(target); - } catch (DbxException e) { - throw DropBoxIOException.wrap(e); - } + final Metadata metadata = getMetadata(target); - if (entry == null) + if (metadata == null) throw new NoSuchFileException(target); - if (!entry.isFile()) + if (!(metadata instanceof FileMetadata)) return; // TODO: assumed; not a file == directory @@ -301,6 +298,23 @@ public void checkAccess(final Path path, final AccessMode... modes) throw new AccessDeniedException(target); } + private Metadata getMetadata(String target) throws NoSuchFileException, DropBoxIOException + { + Metadata metadata; + try { + metadata = client.files().getMetadata(target); + } catch (GetMetadataErrorException e) { + if (e.errorValue.isPath() && e.errorValue.getPathValue().isNotFound()) { + throw new NoSuchFileException(target); + } else { + throw DropBoxIOException.wrap(e); + } + } catch (DbxException e) { + throw DropBoxIOException.wrap(e); + } + return metadata; + } + @Override public void close() throws IOException @@ -314,7 +328,7 @@ public Object getPathMetadata(final Path path) throws IOException { try { - return client.getMetadata(path.toRealPath().toString()); + return client.files().getMetadata(path.toRealPath().toString()); } catch (DbxException e) { throw DropBoxIOException.wrap(e); } diff --git a/src/main/java/com/github/fge/fs/dropbox/filestore/DropBoxFileStore.java b/src/main/java/com/github/fge/fs/dropbox/filestore/DropBoxFileStore.java index 4d963f4..37e2747 100644 --- a/src/main/java/com/github/fge/fs/dropbox/filestore/DropBoxFileStore.java +++ b/src/main/java/com/github/fge/fs/dropbox/filestore/DropBoxFileStore.java @@ -1,9 +1,8 @@ package com.github.fge.fs.dropbox.filestore; -import com.dropbox.core.DbxAccountInfo; -import com.dropbox.core.DbxAccountInfo.Quota; -import com.dropbox.core.DbxClient; import com.dropbox.core.DbxException; +import com.dropbox.core.v2.DbxClientV2; +import com.dropbox.core.v2.users.SpaceUsage; import com.github.fge.filesystem.attributes.FileAttributesFactory; import com.github.fge.filesystem.filestore.FileStoreBase; @@ -13,20 +12,20 @@ /** * A simple DropBox {@link FileStore} * - *

This makes use of information available in {@link DbxAccountInfo.Quota}. + *

This makes use of information available in {@link SpaceUsage}. * Information is computed in "real time".

*/ public final class DropBoxFileStore extends FileStoreBase { - private final DbxClient client; + private final DbxClientV2 client; /** * Constructor * * @param client the (valid) DropBox client to use */ - public DropBoxFileStore(final DbxClient client, + public DropBoxFileStore(final DbxClientV2 client, final FileAttributesFactory factory) { super("dropbox", factory, false); @@ -44,7 +43,7 @@ public DropBoxFileStore(final DbxClient client, public long getTotalSpace() throws IOException { - return getQuota().total; + return getSpaceUsage().getAllocation().getTeamValue().getAllocated(); } /** @@ -66,8 +65,9 @@ public long getTotalSpace() public long getUsableSpace() throws IOException { - final Quota quota = getQuota(); - return quota.total - quota.normal; + final SpaceUsage spaceUsage = getSpaceUsage(); + //TODO: team or individual valeu? + return spaceUsage.getAllocation().getTeamValue().getAllocated() - spaceUsage.getUsed(); } /** @@ -87,15 +87,16 @@ public long getUsableSpace() public long getUnallocatedSpace() throws IOException { - final Quota quota = getQuota(); - return quota.total - quota.normal; + final SpaceUsage spaceUsage = getSpaceUsage(); + //TODO: team or individual valeu? + return spaceUsage.getAllocation().getTeamValue().getAllocated() - spaceUsage.getUsed(); } - private Quota getQuota() + private SpaceUsage getSpaceUsage() throws IOException { try { - return client.getAccountInfo().quota; + return client.users().getSpaceUsage(); } catch (DbxException e) { throw new IOException("cannot get quota info from account", e); } diff --git a/src/main/java/com/github/fge/fs/dropbox/misc/DropBoxIOException.java b/src/main/java/com/github/fge/fs/dropbox/misc/DropBoxIOException.java index 0142481..e703b09 100644 --- a/src/main/java/com/github/fge/fs/dropbox/misc/DropBoxIOException.java +++ b/src/main/java/com/github/fge/fs/dropbox/misc/DropBoxIOException.java @@ -1,7 +1,7 @@ package com.github.fge.fs.dropbox.misc; -import com.dropbox.core.DbxClient; import com.dropbox.core.DbxException; +import com.dropbox.core.v1.DbxClientV1; import java.io.Closeable; import java.io.IOException; @@ -10,8 +10,8 @@ * Class used as a wrapper over the DropBox API's unchecked exceptions when * an {@link IOException} is needed * - *

The problem mainly comes from {@link DbxClient.Uploader} and {@link - * DbxClient.Uploader}. Both of these methods define a {@code close()} method + *

The problem mainly comes from {@link DbxClientV1.Uploader} and {@link + * DbxClientV1.Uploader}. Both of these methods define a {@code close()} method * but none of them implement {@link Closeable}. Worse than that, at least as * far as the uploader is concerned, this method is not even idempotent, and all * exceptions it throws are unchecked.

diff --git a/src/main/java/com/github/fge/fs/dropbox/misc/DropBoxInputStream.java b/src/main/java/com/github/fge/fs/dropbox/misc/DropBoxInputStream.java index 6898892..452b6a4 100644 --- a/src/main/java/com/github/fge/fs/dropbox/misc/DropBoxInputStream.java +++ b/src/main/java/com/github/fge/fs/dropbox/misc/DropBoxInputStream.java @@ -1,6 +1,8 @@ package com.github.fge.fs.dropbox.misc; -import com.dropbox.core.DbxClient; +import com.dropbox.core.DbxDownloader; +import com.dropbox.core.v1.DbxClientV1; +import com.dropbox.core.v2.files.FileMetadata; import com.github.fge.filesystem.driver.FileSystemDriver; import javax.annotation.ParametersAreNonnullByDefault; @@ -10,7 +12,7 @@ import java.nio.file.Path; /** - * Wrapper over {@link DbxClient.Downloader} extending {@link InputStream} + * Wrapper over {@link DbxClientV1.Downloader} extending {@link InputStream} * *

This class wraps a DropBox downloader class by extending {@code * InputStream} and delegating all of its methods to the downloader's @@ -22,7 +24,7 @@ * throw an exception; which means it may throw none, or it may throw an * unchecked exception. As such, the {@link #close()} method of this * class captures all {@link RuntimeException}s which {@link - * DbxClient.Downloader#close()} may throw and wrap it into a {@link + * DbxClientV1.Downloader#close()} may throw and wrap it into a {@link * DropBoxIOException}. If the underlying input stream did throw an * exception, however, then such an exception is {@link * Throwable#addSuppressed(Throwable) suppressed}.

@@ -34,13 +36,13 @@ public final class DropBoxInputStream extends InputStream { - private final DbxClient.Downloader downloader; + private final DbxDownloader downloader; private final InputStream delegate; - public DropBoxInputStream(final DbxClient.Downloader downloader) + public DropBoxInputStream(final DbxDownloader downloader) { this.downloader = downloader; - delegate = downloader.body; + delegate = downloader.getInputStream(); } @Override diff --git a/src/main/java/com/github/fge/fs/dropbox/misc/DropBoxOutputStream.java b/src/main/java/com/github/fge/fs/dropbox/misc/DropBoxOutputStream.java index 3deb5fa..bda9c42 100644 --- a/src/main/java/com/github/fge/fs/dropbox/misc/DropBoxOutputStream.java +++ b/src/main/java/com/github/fge/fs/dropbox/misc/DropBoxOutputStream.java @@ -1,7 +1,8 @@ package com.github.fge.fs.dropbox.misc; -import com.dropbox.core.DbxClient; import com.dropbox.core.DbxException; +import com.dropbox.core.v1.DbxClientV1; +import com.dropbox.core.v2.files.UploadUploader; import javax.annotation.Nonnull; import java.io.IOException; @@ -10,7 +11,7 @@ import java.util.concurrent.atomic.AtomicBoolean; /** - * Wrapper over {@link DbxClient.Uploader} extending {@link OutputStream} + * Wrapper over {@link DbxClientV1.Uploader} extending {@link OutputStream} * *

This class wraps a DropBox downloader class by extending {@code * InputStream} and delegating all of its methods to the downloader's @@ -22,7 +23,7 @@ * throw an exception; which means it may throw none, or it may throw an * unchecked exception. As such, the {@link #close()} method of this * class captures all {@link RuntimeException}s which {@link - * DbxClient.Uploader#close()} may throw and wrap it into a {@link + * DbxClientV1.Uploader#close()} may throw and wrap it into a {@link * DropBoxIOException}. If the underlying output stream did throw an * exception, however, then such an exception is {@link * Throwable#addSuppressed(Throwable) suppressed}.

@@ -34,13 +35,14 @@ public final class DropBoxOutputStream { private final AtomicBoolean closeCalled = new AtomicBoolean(false); - private final DbxClient.Uploader uploader; + private final UploadUploader uploader; private final OutputStream out; - public DropBoxOutputStream(@Nonnull final DbxClient.Uploader uploader) + public DropBoxOutputStream(@Nonnull final UploadUploader uploader) { + this.uploader = Objects.requireNonNull(uploader); - out = uploader.getBody(); + out = uploader.getOutputStream(); } @Override diff --git a/src/main/java/com/github/fge/fs/dropbox/provider/DropBoxFileSystemRepository.java b/src/main/java/com/github/fge/fs/dropbox/provider/DropBoxFileSystemRepository.java index a559818..431fd9f 100644 --- a/src/main/java/com/github/fge/fs/dropbox/provider/DropBoxFileSystemRepository.java +++ b/src/main/java/com/github/fge/fs/dropbox/provider/DropBoxFileSystemRepository.java @@ -1,6 +1,6 @@ package com.github.fge.fs.dropbox.provider; -import com.dropbox.core.DbxClient; +import com.dropbox.core.v2.DbxClientV2; import com.dropbox.core.DbxRequestConfig; import com.github.fge.filesystem.driver.FileSystemDriver; import com.github.fge.filesystem.provider.FileSystemRepositoryBase; @@ -36,8 +36,8 @@ public FileSystemDriver createDriver(final URI uri, if (accessToken == null) throw new IllegalArgumentException("access token not found"); - final DbxRequestConfig config = new DbxRequestConfig(NAME, LOCALE); - final DbxClient client = new DbxClient(config, accessToken); + final DbxRequestConfig config = DbxRequestConfig.newBuilder(NAME).withUserLocale(LOCALE).build(); + final DbxClientV2 client = new DbxClientV2(config, accessToken); final DropBoxFileStore fileStore = new DropBoxFileStore(client, factoryProvider.getAttributesFactory()); diff --git a/src/test/java/com/github/fge/fs/dropbox/FileSystemTest.java b/src/test/java/com/github/fge/fs/dropbox/FileSystemTest.java new file mode 100644 index 0000000..813ce1a --- /dev/null +++ b/src/test/java/com/github/fge/fs/dropbox/FileSystemTest.java @@ -0,0 +1,451 @@ +package com.github.fge.fs.dropbox; + +import com.dropbox.core.util.IOUtil; +import com.github.fge.filesystem.exceptions.ReadOnlyAttributeException; +import com.github.fge.filesystem.provider.FileSystemRepository; +import com.github.fge.fs.dropbox.misc.DropBoxIOException; +import com.github.fge.fs.dropbox.provider.DropBoxFileSystemProvider; +import com.github.fge.fs.dropbox.provider.DropBoxFileSystemRepository; +import com.github.marschall.memoryfilesystem.MemoryFileSystemBuilder; +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.nio.file.AccessDeniedException; +import java.nio.file.AccessMode; +import java.nio.file.DirectoryStream; +import java.nio.file.FileAlreadyExistsException; +import java.nio.file.FileSystem; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.nio.file.spi.FileSystemProvider; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +public class FileSystemTest +{ + private static final String ACCESS_TOKEN = "AYN4WsiaBTgAAAAAAAAAIL4iJnTphS5Ou9kbp3Wu2xA0BvktlbJPWCDukj76qhbs"; + private static FileSystemProvider provider; + private static URI uri; + private static Map env; + private static String testDirectoryPath; + + + @BeforeClass + public static void setUpClass() throws Exception + { + uri = URI.create("dropbox://foo/"); + env = new HashMap<>(); + env.put("accessToken", ACCESS_TOKEN); + + final FileSystemRepository repository = new DropBoxFileSystemRepository(); + provider = new DropBoxFileSystemProvider(repository); + + testDirectoryPath = "/test-" + UUID.randomUUID().toString(); + + try (final FileSystem dropboxfs = provider.newFileSystem(uri, env)) + { + Files.createDirectory(dropboxfs.getPath(testDirectoryPath)); + } + } + + @BeforeMethod + public void setUp() throws Exception + { + } + + @AfterClass + public static void tearDown() throws Exception + { + try (final FileSystem dropboxfs = provider.newFileSystem(uri, env)) + { + deleteRecursive(dropboxfs, dropboxfs.getPath(testDirectoryPath)); + } + } + + private static void deleteRecursive(FileSystem fs, Path directoryPath) throws IOException + { + if (Files.exists(directoryPath)) + { + try (DirectoryStream paths= Files.newDirectoryStream(directoryPath)) + { + for (Path path : paths) + { + if (Files.isDirectory(path)) + { + deleteRecursive(fs, path); + } + else + { + Files.delete(fs.getPath(path.toString())); + } + } + } + Files.delete(fs.getPath(directoryPath.toString())); + } + } + + @Test(expectedExceptions = DropBoxIOException.class) + public void testDirectoryStreamRoot() throws IOException + { + try (final FileSystem dropboxfs = provider.newFileSystem(uri, env)) + { + Files.newDirectoryStream(dropboxfs.getPath("")); + } + } + + @Test + public void testDirectoryStream() throws IOException + { + try (final FileSystem dropboxfs = provider.newFileSystem(uri, env)) + { + Files.newDirectoryStream(dropboxfs.getPath(testDirectoryPath)); + } + } + + @Test + public void testCreateFile() throws IOException + { + boolean thrown = false; + try (final FileSystem dropboxfs = provider.newFileSystem(uri, env)) + { + Path path = dropboxfs.getPath(testDirectoryPath + "/create_test"); + Files.createFile(path); + } + catch (UnsupportedOperationException e) + { + thrown = true; + } + Assert.assertTrue(thrown); + } + + @Test + public void testCreateDirectory() throws IOException + { + try (final FileSystem dropboxfs = provider.newFileSystem(uri, env)) + { + for (int i = 1; i <= 5; i++) + { + Path path = dropboxfs.getPath(testDirectoryPath + "/create_directory_test-" + i); + Path file = Files.createDirectory(path); + Assert.assertNotNull(file); + Assert.assertTrue(Files.exists(path)); + } + } + } + + @Test + public void testCreateDirectories() throws IOException + { + try (final FileSystem dropboxfs = provider.newFileSystem(uri, env)) + { + for (int i = 1; i <= 5; i++) + { + Path path = dropboxfs.getPath(testDirectoryPath + "/create_directories_test-" + i + "/sub_dir-" + i); + Path file = Files.createDirectories(path); + Assert.assertNotNull(file); + Assert.assertTrue(Files.exists(path)); + } + } + } + + @Test + public void testCreateDirectoryAlreadyExists() throws Exception + { + try (final FileSystem dropboxfs = provider.newFileSystem(uri, env)) + { + Path path = dropboxfs.getPath(testDirectoryPath + "/create_directory_already_exists_test"); + Files.createDirectory(path); + + boolean thrown = false; + try { + Files.createDirectory(path); + } catch (FileAlreadyExistsException e) { + thrown = true; + } + Assert.assertTrue(thrown); + } + } + + @Test + public void testCopy() throws IOException + { + try + ( + final FileSystem memfs = MemoryFileSystemBuilder.newEmpty().build("test"); + final FileSystem dropboxfs = provider.newFileSystem(uri, env) + ) + { + String contentOriginal = "This will be copied"; + String contentCopy; + + //create the source file in memory + Path source = memfs.getPath("/copy.txt"); + Files.createFile(source); + Files.write(source, contentOriginal.getBytes()); + + //create the destination on Dropbox + Path dest = dropboxfs.getPath(testDirectoryPath + "/copy.txt"); + + //copy to Dropbox + Files.copy(source, dest); + Assert.assertTrue(Files.exists(dest)); + Assert.assertTrue(Files.isRegularFile(dest)); + try (InputStream in = Files.newInputStream(dest)) + { + contentCopy = IOUtil.toUtf8String(in); //inputstream, SeekableByteChannel not supported + } + Assert.assertEquals(contentOriginal, contentCopy); + + Files.delete(source); + + //copy from Dropbox + Files.copy(dest, source); + Assert.assertTrue(Files.exists(source)); + Assert.assertTrue(Files.isRegularFile(source)); + contentCopy = new String(Files.readAllBytes(source)); + Assert.assertEquals(contentOriginal, contentCopy); + } + } + + @Test + public void testCopyAlreadyExists() throws IOException + { + try + ( + final FileSystem memfs = MemoryFileSystemBuilder.newEmpty().build("test"); + final FileSystem dropboxfs = provider.newFileSystem(uri, env) + ) + { + Path source = memfs.getPath("/copy.txt"); + Files.createFile(source); + Path dest = dropboxfs.getPath(testDirectoryPath + "/copy_already_exists.txt"); + + boolean thrown = false; + + Files.copy(source, dest); + try + { + Files.copy(source, dest); + } catch (FileAlreadyExistsException e) { + thrown = true; + } + Assert.assertTrue(thrown); + } + } + + @Test + public void testCheckAccessFile() throws IOException + { + try (final FileSystem memfs = MemoryFileSystemBuilder.newEmpty().build("test"); + final FileSystem dropboxfs = provider.newFileSystem(uri, env)) { + Path source = memfs.getPath("/file.txt"); + Files.createFile(source); + Path fileExists = dropboxfs.getPath(testDirectoryPath + "/checkAccess_file_exists.txt"); + Files.copy(source, fileExists); + + boolean thrown = false; + + dropboxfs.provider().checkAccess(fileExists); + dropboxfs.provider().checkAccess(fileExists, AccessMode.READ); + dropboxfs.provider().checkAccess(fileExists, AccessMode.WRITE); + try { + dropboxfs.provider().checkAccess(fileExists, AccessMode.EXECUTE); + } catch (AccessDeniedException e) { + thrown = true; + } + Assert.assertTrue(thrown); + } + } + + @Test + public void testCheckAccessNoSuchFile() throws IOException + { + try (final FileSystem memfs = MemoryFileSystemBuilder.newEmpty().build("test"); + final FileSystem dropboxfs = provider.newFileSystem(uri, env)) { + Path noSuchFile = dropboxfs.getPath(testDirectoryPath + "/checkAccess_file_not_found.txt"); + + boolean thrown = false; + + try { + dropboxfs.provider().checkAccess(noSuchFile); + } catch (NoSuchFileException e) { + thrown = true; + } + Assert.assertTrue(thrown); + thrown = false; + try { + dropboxfs.provider().checkAccess(noSuchFile, AccessMode.READ); + } catch (NoSuchFileException e) { + thrown = true; + } + Assert.assertTrue(thrown); + thrown = false; + try { + dropboxfs.provider().checkAccess(noSuchFile, AccessMode.WRITE); + } catch (NoSuchFileException e) { + thrown = true; + } + Assert.assertTrue(thrown); + thrown = false; + try { + dropboxfs.provider().checkAccess(noSuchFile, AccessMode.EXECUTE); + } catch (NoSuchFileException e) { + thrown = true; + } + Assert.assertTrue(thrown); + } + } + + + @Test + public void testCheckAccessDirectory() throws IOException + { + try (final FileSystem memfs = MemoryFileSystemBuilder.newEmpty().build("test"); + final FileSystem dropboxfs = provider.newFileSystem(uri, env)) { + Path directory = Files.createDirectory(dropboxfs.getPath(testDirectoryPath + "/checkAccess_directory")); + + boolean thrown = false; + + try { + dropboxfs.provider().checkAccess(directory); + } catch (NoSuchFileException e) { + thrown = true; + } + Assert.assertFalse(thrown); + thrown = false; + try { + dropboxfs.provider().checkAccess(directory, AccessMode.READ); + } catch (NoSuchFileException e) { + thrown = true; + } + Assert.assertFalse(thrown); + thrown = false; + try { + dropboxfs.provider().checkAccess(directory, AccessMode.WRITE); + } catch (NoSuchFileException e) { + thrown = true; + } + Assert.assertFalse(thrown); + thrown = false; + try { + dropboxfs.provider().checkAccess(directory, AccessMode.EXECUTE); + } catch (NoSuchFileException e) { + thrown = true; + } + Assert.assertFalse(thrown); + } + } + + @Test + public void testMoveDirectory() throws IOException + { + try (final FileSystem dropboxfs = provider.newFileSystem(uri, env)) + { + Path sourcePath = Files.createDirectory(dropboxfs.getPath(testDirectoryPath + "/moveDirectory_dir_source")); + Path targetPath = dropboxfs.getPath(testDirectoryPath + "/moveDirectory_dir_target"); + + Assert.assertFalse(Files.exists(targetPath)); + Files.move(sourcePath, targetPath); + Assert.assertTrue(Files.exists(targetPath)); + } + } + + @Test(expectedExceptions = FileAlreadyExistsException.class) + public void testMoveDirectoryAlreadyExists() throws IOException + { + try (final FileSystem dropboxfs = provider.newFileSystem(uri, env)) + { + Path sourcePath = Files.createDirectory(dropboxfs.getPath(testDirectoryPath + "/moveDirectory_dir_already_exists_source")); + Path targetPath = Files.createDirectory(dropboxfs.getPath(testDirectoryPath + "/moveDirectory_dir_already_exists_target")); + + Files.move(sourcePath, targetPath); + } + } + + @Test + public void testMoveDirectoryReplaceExisting() throws IOException + { + try (final FileSystem dropboxfs = provider.newFileSystem(uri, env)) { + Path sourcePath = Files.createDirectory(dropboxfs.getPath(testDirectoryPath + "/moveDirectory_dir_replace_existing_source")); + Files.createDirectory(dropboxfs.getPath(testDirectoryPath + "/moveDirectory_dir_replace_existing_source/sub-dir")); + Path targetPath = Files.createDirectory(dropboxfs.getPath(testDirectoryPath + "/moveDirectory_dir_replace_existing_target")); + Path targetSubdirectoryPath = dropboxfs.getPath(testDirectoryPath + "/moveDirectory_dir_replace_existing_target/sub-dir"); + + Assert.assertFalse(Files.exists(targetSubdirectoryPath)); + Files.move(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING); + Assert.assertTrue(Files.exists(targetSubdirectoryPath)); + } + } + + @Test + public void testMoveFile() throws IOException + { + try (final FileSystem memfs = MemoryFileSystemBuilder.newEmpty().build("test"); + final FileSystem dropboxfs = provider.newFileSystem(uri, env)) { + Path source = Files.createFile(memfs.getPath("file.txt")); + + Path dropboxSource = dropboxfs.getPath(testDirectoryPath + "/testMoveFile_file.txt"); + Path dropboxTarget = dropboxfs.getPath(testDirectoryPath + "/testMoveFile_renamed_file.txt"); + + Files.copy(source, dropboxSource); + Assert.assertTrue(Files.exists(dropboxSource)); + + Files.move(dropboxSource, dropboxTarget); + Assert.assertTrue(Files.exists(dropboxTarget)); + } + } + + @Test(expectedExceptions = FileAlreadyExistsException.class) + public void testMoveFileAlreadyExists() throws IOException + { + try (final FileSystem memfs = MemoryFileSystemBuilder.newEmpty().build("test"); + final FileSystem dropboxfs = provider.newFileSystem(uri, env)) { + Path source = Files.createFile(memfs.getPath("file.txt")); + + Path dropboxSource = dropboxfs.getPath(testDirectoryPath + "/moveFile_file_already_exists_source.txt"); + Path dropboxTarget = dropboxfs.getPath(testDirectoryPath + "/moveFile_file_already_exists_target.txt"); + + Files.copy(source, dropboxSource); + Files.copy(dropboxSource, dropboxTarget); + Files.move(dropboxSource, dropboxTarget); + } + } + + @Test + public void testMoveFileReplaceExisting() throws IOException + { + try (final FileSystem memfs = MemoryFileSystemBuilder.newEmpty().build("test"); + final FileSystem dropboxfs = provider.newFileSystem(uri, env)) { + Path source = Files.createFile(memfs.getPath("file.txt")); + + Path dropboxSource = dropboxfs.getPath(testDirectoryPath + "/moveFile_file_replace_existing_source.txt"); + Path dropboxTarget = dropboxfs.getPath(testDirectoryPath + "/moveFile_file_replace_existing_target.txt"); + + Files.copy(source, dropboxSource); + Files.copy(dropboxSource, dropboxTarget); + Files.move(dropboxSource, dropboxTarget, StandardCopyOption.REPLACE_EXISTING); + } + } + + @Test(expectedExceptions = ReadOnlyAttributeException.class) + public void testMoveFileToForeignTarget() throws IOException + { + try (final FileSystem memfs = MemoryFileSystemBuilder.newEmpty().build("test"); + final FileSystem dropboxfs = provider.newFileSystem(uri, env)) { + Path source = Files.createFile(memfs.getPath("file.txt")); + Path target = dropboxfs.getPath(testDirectoryPath + "/move_file_to_foreign_target_target.txt"); + + Files.move(source, target); // TODO: BasicFileAttributesProvider is readonly. I don't know if it has to be + } + } + +} diff --git a/src/test/java/com/github/fge/fs/dropbox/attr/DropBoxBasicFileAttributesProviderTest.java b/src/test/java/com/github/fge/fs/dropbox/attr/DropBoxBasicFileAttributesProviderTest.java new file mode 100644 index 0000000..e2b6891 --- /dev/null +++ b/src/test/java/com/github/fge/fs/dropbox/attr/DropBoxBasicFileAttributesProviderTest.java @@ -0,0 +1,114 @@ +package com.github.fge.fs.dropbox.attr; + +import com.dropbox.core.util.LangUtil; +import com.dropbox.core.v2.files.FileMetadata; +import com.dropbox.core.v2.files.FolderMetadata; +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.nio.file.attribute.FileTime; +import java.util.Date; + +public class DropBoxBasicFileAttributesProviderTest +{ +// private DropBoxBasicFileAttributesProvider nullProvider; + private DropBoxBasicFileAttributesProvider fileProvider; + private DropBoxBasicFileAttributesProvider folderProvider; + + private Date date; + private long size; + + @BeforeMethod + public void setUp() throws Exception + { +// nullProvider = new DropBoxBasicFileAttributesProvider(null); //not allowed (@Nonnull) + + date = new Date(); + size = 1L; + FileMetadata fileMetadata = FileMetadata.newBuilder("mockFile", "id", date, date, "123456789", size).build(); + fileProvider = new DropBoxBasicFileAttributesProvider(fileMetadata); + + FolderMetadata folderMetadata = FolderMetadata.newBuilder("mockFolder", "id").build(); + folderProvider = new DropBoxBasicFileAttributesProvider(folderMetadata); + } + + @AfterMethod + public void tearDown() throws Exception + { + } + +// @Test +// public void testLastModifiedTimeNull() throws Exception +// { +// Assert.assertTrue(nullProvider.lastModifiedTime(), FileTime.fromMillis(0L)); +// } + + @Test + public void testLastModifiedTimeFile() throws Exception + { + Assert.assertEquals(fileProvider.lastModifiedTime(), FileTime.fromMillis(LangUtil.truncateMillis(this.date).getTime())); + } + + @Test + public void testLastModifiedTimeFolder() throws Exception + { + Assert.assertEquals(folderProvider.lastModifiedTime(), FileTime.fromMillis(0L)); + } + +// @Test +// public void testIsRegularFileNull() throws Exception +// { +// Assert.assertFalse(nullProvider.isRegularFile()); +// } + + @Test + public void testIsRegularFileFile() throws Exception + { + Assert.assertTrue(fileProvider.isRegularFile()); + } + + @Test + public void testIsRegularFileFolder() throws Exception + { + Assert.assertFalse(folderProvider.isRegularFile()); + } + +// @Test +// public void testIsDirectoryNull() throws Exception +// { +// Assert.assertFalse(nullProvider.isDirectory()); //TODO: not sure if intended, or if this should throw exception +// } + + @Test + public void testIsDirectoryFile() throws Exception + { + Assert.assertFalse(fileProvider.isDirectory()); + } + + @Test + public void testIsDirectoryFolder() throws Exception + { + Assert.assertTrue(folderProvider.isDirectory()); + } + +// @Test +// public void testSizeNull() throws Exception +// { +// Assert.assertEquals(nullProvider.size(), 0L); +// } + + @Test + public void testSizeFile() throws Exception + { + Assert.assertEquals(fileProvider.size(), size); + } + + @Test + public void testSizeFolder() throws Exception + { + Assert.assertEquals(folderProvider.size(), 0L); + } + +} \ No newline at end of file From 144808bc40e03aa5c9bb76dcc2b8fae46f8b02f9 Mon Sep 17 00:00:00 2001 From: S1eth Date: Tue, 17 Oct 2017 15:26:43 +0200 Subject: [PATCH 2/2] access token fix --- src/test/java/com/github/fge/fs/dropbox/FileSystemTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/github/fge/fs/dropbox/FileSystemTest.java b/src/test/java/com/github/fge/fs/dropbox/FileSystemTest.java index 813ce1a..b92dec7 100644 --- a/src/test/java/com/github/fge/fs/dropbox/FileSystemTest.java +++ b/src/test/java/com/github/fge/fs/dropbox/FileSystemTest.java @@ -32,7 +32,7 @@ public class FileSystemTest { - private static final String ACCESS_TOKEN = "AYN4WsiaBTgAAAAAAAAAIL4iJnTphS5Ou9kbp3Wu2xA0BvktlbJPWCDukj76qhbs"; + private static final String ACCESS_TOKEN = "yourAccessTokenHere"; private static FileSystemProvider provider; private static URI uri; private static Map env;