Skip to content

Commit

Permalink
Also use ExpectedException rule for PageSwapperTest
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvest committed Sep 28, 2016
1 parent 6c24d5d commit ad7c1d2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.RuleChain;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -109,8 +111,10 @@ private File file( String filename ) throws IOException
return file;
}

@Rule
public final TestDirectory testDir = TestDirectory.testDirectory();
public final ExpectedException expectedException = ExpectedException.none();
@Rule
public final RuleChain rules = RuleChain.outerRule( testDir ).around( expectedException );

private final ConcurrentLinkedQueue<PageSwapper> openedSwappers = new ConcurrentLinkedQueue<>();

Expand Down Expand Up @@ -566,10 +570,11 @@ public void mustNotIssueEvictionCallbacksAfterSwapperHasBeenClosed() throws Exce
assertFalse( gotCallback.get() );
}

@Test( expected = NoSuchFileException.class )
@Test
public void mustThrowExceptionIfFileDoesNotExist() throws Exception
{
PageSwapperFactory factory = swapperFactory();
expectedException.expect( NoSuchFileException.class );
createSwapper( factory, file( "does not exist" ), cachePageSize(), NO_CALLBACK, false );
}

Expand Down Expand Up @@ -1018,47 +1023,51 @@ public void mustThrowNullPointerExceptionFromWriteWhenPageArrayIsNull() throws E
}
}

@Test( expected = IOException.class )
@Test
public void readMustThrowForNegativeFilePageIds() throws Exception
{
File file = file( "file" );
PageSwapperFactory factory = swapperFactory();
PageSwapper swapper = createSwapper( factory, file, 4, NO_CALLBACK, true );

expectedException.expect( IOException.class );
swapper.read( -1, createPage( 4 ) );
}

@Test( expected = IOException.class )
@Test
public void writeMustThrowForNegativeFilePageIds() throws Exception
{
File file = file( "file" );
PageSwapperFactory factory = swapperFactory();
PageSwapper swapper = createSwapper( factory, file, 4, NO_CALLBACK, true );

expectedException.expect( IOException.class );
swapper.write( -1, createPage( 4 ) );
}

@Test( expected = IOException.class )
@Test
public void vectoredReadMustThrowForNegativeFilePageIds() throws Exception
{
File file = file( "file" );
PageSwapperFactory factory = swapperFactory();
PageSwapper swapper = createSwapper( factory, file, 4, NO_CALLBACK, true );

expectedException.expect( IOException.class );
swapper.read( -1, new Page[]{createPage( 4 ), createPage( 4 )}, 0, 2 );
}

@Test( expected = IOException.class )
@Test
public void vectoredWriteMustThrowForNegativeFilePageIds() throws Exception
{
File file = file( "file" );
PageSwapperFactory factory = swapperFactory();
PageSwapper swapper = createSwapper( factory, file, 4, NO_CALLBACK, true );

expectedException.expect( IOException.class );
swapper.write( -1, new Page[] {createPage( 4 ), createPage( 4 )}, 0, 2 );
}

@Test( expected = ArrayIndexOutOfBoundsException.class )
@Test
public void vectoredReadMustThrowForNegativeArrayOffsets() throws Exception
{
File file = file( "file" );
Expand All @@ -1067,21 +1076,23 @@ public void vectoredReadMustThrowForNegativeArrayOffsets() throws Exception

Page[] pages = {createPage( 4 ), createPage( 4 )};
swapper.write( 0, pages, 0, 2 );
expectedException.expect( ArrayIndexOutOfBoundsException.class );
swapper.read( 0, pages, -1, 2 );
}

@Test( expected = ArrayIndexOutOfBoundsException.class )
@Test
public void vectoredWriteMustThrowForNegativeArrayOffsets() throws Exception
{
File file = file( "file" );
PageSwapperFactory factory = swapperFactory();
PageSwapper swapper = createSwapper( factory, file, 4, NO_CALLBACK, true );

Page[] pages = {createPage( 4 ), createPage( 4 )};
expectedException.expect( ArrayIndexOutOfBoundsException.class );
swapper.write( 0, pages, -1, 2 );
}

@Test( expected = ArrayIndexOutOfBoundsException.class )
@Test
public void vectoredReadMustThrowWhenLengthGoesBeyondArraySize() throws Exception
{
File file = file( "file" );
Expand All @@ -1090,21 +1101,23 @@ public void vectoredReadMustThrowWhenLengthGoesBeyondArraySize() throws Exceptio

Page[] pages = {createPage( 4 ), createPage( 4 )};
swapper.write( 0, pages, 0, 2 );
expectedException.expect( ArrayIndexOutOfBoundsException.class );
swapper.read( 0, pages, 1, 2 );
}

@Test( expected = ArrayIndexOutOfBoundsException.class )
@Test
public void vectoredWriteMustThrowWhenLengthGoesBeyondArraySize() throws Exception
{
File file = file( "file" );
PageSwapperFactory factory = swapperFactory();
PageSwapper swapper = createSwapper( factory, file, 4, NO_CALLBACK, true );

Page[] pages = {createPage( 4 ), createPage( 4 )};
expectedException.expect( ArrayIndexOutOfBoundsException.class );
swapper.write( 0, pages, 1, 2 );
}

