Skip to content

Commit 2a218c2

Browse files
add test for DeleteRecursive
1 parent 7602426 commit 2a218c2

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
import java.nio.file.Paths;
3939
import java.util.ArrayList;
4040
import java.util.List;
41+
import java.nio.file.SimpleFileVisitor;
42+
import java.nio.file.FileVisitResult;
43+
import java.nio.file.attribute.BasicFileAttributes;
4144

4245
/**
4346
* Unit tests for {@link CloudStorageFileSystem}.
@@ -250,6 +253,45 @@ public void testDeleteEmptiedFolder() throws IOException {
250253
}
251254
}
252255

256+
@Test
257+
public void testDeleteRecursive() throws IOException {
258+
try (FileSystem fs = CloudStorageFileSystem.forBucket("bucket")) {
259+
List<Path> paths = new ArrayList<>();
260+
paths.add(fs.getPath("atroot"));
261+
paths.add(fs.getPath("dir/angel"));
262+
paths.add(fs.getPath("dir/dir2/another_angel"));
263+
paths.add(fs.getPath("dir/dir2/angel3"));
264+
paths.add(fs.getPath("dir/dir3/cloud"));
265+
for (Path path : paths) {
266+
Files.write(path, ALONE.getBytes(UTF_8));
267+
}
268+
269+
deleteRecursive(fs.getPath("dir/"));
270+
assertThat(Files.exists(fs.getPath("dir/angel"))).isFalse();
271+
assertThat(Files.exists(fs.getPath("dir/dir3/cloud"))).isFalse();
272+
assertThat(Files.exists(fs.getPath("atroot"))).isTrue();
273+
}
274+
}
275+
276+
/**
277+
* Delete the given directory and all of its contents if non-empty.
278+
* @param directory the directory to delete
279+
* @throws IOException
280+
*/
281+
static private void deleteRecursive(Path directory) throws IOException {
282+
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
283+
@Override
284+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
285+
Files.delete(file);
286+
return FileVisitResult.CONTINUE;
287+
}
288+
@Override
289+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
290+
Files.delete(dir);
291+
return FileVisitResult.CONTINUE;
292+
}
293+
});
294+
}
253295

254296
private void assertMatches(FileSystem fs, PathMatcher matcher, String toMatch, boolean expected) {
255297
assertThat(matcher.matches(fs.getPath(toMatch).getFileName())).isEqualTo(expected);

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@
4444
import java.nio.channels.ReadableByteChannel;
4545
import java.nio.channels.SeekableByteChannel;
4646
import java.nio.file.FileSystem;
47+
import java.nio.file.FileVisitResult;
4748
import java.nio.file.Files;
4849
import java.nio.file.NoSuchFileException;
4950
import java.nio.file.Path;
51+
import java.nio.file.SimpleFileVisitor;
5052
import java.nio.file.StandardOpenOption;
53+
import java.nio.file.attribute.BasicFileAttributes;
5154
import java.util.ArrayList;
5255
import java.util.Arrays;
5356
import java.util.List;
@@ -354,6 +357,45 @@ public void testListFiles() throws IOException {
354357
}
355358
}
356359

360+
361+
@Test
362+
public void testDeleteRecursive() throws IOException {
363+
try (FileSystem fs = getTestBucket()) {
364+
List<Path> paths = new ArrayList<>();
365+
paths.add(fs.getPath("Racine"));
366+
paths.add(fs.getPath("playwrights/Moliere"));
367+
paths.add(fs.getPath("playwrights/French/Corneille"));
368+
for (Path path : paths) {
369+
Files.write(path, FILE_CONTENTS, UTF_8);
370+
}
371+
deleteRecursive(fs.getPath("playwrights/"));
372+
assertThat(Files.exists(fs.getPath("playwrights/Moliere"))).isFalse();
373+
assertThat(Files.exists(fs.getPath("playwrights/French/Corneille"))).isFalse();
374+
assertThat(Files.exists(fs.getPath("Racine"))).isTrue();
375+
Files.deleteIfExists(fs.getPath("Racine"));
376+
}
377+
}
378+
379+
/**
380+
* Delete the given directory and all of its contents if non-empty.
381+
* @param directory the directory to delete
382+
* @throws IOException
383+
*/
384+
static private void deleteRecursive(Path directory) throws IOException {
385+
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
386+
@Override
387+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
388+
Files.delete(file);
389+
return FileVisitResult.CONTINUE;
390+
}
391+
@Override
392+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
393+
Files.delete(dir);
394+
return FileVisitResult.CONTINUE;
395+
}
396+
});
397+
}
398+
357399
private int readFully(ReadableByteChannel chan, byte[] outputBuf) throws IOException {
358400
ByteBuffer buf = ByteBuffer.wrap(outputBuf);
359401
int sofar = 0;

0 commit comments

Comments
 (0)