Search in sources :

Example 56 with AllJavaTypes

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());
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test) UiThreadTest(android.support.test.annotation.UiThreadTest)

Example 57 with AllJavaTypes

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());
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test) UiThreadTest(android.support.test.annotation.UiThreadTest)

Example 58 with AllJavaTypes

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());
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test) UiThreadTest(android.support.test.annotation.UiThreadTest)

Example 59 with AllJavaTypes

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);
    }
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) AllJavaTypes(io.realm.entities.AllJavaTypes) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) UiThreadTest(android.support.test.annotation.UiThreadTest)

Example 60 with AllJavaTypes

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());
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test) UiThreadTest(android.support.test.annotation.UiThreadTest)

Aggregations

AllJavaTypes (io.realm.entities.AllJavaTypes)90 Test (org.junit.Test)78 UiThreadTest (android.support.test.annotation.UiThreadTest)24 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Date (java.util.Date)5 RealmException (io.realm.exceptions.RealmException)4 Ignore (org.junit.Ignore)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 ExpectedException (org.junit.rules.ExpectedException)2 Pair (android.util.Pair)1 CyclicType (io.realm.entities.CyclicType)1 Dog (io.realm.entities.Dog)1 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)1 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)1 RealmPrimaryKeyConstraintException (io.realm.exceptions.RealmPrimaryKeyConstraintException)1 RunInLooperThread (io.realm.rule.RunInLooperThread)1 Field (java.lang.reflect.Field)1