use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class OrderedRealmCollectionIteratorTests method useCase_forEachIterator_modifyQueryResult_innerTransaction.
@Test
public void useCase_forEachIterator_modifyQueryResult_innerTransaction() {
if (skipTest(CollectionClass.MANAGED_REALMLIST, CollectionClass.UNMANAGED_REALMLIST)) {
return;
}
assertEquals(TEST_SIZE, collection.size());
for (AllJavaTypes obj : collection) {
realm.beginTransaction();
obj.setFieldLong(obj.getFieldLong() + TEST_SIZE);
realm.commitTransaction();
}
// Verifies that all elements were modified.
assertEquals(0, realm.where(AllJavaTypes.class).lessThan(AllJavaTypes.FIELD_LONG, TEST_SIZE).count());
}
use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class OrderedRealmCollectionIteratorTests method iterator_remove.
@Test
public void iterator_remove() {
Iterator<AllJavaTypes> it = collection.iterator();
AllJavaTypes obj = it.next();
assertEquals("test data 0", obj.getFieldString());
realm.beginTransaction();
try {
it.remove();
} catch (UnsupportedOperationException e) {
// RealmResults doesn't support remove.
assertResultsOrSnapshot();
return;
}
// Unmanaged objects are always invalid, but cannot be GC'ed while we have a reference.
// managed objects should not be deleted (= invalid).
assertNotEquals(CollectionClass.REALMRESULTS, collectionClass);
assertTrue(obj.isValid());
assertEquals("test data 1", collection.iterator().next().getFieldString());
assertEquals(TEST_SIZE - 1, collection.size());
}
use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class OrderedRealmCollectionIteratorTests method useCase_simpleIterator_modifyQueryResult_outerTransaction.
@Test
public void useCase_simpleIterator_modifyQueryResult_outerTransaction() {
if (skipTest(CollectionClass.MANAGED_REALMLIST, CollectionClass.UNMANAGED_REALMLIST, CollectionClass.REALMRESULTS)) {
return;
}
assertEquals(TEST_SIZE, collection.size());
realm.beginTransaction();
for (int i = 0; i < collection.size(); i++) {
AllJavaTypes obj = collection.get(i);
obj.setFieldLong(obj.getFieldLong() + TEST_SIZE);
}
realm.commitTransaction();
// Verifies that all elements were modified.
assertEquals(0, realm.where(AllJavaTypes.class).lessThan(AllJavaTypes.FIELD_LONG, TEST_SIZE).count());
}
use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class OrderedRealmCollectionIteratorTests method iterator_realmResultsThrowConcurrentModification_snapshotJustWorks.
// Accessing RealmResults iterator after receving remote change notification will throw.
// But it is valid operation for snapshot.
@Test
public void iterator_realmResultsThrowConcurrentModification_snapshotJustWorks() {
if (skipTest(CollectionClass.MANAGED_REALMLIST, CollectionClass.UNMANAGED_REALMLIST)) {
return;
}
// Verifies that ConcurrentModification is correctly detected on non-looper threads.
Iterator<AllJavaTypes> it = collection.iterator();
final CountDownLatch bgDone = new CountDownLatch(1);
new Thread(new Runnable() {
@Override
public void run() {
Realm bgRealm = Realm.getInstance(realm.getConfiguration());
bgRealm.beginTransaction();
bgRealm.createObject(AllJavaTypes.class, TEST_SIZE);
bgRealm.commitTransaction();
bgRealm.close();
bgDone.countDown();
}
}).start();
TestHelper.awaitOrFail(bgDone);
realm.waitForChange();
try {
it.next();
assertEquals(TEST_SIZE, collection.size());
} catch (ConcurrentModificationException ignored) {
assertEquals(collectionClass, CollectionClass.REALMRESULTS);
}
}
use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class OrderedRealmCollectionIteratorTests method listIterator_deleteManagedObjectIndirectly.
@Test
public void listIterator_deleteManagedObjectIndirectly() {
realm.beginTransaction();
ListIterator<AllJavaTypes> it = collection.listIterator();
it.next();
it.next().deleteFromRealm();
realm.commitTransaction();
switch(collectionClass) {
case MANAGED_REALMLIST:
case REALMRESULTS:
assertEquals(TEST_SIZE - 1, collection.size());
break;
case UNMANAGED_REALMLIST:
assertEquals(TEST_SIZE, collection.size());
break;
}
it.previous();
// Iterator can still access the deleted object
AllJavaTypes types = it.next();
//noinspection SimplifiableConditionalExpression
assertTrue(collectionClass == CollectionClass.MANAGED_REALMLIST ? types.isValid() : !types.isValid());
}
Aggregations