use of io.realm.entities.OwnerPrimaryKey in project realm-java by realm.
the class RealmJsonTests method createOrUpdateObjectFromJson_objectWithPrimaryKeySetValueDirectlyFromStream.
/**
* Checks that using createOrUpdateObject will set the primary key directly instead of first setting
* it to the default value (which can fail).
*/
@Test
public void createOrUpdateObjectFromJson_objectWithPrimaryKeySetValueDirectlyFromStream() throws JSONException, IOException {
assumeThat(Build.VERSION.SDK_INT, greaterThanOrEqualTo(Build.VERSION_CODES.HONEYCOMB));
InputStream stream = TestHelper.stringToStream("{\"id\": 1, \"name\": \"bar\"}");
realm.beginTransaction();
// id = 0
realm.createObject(OwnerPrimaryKey.class, 0);
realm.createOrUpdateObjectFromJson(OwnerPrimaryKey.class, stream);
realm.commitTransaction();
RealmResults<OwnerPrimaryKey> owners = realm.where(OwnerPrimaryKey.class).findAll();
assertEquals(2, owners.size());
assertEquals(1, owners.get(1).getId());
assertEquals("bar", owners.get(1).getName());
}
use of io.realm.entities.OwnerPrimaryKey in project realm-java by realm.
the class RealmTests method copyToRealmOrUpdate_primaryKeyMixInObjectGraph.
@Test
public void copyToRealmOrUpdate_primaryKeyMixInObjectGraph() {
// Crate Object graph where tier 2 consists of 1 object with primary key and one doesn't.
// Tier 3 both have objects with primary keys.
//
// PK
// / \
// PK nonPK
// | |
// PK PK
DogPrimaryKey dog = new DogPrimaryKey(1, "Dog");
OwnerPrimaryKey owner = new OwnerPrimaryKey(1, "Owner");
owner.setDog(dog);
Cat cat = new Cat();
cat.setScaredOfDog(dog);
PrimaryKeyMix mixObject = new PrimaryKeyMix(1);
mixObject.setDogOwner(owner);
mixObject.setCat(cat);
realm.beginTransaction();
PrimaryKeyMix realmObject = realm.copyToRealmOrUpdate(mixObject);
realm.commitTransaction();
assertEquals("Dog", realmObject.getCat().getScaredOfDog().getName());
assertEquals("Dog", realmObject.getDogOwner().getDog().getName());
}
use of io.realm.entities.OwnerPrimaryKey in project realm-java by realm.
the class RealmTests method copyToRealm_primaryKeyIsSetDirectly.
// Check that using copyToRealm will set the primary key directly instead of first setting
// it to the default value (which can fail).
@Test
public void copyToRealm_primaryKeyIsSetDirectly() {
realm.beginTransaction();
realm.createObject(OwnerPrimaryKey.class, 0);
realm.copyToRealm(new OwnerPrimaryKey(1, "Foo"));
realm.commitTransaction();
assertEquals(2, realm.where(OwnerPrimaryKey.class).count());
}
use of io.realm.entities.OwnerPrimaryKey in project realm-java by realm.
the class RealmTests method setter_updateField.
// TODO Does this test something meaningfull not tested elsewhere?
@Test
public void setter_updateField() throws Exception {
realm.beginTransaction();
// Creates an owner with two dogs.
OwnerPrimaryKey owner = realm.createObject(OwnerPrimaryKey.class, 1);
owner.setName("Jack");
Dog rex = realm.createObject(Dog.class);
rex.setName("Rex");
Dog fido = realm.createObject(Dog.class);
fido.setName("Fido");
owner.getDogs().add(rex);
owner.getDogs().add(fido);
assertEquals(2, owner.getDogs().size());
// Changing the name of the owner should not affect the number of dogs.
owner.setName("Peter");
assertEquals(2, owner.getDogs().size());
// Updating the user should not affect it either. This is actually a no-op since owner is a Realm backed object.
OwnerPrimaryKey owner2 = realm.copyToRealmOrUpdate(owner);
assertEquals(2, owner.getDogs().size());
assertEquals(2, owner2.getDogs().size());
realm.commitTransaction();
}
use of io.realm.entities.OwnerPrimaryKey in project realm-java by realm.
the class RealmTests method copyToRealmOrUpdate_objectInOtherThreadThrows.
@Test
public void copyToRealmOrUpdate_objectInOtherThreadThrows() {
final CountDownLatch bgThreadDoneLatch = new CountDownLatch(1);
realm.beginTransaction();
final OwnerPrimaryKey ownerPrimaryKey = realm.createObject(OwnerPrimaryKey.class, 0);
realm.commitTransaction();
new Thread(new Runnable() {
@Override
public void run() {
final Realm bgRealm = Realm.getInstance(realm.getConfiguration());
bgRealm.beginTransaction();
try {
bgRealm.copyToRealm(ownerPrimaryKey);
fail();
} catch (IllegalArgumentException expected) {
assertEquals("Objects which belong to Realm instances in other threads cannot be copied into this" + " Realm instance.", expected.getMessage());
}
bgRealm.cancelTransaction();
bgRealm.close();
bgThreadDoneLatch.countDown();
}
}).start();
TestHelper.awaitOrFail(bgThreadDoneLatch);
}
Aggregations