Search in sources :

Example 11 with AllTypesPrimaryKey

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

the class RealmJsonTests method createOrUpdateObjectFromJson_jsonObjectNullClass.

@Test
public void createOrUpdateObjectFromJson_jsonObjectNullClass() throws JSONException {
    TestHelper.populateSimpleAllTypesPrimaryKey(realm);
    realm.beginTransaction();
    JSONObject json = new JSONObject();
    json.put("columnLong", 1);
    json.put("columnString", "bar");
    assertNull(realm.createOrUpdateObjectFromJson(null, json));
    realm.commitTransaction();
    AllTypesPrimaryKey obj2 = realm.where(AllTypesPrimaryKey.class).findFirst();
    assertEquals("Foo", obj2.getColumnString());
}
Also used : JSONObject(org.json.JSONObject) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Example 12 with AllTypesPrimaryKey

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

the class RealmObjectTests method addChangeListener_returnedObjectOfCopyToRealmOrUpdate.

// Bug https://github.com/realm/realm-java/issues/2569
@Test
@RunTestInLooperThread
public void addChangeListener_returnedObjectOfCopyToRealmOrUpdate() {
    Realm realm = looperThread.realm;
    realm.beginTransaction();
    realm.createObject(AllTypesPrimaryKey.class, 1);
    AllTypesPrimaryKey allTypesPrimaryKey = new AllTypesPrimaryKey();
    allTypesPrimaryKey.setColumnLong(1);
    allTypesPrimaryKey.setColumnFloat(0f);
    allTypesPrimaryKey = realm.copyToRealmOrUpdate(allTypesPrimaryKey);
    realm.commitTransaction();
    looperThread.keepStrongReference.add(allTypesPrimaryKey);
    allTypesPrimaryKey.addChangeListener(new RealmChangeListener<AllTypesPrimaryKey>() {

        @Override
        public void onChange(AllTypesPrimaryKey element) {
            assertEquals(42.0f, element.getColumnFloat(), 0f);
            looperThread.testComplete();
        }
    });
    // Change the object to trigger the listener.
    realm.beginTransaction();
    allTypesPrimaryKey.setColumnFloat(42f);
    realm.commitTransaction();
}
Also used : AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) UiThreadTest(android.support.test.annotation.UiThreadTest) Test(org.junit.Test)

Example 13 with AllTypesPrimaryKey

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

the class BulkInsertTests method insertOrUpdate_mixingNoPrimaryKeyAndPrimaryKeyModels.

@Test
public void insertOrUpdate_mixingNoPrimaryKeyAndPrimaryKeyModels() {
    AllTypesPrimaryKey objB_pk = new AllTypesPrimaryKey();
    objB_pk.setColumnLong(7);
    objB_pk.setColumnString("B");
    NoPrimaryKeyWithPrimaryKeyObjectRelation objA_no_pk = new NoPrimaryKeyWithPrimaryKeyObjectRelation();
    objA_no_pk.setColumnRealmObjectPK(objB_pk);
    objA_no_pk.setColumnString("A");
    realm.beginTransaction();
    realm.insert(objA_no_pk);
    realm.commitTransaction();
    RealmResults<NoPrimaryKeyWithPrimaryKeyObjectRelation> all = realm.where(NoPrimaryKeyWithPrimaryKeyObjectRelation.class).findAll();
    assertEquals(1, all.size());
    assertEquals("A", all.get(0).getColumnString());
    assertEquals(8, all.get(0).getColumnInt());
    assertNotNull(all.get(0).getColumnRealmObjectPK());
    assertEquals(7, all.get(0).getColumnRealmObjectPK().getColumnLong());
    assertEquals("B", all.get(0).getColumnRealmObjectPK().getColumnString());
    assertEquals(1, realm.where(AllTypesPrimaryKey.class).findAll().size());
    objA_no_pk.setColumnString("different A");
    // Should insert a new instance
    objA_no_pk.setColumnInt(42);
    // Updates (since it has a PK) now both AllTypesPrimaryKey points to the same objB_pk instance.
    objB_pk.setColumnString("updated B");
    realm.beginTransaction();
    realm.insertOrUpdate(objA_no_pk);
    realm.commitTransaction();
    all = realm.where(NoPrimaryKeyWithPrimaryKeyObjectRelation.class).findAllSorted("columnString");
    assertEquals(2, all.size());
    assertEquals("A", all.get(0).getColumnString());
    assertEquals(8, all.get(0).getColumnInt());
    assertEquals("different A", all.get(1).getColumnString());
    assertEquals(42, all.get(1).getColumnInt());
    assertNotNull(all.get(0).getColumnRealmObjectPK());
    assertNotNull(all.get(1).getColumnRealmObjectPK());
    assertEquals(7, all.get(0).getColumnRealmObjectPK().getColumnLong());
    assertEquals(7, all.get(1).getColumnRealmObjectPK().getColumnLong());
    assertEquals("updated B", all.get(0).getColumnRealmObjectPK().getColumnString());
    assertEquals("updated B", all.get(1).getColumnRealmObjectPK().getColumnString());
    assertEquals(1, realm.where(AllTypesPrimaryKey.class).findAll().size());
}
Also used : NoPrimaryKeyWithPrimaryKeyObjectRelation(io.realm.entities.NoPrimaryKeyWithPrimaryKeyObjectRelation) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Example 14 with AllTypesPrimaryKey

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

the class BulkInsertTests method insertOrUpdate_collectionOfManagedObjects.

@Test
public void insertOrUpdate_collectionOfManagedObjects() {
    realm.beginTransaction();
    AllTypesPrimaryKey allTypes = realm.createObject(AllTypesPrimaryKey.class, 0);
    allTypes.getColumnRealmList().add(realm.createObject(DogPrimaryKey.class, 0));
    realm.commitTransaction();
    assertEquals(1, allTypes.getColumnRealmList().size());
    List<AllTypesPrimaryKey> list = new ArrayList<AllTypesPrimaryKey>();
    // Although there are two same objects in the list, none of them should be saved to the db since they are managed
    // already.
    list.add(allTypes);
    list.add(allTypes);
    realm.beginTransaction();
    realm.insertOrUpdate(list);
    realm.commitTransaction();
    allTypes = realm.where(AllTypesPrimaryKey.class).findFirst();
    assertNotNull(allTypes);
    assertEquals(1, allTypes.getColumnRealmList().size());
}
Also used : DogPrimaryKey(io.realm.entities.DogPrimaryKey) ArrayList(java.util.ArrayList) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Example 15 with AllTypesPrimaryKey

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

the class RealmJsonTests method createOrUpdateObjectFromJson_inputString.

@Test
public void createOrUpdateObjectFromJson_inputString() throws IOException {
    TestHelper.populateSimpleAllTypesPrimaryKey(realm);
    realm.beginTransaction();
    AllTypesPrimaryKey newObj = realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, "{ \"columnLong\" : 1, \"columnString\" : \"bar\" }");
    realm.commitTransaction();
    assertEquals(1, realm.where(AllTypesPrimaryKey.class).count());
    assertEquals("bar", newObj.getColumnString());
}
Also used : 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