Search in sources :

Example 16 with CyclicType

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

the class RealmTests method copyToRealm_cyclicListReferences.

@Test
public void copyToRealm_cyclicListReferences() {
    CyclicType oneCyclicType = new CyclicType();
    oneCyclicType.setName("One");
    CyclicType anotherCyclicType = new CyclicType();
    anotherCyclicType.setName("Two");
    oneCyclicType.setObjects(new RealmList<>(anotherCyclicType));
    anotherCyclicType.setObjects(new RealmList<>(oneCyclicType));
    realm.beginTransaction();
    CyclicType realmObject = realm.copyToRealm(oneCyclicType);
    realm.commitTransaction();
    assertEquals("One", realmObject.getName());
    assertEquals(2, realm.where(CyclicType.class).count());
}
Also used : CyclicType(io.realm.entities.CyclicType) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 17 with CyclicType

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

the class RealmTests method copyToRealm_cyclicObjectReferences.

@Test
public void copyToRealm_cyclicObjectReferences() {
    CyclicType oneCyclicType = new CyclicType();
    oneCyclicType.setName("One");
    CyclicType anotherCyclicType = new CyclicType();
    anotherCyclicType.setName("Two");
    oneCyclicType.setObject(anotherCyclicType);
    anotherCyclicType.setObject(oneCyclicType);
    realm.beginTransaction();
    CyclicType realmObject = realm.copyToRealm(oneCyclicType);
    realm.commitTransaction();
    assertEquals("One", realmObject.getName());
    assertEquals("Two", realmObject.getObject().getName());
    assertEquals(2, realm.where(CyclicType.class).count());
    // Tests copyToRealm overload that uses the Iterator.
    // Makes sure we reuse the same graph cache Map to avoid duplicates.
    realm.beginTransaction();
    realm.deleteAll();
    realm.commitTransaction();
    assertEquals(0, realm.where(CyclicType.class).count());
    realm.beginTransaction();
    List<CyclicType> cyclicTypes = realm.copyToRealm(Arrays.asList(oneCyclicType, anotherCyclicType));
    realm.commitTransaction();
    assertEquals(2, cyclicTypes.size());
    assertEquals("One", cyclicTypes.get(0).getName());
    assertEquals("Two", cyclicTypes.get(1).getName());
    assertEquals(2, realm.where(CyclicType.class).count());
}
Also used : CyclicType(io.realm.entities.CyclicType) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 18 with CyclicType

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

the class RealmTests method copyFromRealm_list_sameElements.

// Tests that the same Realm objects in a list result in the same Java in-memory copy.
// List: A -> [(B -> C), (B -> C)] should result in only 2 copied objects A and B and not A1, B1, A2, B2
@Test
public void copyFromRealm_list_sameElements() {
    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");
    objB.setObject(objC);
    objA.getObjects().add(objB);
    objA.getObjects().add(objB);
    realm.commitTransaction();
    List<CyclicType> results = realm.copyFromRealm(objA.getObjects());
    assertEquals(2, results.size());
    assertEquals("B", results.get(0).getName());
    assertTrue(results.get(0) == results.get(1));
}
Also used : CyclicType(io.realm.entities.CyclicType) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 19 with CyclicType

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

the class RealmResultsTests method asJSON_cycles.

