protected Box<TestEntity> getTestEntityBox() { return store.boxFor(TestEntity.class); }
@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(); }
@Before public void setUp() throws IOException { Cursor.TRACK_CREATION_STACK = true; Transaction.TRACK_CREATION_STACK = true; // This works with Android without needing any context File tempFile = File.createTempFile("object-store-test", ""); tempFile.delete(); boxStoreDir = tempFile; if (!printedVersionsOnce) { System.out.println("ObjectBox Java version: " + BoxStore.getVersion()); System.out.println("ObjectBox Core version: " + BoxStore.getVersionNative()); System.out.println("First DB dir: " + boxStoreDir); printedVersionsOnce = true; } store = createBoxStore(); runExtensiveTests = System.getProperty("extensive-tests") != null; }
private void ensureBoxes(TARGET target) { // Only check the property set last if (targetBox == null) { Field boxStoreField = ReflectionCache.getInstance().getField(entity.getClass(), "__boxStore"); try { boxStore = (BoxStore) boxStoreField.get(entity); if (boxStore == null) { if (target != null) { boxStoreField = ReflectionCache.getInstance().getField(target.getClass(), "__boxStore"); boxStore = (BoxStore) boxStoreField.get(target); } if (boxStore == null) { throw new DbDetachedException("Cannot resolve relation for detached entities, " + "call box.attach(entity) beforehand."); } } debugRelations = boxStore.isDebugRelations(); } catch (IllegalAccessException e) { throw new RuntimeException(e); } entityBox = boxStore.boxFor(relationInfo.sourceInfo.getEntityClass()); //noinspection unchecked targetBox = boxStore.boxFor(relationInfo.targetInfo.getEntityClass()); } }
private void closeStoreForTest() { assertTrue(boxStoreDir.exists()); store.close(); assertTrue(store.deleteAllFiles()); assertFalse(boxStoreDir.exists()); }
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; } }
@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()); }
@Test public void testOverwriteIndexedValue() throws InterruptedException { final Box<EntityLongIndex> box = store.boxFor(EntityLongIndex.class); final int initialValue = 1; store.subscribe(EntityLongIndex.class).transform(new DataTransformer<Class<EntityLongIndex>, EntityLongIndex>() { @Override public EntityLongIndex transform(Class<EntityLongIndex> clazz) throws Exception {
public void testTwoObjectClassesChanged_oneClassObserver(boolean weak) throws InterruptedException { DataSubscription subscription = subscribe(weak, TestEntityMinimal.class); runTxAndWaitForObservers(1); assertEquals(1, classesWithChanges.size()); assertEquals(classesWithChanges.get(0), TestEntityMinimal.class); classesWithChanges.clear(); putTestEntities(1); assertEquals(0, classesWithChanges.size()); // Adding twice should not trigger notification twice DataSubscription subscription2 = subscribe(weak, TestEntityMinimal.class); Box<TestEntityMinimal> boxMini = store.boxFor(TestEntityMinimal.class); observerLatch = new CountDownLatch(1); boxMini.put(new TestEntityMinimal(), new TestEntityMinimal()); assertLatchCountedDown(observerLatch, 5); Thread.sleep(20); assertEquals(1, classesWithChanges.size()); classesWithChanges.clear(); subscription.cancel(); store.runInTx(txRunnable); assertNoStaleObservers(); }
@Test public void testCloseThreadResources() { Box<TestEntity> box = store.boxFor(TestEntity.class); Cursor<TestEntity> reader = box.getReader(); box.releaseReader(reader); Cursor<TestEntity> reader2 = box.getReader(); box.releaseReader(reader2); assertSame(reader, reader2); store.closeThreadResources(); Cursor<TestEntity> reader3 = box.getReader(); box.releaseReader(reader3); assertNotSame(reader, reader3); }
/** * 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); }
@Override protected void finalize() throws Throwable { close(); super.finalize(); }
@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()); }
prepareOneEntryWith1230(); Transaction txWrite = store.beginTx(); Transaction txRead = store.beginReadTx(); txRead = store.beginReadTx(); cursorRead = txRead.createKeyValueCursor(); assertArrayEquals(new byte[]{3, 2, 1, 0}, cursorRead.get(123)); store.close();
public void testObserverError(Scheduler scheduler) throws InterruptedException { final List<Throwable> errors = new CopyOnWriteArrayList<>(); final CountDownLatch latch = new CountDownLatch(2); final Thread testThread = Thread.currentThread(); DataSubscription subscription = store.subscribe().onlyChanges().onError(new ErrorObserver() { @Override public void onError(Throwable th) { assertNotSame(testThread, Thread.currentThread()); errors.add(th); latch.countDown(); } }).on(scheduler).observer(new DataObserver<Class>() { @Override public void onData(Class data) { throw new RuntimeException("Boo"); } }); store.runInTx(txRunnable); assertLatchCountedDown(latch, 5); assertEquals(2, errors.size()); assertEquals("Boo", errors.get(0).getMessage()); errors.clear(); subscription.cancel(); store.runInTx(txRunnable); Thread.sleep(20); assertEquals(0, errors.size()); }
@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 } }
/** Loads the remaining entities (if any) that were not loaded before. Applies to cached lazy lists only. */ public void loadRemaining() { if (loadedCount != size) { checkCached(); // use single reader only for efficiency box.getStore().runInReadTx(new Runnable() { @Override public void run() { for (int i = 0; i < size; i++) { get(i); } } }); } }
@Override public void subscribe(final ObservableEmitter<Class> emitter) throws Exception { final DataSubscription dataSubscription = boxStore.subscribe().observer(new DataObserver<Class>() { @Override public void onData(Class data) { if (!emitter.isDisposed()) { emitter.onNext(data); } } }); emitter.setCancellable(new Cancellable() { @Override public void cancel() throws Exception { dataSubscription.cancel(); } }); } });
@Before public void loadLib() { BoxStore.getVersionNative(); }