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());
}
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());
}
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));
}
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);
}
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();
}
}
Aggregations