blob: e1c4735faa736021d6829546baf545440f80332e [file] [log] [blame]
Enrico Granata9c1f2722017-09-21 17:19:04 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.car.test.utils;
18
19import android.annotation.Nullable;
20import android.os.SystemClock;
21import java.io.File;
22import java.io.IOException;
23import java.nio.file.FileVisitResult;
24import java.nio.file.Files;
25import java.nio.file.Path;
26import java.nio.file.SimpleFileVisitor;
27import java.nio.file.attribute.BasicFileAttributes;
28
29/**
30 * A utility class that creates a temporary directory.
31 * The directory, and any files contained within, are automatically deleted when calling close().
32 *
33 * Example usage:
34 *
35 * try (TemporaryDirectory tf = new TemporaryDirectory("myTest")) {
36 * ...
37 * } // directory gets deleted here
38 */
39public class TemporaryDirectory implements AutoCloseable {
40 private Path mDirectory;
41
42 private static final class DeletingVisitor extends SimpleFileVisitor<Path> {
43 FileVisitResult consume(Path path) throws IOException {
44 Files.delete(path);
45 return FileVisitResult.CONTINUE;
46 }
47
48 @Override
49 public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes)
50 throws IOException {
51 return consume(path);
52 }
53
54 @Override
55 public FileVisitResult postVisitDirectory(Path path, IOException e)
56 throws IOException {
57 return consume(path);
58 }
59 }
60
61 private static final SimpleFileVisitor<Path> DELETE = new DeletingVisitor();
62
Enrico Granata1690a622018-01-22 17:34:46 -080063 TemporaryDirectory(Path directory) throws IOException {
64 mDirectory = Files.createDirectory(directory);
65 }
66
Enrico Granata9c1f2722017-09-21 17:19:04 -070067 public TemporaryDirectory(@Nullable String prefix) throws IOException {
68 if (prefix == null) {
69 prefix = TemporaryDirectory.class.getSimpleName();
70 }
71 prefix += String.valueOf(SystemClock.elapsedRealtimeNanos());
72
73 mDirectory = Files.createTempDirectory(prefix);
74 }
75
76 @Override
77 public void close() throws Exception {
78 Files.walkFileTree(mDirectory, DELETE);
79 }
80
81 public File getDirectory() {
82 return mDirectory.toFile();
83 }
Enrico Granata1690a622018-01-22 17:34:46 -080084
85 public Path getPath() { return mDirectory; }
86
87 public TemporaryDirectory getSubdirectory(String name) throws IOException {
88 return new TemporaryDirectory(mDirectory.resolve(name));
89 }
Enrico Granata9c1f2722017-09-21 17:19:04 -070090}