Skip to content

Commit

Permalink
Remove resource rule. Replace single usage with common tests rules.
Browse files Browse the repository at this point in the history
Also as part f this pr introduce possibility to create files in
test directory.
  • Loading branch information
MishaDemianenko committed Sep 28, 2018
1 parent 9e7515d commit c25f3a5
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 193 deletions.
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -185,7 +185,7 @@ public void mkdirs( File fileName ) throws IOException
}

@Override
public boolean fileExists( File fileName )
public boolean fileExists( File file )
{
return false;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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 );

Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ) );
}
}
22 changes: 22 additions & 0 deletions community/io/src/test/java/org/neo4j/test/rule/TestDirectory.java
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -19,7 +19,6 @@
*/
package org.neo4j.kernel.impl.store.kvstore;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

Expand All @@ -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;
Expand All @@ -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
{
Expand All @@ -58,13 +56,7 @@ public class KeyValueDatabaseStoreFormatTest
@Rule
public final ConfigurablePageCacheRule pages = new ConfigurablePageCacheRule();
@Rule
public final ResourceRule<File> 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
Expand Down Expand Up @@ -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() );
}
}
}
Expand Down Expand Up @@ -527,13 +519,13 @@ private Format( HeaderField<byte[]>[] headerFields )

void createEmpty( Map<String,byte[]> headers ) throws IOException
{
createEmptyStore( fs.get(), storeFile.get(), 16, 16, headers( headers ) );
createEmptyStore( fs.get(), getStoreFile(), 16, 16, headers( headers ) );
}

KeyValueStoreFile create( Map<String,byte[]> 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 );
}

Expand All @@ -544,7 +536,7 @@ KeyValueStoreFile create( Map<String,String> config, Map<String,byte[]> 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 );
}

Expand All @@ -560,7 +552,7 @@ private Headers headers( Map<String,byte[]> 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
Expand All @@ -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 )
Expand Down

0 comments on commit c25f3a5

Please sign in to comment.