Search in sources :

Example 16 with AllTypesPrimaryKey

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

the class RealmJsonTests method assertAllTypesPrimaryKeyUpdated.

// Asserts that the list of AllTypesPrimaryKey objects where inserted and updated properly.
private void assertAllTypesPrimaryKeyUpdated() {
    assertEquals(1, realm.where(AllTypesPrimaryKey.class).count());
    AllTypesPrimaryKey obj = realm.where(AllTypesPrimaryKey.class).findFirst();
    assertEquals("Bar", obj.getColumnString());
    assertEquals(2.23F, obj.getColumnFloat(), 0F);
    assertEquals(2.234D, obj.getColumnDouble(), 0D);
    assertEquals(true, obj.isColumnBoolean());
    assertArrayEquals(new byte[] { 1, 2, 3 }, obj.getColumnBinary());
    assertEquals(new Date(2000), obj.getColumnDate());
    assertEquals("Dog4", obj.getColumnRealmObject().getName());
    assertEquals(2, obj.getColumnRealmList().size());
    assertEquals("Dog5", obj.getColumnRealmList().get(0).getName());
}
Also used : Date(java.util.Date) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey)

Example 17 with AllTypesPrimaryKey

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

the class RealmJsonTests method createOrUpdateObjectFromJson_invalidJsonObject.

@Test
public void createOrUpdateObjectFromJson_invalidJsonObject() throws JSONException {
    TestHelper.populateSimpleAllTypesPrimaryKey(realm);
    realm.beginTransaction();
    JSONObject json = new JSONObject();
    json.put("columnLong", "A");
    try {
        realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, json);
        fail();
    } catch (RealmException ignored) {
    } finally {
        realm.commitTransaction();
    }
    AllTypesPrimaryKey obj2 = realm.where(AllTypesPrimaryKey.class).findFirst();
    assertEquals("Foo", obj2.getColumnString());
}
Also used : JSONObject(org.json.JSONObject) RealmException(io.realm.exceptions.RealmException) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Example 18 with AllTypesPrimaryKey

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

the class RealmTests method callMutableMethodOutsideTransaction.

// Tests that all methods that require a transaction. (ie. any function that mutates Realm data)
@Test
public void callMutableMethodOutsideTransaction() throws JSONException, IOException {
    // Prepares unmanaged object data.
    AllTypesPrimaryKey t = new AllTypesPrimaryKey();
    List<AllTypesPrimaryKey> ts = Arrays.asList(t, t);
    // Prepares JSON data.
    String jsonObjStr = "{ \"columnLong\" : 1 }";
    JSONObject jsonObj = new JSONObject(jsonObjStr);
    InputStream jsonObjStream = TestHelper.stringToStream(jsonObjStr);
    InputStream jsonObjStream2 = TestHelper.stringToStream(jsonObjStr);
    String jsonArrStr = " [{ \"columnLong\" : 1 }] ";
    JSONArray jsonArr = new JSONArray(jsonArrStr);
    InputStream jsonArrStream = TestHelper.stringToStream(jsonArrStr);
    InputStream jsonArrStream2 = TestHelper.stringToStream(jsonArrStr);
    // Tests all methods that should require a transaction.
    try {
        realm.createObject(AllTypes.class);
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        realm.copyToRealm(t);
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        realm.copyToRealm(ts);
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        realm.copyToRealmOrUpdate(t);
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        realm.copyToRealmOrUpdate(ts);
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        realm.delete(AllTypes.class);
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        realm.deleteAll();
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        realm.createObjectFromJson(AllTypesPrimaryKey.class, jsonObj);
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        realm.createObjectFromJson(AllTypesPrimaryKey.class, jsonObjStr);
        fail();
    } catch (IllegalStateException expected) {
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        try {
            realm.createObjectFromJson(NoPrimaryKeyNullTypes.class, jsonObjStream);
            fail();
        } catch (IllegalStateException expected) {
        }
    }
    try {
        realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, jsonObj);
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, jsonObjStr);
        fail();
    } catch (IllegalStateException expected) {
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        try {
            realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, jsonObjStream2);
            fail();
        } catch (IllegalStateException expected) {
        }
    }
    try {
        realm.createAllFromJson(AllTypesPrimaryKey.class, jsonArr);
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        realm.createAllFromJson(AllTypesPrimaryKey.class, jsonArrStr);
        fail();
    } catch (IllegalStateException expected) {
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        try {
            realm.createAllFromJson(NoPrimaryKeyNullTypes.class, jsonArrStream);
            fail();
        } catch (IllegalStateException expected) {
        }
    }
    try {
        realm.createOrUpdateAllFromJson(AllTypesPrimaryKey.class, jsonArr);
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        realm.createOrUpdateAllFromJson(AllTypesPrimaryKey.class, jsonArrStr);
        fail();
    } catch (IllegalStateException expected) {
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        try {
            realm.createOrUpdateAllFromJson(AllTypesPrimaryKey.class, jsonArrStream2);
            fail();
        } catch (IllegalStateException expected) {
        }
    }
}
Also used : JSONObject(org.json.JSONObject) InputStream(java.io.InputStream) JSONArray(org.json.JSONArray) PrimaryKeyRequiredAsString(io.realm.entities.PrimaryKeyRequiredAsString) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Example 19 with AllTypesPrimaryKey

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

the class RealmTests method copyToRealmOrUpdate_updateExistingObject.

@Test
public void copyToRealmOrUpdate_updateExistingObject() {
    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.copyToRealm(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.copyToRealmOrUpdate(obj2);
        }
    });
    assertEquals(1, realm.where(AllTypesPrimaryKey.class).count());
    AllTypesPrimaryKey obj = realm.where(AllTypesPrimaryKey.class).findFirst();
    // Checks that the the only element has all its properties updated.
    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());
    assertFalse(obj.getColumnBoxedBoolean());
}
Also used : DogPrimaryKey(io.realm.entities.DogPrimaryKey) SharedRealm(io.realm.internal.SharedRealm) Date(java.util.Date) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Example 20 with AllTypesPrimaryKey

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

the class RealmTests method copyToRealmOrUpdate_iterableChildObjects.

// Tests that a collection of objects with references all gets copied.
@Test
public void copyToRealmOrUpdate_iterableChildObjects() {
    DogPrimaryKey dog = new DogPrimaryKey(1, "Snoop");
    AllTypesPrimaryKey allTypes1 = new AllTypesPrimaryKey();
    allTypes1.setColumnLong(1);
    allTypes1.setColumnRealmObject(dog);
    AllTypesPrimaryKey allTypes2 = new AllTypesPrimaryKey();
    allTypes1.setColumnLong(2);
    allTypes2.setColumnRealmObject(dog);
    realm.beginTransaction();
    realm.copyToRealmOrUpdate(Arrays.asList(allTypes1, allTypes2));
    realm.commitTransaction();
    assertEquals(2, realm.where(AllTypesPrimaryKey.class).count());
    assertEquals(1, realm.where(DogPrimaryKey.class).count());
}
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