use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class LinkingObjectsManagedTests method basic_singleBacklinkObject.
// Setting the linked object field creates the correct backlink
@Test
public void basic_singleBacklinkObject() {
realm.beginTransaction();
AllJavaTypes child = realm.createObject(AllJavaTypes.class, 1);
AllJavaTypes parent = realm.createObject(AllJavaTypes.class, 2);
parent.setFieldObject(child);
realm.commitTransaction();
assertEquals(1, child.getObjectParents().size());
assertTrue(child.getObjectParents().contains(parent));
}
use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class LinkingObjectsManagedTests method json_updateList.
// Fields annotated with @LinkingObjects should not be affected by JSON updates
@Test
public void json_updateList() {
realm.beginTransaction();
AllJavaTypes child = realm.createObject(AllJavaTypes.class, 1);
AllJavaTypes parent = realm.createObject(AllJavaTypes.class, 2);
parent.getFieldList().add(child);
realm.commitTransaction();
RealmResults<AllJavaTypes> parents = child.getListParents();
assertNotNull(parents);
assertEquals(1, parents.size());
assertTrue(parents.contains(parent));
realm.beginTransaction();
try {
realm.createOrUpdateAllFromJson(AllJavaTypes.class, "[{ \"fieldId\" : 1, \"listParents\" : null }]");
} catch (RealmException e) {
fail("Failed loading JSON" + e);
}
realm.commitTransaction();
parents = child.getListParents();
assertNotNull(parents);
assertEquals(1, parents.size());
assertTrue(parents.contains(parent));
}
use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class LinkingObjectsManagedTests method notification_onDeleteRealmResults.
// A listener registered on the backlinked object should be called when a backlinked object is deleted
@Test
@RunTestInLooperThread
public void notification_onDeleteRealmResults() {
final Realm looperThreadRealm = looperThread.realm;
looperThreadRealm.beginTransaction();
AllJavaTypes child = looperThreadRealm.createObject(AllJavaTypes.class, 10);
AllJavaTypes parent = looperThreadRealm.createObject(AllJavaTypes.class, 1);
parent.setFieldObject(child);
looperThreadRealm.commitTransaction();
final AtomicInteger counter = new AtomicInteger(0);
RealmChangeListener<RealmResults<AllJavaTypes>> listener = new RealmChangeListener<RealmResults<AllJavaTypes>>() {
@Override
public void onChange(RealmResults<AllJavaTypes> object) {
counter.incrementAndGet();
}
};
child.getObjectParents().addChangeListener(listener);
looperThreadRealm.beginTransaction();
looperThreadRealm.where(AllJavaTypes.class).equalTo("fieldId", 1).findAll().deleteAllFromRealm();
looperThreadRealm.commitTransaction();
verifyPostConditions(looperThreadRealm, new PostConditions() {
public void run(Realm realm) {
assertEquals(1, looperThreadRealm.where(AllJavaTypes.class).findAll().size());
assertEquals(1, counter.get());
}
}, child, parent);
}
use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class LinkingObjectsManagedTests method notification_onCommitRealmResults.
// A listener registered on the backlinked field should be called when a commit adds a backlink
@Test
@RunTestInLooperThread
public void notification_onCommitRealmResults() {
final Realm looperThreadRealm = looperThread.realm;
looperThreadRealm.beginTransaction();
AllJavaTypes child = looperThreadRealm.createObject(AllJavaTypes.class, 10);
looperThreadRealm.commitTransaction();
final AtomicInteger counter = new AtomicInteger(0);
RealmChangeListener<RealmResults<AllJavaTypes>> listener = new RealmChangeListener<RealmResults<AllJavaTypes>>() {
@Override
public void onChange(RealmResults<AllJavaTypes> object) {
counter.incrementAndGet();
}
};
child.getObjectParents().addChangeListener(listener);
looperThreadRealm.beginTransaction();
AllJavaTypes parent = looperThreadRealm.createObject(AllJavaTypes.class, 1);
parent.setFieldObject(child);
looperThreadRealm.commitTransaction();
verifyPostConditions(looperThreadRealm, new PostConditions() {
public void run(Realm realm) {
assertEquals(2, looperThreadRealm.where(AllJavaTypes.class).findAll().size());
assertEquals(1, counter.get());
}
}, child, parent);
}
use of io.realm.entities.AllJavaTypes in project realm-java by realm.
the class LinkingObjectsManagedTests method basic_multipleReferencesFromParentList.
// Adding multiple list links creates multiple backlinks,
// even if the links are to a single object
@Test
public void basic_multipleReferencesFromParentList() {
realm.beginTransaction();
AllJavaTypes child = realm.createObject(AllJavaTypes.class, 1);
AllJavaTypes parent = realm.createObject(AllJavaTypes.class, 2);
parent.getFieldList().add(child);
parent.getFieldList().add(child);
realm.commitTransaction();
// One entry for each reference, so two references from a LinkList will
// result in two backlinks.
assertEquals(2, child.getListParents().size());
assertEquals(parent, child.getListParents().first());
assertEquals(parent, child.getListParents().last());
}
Aggregations