Skip to content

Commit 998ae3d

Browse files
Add a PathMatcher for CloudStorageFileSystem
Add a test, as well. We reuse the default PathMatcher since it does a fine job of globbing files.
1 parent 27962a3 commit 998ae3d

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.net.URISyntaxException;
2828
import java.nio.file.FileStore;
2929
import java.nio.file.FileSystem;
30+
import java.nio.file.FileSystems;
3031
import java.nio.file.Path;
3132
import java.nio.file.PathMatcher;
3233
import java.nio.file.WatchService;
@@ -210,13 +211,9 @@ public Set<String> supportedFileAttributeViews() {
210211
return SUPPORTED_VIEWS;
211212
}
212213

213-
/**
214-
* Throws {@link UnsupportedOperationException} because this feature hasn't been implemented yet.
215-
*/
216214
@Override
217215
public PathMatcher getPathMatcher(String syntaxAndPattern) {
218-
// TODO(#813): Implement me.
219-
throw new UnsupportedOperationException();
216+
return FileSystems.getDefault().getPathMatcher(syntaxAndPattern);
220217
}
221218

222219
/**

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package com.google.cloud.storage.contrib.nio;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static com.google.common.truth.Truth.assertWithMessage;
2021
import static java.nio.charset.StandardCharsets.UTF_8;
2122

2223
import com.google.cloud.storage.StorageOptions;
24+
import com.google.common.collect.ImmutableList;
2325
import com.google.common.testing.EqualsTester;
2426
import com.google.common.testing.NullPointerTester;
2527

@@ -34,6 +36,7 @@
3436
import java.nio.file.FileSystems;
3537
import java.nio.file.Files;
3638
import java.nio.file.Path;
39+
import java.nio.file.PathMatcher;
3740
import java.nio.file.Paths;
3841
import java.util.ArrayList;
3942
import java.util.List;
@@ -159,4 +162,22 @@ public void testListFiles() throws IOException {
159162
assertThat(got).containsExactlyElementsIn(goodPaths);
160163
}
161164
}
165+
166+
@Test
167+
public void testMatcher() throws IOException {
168+
try (FileSystem fs = CloudStorageFileSystem.forBucket("bucket")) {
169+
List<String> paths = ImmutableList.of("a.java", "a.text", "folder/c.java", "d");
170+
String pattern1 = "glob:*.java";
171+
PathMatcher matcher1 = fs.getPathMatcher(pattern1);
172+
List<Boolean> matches1 = ImmutableList.of(true, false, true, false);
173+
String pattern2 = "glob:*.{java,text}";
174+
PathMatcher matcher2 = fs.getPathMatcher(pattern2);
175+
List<Boolean> matches2 = ImmutableList.of(true, true, true, false);
176+
for (int i=0; i<paths.size(); i++) {
177+
Path input = fs.getPath(paths.get(i)).getFileName();
178+
assertWithMessage(pattern1 + " on " + paths.get(i)).that(matcher1.matches(input)).isEqualTo(matches1.get(i));
179+
assertWithMessage(pattern2 + " on " + paths.get(i)).that(matcher2.matches(input)).isEqualTo(matches2.get(i));
180+
}
181+
}
182+
}
162183
}

0 commit comments

Comments
 (0)