Search in sources :

Example 6 with CyclicType

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

the class RealmTests method copyFromRealm_sameObjectDifferentDepths.

// Tests that depth restriction is calculated from the top-most encountered object, i.e. it is possible for some
// objects to exceed the depth limit.
// A -> B -> C -> D -> E
// A -> D -> E
// D is both at depth 1 and 3. For maxDepth = 3, E should still be copied.
@Test
public void copyFromRealm_sameObjectDifferentDepths() {
    realm.beginTransaction();
    CyclicType objA = realm.createObject(CyclicType.class);
    objA.setName("A");
    CyclicType objB = realm.createObject(CyclicType.class);
    objB.setName("B");
    CyclicType objC = realm.createObject(CyclicType.class);
    objC.setName("C");
    CyclicType objD = realm.createObject(CyclicType.class);
    objD.setName("D");
    CyclicType objE = realm.createObject(CyclicType.class);
    objE.setName("E");
    objA.setObject(objB);
    objB.setObject(objC);
    objC.setObject(objD);
    objD.setObject(objE);
    objA.setOtherObject(objD);
    realm.commitTransaction();
    // Object is filled before otherObject. (because of field order - WARNING: Not guaranteed)
    // This means that the object will be encountered first time at max depth, so E will not be copied.
    // If the object cache does not handle this, otherObject will be wrong.
    CyclicType copyA = realm.copyFromRealm(objA, 3);
    assertEquals("E", copyA.getOtherObject().getObject().getName());
}
Also used : CyclicType(io.realm.entities.CyclicType) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 7 with CyclicType

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

the class RealmTests method copyFromRealm_cyclicObjectGraph.

// Tests that the object graph is copied as it is and no extra copies are made.
// 1) (A -> B/[B,C])
// 2) (C -> B/[B,A])
// A copy should result in only 3 distinct objects.
@Test
public void copyFromRealm_cyclicObjectGraph() {
    realm.beginTransaction();
    CyclicType objA = realm.createObject(CyclicType.class);
    objA.setName("A");
    CyclicType objB = realm.createObject(CyclicType.class);
    objB.setName("B");
    CyclicType objC = realm.createObject(CyclicType.class);
    objC.setName("C");
    objA.setObject(objB);
    objC.setObject(objB);
    objA.getObjects().add(objB);
    objA.getObjects().add(objC);
    objC.getObjects().add(objB);
    objC.getObjects().add(objA);
    realm.commitTransaction();
    CyclicType copyA = realm.copyFromRealm(objA);
    CyclicType copyB = copyA.getObject();
    CyclicType copyC = copyA.getObjects().get(1);
    assertEquals("A", copyA.getName());
    assertEquals("B", copyB.getName());
    assertEquals("C", copyC.getName());
    // Asserts object equality on the object graph.
    assertTrue(copyA.getObject() == copyC.getObject());
    assertTrue(copyA.getObjects().get(0) == copyC.getObjects().get(0));
    assertTrue(copyA == copyC.getObjects().get(1));
    assertTrue(copyC == copyA.getObjects().get(1));
}
Also used : CyclicType(io.realm.entities.CyclicType) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 8 with CyclicType

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

the class RxJavaTests method wrongGenericClassThrows.

@Test
@RunTestInLooperThread
public void wrongGenericClassThrows() {
    realm.beginTransaction();
    final AllTypes obj = realm.createObject(AllTypes.class);
    realm.commitTransaction();
    Flowable<CyclicType> obs = obj.asFlowable();
    subscription = obs.subscribe(cyclicType -> fail(), ignoredError -> {
        disposeSuccessfulTest(realm);
    });
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) RunWith(org.junit.runner.RunWith) Dog(io.realm.entities.Dog) SystemClock(android.os.SystemClock) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) Flowable(io.reactivex.Flowable) RunInLooperThread(io.realm.rule.RunInLooperThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Schedulers(io.reactivex.schedulers.Schedulers) Pair(io.realm.internal.util.Pair) Assert.fail(org.junit.Assert.fail) Before(org.junit.Before) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) RealmLog(io.realm.log.RealmLog) Assert.assertNotNull(org.junit.Assert.assertNotNull) AllTypes(io.realm.entities.AllTypes) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Consumer(io.reactivex.functions.Consumer) CyclicType(io.realm.entities.CyclicType) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) Disposable(io.reactivex.disposables.Disposable) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) AllTypes(io.realm.entities.AllTypes) CyclicType(io.realm.entities.CyclicType) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 9 with CyclicType

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

the class RealmListTests method set_unmanagedObjectToManagedList.

// Tests that set correctly uses Realm.copyToRealm() on unmanaged objects.
@Test
public void set_unmanagedObjectToManagedList() {
    realm.beginTransaction();
    CyclicType parent = realm.copyToRealm(new CyclicType("Parent"));
    RealmList<CyclicType> children = parent.getObjects();
    children.add(new CyclicType());
    children.add(new CyclicType("original"));
    children.add(new CyclicType());
    children.set(1, new CyclicType("updated"));
    realm.commitTransaction();
    RealmList<CyclicType> list = realm.where(CyclicType.class).findFirst().getObjects();
    assertEquals(3, list.size());
    assertEquals("updated", list.get(1).getName());
    assertEquals(5, realm.where(CyclicType.class).count());
}
Also used : CyclicType(io.realm.entities.CyclicType) Test(org.junit.Test)

Example 10 with CyclicType

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

the class RealmObjectTests method setter_list_withClosedObject.

@Test
public void setter_list_withClosedObject() {
    realm.beginTransaction();
    CyclicType closed = realm.createObject(CyclicType.class);
    realm.commitTransaction();
    realm.close();
    assertTrue(realm.isClosed());
    realm = Realm.getInstance(realmConfig);
    realm.beginTransaction();
    try {
        CyclicType target = realm.createObject(CyclicType.class);
        RealmList<CyclicType> list = new RealmList<CyclicType>();
        list.add(realm.createObject(CyclicType.class));
        // List contains a closed object.
        list.add(closed);
        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)

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