Search in sources :

Example 71 with AllJavaTypes

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

the class ManagedOrderedRealmCollectionTests method runMethodOnWrongThread.

private boolean runMethodOnWrongThread(final ListMethod method) throws ExecutionException, InterruptedException {
    realm.beginTransaction();
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    Future<Boolean> future = executorService.submit(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            // Defines expected exception.
            Class<? extends Throwable> expected = IllegalStateException.class;
            if (collectionClass == ManagedCollection.REALMRESULTS || isSnapshot(collectionClass)) {
                switch(method) {
                    case ADD_INDEX:
                    case ADD_ALL_INDEX:
                    case SET:
                    case REMOVE_INDEX:
                        expected = UnsupportedOperationException.class;
                        break;
                    default:
                }
            }
            try {
                switch(method) {
                    case FIRST:
                        collection.first();
                        break;
                    case LAST:
                        collection.last();
                        break;
                    case ADD_INDEX:
                        collection.add(0, new AllJavaTypes());
                        break;
                    case ADD_ALL_INDEX:
                        collection.addAll(0, Collections.singletonList(new AllJavaTypes()));
                        break;
                    case GET_INDEX:
                        collection.get(0);
                        break;
                    case INDEX_OF:
                        collection.indexOf(new AllJavaTypes());
                        break;
                    case LAST_INDEX_OF:
                        collection.lastIndexOf(new AllJavaTypes());
                        break;
                    case LIST_ITERATOR:
                        collection.listIterator();
                        break;
                    case LIST_ITERATOR_INDEX:
                        collection.listIterator(0);
                        break;
                    case REMOVE_INDEX:
                        collection.remove(0);
                        break;
                    case SET:
                        collection.set(0, new AllJavaTypes());
                        break;
                    case SUBLIST:
                        collection.subList(0, 1);
                        break;
                }
                return false;
            } catch (Throwable t) {
                if (!t.getClass().equals(expected)) {
                    return false;
                }
            }
            return true;
        }
    });
    Boolean result = future.get();
    realm.cancelTransaction();
    return result;
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) AllJavaTypes(io.realm.entities.AllJavaTypes) ExecutionException(java.util.concurrent.ExecutionException) ExpectedException(org.junit.rules.ExpectedException)

Example 72 with AllJavaTypes

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

the class ManagedRealmCollectionTests method where_lessThanGreaterThan.

/**
     * Tests to see if a particular item that does exist in the same Realm does not
     * exist in the result set of another query.
     */
@Test
public void where_lessThanGreaterThan() {
    RealmResults<AllJavaTypes> items = realm.where(AllJavaTypes.class).lessThan(AllJavaTypes.FIELD_LONG, 1000).findAll();
    AllJavaTypes anotherType = realm.where(AllJavaTypes.class).greaterThan(AllJavaTypes.FIELD_LONG, 1000).findFirst();
    assertFalse("Should not be able to find item in another result list.", items.contains(anotherType));
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test)

Example 73 with AllJavaTypes

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

the class ManagedRealmCollectionTests method contains_deletedRealmObject.

@Test
public void contains_deletedRealmObject() {
    AllJavaTypes obj = collection.iterator().next();
    realm.beginTransaction();
    obj.deleteFromRealm();
    realm.commitTransaction();
    assertFalse(collection.contains(obj));
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test)

Example 74 with AllJavaTypes

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

the class ManagedRealmCollectionTests method where_contains.

@Test
public void where_contains() {
    RealmQuery<AllJavaTypes> query = realm.where(AllJavaTypes.class).findAll().where();
    AllJavaTypes item = query.findFirst();
    assertTrue("Item should exist in results.", query.findAll().contains(item));
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test)

Example 75 with AllJavaTypes

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

the class CollectionTests method populateRealm.

protected void populateRealm(Realm realm, int objects) {
    realm.beginTransaction();
    realm.delete(AllJavaTypes.class);
    realm.delete(NonLatinFieldNames.class);
    if (objects > 0) {
        for (int i = 0; i < objects; i++) {
            AllJavaTypes obj = realm.createObject(AllJavaTypes.class, i);
            fillObject(i, objects, obj);
            NonLatinFieldNames nonLatinFieldNames = realm.createObject(NonLatinFieldNames.class);
            nonLatinFieldNames.set델타(i);
            nonLatinFieldNames.setΔέλτα(i);
            // Sets the linked object to itself.
            obj.setFieldObject(obj);
        }
        // Adds all items to the RealmList on the first object.
        AllJavaTypes firstObj = realm.where(AllJavaTypes.class).equalTo(AllJavaTypes.FIELD_ID, 0).findFirst();
        RealmResults<AllJavaTypes> listData = realm.where(AllJavaTypes.class).findAllSorted(AllJavaTypes.FIELD_ID, Sort.ASCENDING);
        RealmList<AllJavaTypes> list = firstObj.getFieldList();
        for (int i = 0; i < listData.size(); i++) {
            list.add(listData.get(i));
        }
    }
    realm.commitTransaction();
}
Also used : NonLatinFieldNames(io.realm.entities.NonLatinFieldNames) AllJavaTypes(io.realm.entities.AllJavaTypes)

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