/* * Copyright (c) 2006-2015 DMDirc Developers * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.dmdirc.tests; import com.google.common.jimfs.Configuration; import com.google.common.jimfs.Jimfs; import java.nio.file.FileSystem; import java.nio.file.Path; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; /** * JUnit rule to create a JimFS file system and automatically close it on completion. */ public class JimFsRule implements TestRule { private FileSystem fileSystem; @Override public Statement apply(final Statement base, final Description description) { return new Statement() { @Override public void evaluate() throws Throwable { fileSystem = Jimfs.newFileSystem(Configuration.unix()); try { base.evaluate(); } finally { fileSystem.close(); } } }; } /** * Gets the file system for use in the test. * * @return The JimFS filesystem. */ public FileSystem getFileSystem() { return fileSystem; } /** * Utility method to get a path from the file system. * * @param first The first component of the path * @param more Any additional path components * @return A corresponding {@link Path} object from the JimFS file system. */ public Path getPath(final String first, final String ... more) { return fileSystem.getPath(first, more); } }