use of io.realm.entities.OwnerPrimaryKey in project realm-java by realm.
the class RealmTests method copyToRealmOrUpdate_listHasObjectInOtherThreadThrows.
@Test
public void copyToRealmOrUpdate_listHasObjectInOtherThreadThrows() {
final CountDownLatch bgThreadDoneLatch = new CountDownLatch(1);
final OwnerPrimaryKey ownerPrimaryKey = new OwnerPrimaryKey();
realm.beginTransaction();
Dog dog = realm.createObject(Dog.class);
realm.commitTransaction();
ownerPrimaryKey.setDogs(new RealmList<Dog>(dog));
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);
}
use of io.realm.entities.OwnerPrimaryKey in project realm-java by realm.
the class RealmJsonTests method createObjectFromJson_objectWithPrimaryKeySetValueDirectlyFromStream.
/**
* createObject using primary keys doesn't work if the Check that using createOrUpdateObject
* will set the primary key directly instead of first setting it to the default value (which can fail).
*/
@Test
public void createObjectFromJson_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.createObjectFromJson(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 RealmJsonTests method createOrUpdateObjectFromJson_objectWithPrimaryKeySetValueDirectlyFromJsonObject.
/**
* 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_objectWithPrimaryKeySetValueDirectlyFromJsonObject() throws JSONException {
JSONObject newObject = new JSONObject("{\"id\": 1, \"name\": \"bar\"}");
realm.beginTransaction();
// id = 0
realm.createObject(OwnerPrimaryKey.class, 0);
realm.createOrUpdateObjectFromJson(OwnerPrimaryKey.class, newObject);
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 RealmJsonTests method createObjectFromJson_objectWithPrimaryKeySetValueDirectlyFromJsonObject.
/**
* 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 createObjectFromJson_objectWithPrimaryKeySetValueDirectlyFromJsonObject() throws JSONException {
JSONObject newObject = new JSONObject("{\"id\": 1, \"name\": \"bar\"}");
realm.beginTransaction();
// id = 0
realm.createObject(OwnerPrimaryKey.class, 0);
realm.createObjectFromJson(OwnerPrimaryKey.class, newObject);
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());
}
Aggregations