@Test
public void asJSON_cycles() throws JSONException {
    Date date = Calendar.getInstance().getTime();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    // Core return dates in UTC time
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    String now = sdf.format(date);
    CyclicType oneCyclicType = new CyclicType();
    oneCyclicType.setName("One");
    oneCyclicType.setDate(date);
    CyclicType anotherCyclicType = new CyclicType();
    anotherCyclicType.setName("Two");
    anotherCyclicType.setDate(date);
    oneCyclicType.setObject(anotherCyclicType);
    anotherCyclicType.setObject(oneCyclicType);
    realm.beginTransaction();
    realm.insert(Arrays.asList(oneCyclicType, anotherCyclicType));
    realm.commitTransaction();
    RealmResults<CyclicType> realmObjects = realm.where(CyclicType.class).sort(CyclicType.FIELD_NAME).findAll();
    assertEquals(2, realmObjects.size());
    String json = realmObjects.asJSON();
    String expectedJSON = "[\n" + "    {\n" + "        \"_key\": 0,\n" + "        \"id\": 0,\n" + "        \"name\": \"One\",\n" + "        \"date\": \"" + now + "\",\n" + "        \"object\": {\n" + "            \"_key\": 1,\n" + "            \"id\": 0,\n" + "            \"name\": \"Two\",\n" + "            \"date\": \"" + now + "\",\n" + "            \"object\": {\n" + "                \"table\": \"class_CyclicType\",\n" + "                \"key\": 0\n" + "            },\n" + "            \"otherObject\": null,\n" + "            \"objects\": []\n" + "        },\n" + "        \"otherObject\": null,\n" + "        \"objects\": []\n" + "    },\n" + "    {\n" + "        \"_key\": 1,\n" + "        \"id\": 0,\n" + "        \"name\": \"Two\",\n" + "        \"date\": \"" + now + "\",\n" + "        \"object\": {\n" + "            \"_key\": 0,\n" + "            \"id\": 0,\n" + "            \"name\": \"One\",\n" + "            \"date\": \"" + now + "\",\n" + "            \"object\": {\n" + "                \"table\": \"class_CyclicType\",\n" + "                \"key\": 1\n" + "            },\n" + "            \"otherObject\": null,\n" + "            \"objects\": []\n" + "        },\n" + "        \"otherObject\": null,\n" + "        \"objects\": []\n" + "    }\n" + "]";
    JSONAssert.assertEquals(expectedJSON, json, false);
}
Also used : PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) SimpleDateFormat(java.text.SimpleDateFormat) CyclicType(io.realm.entities.CyclicType) Date(java.util.Date) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 20 with CyclicType

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

the class RealmObjectTests method hashCode_cyclicObject.

@Test
public void hashCode_cyclicObject() {
    realm.beginTransaction();
    final CyclicType foo = createCyclicData();
    realm.commitTransaction();
    // Checks that the hash code is always the same between multiple calls.
    assertEquals(foo.hashCode(), foo.hashCode());
    // Checks that the hash code is the same among same object.
    assertEquals(foo.hashCode(), realm.where(CyclicType.class).equalTo("name", foo.getName()).findFirst().hashCode());
    // Hash code is different from other objects.
    assertNotEquals(foo.getObject().hashCode(), foo.hashCode());
    final int originalHashCode = foo.hashCode();
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            foo.setName(foo.getName() + "1234");
        }
    });
    // Checks that Updating the value of its field does not affect the hash code.
    assertEquals(originalHashCode, foo.hashCode());
    // Checks the hash code of the object from a Realm in different file name.
    RealmConfiguration realmConfig_differentName = configFactory.createConfiguration("another_" + realmConfig.getRealmFileName());
    Realm realm_differentName = Realm.getInstance(realmConfig_differentName);
    // noinspection TryFinallyCanBeTryWithResources
    try {
        realm_differentName.beginTransaction();
        CyclicType fooFromDifferentName = createCyclicData(realm_differentName);
        realm_differentName.commitTransaction();
        assertNotEquals(fooFromDifferentName.hashCode(), foo.hashCode());
    } finally {
        realm_differentName.close();
    }
    // Checks the hash code of the object from a Realm in different directory.
    RealmConfiguration realmConfig_differentPath = configFactory.createConfiguration("anotherDir", realmConfig.getRealmFileName());
    Realm realm_differentPath = Realm.getInstance(realmConfig_differentPath);
    // noinspection TryFinallyCanBeTryWithResources
    try {
        realm_differentPath.beginTransaction();
        CyclicType fooFromDifferentPath = createCyclicData(realm_differentPath);
        realm_differentPath.commitTransaction();
        assertNotEquals(fooFromDifferentPath.hashCode(), foo.hashCode());
    } finally {
        realm_differentPath.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