/** * Closes the id generator flushing defragged ids in memory to file. The * file will be truncated to the minimal size required to hold all defragged * ids and it will be marked as clean (not sticky). * <p> * An invoke to the <CODE>nextId</CODE> or <CODE>freeId</CODE> after * this method has been invoked will result in an <CODE>IOException</CODE> * since the highest returned id has been set to a negative value. */ @Override public synchronized void close() { idContainer.close( highId ); }
@Test public void shouldReturnTrueOnInitIfAProperFileWasThere() { // Given // A properly created and closed id file IdContainer idContainer = new IdContainer( fs, file, 100, false ); idContainer.init(); idContainer.close( 100 ); // When // An IdContainer is created over it idContainer = new IdContainer( fs, file, 100, false ); // Then // init() should return true assertTrue( idContainer.init() ); idContainer.close( 100 ); }
@Test public void shouldReturnFalseOnInitIfTheFileWasCreated() { // When // An IdContainer is created with no underlying file IdContainer idContainer = new IdContainer( fs, file, 100, false ); // Then // Init should return false assertFalse( idContainer.init() ); idContainer.close( 100 ); }
@Test public void idContainerReadWriteBySingleByte() throws IOException { SingleByteFileSystemAbstraction fileSystem = new SingleByteFileSystemAbstraction(); IdContainer idContainer = new IdContainer( fileSystem, file, 100, false ); idContainer.init(); idContainer.close( 100 ); idContainer = new IdContainer( fileSystem, file, 100, false ); idContainer.init(); assertEquals( 100, idContainer.getInitialHighId() ); fileSystem.close(); idContainer.close( 100 ); }
@Test public void shouldDeleteIfClosed() { // GIVEN createEmptyFile(); IdContainer idContainer = new IdContainer( fs, file, 100, false ); idContainer.init(); idContainer.close( 0 ); // WHEN idContainer.delete(); // THEN assertFalse( fs.fileExists( file ) ); }
@Test public void shouldDeleteIfOpen() { // GIVEN createEmptyFile(); IdContainer idContainer = new IdContainer( fs, file, 100, false ); idContainer.init(); // WHEN idContainer.delete(); // THEN assertFalse( fs.fileExists( file ) ); idContainer.close( 0 ); }
@Test public void shouldForceStickyMark() throws Exception { // GIVEN createEmptyFile(); // WHEN opening the id generator, where the jvm crashes right after IdContainer idContainer = new IdContainer( fs, file, 100, false ); idContainer.init(); // THEN try { IdContainer.readHighId( fs, file ); fail( "Should have thrown, saying something with sticky generator" ); } catch ( InvalidIdGeneratorException e ) { // THEN Good } finally { idContainer.close( 0 ); } }
@Test public void shouldTruncateTheFileIfOverwriting() throws Exception { // GIVEN IdContainer.createEmptyIdFile( fs, file, 30, false ); IdContainer idContainer = new IdContainer( fs, file, 5, false ); idContainer.init(); for ( int i = 0; i < 17; i++ ) { idContainer.freeId( i ); } idContainer.close( 30 ); assertThat( (int) fs.getFileSize( file ), greaterThan( IdContainer.HEADER_SIZE ) ); // WHEN IdContainer.createEmptyIdFile( fs, file, 30, false ); // THEN assertEquals( IdContainer.HEADER_SIZE, (int) fs.getFileSize( file ) ); assertEquals( 30, IdContainer.readHighId( fs, file ) ); idContainer = new IdContainer( fs, file, 5, false ); idContainer.init(); assertEquals( 30, idContainer.getInitialHighId() ); idContainer.close( 30 ); }
/** * Closes the id generator flushing defragged ids in memory to file. The * file will be truncated to the minimal size required to hold all defragged * ids and it will be marked as clean (not sticky). * <p> * An invoke to the <CODE>nextId</CODE> or <CODE>freeId</CODE> after * this method has been invoked will result in an <CODE>IOException</CODE> * since the highest returned id has been set to a negative value. */ @Override public synchronized void close() { idContainer.close( highId ); }
@Override public void close() { idContainerLock.lock(); try { idContainer.close( highId ); } finally { idContainerLock.unlock(); } }