### Question:
LogHeaderReader { public static LogHeader readLogHeader( FileSystemAbstraction fileSystem, File file ) throws IOException { return readLogHeader( fileSystem, file, true ); } private  LogHeaderReader(); static LogHeader readLogHeader( FileSystemAbstraction fileSystem, File file ); static LogHeader readLogHeader( FileSystemAbstraction fileSystem, File file, boolean strict ); static LogHeader readLogHeader( ByteBuffer buffer, ReadableByteChannel channel, boolean strict,
            File fileForAdditionalErrorInformationOrNull );  }### Answer:
@Test public void shouldReadALogHeaderFromAByteChannel() throws IOException { final ByteBuffer buffer = ByteBuffer.allocate( LOG_HEADER_SIZE ); final ReadableByteChannel channel = mock( ReadableByteChannel.class ); when( channel.read( buffer ) ).thenAnswer( invocation -> { buffer.putLong( encodeLogVersion( expectedLogVersion ) ); buffer.putLong( expectedTxId ); return 8 + 8; } ); final LogHeader result = readLogHeader( buffer, channel, true, null ); assertEquals( new LogHeader( CURRENT_LOG_VERSION, expectedLogVersion, expectedTxId ), result ); }
@Test public void shouldFailWhenUnableToReadALogHeaderFromAChannel() throws IOException { final ByteBuffer buffer = ByteBuffer.allocate( LOG_HEADER_SIZE ); final ReadableByteChannel channel = mock( ReadableByteChannel.class ); when( channel.read( buffer ) ).thenReturn( 1 ); try { readLogHeader( buffer, channel, true, null ); fail( "should have thrown" ); } catch ( IncompleteLogHeaderException ex ) { } }
@Test public void shouldReadALogHeaderFromAFile() throws IOException { final File file = testDirectory.file( "ReadLogHeader" ); final ByteBuffer buffer = ByteBuffer.allocate( LOG_HEADER_SIZE ); buffer.putLong( encodeLogVersion( expectedLogVersion ) ); buffer.putLong( expectedTxId ); try ( OutputStream stream = fileSystemRule.get().openAsOutputStream( file, false ) ) { stream.write( buffer.array() ); } final LogHeader result = readLogHeader( fileSystemRule.get(), file ); assertEquals( new LogHeader( CURRENT_LOG_VERSION, expectedLogVersion, expectedTxId ), result ); }
@Test public void shouldFailWhenUnableToReadALogHeaderFromAFile() throws IOException { final File file = testDirectory.file( "ReadLogHeader" ); fileSystemRule.create( file ).close(); try { readLogHeader( fileSystemRule.get(), file ); fail( "should have thrown" ); } catch ( IncompleteLogHeaderException ex ) { assertTrue( ex.getMessage(), ex.getMessage().contains( file.getName() ) ); } }