Search in sources :

Example 1 with Cat

use of io.realm.entities.Cat 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 2 with Cat

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

the class RealmLinkTests method populate.

private void populate() {
    testRealm.beginTransaction();
    testRealm.delete(Dog.class);
    testRealm.delete(Cat.class);
    testRealm.delete(Owner.class);
    Dog dog1 = testRealm.createObject(Dog.class);
    dog1.setName("Pluto");
    dog1.setAge(5);
    dog1.setHeight(1.2f);
    dog1.setWeight(9.9);
    dog1.setHasTail(true);
    dog1.setBirthday(new Date(2000));
    Dog dog2 = testRealm.createObject(Dog.class);
    dog2.setName("Fido");
    dog2.setAge(10);
    dog2.setHeight(0.7f);
    dog2.setWeight(11.3);
    dog2.setHasTail(true);
    dog2.setBirthday(new Date(4000));
    Cat cat = testRealm.createObject(Cat.class);
    cat.setName("Blackie");
    cat.setAge(12);
    cat.setHeight(0.3f);
    cat.setWeight(1.1);
    cat.setHasTail(true);
    cat.setBirthday(new Date(6000));
    Owner owner = testRealm.createObject(Owner.class);
    owner.setName("Tim");
    owner.getDogs().add(dog1);
    owner.getDogs().add(dog2);
    owner.setCat(cat);
    cat.setOwner(owner);
    dog1.setOwner(owner);
    dog2.setOwner(owner);
    testRealm.commitTransaction();
}
Also used : Owner(io.realm.entities.Owner) Cat(io.realm.entities.Cat) Dog(io.realm.entities.Dog) Date(java.util.Date)

Example 3 with Cat

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

the class RealmTests method schemaIndexCacheIsUpdatedAfterSchemaChange.

@Test
public void schemaIndexCacheIsUpdatedAfterSchemaChange() {
    final CatRealmProxy.CatColumnInfo catColumnInfo;
    catColumnInfo = (CatRealmProxy.CatColumnInfo) realm.schema.columnIndices.getColumnInfo(Cat.class);
    final long nameIndex = catColumnInfo.nameIndex;
    final AtomicLong nameIndexNew = new AtomicLong(-1L);
    // Changes column index of "name".
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            final Table catTable = realm.getSchema().getTable(Cat.CLASS_NAME);
            final long nameIndex = catTable.getColumnIndex(Cat.FIELD_NAME);
            catTable.removeColumn(nameIndex);
            final long newIndex = catTable.addColumn(RealmFieldType.STRING, Cat.FIELD_NAME, true);
            realm.setVersion(realm.getConfiguration().getSchemaVersion() + 1);
            nameIndexNew.set(newIndex);
        }
    });
    // We need to update index cache if the schema version was changed in the same thread.
    realm.sharedRealm.invokeSchemaChangeListenerIfSchemaChanged();
    // Checks if the index was changed.
    assertNotEquals(nameIndex, nameIndexNew);
    // Checks if index in the ColumnInfo is updated.
    assertEquals(nameIndexNew.get(), catColumnInfo.nameIndex);
    assertEquals(nameIndexNew.get(), (long) catColumnInfo.getIndicesMap().get(Cat.FIELD_NAME));
    // Checks by actual get and set.
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            final Cat cat = realm.createObject(Cat.class);
            cat.setName("pochi");
        }
    });
    //noinspection ConstantConditions
    assertEquals("pochi", realm.where(Cat.class).findFirst().getName());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Table(io.realm.internal.Table) Cat(io.realm.entities.Cat) SharedRealm(io.realm.internal.SharedRealm) Test(org.junit.Test)

Example 4 with Cat

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

the class BulkInsertTests method insertOrUpdate_emptyListWithCompositeMediator.

/**
 * Added to reproduce https://github.com/realm/realm-java/issues/3103
 */
@Test
public void insertOrUpdate_emptyListWithCompositeMediator() {
    final RealmConfiguration config = configFactory.createConfigurationBuilder().modules(new HumanModule(), new AnimalModule()).name("composite.realm").build();
    Realm.deleteRealm(config);
    assertEquals(config.getSchemaMediator().getClass(), CompositeMediator.class);
    final Realm realm = Realm.getInstance(config);
    // noinspection TryFinallyCanBeTryWithResources
    try {
        realm.executeTransaction(new Realm.Transaction() {

            @Override
            public void execute(Realm realm) {
                realm.insertOrUpdate(Collections.<Cat>emptyList());
            }
        });
        assertEquals(0, realm.where(Cat.class).count());
    } finally {
        realm.close();
    }
}
Also used : Cat(io.realm.entities.Cat) AnimalModule(io.realm.entities.AnimalModule) HumanModule(io.realm.entities.HumanModule) Test(org.junit.Test)

Example 5 with Cat

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

the class BulkInsertTests method insert_emptyListWithCompositeMediator.

/**
 * Added to reproduce https://github.com/realm/realm-java/issues/3103
 */
@Test
public void insert_emptyListWithCompositeMediator() {
    final RealmConfiguration config = configFactory.createConfigurationBuilder().modules(new HumanModule(), new AnimalModule()).name("composite.realm").build();
    Realm.deleteRealm(config);
    assertEquals(config.getSchemaMediator().getClass(), CompositeMediator.class);
    final Realm realm = Realm.getInstance(config);
    // noinspection TryFinallyCanBeTryWithResources
    try {
        realm.executeTransaction(new Realm.Transaction() {

            @Override
            public void execute(Realm realm) {
                realm.insert(Collections.<Cat>emptyList());
            }
        });
        assertEquals(0, realm.where(Cat.class).count());
    } finally {
        realm.close();
    }
}
Also used : Cat(io.realm.entities.Cat) AnimalModule(io.realm.entities.AnimalModule) HumanModule(io.realm.entities.HumanModule) Test(org.junit.Test)

Aggregations

Cat (io.realm.entities.Cat)10 Test (org.junit.Test)9 Dog (io.realm.entities.Dog)3 AnimalModule (io.realm.entities.AnimalModule)2 HumanModule (io.realm.entities.HumanModule)2 Owner (io.realm.entities.Owner)2 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)2 UiThreadTest (androidx.test.annotation.UiThreadTest)1 DogPrimaryKey (io.realm.entities.DogPrimaryKey)1 OwnerPrimaryKey (io.realm.entities.OwnerPrimaryKey)1 PrimaryKeyMix (io.realm.entities.PrimaryKeyMix)1 SharedRealm (io.realm.internal.SharedRealm)1 Table (io.realm.internal.Table)1 Date (java.util.Date)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1