Search in sources :

Example 1 with OwnerPrimaryKey

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

Example 2 with OwnerPrimaryKey

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());
}
Also used : DogPrimaryKey(io.realm.entities.DogPrimaryKey) PrimaryKeyMix(io.realm.entities.PrimaryKeyMix) Cat(io.realm.entities.Cat) OwnerPrimaryKey(io.realm.entities.OwnerPrimaryKey) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 3 with OwnerPrimaryKey

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());
}
Also used : OwnerPrimaryKey(io.realm.entities.OwnerPrimaryKey) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 4 with OwnerPrimaryKey

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();
}
Also used : OwnerPrimaryKey(io.realm.entities.OwnerPrimaryKey) Dog(io.realm.entities.Dog) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 5 with OwnerPrimaryKey

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

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