@Test( expected = ArrayIndexOutOfBoundsException.class )
@Test
public void vectoredReadMustThrowWhenArrayOffsetIsEqualToArrayLength() throws Exception
{
File file = file( "file" );
Expand All @@ -1113,21 +1126,23 @@ public void vectoredReadMustThrowWhenArrayOffsetIsEqualToArrayLength() throws Ex

Page[] pages = {createPage( 4 ), createPage( 4 )};
swapper.write( 0, pages, 0, 2 );
expectedException.expect( ArrayIndexOutOfBoundsException.class );
swapper.read( 0, pages, 2, 1 );
}

@Test( expected = ArrayIndexOutOfBoundsException.class )
@Test
public void vectoredWriteMustThrowWhenArrayOffsetIsEqualToArrayLength() throws Exception
{
File file = file( "file" );
PageSwapperFactory factory = swapperFactory();
PageSwapper swapper = createSwapper( factory, file, 4, NO_CALLBACK, true );

Page[] pages = {createPage( 4 ), createPage( 4 )};
expectedException.expect( ArrayIndexOutOfBoundsException.class );
swapper.write( 0, pages, 2, 1 );
}

@Test( expected = ArrayIndexOutOfBoundsException.class )
@Test
public void vectoredReadMustThrowWhenArrayOffsetIsGreaterThanArrayLength() throws Exception
{
File file = file( "file" );
Expand All @@ -1136,17 +1151,19 @@ public void vectoredReadMustThrowWhenArrayOffsetIsGreaterThanArrayLength() throw

Page[] pages = {createPage( 4 ), createPage( 4 )};
swapper.write( 0, pages, 0, 2 );
expectedException.expect( ArrayIndexOutOfBoundsException.class );
swapper.read( 0, pages, 3, 1 );
}

@Test( expected = ArrayIndexOutOfBoundsException.class )
@Test
public void vectoredWriteMustThrowWhenArrayOffsetIsGreaterThanArrayLength() throws Exception
{
File file = file( "file" );
PageSwapperFactory factory = swapperFactory();
PageSwapper swapper = createSwapper( factory, file, 4, NO_CALLBACK, true );

Page[] pages = {createPage( 4 ), createPage( 4 )};
expectedException.expect( ArrayIndexOutOfBoundsException.class );
swapper.write( 0, pages, 3, 1 );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,8 @@ public void swappingOutMustNotOverwriteDataBeyondPage() throws IOException

/**
* The OverlappingFileLockException is thrown when tryLock is called on the same file *in the same JVM*.
* @throws Exception
*/
@Test( expected = OverlappingFileLockException.class )
@Test
public void creatingSwapperForFileMustTakeLockOnFile() throws Exception
{
assumeFalse( "No file locking on Windows", SystemUtils.IS_OS_WINDOWS );
Expand All @@ -212,15 +211,16 @@ public void creatingSwapperForFileMustTakeLockOnFile() throws Exception
try
{
StoreChannel channel = fs.open( file, "rw" );
assertThat( channel.tryLock(), is( nullValue() ) );
expectedException.expect( OverlappingFileLockException.class );
channel.tryLock();
}
finally
{
pageSwapper.close();
}
}

@Test( expected = FileLockException.class )
@Test
public void creatingSwapperForInternallyLockedFileMustThrow() throws Exception
{
assumeFalse( "No file locking on Windows", SystemUtils.IS_OS_WINDOWS ); // no file locking on Windows.
Expand All @@ -235,11 +235,12 @@ public void creatingSwapperForInternallyLockedFileMustThrow() throws Exception
try ( FileLock fileLock = channel.tryLock() )
{
assertThat( fileLock, is( not( nullValue() ) ) );
expectedException.expect( FileLockException.class );
createSwapper( factory, file, 4, NO_CALLBACK, true );
}
}

@Test( expected = FileLockException.class )
@Test
public void creatingSwapperForExternallyLockedFileMustThrow() throws Exception
{
assumeFalse( "No file locking on Windows", SystemUtils.IS_OS_WINDOWS ); // no file locking on Windows.
Expand All @@ -263,6 +264,7 @@ public void creatingSwapperForExternallyLockedFileMustThrow() throws Exception

try
{
expectedException.expect( FileLockException.class );
createSwapper( factory, file, 4, NO_CALLBACK, true );
}
finally
Expand Down Expand Up @@ -293,7 +295,7 @@ public void mustUnlockFileWhenThePageSwapperIsClosed() throws Exception
}
}

@Test( expected = OverlappingFileLockException.class )
@Test
public void fileMustRemainLockedEvenIfChannelIsClosedByStrayInterrupt() throws Exception
{
assumeFalse( "No file locking on Windows", SystemUtils.IS_OS_WINDOWS ); // no file locking on Windows.
Expand All @@ -313,7 +315,8 @@ public void fileMustRemainLockedEvenIfChannelIsClosedByStrayInterrupt() throws E
Thread.currentThread().interrupt();
pageSwapper.force();

assertThat( channel.tryLock(), is( nullValue() ) );
expectedException.expect( OverlappingFileLockException.class );
channel.tryLock();
}
finally
{
Expand Down

0 comments on commit ad7c1d2

Please sign in to comment.