Search in sources :

Example 1 with AllJavaTypes

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

the class ManagedOrderedRealmCollectionTests method sort_twoFields.

@Test
public void sort_twoFields() {
    if (isSnapshot(collectionClass)) {
        thrown.expect(UnsupportedOperationException.class);
    }
    OrderedRealmCollection<AllJavaTypes> sortedList = collection.sort(AllJavaTypes.FIELD_BOOLEAN, Sort.ASCENDING, AllJavaTypes.FIELD_LONG, Sort.DESCENDING);
    AllJavaTypes obj = sortedList.first();
    assertFalse(obj.isFieldBoolean());
    assertEquals(TEST_SIZE - 1, obj.getFieldLong());
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test)

Example 2 with AllJavaTypes

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

the class ManagedRealmCollectionTests method where_shouldNotContainRemovedItem.

@Test
public void where_shouldNotContainRemovedItem() {
    RealmQuery<AllJavaTypes> query = realm.where(AllJavaTypes.class).findAll().where();
    AllJavaTypes item = realm.where(AllJavaTypes.class).findFirst();
    realm.beginTransaction();
    item.deleteFromRealm();
    realm.commitTransaction();
    assertFalse("Should not contain a removed item.", query.findAll().contains(item));
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test)

Example 3 with AllJavaTypes

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

the class ManagedRealmCollectionTests method where_queryDateField.

@Test
public void where_queryDateField() {
    RealmQuery<AllJavaTypes> query = realm.where(AllJavaTypes.class).equalTo(AllJavaTypes.FIELD_DATE, new Date(YEAR_MILLIS * 20));
    RealmResults<AllJavaTypes> all = query.findAll();
    assertEquals(1, query.count());
    assertEquals(1, all.size());
    // before 1901
    query = realm.where(AllJavaTypes.class).equalTo(AllJavaTypes.FIELD_DATE, new Date(YEAR_MILLIS * -100));
    all = query.findAll();
    assertEquals(1, query.count());
    assertEquals(1, all.size());
    // after 2038
    query = realm.where(AllJavaTypes.class).equalTo(AllJavaTypes.FIELD_DATE, new Date(YEAR_MILLIS * 80));
    all = query.findAll();
    assertEquals(1, query.count());
    assertEquals(1, all.size());
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Date(java.util.Date) Test(org.junit.Test)

Example 4 with AllJavaTypes

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

the class ManagedRealmCollectionTests method methodsThrowOnWrongThread.

@Test
public void methodsThrowOnWrongThread() throws ExecutionException, InterruptedException {
    realm.beginTransaction();
    AllJavaTypes allJavaTypes = realm.createObject(AllJavaTypes.class, 42);
    realm.commitTransaction();
    for (RealmCollectionMethod method : RealmCollectionMethod.values()) {
        assertTrue(method + " failed", runMethodOnWrongThread(method));
    }
    for (CollectionMethod method : CollectionMethod.values()) {
        assertTrue(method + " failed", runMethodOnWrongThread(method, allJavaTypes));
    }
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test)

Example 5 with AllJavaTypes

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

the class ManagedRealmCollectionTests method runMethodOnWrongThread.

private boolean runMethodOnWrongThread(final CollectionMethod method, final AllJavaTypes tempObject) 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_OBJECT:
                    case ADD_ALL_OBJECTS:
                    case CLEAR:
                    case REMOVE_OBJECT:
                    case REMOVE_ALL:
                    case RETAIN_ALL:
                        expected = UnsupportedOperationException.class;
                }
            }
            try {
                switch(method) {
                    case ADD_OBJECT:
                        collection.add(new AllJavaTypes());
                        break;
                    case ADD_ALL_OBJECTS:
                        collection.addAll(Collections.singletonList(new AllJavaTypes()));
                        break;
                    case CLEAR:
                        collection.clear();
                    case CONTAINS:
                    case CONTAINS_ALL:
                        collection.containsAll(Collections.singletonList(tempObject));
                        break;
                    case EQUALS:
                        //noinspection ResultOfMethodCallIgnored
                        collection.equals(createCollection(collectionClass));
                        break;
                    case HASHCODE:
                        //noinspection ResultOfMethodCallIgnored
                        collection.hashCode();
                        break;
                    case IS_EMPTY:
                        collection.isEmpty();
                        break;
                    // Creating an iterator should be safe. Accessing it will fail, but tested elsewhere.
                    case ITERATOR:
                        return true;
                    case REMOVE_OBJECT:
                        collection.remove(new AllJavaTypes());
                        break;
                    case REMOVE_ALL:
                        collection.removeAll(Collections.singletonList(new AllJavaTypes()));
                        break;
                    case RETAIN_ALL:
                        collection.retainAll(Collections.singletonList(new AllJavaTypes()));
                        break;
                    case SIZE:
                        collection.size();
                        break;
                    case TO_ARRAY:
                        collection.toArray();
                        break;
                    case TO_ARRAY_INPUT:
                        collection.toArray(new Object[collection.size()]);
                        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) ExpectedException(org.junit.rules.ExpectedException) ExecutionException(java.util.concurrent.ExecutionException)

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