Skip to content

Commit ae468f5

Browse files
authored
fix: update CloudStorageFileSystemProvider#getFileAttributeView to return null rather than throw UnsupportedOperationException (#1427)
Currently, when CloudStorageFileSystemProvider#getFileAttributeView is invoked with a class it doesn't recognize it throws an UnsupportedOperationException. The javadocs for FileSystemProvider, however specify that #getFileAttributeView[1] return null if the attribute view type is not available. This change updates the behavior of CloudStorageFileSystemProvider#getFileAttributeView to return null rather than throw an exception. Fixes #1424 [1] https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/spi/FileSystemProvider.html#getFileAttributeView(java.nio.file.Path,java.lang.Class,java.nio.file.LinkOption...)
1 parent bda6874 commit ae468f5

2 files changed

Lines changed: 33 additions & 11 deletions

File tree

java-storage-nio/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1017,10 +1017,15 @@ public String toString() {
10171017
@Override
10181018
public <V extends FileAttributeView> V getFileAttributeView(
10191019
Path path, Class<V> type, LinkOption... options) {
1020+
checkNotNull(path);
10201021
checkNotNull(type);
10211022
CloudStorageUtil.checkNotNullArray(options);
10221023
if (type != CloudStorageFileAttributeView.class && type != BasicFileAttributeView.class) {
1023-
throw new UnsupportedOperationException(type.getSimpleName());
1024+
// the javadocs for getFileAttributeView specify the following for @return
1025+
// a file attribute view of the specified type, or null if the attribute view type is not
1026+
// available
1027+
// Similar type of issue from the JDK itself https://bugs.openjdk.org/browse/JDK-8273935
1028+
return null;
10241029
}
10251030
CloudStoragePath cloudPath = CloudStorageUtil.checkPath(path);
10261031
@SuppressWarnings("unchecked")

java-storage-nio/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProviderTest.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.google.cloud.storage.contrib.nio.CloudStorageFileSystem.forBucket;
2121
import static com.google.common.truth.Truth.assertThat;
2222
import static java.nio.charset.StandardCharsets.UTF_8;
23+
import static java.nio.file.Files.readAllBytes;
2324
import static java.nio.file.StandardCopyOption.ATOMIC_MOVE;
2425
import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
2526
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
@@ -36,7 +37,6 @@
3637
import com.google.cloud.storage.Acl;
3738
import com.google.cloud.storage.Acl.User;
3839
import com.google.cloud.storage.contrib.nio.testing.LocalStorageHelper;
39-
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
4040
import com.google.common.base.Optional;
4141
import com.google.common.collect.ImmutableList;
4242
import com.google.common.testing.NullPointerTester;
@@ -71,13 +71,17 @@
7171
import org.junit.Before;
7272
import org.junit.Rule;
7373
import org.junit.Test;
74+
import org.junit.rules.TemporaryFolder;
7475
import org.junit.runner.RunWith;
7576
import org.junit.runners.JUnit4;
7677

7778
/** Unit tests for {@link CloudStorageFileSystemProvider}. */
7879
@RunWith(JUnit4.class)
7980
public class CloudStorageFileSystemProviderTest {
80-
@Rule public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3);
81+
// @Rule(order = 1) public final MultipleAttemptsRule multipleAttemptsRule = new
82+
// MultipleAttemptsRule(3);
83+
@Rule(order = 1)
84+
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
8185

8286
private static final List<String> FILE_CONTENTS =
8387
ImmutableList.of(
@@ -153,14 +157,14 @@ public void testSize_trailingSlash_disablePseudoDirectories() throws Exception {
153157
public void testReadAllBytes() throws Exception {
154158
Path path = Paths.get(URI.create("gs://bucket/wat"));
155159
Files.write(path, SINGULARITY.getBytes(UTF_8));
156-
assertThat(new String(Files.readAllBytes(path), UTF_8)).isEqualTo(SINGULARITY);
160+
assertThat(new String(readAllBytes(path), UTF_8)).isEqualTo(SINGULARITY);
157161
Files.delete(path);
158162
}
159163

160164
@Test
161165
public void testReadAllBytes_trailingSlash() throws Exception {
162166
try {
163-
Files.readAllBytes(Paths.get(URI.create("gs://bucket/wat/")));
167+
readAllBytes(Paths.get(URI.create("gs://bucket/wat/")));
164168
Assert.fail();
165169
} catch (CloudStoragePseudoDirectoryException ex) {
166170
assertThat(ex.getMessage()).isNotNull();
@@ -255,7 +259,7 @@ public void testNewByteChannelWrite() throws Exception {
255259
assertThat(output.position()).isEqualTo(10);
256260
assertThat(output.size()).isEqualTo(10);
257261
}
258-
assertThat(new String(Files.readAllBytes(path), UTF_8)).isEqualTo("fileconten");
262+
assertThat(new String(readAllBytes(path), UTF_8)).isEqualTo("fileconten");
259263
}
260264

261265
@Test
@@ -298,7 +302,7 @@ public void testNewOutputStream() throws Exception {
298302
try (OutputStream output = Files.newOutputStream(path)) {
299303
output.write(SINGULARITY.getBytes(UTF_8));
300304
}
301-
assertThat(new String(Files.readAllBytes(path), UTF_8)).isEqualTo(SINGULARITY);
305+
assertThat(new String(readAllBytes(path), UTF_8)).isEqualTo(SINGULARITY);
302306
}
303307

304308
@Test
@@ -309,7 +313,7 @@ public void testNewOutputStream_truncateByDefault() throws Exception {
309313
try (OutputStream output = Files.newOutputStream(path)) {
310314
output.write(SINGULARITY.getBytes(UTF_8));
311315
}
312-
assertThat(new String(Files.readAllBytes(path), UTF_8)).isEqualTo(SINGULARITY);
316+
assertThat(new String(readAllBytes(path), UTF_8)).isEqualTo(SINGULARITY);
313317
}
314318

315319
@Test
@@ -320,7 +324,7 @@ public void testNewOutputStream_truncateExplicitly() throws Exception {
320324
try (OutputStream output = Files.newOutputStream(path, TRUNCATE_EXISTING)) {
321325
output.write(SINGULARITY.getBytes(UTF_8));
322326
}
323-
assertThat(new String(Files.readAllBytes(path), UTF_8)).isEqualTo(SINGULARITY);
327+
assertThat(new String(readAllBytes(path), UTF_8)).isEqualTo(SINGULARITY);
324328
}
325329

326330
@Test
@@ -577,7 +581,7 @@ public void testCopy() throws Exception {
577581
Path target = Paths.get(URI.create("gs://greenbean/adipose"));
578582
Files.write(source, "(✿◕ ‿◕ )ノ".getBytes(UTF_8));
579583
Files.copy(source, target);
580-
assertThat(new String(Files.readAllBytes(target), UTF_8)).isEqualTo("(✿◕ ‿◕ )ノ");
584+
assertThat(new String(readAllBytes(target), UTF_8)).isEqualTo("(✿◕ ‿◕ )ノ");
581585
assertThat(Files.exists(source)).isTrue();
582586
assertThat(Files.exists(target)).isTrue();
583587
}
@@ -641,7 +645,7 @@ public void testMove() throws Exception {
641645
Path target = Paths.get(URI.create("gs://greenbean/adipose"));
642646
Files.write(source, "(✿◕ ‿◕ )ノ".getBytes(UTF_8));
643647
Files.move(source, target);
644-
assertThat(new String(Files.readAllBytes(target), UTF_8)).isEqualTo("(✿◕ ‿◕ )ノ");
648+
assertThat(new String(readAllBytes(target), UTF_8)).isEqualTo("(✿◕ ‿◕ )ノ");
645649
assertThat(Files.exists(source)).isFalse();
646650
assertThat(Files.exists(target)).isTrue();
647651
}
@@ -763,6 +767,19 @@ public void testCopy_overwriteAttributes() throws Exception {
763767
Files.delete(target2);
764768
}
765769

770+
@Test
771+
public void testCopy_path_toLocalFileSystem() throws IOException {
772+
Path source = Paths.get(URI.create("gs://mybucket/myobject"));
773+
byte[] helloWorldBytes = "Hello, World!".getBytes(UTF_8);
774+
Files.write(source, helloWorldBytes);
775+
776+
Path path = temporaryFolder.newFile().toPath();
777+
// The new file created by temporaryFolder is an empty file on disk, specify REPLACE_EXISTING
778+
// so we can overwrite its contents.
779+
Files.copy(source, path, REPLACE_EXISTING);
780+
assertThat(Files.readAllBytes(path)).isEqualTo(helloWorldBytes);
781+
}
782+
766783
@Test
767784
public void testNullness() throws Exception {
768785
try (FileSystem fs = FileSystems.getFileSystem(URI.create("gs://blood"))) {

0 commit comments

Comments
 (0)