/** * Danger zone! This will delete all data (files) of this BoxStore! * You must call {@link #close()} before and read the docs of that method carefully! * <p> * A safer alternative: use the static {@link #deleteAllFiles(File)} method before opening the BoxStore. * * @return true if the directory 1) was deleted successfully OR 2) did not exist in the first place. * Note: If false is returned, any number of files may have been deleted before the failure happened. */ public boolean deleteAllFiles() { if (!closed) { throw new IllegalStateException("Store must be closed"); } return deleteAllFiles(directory); }
/** * Danger zone! This will delete all files in the given directory! * <p> * No {@link BoxStore} may be alive using the given name. * <p> * If you did not use a custom name with BoxStoreBuilder, you can pass "new File({@link * BoxStoreBuilder#DEFAULT_NAME})". * * @param androidContext provide an Android Context like Application or Service * @param customDbNameOrNull use null for default name, or the name you previously provided to {@link * BoxStoreBuilder#name(String)}. * @return true if the directory 1) was deleted successfully OR 2) did not exist in the first place. * Note: If false is returned, any number of files may have been deleted before the failure happened. * @throws IllegalStateException if the given name is still used by a open {@link BoxStore}. * */ public static boolean deleteAllFiles(Object androidContext, @Nullable String customDbNameOrNull) { File dbDir = BoxStoreBuilder.getAndroidDbDir(androidContext, customDbNameOrNull); return deleteAllFiles(dbDir); }
/** * Danger zone! This will delete all files in the given directory! * <p> * No {@link BoxStore} may be alive using the given directory. * <p> * If you did not use a custom name with BoxStoreBuilder, you can pass "new File({@link * BoxStoreBuilder#DEFAULT_NAME})". * * @param baseDirectoryOrNull use null for no base dir, or the value you previously provided to {@link * BoxStoreBuilder#baseDirectory(File)} * @param customDbNameOrNull use null for default name, or the name you previously provided to {@link * BoxStoreBuilder#name(String)}. * @return true if the directory 1) was deleted successfully OR 2) did not exist in the first place. * Note: If false is returned, any number of files may have been deleted before the failure happened. * @throws IllegalStateException if the given directory (+name) is still used by a open {@link BoxStore}. */ public static boolean deleteAllFiles(@Nullable File baseDirectoryOrNull, @Nullable String customDbNameOrNull) { File dbDir = BoxStoreBuilder.getDbDir(baseDirectoryOrNull, customDbNameOrNull); return deleteAllFiles(dbDir); }
@After public void deleteDbFiles() { BoxStore.deleteAllFiles(new File(BoxStoreBuilder.DEFAULT_NAME)); }
@Test(expected = IllegalStateException.class) public void testDeleteAllFiles_openStore() { BoxStore.deleteAllFiles(boxStoreDir); }
private void closeStoreForTest() { assertTrue(boxStoreDir.exists()); store.close(); assertTrue(store.deleteAllFiles()); assertFalse(boxStoreDir.exists()); }
@After public void tearDown() throws Exception { // Collect dangling Cursors and TXs before store closes System.gc(); System.runFinalization(); if (store != null) { try { store.close(); store.deleteAllFiles(); File[] files = boxStoreDir.listFiles(); if (files != null) { for (File file : files) { logError("File was not deleted: " + file.getAbsolutePath()); } } } catch (Exception e) { logError("Could not clean up test", e); } } deleteAllFiles(); }
@After public void tearDown() throws Exception { if (store != null) { try { store.close(); store.deleteAllFiles(); File[] files = boxStoreDir.listFiles(); if (files != null) { for (File file : files) { logError("File was not deleted: " + file.getAbsolutePath()); } } } catch (Exception e) { logError("Could not clean up test", e); } } if (boxStoreDir != null && boxStoreDir.exists()) { File[] files = boxStoreDir.listFiles(); for (File file : files) { delete(file); } delete(boxStoreDir); } }
@Test public void testDeleteAllFiles_staticDir() { closeStoreForTest(); File boxStoreDir2 = new File(boxStoreDir.getAbsolutePath() + "-2"); BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(false)).directory(boxStoreDir2); BoxStore store2 = builder.build(); store2.close(); assertTrue(boxStoreDir2.exists()); assertTrue(BoxStore.deleteAllFiles(boxStoreDir2)); assertFalse(boxStoreDir2.exists()); }
@Test public void testDeleteAllFiles_baseDirName() { closeStoreForTest(); File basedir = new File("test-base-dir"); String name = "mydb"; basedir.mkdir(); assertTrue(basedir.isDirectory()); File dbDir = new File(basedir, name); assertFalse(dbDir.exists()); BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(false)).baseDirectory(basedir).name(name); BoxStore store2 = builder.build(); store2.close(); assertTrue(dbDir.exists()); assertTrue(BoxStore.deleteAllFiles(basedir, name)); assertFalse(dbDir.exists()); assertTrue(basedir.delete()); }
@Test public void testTwoReaders() { store.close(); store.deleteAllFiles(); store = createBoxStoreBuilderWithTwoEntities(false).build(); box = store.boxFor(TestEntity.class); box.count(); Box<TestEntityMinimal> box2 = store.boxFor(TestEntityMinimal.class); box2.count(); box.count(); }
@Test public void testDateParam() { store.close(); assertTrue(store.deleteAllFiles()); store = MyObjectBox.builder().baseDirectory(boxStoreDir).debugFlags(DebugFlags.LOG_QUERY_PARAMETERS).build(); Date now = new Date(); Order order = new Order(); order.setDate(now); Box<Order> box = store.boxFor(Order.class); box.put(order); Query<Order> query = box.query().equal(Order_.date, 0).build(); assertEquals(0, query.count()); query.setParameter(Order_.date, now); }
@Test public void testPutAndGetTwoEntities() { store.close(); store.deleteAllFiles(); store = createBoxStoreBuilderWithTwoEntities(false).build(); box = store.boxFor(TestEntity.class); long key = putTestEntity(null, 1977).getId(); TestEntity entityRead = box.get(key); assertEquals(1977, entityRead.getSimpleInt()); Box<TestEntityMinimal> box2 = store.boxFor(TestEntityMinimal.class); TestEntityMinimal entity2 = new TestEntityMinimal(); entity2.setText("foo"); long key2 = box2.put(entity2); TestEntityMinimal entity2Read = box2.get(key2); assertEquals("foo", entity2Read.getText()); }
@Override public void tearDown() { store.close(); store.deleteAllFiles(); }
@After public void tearDown() throws Exception { if (store != null) { store.close(); store.deleteAllFiles(); } }
@After public void tearDown() throws Exception { if (store != null) { store.close(); store = null; } BoxStore.deleteAllFiles(TEST_DIRECTORY); } }
@Before public void setUp() throws Exception { // delete database files before each test to start with a clean database BoxStore.deleteAllFiles(TEST_DIRECTORY); store = MyObjectBox.builder() // add directory flag to change where ObjectBox puts its database files .directory(TEST_DIRECTORY) // optional: add debug flags for more detailed ObjectBox log output .debugFlags(DebugFlags.LOG_QUERIES | DebugFlags.LOG_QUERY_PARAMETERS) .build(); }
public void setUp(Context context, PerfTestRunner testRunner) { super.setUp(context, testRunner); store = MyObjectBox.builder().androidContext(context).build(); store.close(); store.deleteAllFiles(); store = MyObjectBox.builder().androidContext(context).maxSizeInKByte(200 * 1024).build(); box = store.boxFor(SimpleEntity.class); boxIndexed = store.boxFor(SimpleEntityIndexed.class); if (!versionLoggedOnce) { String versionNative = BoxStore.getVersionNative(); String versionJava = BoxStore.getVersion(); if (versionJava.equals(versionNative)) { log("ObjectBox " + versionNative); } else { log("ObjectBox " + versionNative + " (Java: " + versionJava + ")"); } versionLoggedOnce = true; } }