Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.mapdb;
  
  import java.io.*;
  import java.util.Arrays;
  
  import org.junit.After;
  import org.junit.Assert;
  import org.junit.Before;
  import org.junit.Test;
 
 public class BrokenDBTest {
     File index;
     File data;
     File log;
 
     @Before
     public void before() throws IOException {
          = UtilsTest.tempDbFile();
          = new File(.getPath() + .);
          = new File(.getPath() + .);
     }

    
Verify that DB files are properly closed when opening the database fails, allowing an application to recover by purging the database and starting over.

 
     @Test
     public void canDeleteDBOnBrokenIndex() throws FileNotFoundExceptionIOException {
         for (final File f : Arrays.asList()) {
             final FileOutputStream fos = new FileOutputStream(f);
             fos.write("Some Junk".getBytes());
             fos.close();
         }
 
         try {
             DBMaker.newFileDB().make();
             Assert.fail("Expected exception not thrown");
         } catch (final IOError e) {
             // will fail!
             Assert.assertTrue("Wrong message"e.getMessage().contains("storage has invalid header"));
         }
 
         .delete();
         .delete();
         .delete();
 
         // assert that we can delete the db files
         Assert.assertFalse("Can't delete index".exists());
         Assert.assertFalse("Can't delete data".exists());
         Assert.assertFalse("Can't delete log".exists());
     }

    
Verify that DB files are properly closed when opening the database fails, allowing an application to recover by purging the database and starting over.

 
     @Test
     public void canDeleteDBOnBrokenLog() throws IOException {
         // init empty, but valid DB
         DBMaker.newFileDB().make().close();
 
         // trash the log
         MappedFileVol logVol = new Volume.MappedFileVol(false, 0, false);
         logVol.ensureAvailable(32);
         logVol.putInt(0, .);
         logVol.putUnsignedShort(4, .);
         logVol.putLong(8, .);
         logVol.putLong(16, 123456789L);
         logVol.sync();
         logVol.close();
 
         try {
             DBMaker.newFileDB().make();
             Assert.fail("Expected exception not thrown");
         } catch (final Error e) {
             // will fail!
             Assert.assertTrue("Wrong message"e.getMessage().contains("unknown trans log instruction"));
         }
 
         .delete();
         .delete();
         .delete();
 
         // assert that we can delete the db files
         Assert.assertFalse("Can't delete index".exists());
         Assert.assertFalse("Can't delete data".exists());
         Assert.assertFalse("Can't delete log".exists());
     }
 
     @After
     public void after() throws IOException {
         if ( != null)
            .deleteOnExit();
        if ( != null)
            .deleteOnExit();
        if ( != null)
            .deleteOnExit();
    }
    public static class SomeDataObject implements Serializable {
        private static final long serialVersionUID = 1L;
        public int someField = 42;
    }

    
Verify that DB files are properly closed when opening the database fails, allowing an application to recover by purging the database and starting over.

    @Test
    public void canDeleteDBOnBrokenContent() throws IOException {
        // init empty, but valid DB
        DB db = DBMaker.newFileDB().make();
        db.getHashMap("foo").put("foo"new SomeDataObject());
        db.commit();
        db.close();
        // Fudge the content so that the data refers to an undefined field in SomeDataObject.
        RandomAccessFile dataFile = new RandomAccessFile("rw");
        byte grep[] = "someField".getBytes();
        int p = 0, read;
        while ((read = dataFile.read()) >= 0)
            if (((byteread) == grep[p]) {
                if (++p == grep.length) {
                    dataFile.seek(dataFile.getFilePointer() - grep.length);
                    dataFile.write("xxxxField".getBytes());
                    break;
                }
            } else
                p = 0;
        dataFile.close();
        try {
            DBMaker.newFileDB().make();
            Assert.fail("Expected exception not thrown");
        } catch (final RuntimeException e) {
            // will fail!
            Assert.assertTrue("Wrong message"e.getMessage().contains("Could not set field value"));
        }
        .delete();
        .delete();
        .delete();
        // assert that we can delete the db files
        Assert.assertFalse("Can't delete index".exists());
        Assert.assertFalse("Can't delete data".exists());
        Assert.assertFalse("Can't delete log".exists());
    }
New to GrepCode? Check out our FAQ X