diff --git a/community/community-it/kernel-it/src/test/java/org/neo4j/kernel/internal/StoreLockerTest.java b/community/community-it/kernel-it/src/test/java/org/neo4j/kernel/internal/StoreLockerTest.java index 4911435d2c47..ab4d7192ec05 100644 --- a/community/community-it/kernel-it/src/test/java/org/neo4j/kernel/internal/StoreLockerTest.java +++ b/community/community-it/kernel-it/src/test/java/org/neo4j/kernel/internal/StoreLockerTest.java @@ -136,7 +136,7 @@ public void shouldObtainLockWhenStoreFileNotLocked() throws Exception FileSystemAbstraction fileSystemAbstraction = new DelegatingFileSystemAbstraction( fileSystemRule.get() ) { @Override - public boolean fileExists( File fileName ) + public boolean fileExists( File file ) { return true; } @@ -160,7 +160,7 @@ public void shouldCreateStoreDirAndObtainLockWhenStoreDirDoesNotExist() throws E FileSystemAbstraction fileSystemAbstraction = new DelegatingFileSystemAbstraction( fileSystemRule.get() ) { @Override - public boolean fileExists( File fileName ) + public boolean fileExists( File file ) { return false; } @@ -185,7 +185,7 @@ public void mkdirs( File fileName ) throws IOException } @Override - public boolean fileExists( File fileName ) + public boolean fileExists( File file ) { return false; } @@ -218,7 +218,7 @@ public StoreChannel open( File fileName, OpenMode openMode ) throws IOException } @Override - public boolean fileExists( File fileName ) + public boolean fileExists( File file ) { return false; } @@ -247,7 +247,7 @@ public void shouldNotObtainLockWhenStoreAlreadyInUse() throws Exception FileSystemAbstraction fileSystemAbstraction = new DelegatingFileSystemAbstraction( fileSystemRule.get() ) { @Override - public boolean fileExists( File fileName ) + public boolean fileExists( File file ) { return false; } diff --git a/community/io/src/main/java/org/neo4j/io/fs/DefaultFileSystemAbstraction.java b/community/io/src/main/java/org/neo4j/io/fs/DefaultFileSystemAbstraction.java index 1e2738ef216c..bfba3e7fd527 100644 --- a/community/io/src/main/java/org/neo4j/io/fs/DefaultFileSystemAbstraction.java +++ b/community/io/src/main/java/org/neo4j/io/fs/DefaultFileSystemAbstraction.java @@ -125,9 +125,9 @@ public void mkdirs( File path ) throws IOException } @Override - public boolean fileExists( File fileName ) + public boolean fileExists( File file ) { - return fileName.exists(); + return file.exists(); } @Override diff --git a/community/io/src/main/java/org/neo4j/io/fs/DelegateFileSystemAbstraction.java b/community/io/src/main/java/org/neo4j/io/fs/DelegateFileSystemAbstraction.java index 948c7f2bdbaf..6df818eec90a 100644 --- a/community/io/src/main/java/org/neo4j/io/fs/DelegateFileSystemAbstraction.java +++ b/community/io/src/main/java/org/neo4j/io/fs/DelegateFileSystemAbstraction.java @@ -116,9 +116,9 @@ public StoreChannel create( File fileName ) throws IOException } @Override - public boolean fileExists( File fileName ) + public boolean fileExists( File file ) { - return Files.exists( path( fileName ) ); + return Files.exists( path( file ) ); } @Override diff --git a/community/io/src/main/java/org/neo4j/io/fs/FileSystemAbstraction.java b/community/io/src/main/java/org/neo4j/io/fs/FileSystemAbstraction.java index 8b9409ec089b..6e926b606445 100644 --- a/community/io/src/main/java/org/neo4j/io/fs/FileSystemAbstraction.java +++ b/community/io/src/main/java/org/neo4j/io/fs/FileSystemAbstraction.java @@ -60,7 +60,7 @@ public interface FileSystemAbstraction extends Closeable StoreChannel create( File fileName ) throws IOException; - boolean fileExists( File fileName ); + boolean fileExists( File file ); boolean mkdir( File fileName ); diff --git a/community/io/src/test/java/org/neo4j/adversaries/fs/AdversarialFileSystemAbstraction.java b/community/io/src/test/java/org/neo4j/adversaries/fs/AdversarialFileSystemAbstraction.java index b96a70cb0613..a6aba2f45a4d 100644 --- a/community/io/src/test/java/org/neo4j/adversaries/fs/AdversarialFileSystemAbstraction.java +++ b/community/io/src/test/java/org/neo4j/adversaries/fs/AdversarialFileSystemAbstraction.java @@ -202,10 +202,10 @@ public boolean isDirectory( File file ) } @Override - public boolean fileExists( File fileName ) + public boolean fileExists( File file ) { adversary.injectFailure( SecurityException.class ); - return delegate.fileExists( fileName ); + return delegate.fileExists( file ); } @Override diff --git a/community/io/src/test/java/org/neo4j/graphdb/mockfs/DelegatingFileSystemAbstraction.java b/community/io/src/test/java/org/neo4j/graphdb/mockfs/DelegatingFileSystemAbstraction.java index 1bcecd2cddf4..8de8bf4ba050 100644 --- a/community/io/src/test/java/org/neo4j/graphdb/mockfs/DelegatingFileSystemAbstraction.java +++ b/community/io/src/test/java/org/neo4j/graphdb/mockfs/DelegatingFileSystemAbstraction.java @@ -145,9 +145,9 @@ public InputStream openAsInputStream( File fileName ) throws IOException } @Override - public boolean fileExists( File fileName ) + public boolean fileExists( File file ) { - return delegate.fileExists( fileName ); + return delegate.fileExists( file ); } @Override diff --git a/community/io/src/test/java/org/neo4j/graphdb/mockfs/SelectiveFileSystemAbstraction.java b/community/io/src/test/java/org/neo4j/graphdb/mockfs/SelectiveFileSystemAbstraction.java index 5332b8d228d0..2719ce118308 100644 --- a/community/io/src/test/java/org/neo4j/graphdb/mockfs/SelectiveFileSystemAbstraction.java +++ b/community/io/src/test/java/org/neo4j/graphdb/mockfs/SelectiveFileSystemAbstraction.java @@ -102,9 +102,9 @@ public StoreChannel create( File fileName ) throws IOException } @Override - public boolean fileExists( File fileName ) + public boolean fileExists( File file ) { - return chooseFileSystem( fileName ).fileExists( fileName ); + return chooseFileSystem( file ).fileExists( file ); } @Override diff --git a/community/io/src/test/java/org/neo4j/test/extension/TestDirectoryExtensionTest.java b/community/io/src/test/java/org/neo4j/test/extension/TestDirectoryExtensionTest.java index 41b7fa4a7e6e..b29e71456354 100644 --- a/community/io/src/test/java/org/neo4j/test/extension/TestDirectoryExtensionTest.java +++ b/community/io/src/test/java/org/neo4j/test/extension/TestDirectoryExtensionTest.java @@ -29,6 +29,7 @@ import org.neo4j.io.fs.DefaultFileSystemAbstraction; import org.neo4j.test.rule.TestDirectory; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -62,4 +63,12 @@ void testDirectoryUsesFileSystemFromExtension() { assertSame( fileSystem, testDirectory.getFileSystem() ); } + + @Test + void createTestFile() + { + File file = testDirectory.createFile( "a" ); + assertEquals( "a", file.getName() ); + assertTrue( fileSystem.fileExists( file ) ); + } } diff --git a/community/io/src/test/java/org/neo4j/test/rule/TestDirectory.java b/community/io/src/test/java/org/neo4j/test/rule/TestDirectory.java index ebfeac79a174..7d56ffd1eb3a 100644 --- a/community/io/src/test/java/org/neo4j/test/rule/TestDirectory.java +++ b/community/io/src/test/java/org/neo4j/test/rule/TestDirectory.java @@ -160,6 +160,13 @@ public File file( String name ) return new File( directory(), name ); } + public File createFile( String name ) + { + File file = file( name ); + ensureFileExists( file ); + return file; + } + public File databaseDir() { return databaseLayout().databaseDirectory(); @@ -281,6 +288,21 @@ private void directoryForDescription( Description description ) throws IOExcepti prepareDirectory( description.getTestClass(), description.getMethodName() ); } + private void ensureFileExists( File file ) + { + try + { + if ( !fileSystem.fileExists( file ) ) + { + fileSystem.create( file ).close(); + } + } + catch ( IOException e ) + { + throw new UncheckedIOException( "Failed to create file: " + file, e ); + } + } + private void createDirectory( File databaseDirectory ) { try 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 index 291a157bcb22..b202442c13c7 100644 --- 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 @@ -120,9 +120,9 @@ public StoreChannel create( File fileName ) throws IOException } @Override - public boolean fileExists( File fileName ) + public boolean fileExists( File file ) { - return fs.fileExists( fileName ); + return fs.fileExists( file ); } @Override diff --git a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/kvstore/KeyValueDatabaseStoreFormatTest.java b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/kvstore/KeyValueDatabaseStoreFormatTest.java index 672a10f86b4a..9ca869800aa9 100644 --- a/community/kernel/src/test/java/org/neo4j/kernel/impl/store/kvstore/KeyValueDatabaseStoreFormatTest.java +++ b/community/kernel/src/test/java/org/neo4j/kernel/impl/store/kvstore/KeyValueDatabaseStoreFormatTest.java @@ -19,7 +19,6 @@ */ package org.neo4j.kernel.impl.store.kvstore; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -40,7 +39,7 @@ import org.neo4j.kernel.configuration.Config; import org.neo4j.test.rule.ConfigurablePageCacheRule; import org.neo4j.test.rule.PageCacheRule; -import org.neo4j.test.rule.ResourceRule; +import org.neo4j.test.rule.TestDirectory; import org.neo4j.test.rule.fs.EphemeralFileSystemRule; import static org.junit.Assert.assertArrayEquals; @@ -49,7 +48,6 @@ import static org.junit.Assert.fail; import static org.neo4j.kernel.impl.store.kvstore.KeyValueDatabaseStoreFormatTest.Data.data; import static org.neo4j.kernel.impl.store.kvstore.KeyValueDatabaseStoreFormatTest.DataEntry.entry; -import static org.neo4j.test.rule.ResourceRule.testPath; public class KeyValueDatabaseStoreFormatTest { @@ -58,13 +56,7 @@ public class KeyValueDatabaseStoreFormatTest @Rule public final ConfigurablePageCacheRule pages = new ConfigurablePageCacheRule(); @Rule - public final ResourceRule storeFile = testPath(); - - @Before - public void existingStoreDirectory() - { - fs.get().mkdirs( storeFile.get().getParentFile() ); - } + public final TestDirectory directory = TestDirectory.testDirectory( fs ); @Test public void shouldCreateAndOpenEmptyStoreWithEmptyHeader() throws Exception @@ -318,7 +310,7 @@ public boolean visit( WritableBuffer key, WritableBuffer value ) throws IOExcept { // then only headers are present in the file and not the old content assertEquals( "boom!", io.getMessage() ); - assertFormatSpecifierAndHeadersOnly( headers, fs.get(), storeFile.get() ); + assertFormatSpecifierAndHeadersOnly( headers, fs.get(), getStoreFile() ); } } } @@ -527,13 +519,13 @@ private Format( HeaderField[] headerFields ) void createEmpty( Map headers ) throws IOException { - createEmptyStore( fs.get(), storeFile.get(), 16, 16, headers( headers ) ); + createEmptyStore( fs.get(), getStoreFile(), 16, 16, headers( headers ) ); } KeyValueStoreFile create( Map headers, DataProvider data ) throws IOException { - return createStore( fs.get(), pages.getPageCache( fs.get() ), storeFile.get(), 16, 16, headers( headers ), + return createStore( fs.get(), pages.getPageCache( fs.get() ), getStoreFile(), 16, 16, headers( headers ), data ); } @@ -544,7 +536,7 @@ KeyValueStoreFile create( Map config, Map headers, PageCacheRule.PageCacheConfig pageCacheConfig = PageCacheRule.config(); PageCache pageCache = pages.getPageCache( fs.get(), pageCacheConfig, Config.defaults( config ) ); return createStore( fs.get(), - pageCache, storeFile.get(), 16, 16, + pageCache, getStoreFile(), 16, 16, headers( headers ), data ); } @@ -560,7 +552,7 @@ private Headers headers( Map headers ) KeyValueStoreFile open() throws IOException { - return openStore( fs.get(), pages.getPageCache( fs.get() ), storeFile.get() ); + return openStore( fs.get(), pages.getPageCache( fs.get() ), getStoreFile() ); } @Override @@ -573,6 +565,11 @@ protected void writeFormatSpecifier( WritableBuffer formatSpecifier ) } } + private File getStoreFile() + { + return directory.createFile( "storeFile" ); + } + static class Data implements DataProvider { static Data data( final DataEntry... data ) diff --git a/community/kernel/src/test/java/org/neo4j/test/rule/ResourceRule.java b/community/kernel/src/test/java/org/neo4j/test/rule/ResourceRule.java deleted file mode 100644 index 06b0739f69a2..000000000000 --- a/community/kernel/src/test/java/org/neo4j/test/rule/ResourceRule.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright (c) 2002-2018 "Neo4j," - * Neo4j Sweden AB [http://neo4j.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; - -import org.junit.rules.TestRule; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; - -import java.io.File; -import java.util.function.Supplier; - -import org.neo4j.io.fs.FileSystemAbstraction; -import org.neo4j.io.pagecache.PageCache; - -public abstract class ResourceRule implements TestRule, Supplier -{ - public static ResourceRule fileInExistingDirectory( final Supplier fs ) - { - return new ResourceRule() - { - @Override - protected File createResource( Description description ) - { - File path = path( description ); - fs.get().mkdir( path.getParentFile() ); - return path; - } - }; - } - - public static ResourceRule existingDirectory( final Supplier fs ) - { - return new ResourceRule() - { - @Override - protected File createResource( Description description ) - { - File path = path( description ); - fs.get().mkdir( path ); - return path; - } - }; - } - - public static ResourceRule testPath() - { - return new ResourceRule() - { - @Override - protected File createResource( Description description ) - { - return path( description ); - } - }; - } - - public static ResourceRule pageCache( final Supplier fs ) - { - return new ResourceRule() - { - final PageCacheRule pageCache = new PageCacheRule(); - - @Override - protected PageCache createResource( Description description ) - { - return pageCache.getPageCache( fs.get() ); - } - - @Override - protected void destroyResource( PageCache done, Throwable failure ) - { - pageCache.after( failure == null ); - } - }; - } - - private static File path( Description description ) - { - return new File( description.getClassName(), description.getMethodName() ); - } - - private RESOURCE resource; - - @Override - public final RESOURCE get() - { - return resource; - } - - @Override - public final Statement apply( final Statement base, final Description description ) - { - return new Statement() - { - @Override - public void evaluate() throws Throwable - { - resource = createResource( description ); - Throwable failure = null; - try - { - base.evaluate(); - } - catch ( Throwable err ) - { - failure = err; - throw err; - } - finally - { - RESOURCE done = resource; - resource = null; - try - { - destroyResource( done, failure ); - } - catch ( Throwable err ) - { - if ( failure != null ) - { - failure.addSuppressed( err ); - } - else - { - throw err; - } - } - } - } - }; - } - - protected abstract RESOURCE createResource( Description description ); - - protected void destroyResource( RESOURCE done, Throwable failure ) - { - } -} diff --git a/community/kernel/src/test/java/org/neo4j/test/rule/Resources.java b/community/kernel/src/test/java/org/neo4j/test/rule/Resources.java index 10a408863309..8d1d11460f8f 100644 --- a/community/kernel/src/test/java/org/neo4j/test/rule/Resources.java +++ b/community/kernel/src/test/java/org/neo4j/test/rule/Resources.java @@ -70,14 +70,14 @@ void initialize( LifeRule life ) } private final EphemeralFileSystemRule fs = new EphemeralFileSystemRule(); - private final ResourceRule pageCache = ResourceRule.pageCache( fs ); + private final PageCacheRule pageCacheRule = new PageCacheRule(); private final TestDirectory testDirectory = TestDirectory.testDirectory( fs ); private final LifeRule life = new LifeRule(); @Override public Statement apply( Statement base, Description description ) { - return fs.apply( testDirectory.apply( pageCache.apply( lifeStatement( base, description ), description ), description ), description ); + return fs.apply( testDirectory.apply( pageCacheRule.apply( lifeStatement( base, description ), description ), description ), description ); } private Statement lifeStatement( Statement base, Description description ) @@ -110,7 +110,7 @@ public FileSystemAbstraction fileSystem() public PageCache pageCache() { - return pageCache.get(); + return pageCacheRule.getPageCache( fileSystem() ); } public TestDirectory testDirectory() diff --git a/community/logging/src/test/java/org/neo4j/logging/RotatingFileOutputStreamSupplierTest.java b/community/logging/src/test/java/org/neo4j/logging/RotatingFileOutputStreamSupplierTest.java index f9273d990d6d..422b1cfe1dac 100644 --- a/community/logging/src/test/java/org/neo4j/logging/RotatingFileOutputStreamSupplierTest.java +++ b/community/logging/src/test/java/org/neo4j/logging/RotatingFileOutputStreamSupplierTest.java @@ -668,11 +668,11 @@ public void write( byte[] b, int off, int len ) throws IOException } @Override - public boolean fileExists( File fileName ) + public boolean fileExists( File file ) { // Default adversarial might throw a java.lang.SecurityException here, which is an exception // that should not be survived - return delegate.fileExists( fileName ); + return delegate.fileExists( file ); } @Override