Search in sources :

Example 86 with AllTypes

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

the class RealmTests method refresh_triggerNotifications.

@Test
public void refresh_triggerNotifications() {
    looperThread.runBlocking(() -> {
        final CountDownLatch bgThreadDone = new CountDownLatch(1);
        final AtomicBoolean listenerCalled = new AtomicBoolean(false);
        final Realm realm = Realm.getInstance((realmConfig));
        looperThread.closeAfterTest(realm);
        RealmResults<AllTypes> results = realm.where(AllTypes.class).findAll();
        assertEquals(0, results.size());
        results.addChangeListener(new RealmChangeListener<RealmResults<AllTypes>>() {

            @Override
            public void onChange(RealmResults<AllTypes> results) {
                assertEquals(1, results.size());
                listenerCalled.set(true);
            }
        });
        // Advance the Realm on a background while blocking this thread. When we refresh, it should trigger
        // the listener.
        new Thread(() -> {
            Realm realm1 = Realm.getInstance(realmConfig);
            realm1.beginTransaction();
            realm1.createObject(AllTypes.class);
            realm1.commitTransaction();
            realm1.close();
            bgThreadDone.countDown();
        }).start();
        TestHelper.awaitOrFail(bgThreadDone);
        realm.refresh();
        assertTrue(listenerCalled.get());
        looperThread.testComplete();
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AllTypes(io.realm.entities.AllTypes) CountDownLatch(java.util.concurrent.CountDownLatch) OsSharedRealm(io.realm.internal.OsSharedRealm) RealmThread(io.realm.util.RealmThread) BlockingLooperThread(io.realm.rule.BlockingLooperThread) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 87 with AllTypes

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

the class RealmTests method commitTransaction.

@Test
public void commitTransaction() {
    populateTestRealm();
    realm.beginTransaction();
    AllTypes allTypes = realm.createObject(AllTypes.class);
    allTypes.setColumnBoolean(true);
    realm.commitTransaction();
    RealmResults<AllTypes> resultList = realm.where(AllTypes.class).findAll();
    assertEquals(TEST_DATA_SIZE + 1, resultList.size());
}
Also used : AllTypes(io.realm.entities.AllTypes) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 88 with AllTypes

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

the class RealmTests method refresh_nonLooperThreadAdvances.

@Test
public void refresh_nonLooperThreadAdvances() {
    final CountDownLatch bgThreadDone = new CountDownLatch(1);
    RealmResults<AllTypes> results = realm.where(AllTypes.class).findAll();
    assertEquals(0, results.size());
    new Thread(new Runnable() {

        @Override
        public void run() {
            Realm realm = Realm.getInstance(RealmTests.this.realm.getConfiguration());
            realm.beginTransaction();
            realm.createObject(AllTypes.class);
            realm.commitTransaction();
            realm.close();
            bgThreadDone.countDown();
        }
    }).start();
    TestHelper.awaitOrFail(bgThreadDone);
    realm.refresh();
    assertEquals(1, results.size());
}
Also used : AllTypes(io.realm.entities.AllTypes) CountDownLatch(java.util.concurrent.CountDownLatch) OsSharedRealm(io.realm.internal.OsSharedRealm) RealmThread(io.realm.util.RealmThread) BlockingLooperThread(io.realm.rule.BlockingLooperThread) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 89 with AllTypes

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

the class RealmTests method copyFromRealm.

@Test
public void copyFromRealm() {
    populateTestRealm();
    AllTypes realmObject = realm.where(AllTypes.class).sort("columnLong").findAll().first();
    AllTypes unmanagedObject = realm.copyFromRealm(realmObject);
    assertArrayEquals(realmObject.getColumnBinary(), unmanagedObject.getColumnBinary());
    assertEquals(realmObject.getColumnString(), unmanagedObject.getColumnString());
    assertEquals(realmObject.getColumnLong(), unmanagedObject.getColumnLong());
    assertEquals(realmObject.getColumnFloat(), unmanagedObject.getColumnFloat(), 0.00000000001);
    assertEquals(realmObject.getColumnDouble(), unmanagedObject.getColumnDouble(), 0.00000000001);
    assertEquals(realmObject.isColumnBoolean(), unmanagedObject.isColumnBoolean());
    assertEquals(realmObject.getColumnDate(), unmanagedObject.getColumnDate());
    assertEquals(realmObject.getColumnObjectId(), unmanagedObject.getColumnObjectId());
    assertEquals(realmObject.getColumnDecimal128(), unmanagedObject.getColumnDecimal128());
    assertEquals(realmObject.getColumnUUID(), unmanagedObject.getColumnUUID());
    assertEquals(realmObject.getColumnRealmAny(), unmanagedObject.getColumnRealmAny());
}
Also used : AllTypes(io.realm.entities.AllTypes) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 90 with AllTypes

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

the class RealmTests method copyToRealmOrUpdate_noPrimaryKeyField.

@Test
public void copyToRealmOrUpdate_noPrimaryKeyField() {
    realm.beginTransaction();
    thrown.expect(IllegalArgumentException.class);
    realm.copyToRealmOrUpdate(new AllTypes());
}
Also used : AllTypes(io.realm.entities.AllTypes) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Aggregations

AllTypes (io.realm.entities.AllTypes)225 Test (org.junit.Test)210 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)70 UiThreadTest (androidx.test.annotation.UiThreadTest)54 Date (java.util.Date)36 DictionaryAllTypes (io.realm.entities.DictionaryAllTypes)32 Dog (io.realm.entities.Dog)27 JSONObject (org.json.JSONObject)26 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 RunInLooperThread (io.realm.rule.RunInLooperThread)20 InputStream (java.io.InputStream)19 Decimal128 (org.bson.types.Decimal128)19 ByteArrayInputStream (java.io.ByteArrayInputStream)17 ObjectId (org.bson.types.ObjectId)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 UiThreadTest (android.support.test.annotation.UiThreadTest)10 AtomicLong (java.util.concurrent.atomic.AtomicLong)10 OsSharedRealm (io.realm.internal.OsSharedRealm)9 BigDecimal (java.math.BigDecimal)9 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)8