`
seafront12
  • 浏览: 4643 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
文章分类
社区版块
存档分类
最新评论

SandboxFolder

 
阅读更多
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.UUID;

public class SandboxFolder extends ExternalResource {
    private static final String TESTING_SANDBOX = "build/testing-sandbox";
    private final File folder;

    public static SandboxFolder sandboxOf(Class<?> aClass){
        SandboxFolder sandboxFolder = new SandboxFolder(aClass);
        sandboxFolder.init();
        return sandboxFolder;
    }

    public SandboxFolder(Class<?> aClass) {
        deleteFileRecursively(new File(TESTING_SANDBOX + File.separator + aClass.getSimpleName()));
        folder = new File(TESTING_SANDBOX + File.separator + subFolderName(aClass));
    }

    private SandboxFolder(File folder) {
        this.folder = folder;
    }

    @Override
    protected void before() throws Throwable {
        init();
    }

    @Override
    protected void after() {
        clear();
    }

    public void init() {
        createFolder(folder);
    }

    public void clear() {
        deleteFileRecursively(folder);
    }

    public SandboxFolder makeSubFolder(String name) {
        createFolder(new File(folder, name));
        return new SandboxFolder(new File(folder, name));
    }

    public SandboxFolder folder(String name){
        return new SandboxFolder(new File(folder, name));
    }

    public boolean isEmpty() {
        return folder.listFiles().length == 0;
    }

    public File fileWithRandomContent(String filename) {
        return createFile(filename, UUID.randomUUID().toString());
    }

    public File createFile(String filename, String content, long creationTimeMilliseconds) {
        File file = createFile(filename, content);
        file.setLastModified(creationTimeMilliseconds);
        return file;
    }

    public boolean containsFile(String fileName) {
        File file = new File(folder, fileName);
        return file.exists();
    }

    public File fileWithName(String name) {
        return new File(folder, name);
    }

    public File createFile(String filename, String content) {
        File file = null;
        FileWriter writer = null;
        try {
            file = new File(folder, filename);
            writer = new FileWriter(file);
            writer.write(content);
        } catch (IOException e) {
            throw new RuntimeException("Cannot create file:" + filename, e);
        } finally {
            if (writer != null)
                try {
                    writer.close();
                } catch (IOException ignore) {
                }
        }
        return file;
    }

    private static String subFolderName(Class<?> clazz) {
        return clazz.getSimpleName() + File.separator + System.currentTimeMillis();
    }

    private static void createFolder(File folder) {
        if (!folder.exists()) {
            folder.mkdirs();
        } else {
            deleteFileRecursively(folder);
            folder.mkdirs();
        }
    }

    private static void deleteFileRecursively(File path) {
        if (path.exists()) {
            if (path.isDirectory()) {
                for (File f : path.listFiles()) {
                    if (f.isDirectory()) {
                        deleteFileRecursively(f);
                        if (!f.delete()) f.deleteOnExit();
                    } else {
                        if (!f.delete()) f.deleteOnExit();
                    }
                }
            }
            path.delete();
        }
    }

    public File asFile() {
        return folder;
    }

//    public Path asPath() {
//        return folder.toPath();
//    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics