Search in sources :

Example 1 with Owner

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

the class RealmListTests method removeAllFromRealm_outsideTransaction.

@Test
public void removeAllFromRealm_outsideTransaction() {
    Owner owner = realm.where(Owner.class).findFirst();
    RealmList<Dog> dogs = owner.getDogs();
    try {
        dogs.deleteAllFromRealm();
        fail("removeAllFromRealm should be called in a transaction.");
    } catch (IllegalStateException e) {
        assertEquals("Changing Realm data can only be done from inside a write transaction.", e.getMessage());
    }
}
Also used : Owner(io.realm.entities.Owner) Dog(io.realm.entities.Dog) Test(org.junit.Test)

Example 2 with Owner

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

the class RealmListTests method removeAllFromRealm.

@Test
public void removeAllFromRealm() {
    Owner owner = realm.where(Owner.class).findFirst();
    RealmList<Dog> dogs = owner.getDogs();
    assertEquals(TEST_SIZE, dogs.size());
    realm.beginTransaction();
    dogs.deleteAllFromRealm();
    realm.commitTransaction();
    assertEquals(0, dogs.size());
    assertEquals(0, realm.where(Dog.class).count());
}
Also used : Owner(io.realm.entities.Owner) Dog(io.realm.entities.Dog) Test(org.junit.Test)

Example 3 with Owner

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

the class OrderedCollectionChangeSetTests method reorderRealmList.

// Re-adds the dogs so they would be sorted by age in the list.
private void reorderRealmList(Realm realm) {
    RealmResults<Dog> dogs = realm.where(Dog.class).findAllSorted(Dog.FIELD_AGE);
    Owner owner = realm.where(Owner.class).findFirst();
    owner.getDogs().clear();
    for (Dog dog : dogs) {
        owner.getDogs().add(dog);
    }
}
Also used : Owner(io.realm.entities.Owner) Dog(io.realm.entities.Dog)

Example 4 with Owner

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

the class BulkInsertTests method insertOrUpdate_cyclicDependenciesFromOtherRealm.

@Test
public void insertOrUpdate_cyclicDependenciesFromOtherRealm() {
    RealmConfiguration config1 = configFactory.createConfiguration("realm1");
    RealmConfiguration config2 = configFactory.createConfiguration("realm2");
    Realm realm1 = Realm.getInstance(config1);
    Realm realm2 = Realm.getInstance(config2);
    realm1.beginTransaction();
    Owner owner = realm1.createObject(Owner.class);
    owner.setName("Kiba");
    Dog dog = realm1.createObject(Dog.class);
    dog.setName("Akamaru");
    owner.getDogs().add(dog);
    dog.setOwner(owner);
    realm1.commitTransaction();
    // Copies object with relations from realm1 to realm2.
    realm2.beginTransaction();
    realm2.insertOrUpdate(owner);
    realm2.commitTransaction();
    assertEquals(1, realm1.where(Owner.class).count());
    assertEquals(1, realm1.where(Owner.class).findFirst().getDogs().size());
    assertEquals(1, realm1.where(Dog.class).count());
    assertEquals(realm1.where(Owner.class).count(), realm2.where(Owner.class).count());
    assertEquals(realm1.where(Dog.class).count(), realm2.where(Dog.class).count());
    assertEquals(1, realm2.where(Owner.class).findFirst().getDogs().size());
    assertEquals(realm1.where(Owner.class).findFirst().getName(), realm2.where(Owner.class).findFirst().getName());
    assertEquals(realm1.where(Owner.class).findFirst().getDogs().first().getName(), realm2.where(Owner.class).findFirst().getDogs().first().getName());
    realm1.close();
    realm2.close();
}
Also used : CatOwner(io.realm.entities.CatOwner) Owner(io.realm.entities.Owner) Dog(io.realm.entities.Dog) Test(org.junit.Test)

Example 5 with Owner

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

the class RealmAsyncQueryTests method queryingLinkHandover.

// This test makes sure that Async queries update when using link.
@Test
@RunTestInLooperThread
public void queryingLinkHandover() throws Throwable {
    final AtomicInteger numberOfInvocations = new AtomicInteger(0);
    final Realm realm = looperThread.getRealm();
    final RealmResults<Dog> allAsync = realm.where(Dog.class).equalTo("owner.name", "kiba").findAllAsync();
    looperThread.keepStrongReference(allAsync);
    allAsync.addChangeListener(new RealmChangeListener<RealmResults<Dog>>() {

        @Override
        public void onChange(RealmResults<Dog> object) {
            switch(numberOfInvocations.incrementAndGet()) {
                case 1:
                    assertEquals(0, allAsync.size());
                    assertTrue(allAsync.isLoaded());
                    assertTrue(allAsync.isValid());
                    assertTrue(allAsync.isEmpty());
                    new RealmBackgroundTask(realm.getConfiguration()) {

                        @Override
                        public void doInBackground(Realm realm) {
                            realm.beginTransaction();
                            Dog dog = realm.createObject(Dog.class);
                            dog.setAge(10);
                            dog.setName("Akamaru");
                            Owner kiba = realm.createObject(Owner.class);
                            kiba.setName("kiba");
                            dog.setOwner(kiba);
                            realm.commitTransaction();
                        }
                    }.awaitOrFail();
                    break;
                case 2:
                    assertEquals(1, realm.where(Dog.class).count());
                    assertEquals(1, realm.where(Owner.class).count());
                    assertEquals(1, allAsync.size());
                    assertTrue(allAsync.isLoaded());
                    assertTrue(allAsync.isValid());
                    assertFalse(allAsync.isEmpty());
                    assertEquals(1, allAsync.size());
                    assertEquals("Akamaru", allAsync.get(0).getName());
                    assertEquals("kiba", allAsync.get(0).getOwner().getName());
                    looperThread.testComplete();
                    break;
            }
        }
    });
}
Also used : Owner(io.realm.entities.Owner) RealmBackgroundTask(io.realm.util.RealmBackgroundTask) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Dog(io.realm.entities.Dog) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

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