Skip to content

Commit f495c23

Browse files
Add a PathMatcher for CloudStorageFileSystem (#1469)
Add a test, as well. We reuse the default PathMatcher since it does a fine job of globbing files.
1 parent d4d494d commit f495c23

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.nio.file.FileSystems;
3535
import java.nio.file.Files;
3636
import java.nio.file.Path;
37+
import java.nio.file.PathMatcher;
3738
import java.nio.file.Paths;
3839
import java.util.ArrayList;
3940
import java.util.List;
@@ -159,4 +160,27 @@ public void testListFiles() throws IOException {
159160
assertThat(got).containsExactlyElementsIn(goodPaths);
160161
}
161162
}
163+
164+
@Test
165+
public void testMatcher() throws IOException {
166+
try (FileSystem fs = CloudStorageFileSystem.forBucket("bucket")) {
167+
String pattern1 = "glob:*.java";
168+
PathMatcher javaFileMatcher = fs.getPathMatcher(pattern1);
169+
assertMatches(fs, javaFileMatcher, "a.java", true);
170+
assertMatches(fs, javaFileMatcher, "a.text", false);
171+
assertMatches(fs, javaFileMatcher, "folder/c.java", true);
172+
assertMatches(fs, javaFileMatcher, "d", false);
173+
174+
String pattern2 = "glob:*.{java,text}";
175+
PathMatcher javaAndTextFileMatcher = fs.getPathMatcher(pattern2);
176+
assertMatches(fs, javaAndTextFileMatcher, "a.java", true);
177+
assertMatches(fs, javaAndTextFileMatcher, "a.text", true);
178+
assertMatches(fs, javaAndTextFileMatcher, "folder/c.java", true);
179+
assertMatches(fs, javaAndTextFileMatcher, "d", false);
180+
}
181+
}
182+
183+
private void assertMatches(FileSystem fs, PathMatcher matcher, String toMatch, boolean expected) {
184+
assertThat(matcher.matches(fs.getPath(toMatch).getFileName())).isEqualTo(expected);
185+
}
162186
}

0 commit comments

Comments
 (0)