Search in sources :

Example 41 with AllJavaTypes

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

the class LinkingObjectsManagedTests method basic_multipleBacklinksList.

// Setting multiple list links creates multiple backlinks
@Test
public void basic_multipleBacklinksList() {
    realm.beginTransaction();
    AllJavaTypes child = realm.createObject(AllJavaTypes.class, 1);
    AllJavaTypes parent1 = realm.createObject(AllJavaTypes.class, 2);
    AllJavaTypes parent2 = realm.createObject(AllJavaTypes.class, 3);
    parent1.getFieldList().add(child);
    parent2.getFieldList().add(child);
    realm.commitTransaction();
    assertEquals(2, child.getListParents().size());
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test)

Example 42 with AllJavaTypes

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

the class LinkingObjectsManagedTests method notification_notSentOnUnrelatedChangeModelObject.

// A listener registered on the backlinked object is called
// for an unrelated change on the an object of the same type!!
// This test exists only to document existing (but odd) behavior.
@Test
@RunTestInLooperThread
public void notification_notSentOnUnrelatedChangeModelObject() {
    final Realm looperThreadRealm = looperThread.realm;
    looperThreadRealm.beginTransaction();
    AllJavaTypes child = looperThreadRealm.createObject(AllJavaTypes.class, 10);
    AllJavaTypes parent = looperThreadRealm.createObject(AllJavaTypes.class, 1);
    looperThreadRealm.commitTransaction();
    final AtomicInteger counter = new AtomicInteger(0);
    RealmChangeListener<AllJavaTypes> listener = new RealmChangeListener<AllJavaTypes>() {

        @Override
        public void onChange(AllJavaTypes object) {
            counter.incrementAndGet();
        }
    };
    child.addChangeListener(listener);
    looperThreadRealm.beginTransaction();
    looperThreadRealm.where(AllJavaTypes.class).equalTo("fieldId", 1).findAll().deleteAllFromRealm();
    looperThreadRealm.commitTransaction();
    verifyPostConditions(looperThreadRealm, new PostConditions() {

        public void run(Realm realm) {
            assertEquals(1, looperThreadRealm.where(AllJavaTypes.class).findAll().size());
            assertEquals(1, counter.get());
        }
    }, child, parent);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllJavaTypes(io.realm.entities.AllJavaTypes) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 43 with AllJavaTypes

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

the class LinkingObjectsUnmanagedTests method copyToRealm.

// When managed, an object's backlinks fields get live.
@Test
public void copyToRealm() {
    AllJavaTypes unmanagedChild = new AllJavaTypes(1);
    realm.beginTransaction();
    AllJavaTypes parent = realm.createObject(AllJavaTypes.class, 2);
    realm.commitTransaction();
    assertEquals(new AllJavaTypes().getObjectParents(), unmanagedChild.getObjectParents());
    realm.beginTransaction();
    AllJavaTypes child = realm.copyToRealm(unmanagedChild);
    parent.setFieldObject(child);
    realm.commitTransaction();
    RealmResults<AllJavaTypes> parents = child.getObjectParents();
    assertNotNull(parents);
    assertEquals(1, parents.size());
    assertEquals(parent, parents.first());
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test)

Example 44 with AllJavaTypes

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

the class BulkInsertTests method insertOrUpdate_managedObject.

// Inserting a managed object will result in it being copied or updated again.
@Test
public void insertOrUpdate_managedObject() {
    AllJavaTypes obj = new AllJavaTypes();
    obj.setFieldId(42);
    obj.setFieldIgnored("cookie");
    obj.setFieldLong(42);
    obj.setFieldString("obj1");
    realm.beginTransaction();
    AllJavaTypes managedAllJavaTypes = realm.copyToRealm(obj);
    realm.commitTransaction();
    realm.beginTransaction();
    AllJavaTypes filedObject = new AllJavaTypes();
    filedObject.setFieldLong(8);
    filedObject = realm.copyToRealm(filedObject);
    managedAllJavaTypes.setFieldObject(filedObject);
    managedAllJavaTypes.setFieldString("updated");
    realm.insertOrUpdate(managedAllJavaTypes);
    realm.commitTransaction();
    AllJavaTypes first = realm.where(AllJavaTypes.class).equalTo(AllJavaTypes.FIELD_LONG, 42).findFirst();
    assertNotNull(first);
    assertEquals(42, first.getFieldLong(), 0);
    assertEquals("updated", first.getFieldString());
    assertNull(first.getFieldIgnored());
    assertNotNull(first.getFieldObject());
    assertEquals(8, first.getFieldObject().getFieldLong());
    assertEquals(2, realm.where(AllJavaTypes.class).findAll().size());
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test)

Example 45 with AllJavaTypes

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

the class BulkInsertTests method insert.

@Test
public void insert() {
    AllJavaTypes obj = new AllJavaTypes();
    obj.setFieldIgnored("cookie");
    obj.setFieldId(42);
    obj.setFieldLong(42);
    obj.setFieldString("obj1");
    RealmList<AllJavaTypes> list = new RealmList<AllJavaTypes>();
    list.add(obj);
    Date date = new Date();
    AllJavaTypes allTypes = new AllJavaTypes();
    allTypes.setFieldString("String");
    allTypes.setFieldId(1L);
    allTypes.setFieldLong(1L);
    allTypes.setFieldFloat(1F);
    allTypes.setFieldDouble(1D);
    allTypes.setFieldBoolean(true);
    allTypes.setFieldDate(date);
    allTypes.setFieldBinary(new byte[] { 1, 2, 3 });
    allTypes.setFieldObject(obj);
    allTypes.setFieldList(list);
    realm.beginTransaction();
    realm.insert(allTypes);
    realm.commitTransaction();
    AllJavaTypes realmTypes = realm.where(AllJavaTypes.class).findFirst();
    assertNotNull(realmTypes);
    // Objects should not be considered equal
    assertNotSame(allTypes, realmTypes);
    // But they contain the same data
    assertEquals(allTypes.getFieldString(), realmTypes.getFieldString());
    assertEquals(allTypes.getFieldLong(), realmTypes.getFieldLong());
    assertEquals(allTypes.getFieldFloat(), realmTypes.getFieldFloat(), 0);
    assertEquals(allTypes.getFieldDouble(), realmTypes.getFieldDouble(), 0);
    assertEquals(allTypes.isFieldBoolean(), realmTypes.isFieldBoolean());
    assertEquals(allTypes.getFieldDate(), realmTypes.getFieldDate());
    assertArrayEquals(allTypes.getFieldBinary(), realmTypes.getFieldBinary());
    assertEquals(allTypes.getFieldObject().getFieldString(), obj.getFieldString());
    assertEquals(list.size(), realmTypes.getFieldList().size());
    assertEquals(list.get(0).getFieldString(), realmTypes.getFieldList().get(0).getFieldString());
    assertEquals(list.get(0).getFieldLong(), realmTypes.getFieldList().get(0).getFieldLong());
    assertNull(realmTypes.getFieldList().get(0).getFieldIgnored());
    // Makes sure Dog was not inserted twice in the recursive process.
    assertEquals(2, realm.where(AllJavaTypes.class).findAll().size());
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) Date(java.util.Date) Test(org.junit.Test)

Aggregations

AllJavaTypes (io.realm.entities.AllJavaTypes)90 Test (org.junit.Test)78 UiThreadTest (android.support.test.annotation.UiThreadTest)24 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Date (java.util.Date)5 RealmException (io.realm.exceptions.RealmException)4 Ignore (org.junit.Ignore)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 ExpectedException (org.junit.rules.ExpectedException)2 Pair (android.util.Pair)1 CyclicType (io.realm.entities.CyclicType)1 Dog (io.realm.entities.Dog)1 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)1 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)1 RealmPrimaryKeyConstraintException (io.realm.exceptions.RealmPrimaryKeyConstraintException)1 RunInLooperThread (io.realm.rule.RunInLooperThread)1 Field (java.lang.reflect.Field)1