### Question:
IndexProviderStore { public void close() { if ( !fileChannel.isOpen() ) { return; } writeOut(); try { fileChannel.close(); } catch ( IOException e ) { throw new RuntimeException( e ); } } IndexProviderStore( File file, FileSystemAbstraction fileSystem, long expectedVersion, boolean allowUpgrade ); File getFile(); long getCreationTime(); long getVersion(); long getIndexVersion(); synchronized void setVersion( long version ); synchronized void setLastCommittedTx( long txId ); long getLastCommittedTx(); void close();  }### Answer:
@Test( expected = NotCurrentStoreVersionException.class ) public void shouldFailToGoBackToOlderVersion() { String newerVersion = "3.6"; String olderVersion = "3.1"; try { IndexProviderStore store = new IndexProviderStore( file, fileSystem, MetaDataStore.versionStringToLong( newerVersion ), true ); store.close(); store = new IndexProviderStore( file, fileSystem, MetaDataStore.versionStringToLong( olderVersion ), false ); } catch ( NotCurrentStoreVersionException e ) { assertTrue( e.getMessage().contains( newerVersion ) ); assertTrue( e.getMessage().contains( olderVersion ) ); throw e; } }
@Test( expected = NotCurrentStoreVersionException.class ) public void shouldFailToGoBackToOlderVersionEvenIfAllowUpgrade() { String newerVersion = "3.6"; String olderVersion = "3.1"; try { IndexProviderStore store = new IndexProviderStore( file, fileSystem, MetaDataStore.versionStringToLong( newerVersion ), true ); store.close(); store = new IndexProviderStore( file, fileSystem, MetaDataStore.versionStringToLong( olderVersion ), true ); } catch ( NotCurrentStoreVersionException e ) { assertTrue( e.getMessage().contains( newerVersion ) ); assertTrue( e.getMessage().contains( olderVersion ) ); throw e; } }
@Test public void upgradeForMissingVersionRecord() throws Exception { IndexProviderStore store = new IndexProviderStore( file, fileSystem, 0, false ); store.close(); FileUtils.truncateFile( file, 4 * 8 ); try { store = new IndexProviderStore( file, fileSystem, 0, false ); fail( "Should have thrown upgrade exception" ); } catch ( UpgradeNotAllowedByConfigurationException e ) { } store = new IndexProviderStore( file, fileSystem, 0, true ); store.close(); }
@Test public void shouldForceChannelAfterWritingMetadata() throws IOException { final StoreChannel[] channelUsedToCreateFile = {null}; FileSystemAbstraction fs = spy( fileSystem ); StoreChannel tempChannel; when( tempChannel = fs.open( file, OpenMode.READ_WRITE ) ).then( ignored -> { StoreChannel channel = fileSystem.open( file, OpenMode.READ_WRITE ); if ( channelUsedToCreateFile[0] == null ) { StoreChannel channelSpy = spy( channel ); channelUsedToCreateFile[0] = channelSpy; channel = channelSpy; } return channel; } ); tempChannel.close(); fs.deleteFile( file ); IndexProviderStore store = new IndexProviderStore( file, fs, MetaDataStore.versionStringToLong( "3.6" ), false ); StoreChannel channel = channelUsedToCreateFile[0]; verify( channel ).writeAll( any( ByteBuffer.class ), eq( 0L ) ); verify( channel ).force( true ); verify( channel ).close(); verifyNoMoreInteractions( channel ); store.close(); }
@Test public void shouldWriteNewFileWhenExistingFileHasZeroLength() throws IOException { file.createNewFile(); IndexProviderStore store = new IndexProviderStore( file, fileSystem, MetaDataStore.versionStringToLong( "3.6" ), false ); assertTrue( fileSystem.getFileSize( file ) > 0 ); store.close(); }