@Override protected void finalize() throws Throwable { close(); super.finalize(); }
@Test public void testOpenSameBoxStoreAfterClose() { store.close(); createBoxStore(); }
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 testDebugCursor_seekOrNext_get_withoutModel() { TestEntity entity1 = putTestEntity("foo", 23); TestEntity entity2 = putTestEntity("bar", 42); store.close(); store = BoxStoreBuilder.createDebugWithoutModel().directory(boxStoreDir).build(); runTest(entity1, entity2); }
@Test public void testDefaultStore() { BoxStore boxStore = builder.buildDefault(); assertSame(boxStore, BoxStore.getDefault()); assertSame(boxStore, BoxStore.getDefault()); boxStore.close(); // to prevent "Another BoxStore was opened" error try { builder.buildDefault(); fail("Should have thrown"); } catch (IllegalStateException expected) { // OK } }
@Test public void testClearDefaultStore() { BoxStore boxStore1 = builder.buildDefault(); BoxStore.clearDefaultStore(); try { BoxStore.getDefault(); fail("Should have thrown"); } catch (IllegalStateException expected) { // OK } boxStore1.close(); BoxStore boxStore = builder.buildDefault(); assertSame(boxStore, BoxStore.getDefault()); }
@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()); }
@Test // TODO can we improve? More than just "still works"? public void testQueryAttempts() { store.close(); BoxStoreBuilder builder = new BoxStoreBuilder(createTestModel(false)).directory(boxStoreDir) .queryAttempts(5) .failedReadTxAttemptCallback(new TxCallback() { @Override public void txFinished(@Nullable Object result, @Nullable Throwable error) { error.printStackTrace(); } }); builder.entity(new TestEntity_()); store = builder.build(); putTestEntitiesScalars(); Query<TestEntity> query = store.boxFor(TestEntity.class).query().equal(simpleInt, 2007).build(); assertEquals(2007, query.findFirst().getSimpleInt()); }
@Override public void tearDown() { store.close(); store.deleteAllFiles(); }
@After public void tearDown() throws Exception { if (store != null) { store.close(); store.deleteAllFiles(); } }
public static void main(String[] args) { BoxStore store = MyObjectBox.builder().name("objectbox-notes-db").build(); Box<Note> box = store.boxFor(Note.class); String text = args.length > 0 ? String.join(" ", args) : "No text given"; box.put(new Note(text)); System.out.println(box.count() + " notes in ObjectBox database:"); for (Note note : box.getAll()) { System.out.println(note); } store.close(); } }
@After public void tearDown() throws Exception { if (store != null) { store.close(); store = null; } BoxStore.deleteAllFiles(TEST_DIRECTORY); } }