Search in sources :

Example 71 with AllTypes

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

the class RealmResultsTests method addChangeListener_twice.

@Test
@RunTestInLooperThread
public void addChangeListener_twice() {
    final AtomicInteger listenersTriggered = new AtomicInteger(0);
    final Realm realm = looperThread.getRealm();
    RealmResults<AllTypes> collection = realm.where(AllTypes.class).findAll();
    RealmChangeListener<RealmResults<AllTypes>> listener = new RealmChangeListener<RealmResults<AllTypes>>() {

        @Override
        public void onChange(RealmResults<AllTypes> object) {
            listenersTriggered.incrementAndGet();
        }
    };
    realm.addChangeListener(new RealmChangeListener<Realm>() {

        @Override
        public void onChange(Realm object) {
            listenersTriggered.incrementAndGet();
            looperThread.postRunnable(new Runnable() {

                @Override
                public void run() {
                    if (listenersTriggered.get() == 1) {
                        looperThread.testComplete();
                    } else {
                        fail("Only global listener should be triggered");
                    }
                }
            });
        }
    });
    // Adding it twice will be ignored, so removing it will not cause the listener to be triggered.
    looperThread.keepStrongReference(collection);
    collection.addChangeListener(listener);
    collection.addChangeListener(listener);
    collection.removeChangeListener(listener);
    realm.beginTransaction();
    realm.createObject(AllTypes.class);
    realm.commitTransaction();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 72 with AllTypes

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

the class RealmTests method copyFromRealm_dynamicRealmObjectThrows.

@Test
public void copyFromRealm_dynamicRealmObjectThrows() {
    realm.beginTransaction();
    AllTypes obj = realm.createObject(AllTypes.class);
    realm.commitTransaction();
    DynamicRealmObject dObj = new DynamicRealmObject(obj);
    try {
        realm.copyFromRealm(dObj);
        fail();
    } catch (IllegalArgumentException ignored) {
    }
}
Also used : AllTypes(io.realm.entities.AllTypes) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 73 with AllTypes

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

the class RealmTests method copyFromRealm_invalidDepthThrows.

@Test
public void copyFromRealm_invalidDepthThrows() {
    realm.beginTransaction();
    AllTypes obj = realm.createObject(AllTypes.class);
    realm.commitTransaction();
    thrown.expect(IllegalArgumentException.class);
    realm.copyFromRealm(obj, -1);
}
Also used : AllTypes(io.realm.entities.AllTypes) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 74 with AllTypes

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

the class RealmQueryTests method greaterThan_date.

@Test
public void greaterThan_date() {
    final int TEST_OBJECTS_COUNT = 200;
    populateTestRealm(realm, TEST_OBJECTS_COUNT);
    RealmResults<AllTypes> resultList;
    resultList = realm.where(AllTypes.class).greaterThan(AllTypes.FIELD_DATE, new Date(Long.MIN_VALUE)).findAll();
    assertEquals(TEST_OBJECTS_COUNT, resultList.size());
    resultList = realm.where(AllTypes.class).greaterThan(AllTypes.FIELD_DATE, new Date(DECADE_MILLIS * -80)).findAll();
    assertEquals(179, resultList.size());
    resultList = realm.where(AllTypes.class).greaterThan(AllTypes.FIELD_DATE, new Date(0)).findAll();
    assertEquals(TEST_OBJECTS_COUNT / 2 - 1, resultList.size());
    resultList = realm.where(AllTypes.class).greaterThan(AllTypes.FIELD_DATE, new Date(DECADE_MILLIS * 80)).findAll();
    assertEquals(19, resultList.size());
    resultList = realm.where(AllTypes.class).greaterThan(AllTypes.FIELD_DATE, new Date(Long.MAX_VALUE)).findAll();
    assertEquals(0, resultList.size());
}
Also used : AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) Date(java.util.Date) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 75 with AllTypes

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

the class RealmQueryTests method populateTestRealm.

private void populateTestRealm(Realm testRealm, int dataSize) {
    testRealm.beginTransaction();
    testRealm.deleteAll();
    for (int i = 0; i < dataSize; ++i) {
        AllTypes allTypes = testRealm.createObject(AllTypes.class);
        allTypes.setColumnBoolean((i % 3) == 0);
        allTypes.setColumnBinary(new byte[] { 1, 2, 3 });
        allTypes.setColumnDate(new Date(DECADE_MILLIS * (i - (dataSize / 2))));
        allTypes.setColumnDouble(Math.PI);
        allTypes.setColumnFloat(1.2345f + i);
        allTypes.setColumnString("test data " + i);
        allTypes.setColumnLong(i);
        allTypes.setColumnObjectId(new ObjectId(TestHelper.generateObjectIdHexString(i)));
        allTypes.setColumnDecimal128(new Decimal128(new BigDecimal(i + ".23456789")));
        allTypes.setColumnUUID(UUID.fromString(TestHelper.generateUUIDString(i)));
        allTypes.setColumnRealmAny(RealmAny.valueOf(i));
        NonLatinFieldNames nonLatinFieldNames = testRealm.createObject(NonLatinFieldNames.class);
        nonLatinFieldNames.set델타(i);
        nonLatinFieldNames.setΔέλτα(i);
        nonLatinFieldNames.set베타(1.2345f + i);
        nonLatinFieldNames.setΒήτα(1.2345f + i);
        Dog dog = testRealm.createObject(Dog.class);
        dog.setAge(i);
        dog.setName("test data " + i);
        allTypes.setColumnRealmObject(dog);
    }
    testRealm.commitTransaction();
}
Also used : ObjectId(org.bson.types.ObjectId) NonLatinFieldNames(io.realm.entities.NonLatinFieldNames) AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) Decimal128(org.bson.types.Decimal128) Dog(io.realm.entities.Dog) Date(java.util.Date) BigDecimal(java.math.BigDecimal)

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