Search in sources :

Example 6 with AllTypes

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

the class RealmAsyncQueryTests method findFirstAsync_initalEmptyRow.

@Test
@RunTestInLooperThread
public void findFirstAsync_initalEmptyRow() throws Throwable {
    Realm realm = looperThread.realm;
    final AllTypes firstAsync = realm.where(AllTypes.class).findFirstAsync();
    looperThread.keepStrongReference.add(firstAsync);
    firstAsync.addChangeListener(new RealmChangeListener<AllTypes>() {

        @Override
        public void onChange(AllTypes object) {
            assertTrue(firstAsync.load());
            assertTrue(firstAsync.isLoaded());
            assertTrue(firstAsync.isValid());
            assertEquals(0, firstAsync.getColumnLong());
            looperThread.testComplete();
        }
    });
    realm.beginTransaction();
    realm.createObject(AllTypes.class).setColumnLong(0);
    realm.commitTransaction();
    assertTrue(firstAsync.load());
    assertTrue(firstAsync.isLoaded());
}
Also used : AllTypes(io.realm.entities.AllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 7 with AllTypes

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

the class RealmAsyncQueryTests method batchUpdateDifferentTypeOfQueries.

@Test
@RunTestInLooperThread
public void batchUpdateDifferentTypeOfQueries() {
    final Realm realm = looperThread.realm;
    realm.beginTransaction();
    for (int i = 0; i < 5; ) {
        AllTypes allTypes = realm.createObject(AllTypes.class);
        allTypes.setColumnLong(i);
        allTypes.setColumnString("data " + i % 3);
        allTypes = realm.createObject(AllTypes.class);
        allTypes.setColumnLong(i);
        allTypes.setColumnString("data " + (++i % 3));
    }
    final long numberOfBlocks = 25;
    // Must be greater than 1
    final long numberOfObjects = 10;
    realm.commitTransaction();
    populateForDistinct(realm, numberOfBlocks, numberOfObjects, false);
    RealmResults<AllTypes> findAllAsync = realm.where(AllTypes.class).findAllAsync();
    RealmResults<AllTypes> findAllSorted = realm.where(AllTypes.class).findAllSortedAsync("columnString", Sort.ASCENDING);
    RealmResults<AllTypes> findAllSortedMulti = realm.where(AllTypes.class).findAllSortedAsync(new String[] { "columnString", "columnLong" }, new Sort[] { Sort.ASCENDING, Sort.DESCENDING });
    RealmResults<AnnotationIndexTypes> findDistinct = realm.where(AnnotationIndexTypes.class).distinctAsync("indexString");
    looperThread.keepStrongReference.add(findAllAsync);
    looperThread.keepStrongReference.add(findAllSorted);
    looperThread.keepStrongReference.add(findAllSortedMulti);
    looperThread.keepStrongReference.add(findDistinct);
    final CountDownLatch queriesCompleted = new CountDownLatch(4);
    final CountDownLatch bgRealmClosedLatch = new CountDownLatch(1);
    final AtomicInteger batchUpdateCompleted = new AtomicInteger(0);
    final AtomicInteger findAllAsyncInvocation = new AtomicInteger(0);
    final AtomicInteger findAllSortedInvocation = new AtomicInteger(0);
    final AtomicInteger findAllSortedMultiInvocation = new AtomicInteger(0);
    final AtomicInteger findDistinctInvocation = new AtomicInteger(0);
    findAllAsync.addChangeListener(new RealmChangeListener<RealmResults<AllTypes>>() {

        @Override
        public void onChange(RealmResults<AllTypes> object) {
            switch(findAllAsyncInvocation.incrementAndGet()) {
                case 1:
                    {
                        queriesCompleted.countDown();
                        break;
                    }
                case 2:
                    {
                        if (batchUpdateCompleted.incrementAndGet() == 4) {
                            looperThread.testComplete(bgRealmClosedLatch);
                        }
                        break;
                    }
            }
        }
    });
    findAllSorted.addChangeListener(new RealmChangeListener<RealmResults<AllTypes>>() {

        @Override
        public void onChange(RealmResults<AllTypes> object) {
            switch(findAllSortedInvocation.incrementAndGet()) {
                case 1:
                    {
                        queriesCompleted.countDown();
                        break;
                    }
                case 2:
                    {
                        if (batchUpdateCompleted.incrementAndGet() == 4) {
                            looperThread.testComplete(bgRealmClosedLatch);
                        }
                        break;
                    }
            }
        }
    });
    findAllSortedMulti.addChangeListener(new RealmChangeListener<RealmResults<AllTypes>>() {

        @Override
        public void onChange(RealmResults<AllTypes> object) {
            switch(findAllSortedMultiInvocation.incrementAndGet()) {
                case 1:
                    {
                        queriesCompleted.countDown();
                        break;
                    }
                case 2:
                    {
                        if (batchUpdateCompleted.incrementAndGet() == 4) {
                            looperThread.testComplete(bgRealmClosedLatch);
                        }
                        break;
                    }
            }
        }
    });
    findDistinct.addChangeListener(new RealmChangeListener<RealmResults<AnnotationIndexTypes>>() {

        @Override
        public void onChange(RealmResults<AnnotationIndexTypes> object) {
            switch(findDistinctInvocation.incrementAndGet()) {
                case 1:
                    {
                        queriesCompleted.countDown();
                        break;
                    }
                case 2:
                    {
                        if (batchUpdateCompleted.incrementAndGet() == 4) {
                            looperThread.testComplete(bgRealmClosedLatch);
                        }
                        break;
                    }
            }
        }
    });
    // Waits for the queries to complete then sends a commit from
    // another thread to trigger a batch update of the 4 queries.
    new Thread() {

        @Override
        public void run() {
            try {
                queriesCompleted.await();
                Realm bgRealm = Realm.getInstance(realm.getConfiguration());
                bgRealm.beginTransaction();
                bgRealm.createObject(AllTypes.class);
                bgRealm.createObject(AnnotationIndexTypes.class);
                bgRealm.commitTransaction();
                bgRealm.close();
                bgRealmClosedLatch.countDown();
            } catch (InterruptedException e) {
                fail(e.getMessage());
            }
        }
    }.start();
}
Also used : AllTypes(io.realm.entities.AllTypes) CountDownLatch(java.util.concurrent.CountDownLatch) RunInLooperThread(io.realm.rule.RunInLooperThread) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AnnotationIndexTypes(io.realm.entities.AnnotationIndexTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 8 with AllTypes

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

the class NotificationsTest method realmObjectListenerAddedAfterCommit.

// TODO: Fix or delete this test after integration of object notification from Object Store
@Test
@RunTestInLooperThread
@Ignore
public void realmObjectListenerAddedAfterCommit() {
    Realm realm = looperThread.realm;
    realm.beginTransaction();
    AllTypes obj = realm.createObject(AllTypes.class);
    realm.commitTransaction();
    realm.beginTransaction();
    obj.setColumnLong(42);
    realm.commitTransaction();
    obj.addChangeListener(new RealmChangeListener<AllTypes>() {

        @Override
        public void onChange(AllTypes object) {
            looperThread.testComplete();
        }
    });
}
Also used : AllTypes(io.realm.entities.AllTypes) Ignore(org.junit.Ignore) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 9 with AllTypes

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

the class NotificationsTest method listenersNotAllowedOnNonLooperThreads.

@Test
public void listenersNotAllowedOnNonLooperThreads() {
    realm = Realm.getInstance(realmConfig);
    realm.beginTransaction();
    AllTypes obj = realm.createObject(AllTypes.class);
    realm.commitTransaction();
    RealmResults<AllTypes> results = realm.where(AllTypes.class).findAll();
    // Global listener
    try {
        realm.addChangeListener(new RealmChangeListener<Realm>() {

            @Override
            public void onChange(Realm element) {
            }
        });
        fail();
    } catch (IllegalStateException ignored) {
    }
    // RealmResults listener
    try {
        results.addChangeListener(new RealmChangeListener<RealmResults<AllTypes>>() {

            @Override
            public void onChange(RealmResults<AllTypes> element) {
            }
        });
        fail();
    } catch (IllegalStateException ignored) {
    }
    // Object listener
    try {
        obj.addChangeListener(new RealmChangeListener<RealmModel>() {

            @Override
            public void onChange(RealmModel element) {
            }
        });
        fail();
    } catch (IllegalStateException ignored) {
    }
}
Also used : AllTypes(io.realm.entities.AllTypes) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 10 with AllTypes

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

the class RealmJsonTests method createObjectFromJson_emptyChildObjectList.

@Test
public void createObjectFromJson_emptyChildObjectList() throws JSONException {
    JSONObject allTypesObject = new JSONObject();
    JSONArray dogList = new JSONArray();
    allTypesObject.put("columnRealmList", dogList);
    realm.beginTransaction();
    realm.createObjectFromJson(AllTypes.class, allTypesObject);
    realm.commitTransaction();
    AllTypes obj = realm.where(AllTypes.class).findFirst();
    assertEquals(0, obj.getColumnRealmList().size());
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) AllTypes(io.realm.entities.AllTypes) Test(org.junit.Test)

Aggregations

AllTypes (io.realm.entities.AllTypes)134 Test (org.junit.Test)122 UiThreadTest (android.support.test.annotation.UiThreadTest)40 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)33 Date (java.util.Date)27 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 Action1 (rx.functions.Action1)12 JSONObject (org.json.JSONObject)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 Dog (io.realm.entities.Dog)8 InputStream (java.io.InputStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)5 SharedRealm (io.realm.internal.SharedRealm)5 RealmException (io.realm.exceptions.RealmException)4 RunInLooperThread (io.realm.rule.RunInLooperThread)3 IOException (java.io.IOException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ExecutionException (java.util.concurrent.ExecutionException)3 JSONException (org.json.JSONException)3