Search in sources :

Example 21 with AllTypesPrimaryKey

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

the class RealmObjectTests method addChangeListener_listenerShouldBeCalledIfObjectChangesAfterAsyncReturn.

// step 1: findFirstAsync
// step 2: async query returns, change the object in the listener
// step 3: listener gets called again
@Test
@RunTestInLooperThread
public void addChangeListener_listenerShouldBeCalledIfObjectChangesAfterAsyncReturn() {
    final AtomicInteger listenerCounter = new AtomicInteger(0);
    final Realm realm = looperThread.realm;
    realm.beginTransaction();
    realm.createObject(AllTypesPrimaryKey.class, 1);
    realm.commitTransaction();
    // Step 1
    final AllTypesPrimaryKey allTypesPrimaryKey = realm.where(AllTypesPrimaryKey.class).findFirstAsync();
    looperThread.keepStrongReference.add(allTypesPrimaryKey);
    allTypesPrimaryKey.addChangeListener(new RealmChangeListener<AllTypesPrimaryKey>() {

        @Override
        public void onChange(AllTypesPrimaryKey element) {
            int count = listenerCounter.getAndAdd(1);
            if (count == 0) {
                // Step 2
                realm.executeTransactionAsync(new Realm.Transaction() {

                    @Override
                    public void execute(Realm realm) {
                        realm.where(AllTypesPrimaryKey.class).findFirst().setColumnFloat(42f);
                    }
                });
            } else if (count == 1) {
                // Step 3
                assertEquals(allTypesPrimaryKey.getColumnFloat(), 42f, 0);
                looperThread.testComplete();
            } else {
                fail();
            }
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 22 with AllTypesPrimaryKey

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

the class TestHelper method populateSimpleAllTypesPrimaryKey.

public static void populateSimpleAllTypesPrimaryKey(Realm realm) {
    realm.beginTransaction();
    AllTypesPrimaryKey obj = new AllTypesPrimaryKey();
    obj.setColumnLong(1);
    obj.setColumnString("Foo");
    realm.copyToRealm(obj);
    realm.commitTransaction();
}
Also used : AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey)

Example 23 with AllTypesPrimaryKey

use of io.realm.entities.AllTypesPrimaryKey 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 24 with AllTypesPrimaryKey

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

the class BulkInsertTests method insertOrUpdate_mixingPrimaryAndNoPrimaryKeyList.

@Test
public void insertOrUpdate_mixingPrimaryAndNoPrimaryKeyList() {
    NoPrimaryKeyWithPrimaryKeyObjectRelation objA_no_pk = new NoPrimaryKeyWithPrimaryKeyObjectRelation();
    objA_no_pk.setColumnString("A");
    NoPrimaryKeyWithPrimaryKeyObjectRelation objB_no_pk = new NoPrimaryKeyWithPrimaryKeyObjectRelation();
    objB_no_pk.setColumnString("B");
    AllTypesPrimaryKey objC_pk = new AllTypesPrimaryKey();
    objC_pk.setColumnLong(7);
    objC_pk.setColumnString("C");
    AllTypesPrimaryKey objD_pk = new AllTypesPrimaryKey();
    objD_pk.setColumnLong(7);
    objD_pk.setColumnString("D");
    objA_no_pk.setColumnRealmObjectPK(objC_pk);
    objB_no_pk.setColumnRealmObjectPK(objD_pk);
    ArrayList<NoPrimaryKeyWithPrimaryKeyObjectRelation> objects = new ArrayList<NoPrimaryKeyWithPrimaryKeyObjectRelation>(2);
    objects.add(objA_no_pk);
    objects.add(objB_no_pk);
    realm.beginTransaction();
    realm.insertOrUpdate(objects);
    realm.commitTransaction();
    RealmResults<NoPrimaryKeyWithPrimaryKeyObjectRelation> all = realm.where(NoPrimaryKeyWithPrimaryKeyObjectRelation.class).findAllSorted("columnString", Sort.DESCENDING);
    assertEquals(2, all.size());
    assertEquals("B", all.get(0).getColumnString());
    assertEquals("A", all.get(1).getColumnString());
    assertNotNull(all.get(0).getColumnRealmObjectPK());
    assertNotNull(all.get(1).getColumnRealmObjectPK());
    assertEquals("D", all.get(0).getColumnRealmObjectPK().getColumnString());
    assertEquals("D", all.get(1).getColumnRealmObjectPK().getColumnString());
    assertEquals(1, realm.where(AllTypesPrimaryKey.class).findAll().size());
}
Also used : ArrayList(java.util.ArrayList) NoPrimaryKeyWithPrimaryKeyObjectRelation(io.realm.entities.NoPrimaryKeyWithPrimaryKeyObjectRelation) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Example 25 with AllTypesPrimaryKey

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

the class BulkInsertTests method insertOrUpdate_shouldNotClearRealmList.

// To reproduce https://github.com/realm/realm-java/issues/3105.
@Test
public void insertOrUpdate_shouldNotClearRealmList() {
    realm.beginTransaction();
    AllTypesPrimaryKey allTypes = realm.createObject(AllTypesPrimaryKey.class, 0);
    allTypes.getColumnRealmList().add(realm.createObject(DogPrimaryKey.class, 0));
    realm.commitTransaction();
    assertEquals(1, allTypes.getColumnRealmList().size());
    realm.beginTransaction();
    realm.insertOrUpdate(allTypes);
    realm.commitTransaction();
    allTypes = realm.where(AllTypesPrimaryKey.class).findFirst();
    assertNotNull(allTypes);
    assertEquals(1, allTypes.getColumnRealmList().size());
}
Also used : DogPrimaryKey(io.realm.entities.DogPrimaryKey) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Aggregations

AllTypesPrimaryKey (io.realm.entities.AllTypesPrimaryKey)25 Test (org.junit.Test)22 DogPrimaryKey (io.realm.entities.DogPrimaryKey)7 Date (java.util.Date)7 InputStream (java.io.InputStream)5 JSONObject (org.json.JSONObject)5 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)4 RealmException (io.realm.exceptions.RealmException)3 SharedRealm (io.realm.internal.SharedRealm)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 UiThreadTest (android.support.test.annotation.UiThreadTest)2 NoPrimaryKeyWithPrimaryKeyObjectRelation (io.realm.entities.NoPrimaryKeyWithPrimaryKeyObjectRelation)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 AllJavaTypes (io.realm.entities.AllJavaTypes)1 AllTypes (io.realm.entities.AllTypes)1 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)1 PrimaryKeyRequiredAsString (io.realm.entities.PrimaryKeyRequiredAsString)1 RealmFileException (io.realm.exceptions.RealmFileException)1 RealmPrimaryKeyConstraintException (io.realm.exceptions.RealmPrimaryKeyConstraintException)1