Search in sources :

Example 1 with DogPrimaryKey

use of io.realm.entities.DogPrimaryKey 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)

Example 2 with DogPrimaryKey

use of io.realm.entities.DogPrimaryKey in project realm-java by realm.

the class RealmTests method copyToRealmOrUpdate_defaultValuesOverrideExistingData.

// Checks that an unmanaged object with only default values can override data.
@Test
public void copyToRealmOrUpdate_defaultValuesOverrideExistingData() {
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            AllTypesPrimaryKey obj = new AllTypesPrimaryKey();
            obj.setColumnString("Foo");
            obj.setColumnLong(1);
            obj.setColumnFloat(1.23F);
            obj.setColumnDouble(1.234D);
            obj.setColumnBoolean(false);
            obj.setColumnBinary(new byte[] { 1, 2, 3 });
            obj.setColumnDate(new Date(1000));
            obj.setColumnRealmObject(new DogPrimaryKey(1, "Dog1"));
            obj.setColumnRealmList(new RealmList<DogPrimaryKey>(new DogPrimaryKey(2, "Dog2")));
            realm.copyToRealm(obj);
            AllTypesPrimaryKey obj2 = new AllTypesPrimaryKey();
            obj2.setColumnLong(1);
            realm.copyToRealmOrUpdate(obj2);
        }
    });
    assertEquals(1, realm.where(AllTypesPrimaryKey.class).count());
    AllTypesPrimaryKey obj = realm.where(AllTypesPrimaryKey.class).findFirst();
    assertNull(obj.getColumnString());
    assertEquals(1, obj.getColumnLong());
    assertEquals(0.0F, obj.getColumnFloat(), 0);
    assertEquals(0.0D, obj.getColumnDouble(), 0);
    assertEquals(false, obj.isColumnBoolean());
    assertNull(obj.getColumnBinary());
    assertNull(obj.getColumnDate());
    assertNull(obj.getColumnRealmObject());
    assertEquals(0, obj.getColumnRealmList().size());
}
Also used : DogPrimaryKey(io.realm.entities.DogPrimaryKey) SharedRealm(io.realm.internal.SharedRealm) Date(java.util.Date) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Example 3 with DogPrimaryKey

use of io.realm.entities.DogPrimaryKey in project realm-java by realm.

the class RealmTests method copyToRealmOrUpdate_referencesNotDeleted.

// Tests that if references to objects are removed, the objects are still in the Realm.
@Test
public void copyToRealmOrUpdate_referencesNotDeleted() {
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            AllTypesPrimaryKey obj = new AllTypesPrimaryKey();
            obj.setColumnLong(1);
            obj.setColumnRealmObject(new DogPrimaryKey(1, "Dog1"));
            obj.setColumnRealmList(new RealmList<DogPrimaryKey>(new DogPrimaryKey(2, "Dog2")));
            realm.copyToRealm(obj);
            AllTypesPrimaryKey obj2 = new AllTypesPrimaryKey();
            obj2.setColumnLong(1);
            obj2.setColumnRealmObject(new DogPrimaryKey(3, "Dog3"));
            obj2.setColumnRealmList(new RealmList<DogPrimaryKey>(new DogPrimaryKey(4, "Dog4")));
            realm.copyToRealmOrUpdate(obj2);
        }
    });
    assertEquals(1, realm.where(AllTypesPrimaryKey.class).count());
    assertEquals(4, realm.where(DogPrimaryKey.class).count());
}
Also used : DogPrimaryKey(io.realm.entities.DogPrimaryKey) SharedRealm(io.realm.internal.SharedRealm) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Example 4 with DogPrimaryKey

use of io.realm.entities.DogPrimaryKey in project realm-java by realm.

the class BulkInsertTests method insertOrUpdate.

