Search in sources :

Example 16 with AllJavaTypes

use of io.realm.entities.AllJavaTypes in project realm-java by realm.

the class OrderedRealmCollectionIteratorTests method useCase_forEachIterator_modifyQueryResult_innerTransaction_looperThread.

@Test
@UiThreadTest
public void useCase_forEachIterator_modifyQueryResult_innerTransaction_looperThread() {
    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) UiThreadTest(android.support.test.annotation.UiThreadTest)

Example 17 with AllJavaTypes

use of io.realm.entities.AllJavaTypes in project realm-java by realm.

the class OrderedRealmCollectionIteratorTests method listIterator_add.

public void listIterator_add() {
    if (skipTest(CollectionClass.REALMRESULTS)) {
        return;
    }
    realm.beginTransaction();
    ListIterator<AllJavaTypes> it = collection.listIterator();
    // Calling set() before next() should throw.
    try {
        it.add(new AllJavaTypes());
        fail();
    } catch (IllegalStateException ignored) {
    }
    AllJavaTypes obj = it.next();
    assertEquals(0, obj.getFieldLong());
    it.add(new AllJavaTypes(42));
    obj = it.previous();
    assertEquals(42, obj.getFieldLong());
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes)

Example 18 with AllJavaTypes

use of io.realm.entities.AllJavaTypes in project realm-java by realm.

the class OrderedRealmCollectionIteratorTests method iterator_outsideChangeToSizeThrowsConcurrentModification.

@Test
public void iterator_outsideChangeToSizeThrowsConcurrentModification() {
    if (skipTest(CollectionClass.REALMRESULTS, CollectionClass.REALMRESULTS_SNAPSHOT_RESULTS_BASE, CollectionClass.REALMRESULTS_SNAPSHOT_LIST_BASE)) {
        return;
    }
    // Tests all standard collection methods.
    for (CollectionMethod method : CollectionMethod.values()) {
        collection = createCollection(realm, collectionClass, TEST_SIZE);
        realm.beginTransaction();
        Iterator<AllJavaTypes> it = collection.iterator();
        switch(method) {
            case ADD_OBJECT:
                collection.add(new AllJavaTypes(TEST_SIZE));
                break;
            case ADD_ALL_OBJECTS:
                collection.addAll(Collections.singletonList(new AllJavaTypes(TEST_SIZE)));
                break;
            case CLEAR:
                collection.clear();
                break;
            case REMOVE_OBJECT:
                collection.remove(collection.get(0));
                break;
            case REMOVE_ALL:
                collection.removeAll(Collections.singletonList(collection.get(0)));
                break;
            case RETAIN_ALL:
                collection.retainAll(Collections.singletonList(collection.get(0)));
                break;
            // Does not impact size, so does not trigger ConcurrentModificationException.
            case CONTAINS:
            case CONTAINS_ALL:
            case EQUALS:
            case HASHCODE:
            case IS_EMPTY:
            case ITERATOR:
            case SIZE:
            case TO_ARRAY:
            case TO_ARRAY_INPUT:
                realm.cancelTransaction();
                continue;
            default:
                fail("Unknown method: " + method);
        }
        checkIteratorThrowsConcurrentModification(realm, method.toString(), it);
    }
    for (ListMethod method : ListMethod.values()) {
        collection = createCollection(realm, collectionClass, TEST_SIZE);
        realm.beginTransaction();
        Iterator<AllJavaTypes> it = collection.iterator();
        switch(method) {
            case ADD_INDEX:
                collection.add(0, new AllJavaTypes(TEST_SIZE));
                break;
            case ADD_ALL_INDEX:
                collection.addAll(0, Collections.singleton(new AllJavaTypes(TEST_SIZE)));
                break;
            case REMOVE_INDEX:
                collection.remove(0);
                break;
            // Does not impact size, so does not trigger ConcurrentModificationException.
            case FIRST:
            case LAST:
            case GET_INDEX:
            case INDEX_OF:
            case LAST_INDEX_OF:
            case LIST_ITERATOR:
            case LIST_ITERATOR_INDEX:
            case SET:
            case SUBLIST:
                realm.cancelTransaction();
                continue;
            default:
                fail("Unknown method: " + method);
        }
        checkIteratorThrowsConcurrentModification(realm, method.toString(), it);
    }
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test) UiThreadTest(android.support.test.annotation.UiThreadTest)

Example 19 with AllJavaTypes

use of io.realm.entities.AllJavaTypes in project realm-java by realm.

the class OrderedRealmCollectionIteratorTests method listIterator_remove_realmList_doesNotDeleteObject.

@Test
public void listIterator_remove_realmList_doesNotDeleteObject() {
    if (skipTest(CollectionClass.REALMRESULTS, CollectionClass.REALMRESULTS_SNAPSHOT_LIST_BASE, CollectionClass.REALMRESULTS_SNAPSHOT_RESULTS_BASE)) {
        return;
    }
    ListIterator<AllJavaTypes> it = collection.listIterator();
    AllJavaTypes obj = it.next();
    assertEquals("test data 0", obj.getFieldString());
    realm.beginTransaction();
    it.remove();
    assertTrue(obj.isValid());
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test) UiThreadTest(android.support.test.annotation.UiThreadTest)

Example 20 with AllJavaTypes

use of io.realm.entities.AllJavaTypes in project realm-java by realm.

the class OrderedRealmCollectionIteratorTests method useCase_simpleIterator_modifyQueryResult_innerTransaction_looperThread.

@Test
@UiThreadTest
public void useCase_simpleIterator_modifyQueryResult_innerTransaction_looperThread() {
    if (skipTest(CollectionClass.MANAGED_REALMLIST, CollectionClass.UNMANAGED_REALMLIST, CollectionClass.REALMRESULTS)) {
        return;
    }
    assertEquals(TEST_SIZE, collection.size());
    for (int i = 0; i < collection.size(); i++) {
        realm.beginTransaction();
        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) 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