Search in sources :

Example 6 with OwnerPrimaryKey

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);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) OwnerPrimaryKey(io.realm.entities.OwnerPrimaryKey) Dog(io.realm.entities.Dog) OsSharedRealm(io.realm.internal.OsSharedRealm) RealmThread(io.realm.util.RealmThread) BlockingLooperThread(io.realm.rule.BlockingLooperThread) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 7 with OwnerPrimaryKey

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OwnerPrimaryKey(io.realm.entities.OwnerPrimaryKey) Test(org.junit.Test)

Example 8 with OwnerPrimaryKey

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());
}
Also used : JSONObject(org.json.JSONObject) OwnerPrimaryKey(io.realm.entities.OwnerPrimaryKey) Test(org.junit.Test)

Example 9 with OwnerPrimaryKey

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());
}
Also used : JSONObject(org.json.JSONObject) OwnerPrimaryKey(io.realm.entities.OwnerPrimaryKey) Test(org.junit.Test)

Aggregations

OwnerPrimaryKey (io.realm.entities.OwnerPrimaryKey)9 Test (org.junit.Test)9 UiThreadTest (androidx.test.annotation.UiThreadTest)5 Dog (io.realm.entities.Dog)2 OsSharedRealm (io.realm.internal.OsSharedRealm)2 BlockingLooperThread (io.realm.rule.BlockingLooperThread)2 RealmThread (io.realm.util.RealmThread)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 JSONObject (org.json.JSONObject)2 Cat (io.realm.entities.Cat)1 DogPrimaryKey (io.realm.entities.DogPrimaryKey)1 PrimaryKeyMix (io.realm.entities.PrimaryKeyMix)1