diff --git a/community/io/src/test/java/org/neo4j/test/rule/fs/DefaultFileSystemRule.java b/community/io/src/test/java/org/neo4j/test/rule/fs/DefaultFileSystemRule.java index 29238c2d02a6..4dff0f6289ab 100644 --- a/community/io/src/test/java/org/neo4j/test/rule/fs/DefaultFileSystemRule.java +++ b/community/io/src/test/java/org/neo4j/test/rule/fs/DefaultFileSystemRule.java @@ -19,30 +19,12 @@ */ package org.neo4j.test.rule.fs; -import org.junit.rules.ExternalResource; - import org.neo4j.io.fs.DefaultFileSystemAbstraction; -import org.neo4j.io.fs.FileSystemAbstraction; -public class DefaultFileSystemRule extends ExternalResource +public class DefaultFileSystemRule extends FileSystemRule { - private DefaultFileSystemAbstraction fs = new DefaultFileSystemAbstraction(); - - @Override - protected void after() - { - try - { - fs.close(); - } - catch ( Exception e ) - { - throw new RuntimeException( e ); - } - } - - public FileSystemAbstraction get() + public DefaultFileSystemRule() { - return fs; + super( new DefaultFileSystemAbstraction() ); } } diff --git a/community/io/src/test/java/org/neo4j/test/rule/fs/EphemeralFileSystemRule.java b/community/io/src/test/java/org/neo4j/test/rule/fs/EphemeralFileSystemRule.java index df7e697e8bae..6d380f307c35 100644 --- a/community/io/src/test/java/org/neo4j/test/rule/fs/EphemeralFileSystemRule.java +++ b/community/io/src/test/java/org/neo4j/test/rule/fs/EphemeralFileSystemRule.java @@ -19,36 +19,17 @@ */ package org.neo4j.test.rule.fs; -import org.junit.rules.ExternalResource; - import java.io.File; import java.io.IOException; -import java.util.function.Supplier; import org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction; import org.neo4j.io.fs.FileSystemAbstraction; -public class EphemeralFileSystemRule extends ExternalResource implements Supplier +public class EphemeralFileSystemRule extends FileSystemRule { - private EphemeralFileSystemAbstraction fs = new EphemeralFileSystemAbstraction(); - - @Override - protected void after() + public EphemeralFileSystemRule() { - try - { - fs.close(); - } - catch ( Exception e ) - { - throw new RuntimeException( e ); - } - } - - @Override - public final EphemeralFileSystemAbstraction get() - { - return fs; + super( new EphemeralFileSystemAbstraction() ); } public EphemeralFileSystemAbstraction snapshot( Runnable action ) throws Exception @@ -72,29 +53,11 @@ public void clear() throws Exception fs = new EphemeralFileSystemAbstraction(); } - @Override - public int hashCode() - { - return fs.hashCode(); - } - - @Override - public boolean equals( Object obj ) - { - return fs.equals( obj ); - } - public void crash() { fs.crash(); } - @Override - public String toString() - { - return fs.toString(); - } - public EphemeralFileSystemAbstraction snapshot() { return fs.snapshot(); diff --git a/community/io/src/test/java/org/neo4j/test/rule/fs/FileSystemRule.java b/community/io/src/test/java/org/neo4j/test/rule/fs/FileSystemRule.java new file mode 100644 index 000000000000..2ff0ae2cbab7 --- /dev/null +++ b/community/io/src/test/java/org/neo4j/test/rule/fs/FileSystemRule.java @@ -0,0 +1,239 @@ +/* + * Copyright (c) 2002-2017 "Neo Technology," + * Network Engine for Objects in Lund AB [http://neotechnology.com] + * + * This file is part of Neo4j. + * + * Neo4j is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.neo4j.test.rule.fs; + +import org.junit.rules.ExternalResource; + +import java.io.File; +import java.io.FilenameFilter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.io.UncheckedIOException; +import java.io.Writer; +import java.nio.charset.Charset; +import java.nio.file.CopyOption; +import java.util.function.Function; +import java.util.function.Supplier; + +import org.neo4j.io.fs.FileSystemAbstraction; +import org.neo4j.io.fs.StoreChannel; +import org.neo4j.io.fs.watcher.FileWatcher; + +public abstract class FileSystemRule extends ExternalResource + implements FileSystemAbstraction, Supplier +{ + protected volatile FS fs; + + protected FileSystemRule( FS fs ) + { + this.fs = fs; + } + + @Override + protected void after() + { + try + { + fs.close(); + } + catch ( IOException e ) + { + throw new UncheckedIOException( e ); + } + super.after(); + } + + @Override + public FS get() + { + return fs; + } + + @Override + public void close() throws IOException + { + fs.close(); + } + + @Override + public FileWatcher fileWatcher() throws IOException + { + return fs.fileWatcher(); + } + + @Override + public StoreChannel open( File fileName, String mode ) throws IOException + { + return fs.open( fileName, mode ); + } + + @Override + public OutputStream openAsOutputStream( File fileName, boolean append ) throws IOException + { + return fs.openAsOutputStream( fileName, append ); + } + + @Override + public InputStream openAsInputStream( File fileName ) throws IOException + { + return fs.openAsInputStream( fileName ); + } + + @Override + public Reader openAsReader( File fileName, Charset charset ) throws IOException + { + return fs.openAsReader( fileName, charset ); + } + + @Override + public Writer openAsWriter( File fileName, Charset charset, boolean append ) throws IOException + { + return fs.openAsWriter( fileName, charset, append ); + } + + @Override + public StoreChannel create( File fileName ) throws IOException + { + return fs.create( fileName ); + } + + @Override + public boolean fileExists( File fileName ) + { + return fs.fileExists( fileName ); + } + + @Override + public boolean mkdir( File fileName ) + { + return fs.mkdir( fileName ); + } + + @Override + public void mkdirs( File fileName ) throws IOException + { + fs.mkdirs( fileName ); + } + + @Override + public long getFileSize( File fileName ) + { + return fs.getFileSize( fileName ); + } + + @Override + public boolean deleteFile( File fileName ) + { + return fs.deleteFile( fileName ); + } + + @Override + public void deleteRecursively( File directory ) throws IOException + { + fs.deleteRecursively( directory ); + } + + @Override + public void renameFile( File from, File to, CopyOption... copyOptions ) throws IOException + { + fs.renameFile( from, to, copyOptions ); + } + + @Override + public File[] listFiles( File directory ) + { + return fs.listFiles( directory ); + } + + @Override + public File[] listFiles( File directory, FilenameFilter filter ) + { + return fs.listFiles( directory, filter ); + } + + @Override + public boolean isDirectory( File file ) + { + return fs.isDirectory( file ); + } + + @Override + public void moveToDirectory( File file, File toDirectory ) throws IOException + { + fs.moveToDirectory( file, toDirectory ); + } + + @Override + public void copyFile( File from, File to ) throws IOException + { + fs.copyFile( from, to ); + } + + @Override + public void copyRecursively( File fromDirectory, File toDirectory ) throws IOException + { + fs.copyRecursively( fromDirectory, toDirectory ); + } + + @Override + public K getOrCreateThirdPartyFileSystem( Class clazz, + Function,K> creator ) + { + return fs.getOrCreateThirdPartyFileSystem( clazz, creator ); + } + + @Override + public void truncate( File path, long size ) throws IOException + { + fs.truncate( path, size ); + } + + @Override + public long lastModifiedTime( File file ) throws IOException + { + return fs.lastModifiedTime( file ); + } + + @Override + public void deleteFileOrThrow( File file ) throws IOException + { + fs.deleteFileOrThrow( file ); + } + + @Override + public int hashCode() + { + return fs.hashCode(); + } + + @Override + public boolean equals( Object obj ) + { + return fs.equals( obj ); + } + + @Override + public String toString() + { + return fs.toString(); + } +}