protected Box<TestEntity> getTestEntityBox() { return store.boxFor(TestEntity.class); }
private void ensureBoxes() { if (targetBox == null) { Field boxStoreField = ReflectionCache.getInstance().getField(entity.getClass(), "__boxStore"); try { boxStore = (BoxStore) boxStoreField.get(entity); if (boxStore == null) { throw new DbDetachedException("Cannot resolve relation for detached entities, " + "call box.attach(entity) beforehand."); } } catch (IllegalAccessException e) { throw new RuntimeException(e); } entityBox = boxStore.boxFor(relationInfo.sourceInfo.getEntityClass()); targetBox = boxStore.boxFor(relationInfo.targetInfo.getEntityClass()); } }
@Test(expected = RuntimeException.class) public void testBoxForUnknownEntity() { store.boxFor(getClass()); }
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()); } }
@Before public void setUp() throws Exception { // This works with Android without needing any context File tempFile = File.createTempFile("object-store-test", ""); tempFile.delete(); boxStoreDir = tempFile; store = createBoxStore(); box = store.boxFor(ObfuscatedEntity.class); runExtensiveTests = System.getProperty("extensive") != null; }
@Override @SuppressWarnings("NullableProblems") public Long transform(Class source) throws Exception { assertNotSame(testThread, Thread.currentThread()); return store.boxFor(source).count(); } });
@Override public void run() { putTestEntities(3); Box<TestEntityMinimal> boxMini = store.boxFor(TestEntityMinimal.class); boxMini.put(new TestEntityMinimal(), new TestEntityMinimal()); assertEquals(0, classesWithChanges.size()); } };
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 testOldReaderWithIndex() throws InterruptedException { final Box<EntityLongIndex> box = store.boxFor(EntityLongIndex.class); final int initialValue = 1; final Query<EntityLongIndex> query = box.query().equal(EntityLongIndex_.indexedLong, 0).build(); assertNull(query.findUnique()); System.out.println("BEFORE put: " + box.getReaderDebugInfo()); System.out.println("count before: " + box.count()); box.put(createEntityLongIndex(initialValue)); System.out.println("AFTER put: " + box.getReaderDebugInfo()); System.out.println("count after: " + box.count()); query.setParameter(EntityLongIndex_.indexedLong, initialValue); assertNotNull(query.findUnique()); query.setParameter(EntityLongIndex_.indexedLong, 0); assertNull(query.findUnique()); }
@Test public void testOverwriteIndexedValue() throws InterruptedException { final Box<EntityLongIndex> box = store.boxFor(EntityLongIndex.class); final int initialValue = 1;
@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 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); }
@Test public void testOldReaderInThread() throws InterruptedException { final Box<EntityLongIndex> box = store.boxFor(EntityLongIndex.class); final int initialValue = 1;
@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 // 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()); }
public ObjectboxWaypointsRepo(@AppContext Context context, EventBus eventBus, Preferences preferences) { super(eventBus); BoxStore boxStore = MyObjectBox.builder().androidContext(context).build(); this.box = boxStore.boxFor(org.owntracks.android.data.WaypointModel.class); this.preferences = preferences; if(!preferences.isObjectboxMigrated()) { migrateLegacyData(context); } }
@Test public void testPutAndGet() { Box<Note> box = store.boxFor(Note.class); Note note = new Note(); note.setText("Hello Desktop"); note.setDate(new Date()); long id = box.put(note); Note noteFromBox = box.get(id); assertEquals("Hello Desktop", noteFromBox.getText()); }