@Test
public void insertOrUpdate() {
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            AllTypesPrimaryKey obj = new AllTypesPrimaryKey();
            obj.setColumnString("Foo");
            obj.setColumnLong(1);
            obj.setColumnFloat(1.23F);
            obj.setColumnDouble(1.234D);
            obj.setColumnBoolean(false);
            obj.setColumnBinary(new byte[] { 1, 2, 3 });
            obj.setColumnDate(new Date(1000));
            obj.setColumnRealmObject(new DogPrimaryKey(1, "Dog1"));
            obj.setColumnRealmList(new RealmList<DogPrimaryKey>(new DogPrimaryKey(2, "Dog2")));
            obj.setColumnBoxedBoolean(true);
            realm.insert(obj);
            AllTypesPrimaryKey obj2 = new AllTypesPrimaryKey();
            obj2.setColumnString("Bar");
            obj2.setColumnLong(1);
            obj2.setColumnFloat(2.23F);
            obj2.setColumnDouble(2.234D);
            obj2.setColumnBoolean(true);
            obj2.setColumnBinary(new byte[] { 2, 3, 4 });
            obj2.setColumnDate(new Date(2000));
            obj2.setColumnRealmObject(new DogPrimaryKey(3, "Dog3"));
            obj2.setColumnRealmList(new RealmList<DogPrimaryKey>(new DogPrimaryKey(4, "Dog4")));
            obj2.setColumnBoxedBoolean(false);
            realm.insertOrUpdate(obj2);
        }
    });
    assertEquals(1, realm.where(AllTypesPrimaryKey.class).count());
    AllTypesPrimaryKey obj = realm.where(AllTypesPrimaryKey.class).findFirst();
    // Checks that the only element has all its properties updated.
    assertNotNull(obj);
    assertEquals("Bar", obj.getColumnString());
    assertEquals(1, obj.getColumnLong());
    assertEquals(2.23F, obj.getColumnFloat(), 0);
    assertEquals(2.234D, obj.getColumnDouble(), 0);
    assertEquals(true, obj.isColumnBoolean());
    assertArrayEquals(new byte[] { 2, 3, 4 }, obj.getColumnBinary());
    assertEquals(new Date(2000), obj.getColumnDate());
    assertEquals("Dog3", obj.getColumnRealmObject().getName());
    assertEquals(1, obj.getColumnRealmList().size());
    assertEquals("Dog4", obj.getColumnRealmList().get(0).getName());
    assertEquals(4, realm.where(DogPrimaryKey.class).findAll().size());
    assertFalse(obj.getColumnBoxedBoolean());
}
Also used : DogPrimaryKey(io.realm.entities.DogPrimaryKey) Date(java.util.Date) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Example 5 with DogPrimaryKey

use of io.realm.entities.DogPrimaryKey in project realm-java by realm.

the class RealmTests method copyToRealmOrUpdate_updateExistingObject.

@Test
public void copyToRealmOrUpdate_updateExistingObject() {
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            AllTypesPrimaryKey obj = new AllTypesPrimaryKey();
            obj.setColumnString("Foo");
            obj.setColumnLong(1);
            obj.setColumnFloat(1.23F);
            obj.setColumnDouble(1.234D);
            obj.setColumnBoolean(false);
            obj.setColumnBinary(new byte[] { 1, 2, 3 });
            obj.setColumnDate(new Date(1000));
            obj.setColumnRealmObject(new DogPrimaryKey(1, "Dog1"));
            obj.setColumnRealmList(new RealmList<DogPrimaryKey>(new DogPrimaryKey(2, "Dog2")));
            obj.setColumnBoxedBoolean(true);
            realm.copyToRealm(obj);
            AllTypesPrimaryKey obj2 = new AllTypesPrimaryKey();
            obj2.setColumnString("Bar");
            obj2.setColumnLong(1);
            obj2.setColumnFloat(2.23F);
            obj2.setColumnDouble(2.234D);
            obj2.setColumnBoolean(true);
            obj2.setColumnBinary(new byte[] { 2, 3, 4 });
            obj2.setColumnDate(new Date(2000));
            obj2.setColumnRealmObject(new DogPrimaryKey(3, "Dog3"));
            obj2.setColumnRealmList(new RealmList<DogPrimaryKey>(new DogPrimaryKey(4, "Dog4")));
            obj2.setColumnBoxedBoolean(false);
            realm.copyToRealmOrUpdate(obj2);
        }
    });
    assertEquals(1, realm.where(AllTypesPrimaryKey.class).count());
    AllTypesPrimaryKey obj = realm.where(AllTypesPrimaryKey.class).findFirst();
    // Checks that the the only element has all its properties updated.
    assertEquals("Bar", obj.getColumnString());
    assertEquals(1, obj.getColumnLong());
    assertEquals(2.23F, obj.getColumnFloat(), 0);
    assertEquals(2.234D, obj.getColumnDouble(), 0);
    assertEquals(true, obj.isColumnBoolean());
    assertArrayEquals(new byte[] { 2, 3, 4 }, obj.getColumnBinary());
    assertEquals(new Date(2000), obj.getColumnDate());
    assertEquals("Dog3", obj.getColumnRealmObject().getName());
    assertEquals(1, obj.getColumnRealmList().size());
    assertEquals("Dog4", obj.getColumnRealmList().get(0).getName());
    assertFalse(obj.getColumnBoxedBoolean());
}
Also used : DogPrimaryKey(io.realm.entities.DogPrimaryKey) SharedRealm(io.realm.internal.SharedRealm) Date(java.util.Date) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Aggregations

DogPrimaryKey (io.realm.entities.DogPrimaryKey)6 Test (org.junit.Test)6 AllTypesPrimaryKey (io.realm.entities.AllTypesPrimaryKey)5 SharedRealm (io.realm.internal.SharedRealm)3 Date (java.util.Date)3 Cat (io.realm.entities.Cat)1 OwnerPrimaryKey (io.realm.entities.OwnerPrimaryKey)1 PrimaryKeyMix (io.realm.entities.PrimaryKeyMix)1