Search in sources :

Example 21 with Owner

use of io.realm.entities.Owner 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 22 with Owner

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

the class RealmResultsTests method populateRealmResultsOnLinkView.

private RealmResults<Dog> populateRealmResultsOnLinkView(Realm realm) {
    realm.beginTransaction();
    Owner owner = realm.createObject(Owner.class);
    for (int i = 0; i < 10; i++) {
        Dog dog = new Dog();
        dog.setName("name_" + i);
        dog.setOwner(owner);
        dog.setAge(i);
        dog.setBirthday(new Date(i));
        owner.getDogs().add(dog);
    }
    realm.commitTransaction();
    return owner.getDogs().where().lessThan(Dog.FIELD_AGE, 5).findAll();
}
Also used : Owner(io.realm.entities.Owner) Dog(io.realm.entities.Dog) Date(java.util.Date)

Example 23 with Owner

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

the class RealmLinkTests method objects.

@Test
public void objects() {
    RealmResults<Owner> owners = testRealm.where(Owner.class).findAll();
    assertEquals(1, owners.size());
    assertEquals(2, owners.first().getDogs().size());
    assertEquals("Pluto", owners.first().getDogs().first().getName());
    assertEquals("Fido", owners.first().getDogs().last().getName());
    assertEquals("Blackie", owners.first().getCat().getName());
    assertEquals(12, owners.first().getCat().getAge());
    RealmResults<Dog> dogs = testRealm.where(Dog.class).findAll();
    assertEquals(2, dogs.size());
    for (Dog dog : dogs) {
        assertEquals("Tim", dog.getOwner().getName());
    }
    RealmResults<Cat> cats = testRealm.where(Cat.class).findAll();
    assertEquals(1, cats.size());
    assertEquals("Tim", cats.first().getOwner().getName());
}
Also used : Owner(io.realm.entities.Owner) Cat(io.realm.entities.Cat) Dog(io.realm.entities.Dog) Test(org.junit.Test)

Example 24 with Owner

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

the class OrderedCollectionChangeSetTests method createObjects.

// Creates Dogs objects with columnLong set to the value elements in indices array.
private void createObjects(Realm realm, int... indices) {
    for (int index : indices) {
        Dog dog = realm.createObject(Dog.class);
        dog.setAge(index);
        if (type == ObservablesType.REALM_LIST) {
            Owner owner = realm.where(Owner.class).findFirst();
            assertNotNull(owner);
            RealmList<Dog> dogs = owner.getDogs();
            boolean added = false;
            // Insert the newly created dog to the RealmList by the order of age.
            for (int i = 0; i < dogs.size(); i++) {
                if (dog.getAge() <= dogs.get(i).getAge()) {
                    dogs.add(i, dog);
                    added = true;
                    break;
                }
            }
            if (!added) {
                dogs.add(dog);
            }
        }
    }
}
Also used : Owner(io.realm.entities.Owner) Dog(io.realm.entities.Dog)

Example 25 with Owner

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

the class OrderedCollectionChangeSetTests method populateData.

private void populateData(Realm realm, int testSize) {
    Owner owner = null;
    realm.beginTransaction();
    if (type == ObservablesType.REALM_LIST) {
        owner = realm.createObject(Owner.class);
    }
    for (int i = 0; i < testSize; i++) {
        Dog dog = realm.createObject(Dog.class);
        dog.setAge(i);
        if (type == ObservablesType.REALM_LIST) {
            owner.getDogs().add(dog);
        }
    }
    realm.commitTransaction();
}
Also used : Owner(io.realm.entities.Owner) Dog(io.realm.entities.Dog)

Aggregations

Owner (io.realm.entities.Owner)36 Dog (io.realm.entities.Dog)30 Test (org.junit.Test)27 UiThreadTest (androidx.test.annotation.UiThreadTest)3 CatOwner (io.realm.entities.CatOwner)3 Date (java.util.Date)3 Cat (io.realm.entities.Cat)2 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)2 AllTypes (io.realm.entities.AllTypes)1 DictionaryAllTypes (io.realm.entities.DictionaryAllTypes)1 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)1 RealmBackgroundTask (io.realm.util.RealmBackgroundTask)1 BigDecimal (java.math.BigDecimal)1 SimpleDateFormat (java.text.SimpleDateFormat)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Decimal128 (org.bson.types.Decimal128)1 ObjectId (org.bson.types.ObjectId)1 Before (org.junit.Before)1