Search in sources :

Example 26 with CyclicType

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

the class RealmObjectTests method setter_list_withObjectFromAnotherThread.

@Test
public void setter_list_withObjectFromAnotherThread() throws InterruptedException {
    final CountDownLatch createLatch = new CountDownLatch(1);
    final CountDownLatch testEndLatch = new CountDownLatch(1);
    final AtomicReference<CyclicType> objFromAnotherThread = new AtomicReference<CyclicType>();
    java.lang.Thread thread = new java.lang.Thread() {

        @Override
        public void run() {
            Realm realm = Realm.getInstance(realmConfig);
            // 1. Creates an object.
            realm.beginTransaction();
            objFromAnotherThread.set(realm.createObject(CyclicType.class));
            realm.commitTransaction();
            createLatch.countDown();
            TestHelper.awaitOrFail(testEndLatch);
            // 3. Close Realm in this thread and finishes.
            realm.close();
        }
    };
    thread.start();
    TestHelper.awaitOrFail(createLatch);
    // 2. Sets created object to target.
    realm.beginTransaction();
    try {
        CyclicType target = realm.createObject(CyclicType.class);
        RealmList<CyclicType> list = new RealmList<CyclicType>();
        list.add(realm.createObject(CyclicType.class));
        // List contains an object from another thread.
        list.add(objFromAnotherThread.get());
        list.add(realm.createObject(CyclicType.class));
        try {
            target.setObjects(list);
            fail();
        } catch (IllegalArgumentException ignored) {
        }
    } finally {
        testEndLatch.countDown();
        realm.cancelTransaction();
    }
    // Waits for finishing the thread.
    thread.join();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicType(io.realm.entities.CyclicType) RunInLooperThread(io.realm.rule.RunInLooperThread) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 27 with CyclicType

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

the class RealmObjectTests method setter_list_withUnmanagedObject.

@Test
public void setter_list_withUnmanagedObject() {
    CyclicType unmanaged = new CyclicType();
    realm.beginTransaction();
    try {
        CyclicType target = realm.createObject(CyclicType.class);
        RealmList<CyclicType> list = new RealmList<CyclicType>();
        list.add(realm.createObject(CyclicType.class));
        // List contains an unmanaged object
        list.add(unmanaged);
        list.add(realm.createObject(CyclicType.class));
        try {
            target.setObjects(list);
            fail();
        } catch (IllegalArgumentException ignored) {
        }
    } finally {
        realm.cancelTransaction();
    }
}
Also used : CyclicType(io.realm.entities.CyclicType) Test(org.junit.Test)

Example 28 with CyclicType

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

the class RealmObjectTests method equals_sameObjectDifferentInstance.

@Test
public void equals_sameObjectDifferentInstance() {
    realm.beginTransaction();
    CyclicType ct = realm.createObject(CyclicType.class);
    ct.setName("Foo");
    realm.commitTransaction();
    CyclicType ct1 = realm.where(CyclicType.class).findFirst();
    CyclicType ct2 = realm.where(CyclicType.class).findFirst();
    assertTrue(ct1.equals(ct2));
    assertTrue(ct2.equals(ct1));
}
Also used : CyclicType(io.realm.entities.CyclicType) Test(org.junit.Test)

Example 29 with CyclicType

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

the class RealmObjectTests method equals_cyclicObject.

@Test
public void equals_cyclicObject() {
    realm.beginTransaction();
    CyclicType foo = createCyclicData();
    realm.commitTransaction();
    assertEquals(foo, realm.where(CyclicType.class).equalTo("name", "Foo").findFirst());
}
Also used : CyclicType(io.realm.entities.CyclicType) Test(org.junit.Test)

Example 30 with CyclicType

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

the class RealmObjectTests method setter_link_objectFromOtherRealm.

@Test
public void setter_link_objectFromOtherRealm() {
    RealmConfiguration config = configFactory.createConfiguration("another.realm");
    Realm anotherRealm = Realm.getInstance(config);
    // noinspection TryFinallyCanBeTryWithResources
    try {
        anotherRealm.beginTransaction();
        CyclicType objFromAnotherRealm = anotherRealm.createObject(CyclicType.class);
        anotherRealm.commitTransaction();
        realm.beginTransaction();
        try {
            CyclicType target = realm.createObject(CyclicType.class);
            try {
                target.setObject(objFromAnotherRealm);
                fail();
            } catch (IllegalArgumentException ignored) {
            }
        } finally {
            realm.cancelTransaction();
        }
    } finally {
        anotherRealm.close();
    }
}
Also used : CyclicType(io.realm.entities.CyclicType) Test(org.junit.Test)

Aggregations

CyclicType (io.realm.entities.CyclicType)34 Test (org.junit.Test)32 UiThreadTest (androidx.test.annotation.UiThreadTest)7 AllJavaTypes (io.realm.entities.AllJavaTypes)3 RunInLooperThread (io.realm.rule.RunInLooperThread)3 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 SystemClock (android.os.SystemClock)1 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)1 Flowable (io.reactivex.Flowable)1 Disposable (io.reactivex.disposables.Disposable)1 Consumer (io.reactivex.functions.Consumer)1 Schedulers (io.reactivex.schedulers.Schedulers)1 AllTypes (io.realm.entities.AllTypes)1 Dog (io.realm.entities.Dog)1 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)1 Pair (io.realm.internal.util.Pair)1 RealmLog (io.realm.log.RealmLog)1 SimpleDateFormat (java.text.SimpleDateFormat)1