Search in sources :

Example 6 with AllTypesPrimaryKey

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

the class RealmJsonTests method createOrUpdateObjectFromJson_withJsonObject.

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

Example 7 with AllTypesPrimaryKey

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

the class RealmJsonTests method createOrUpdateObjectFromJson_streamInvalidJson.

@Test
public void createOrUpdateObjectFromJson_streamInvalidJson() throws IOException {
    assumeThat(Build.VERSION.SDK_INT, greaterThanOrEqualTo(Build.VERSION_CODES.HONEYCOMB));
    AllTypesPrimaryKey obj = new AllTypesPrimaryKey();
    obj.setColumnLong(1);
    realm.beginTransaction();
    realm.copyToRealm(obj);
    realm.commitTransaction();
    InputStream in = TestHelper.loadJsonFromAssets(context, "all_types_invalid.json");
    realm.beginTransaction();
    try {
        realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, in);
        fail();
    } catch (RealmException ignored) {
    } finally {
        realm.commitTransaction();
        in.close();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RealmException(io.realm.exceptions.RealmException) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Example 8 with AllTypesPrimaryKey

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

the class RealmJsonTests method createOrUpdateObjectFromJson_inputStream.

@Test
public void createOrUpdateObjectFromJson_inputStream() throws IOException {
    assumeThat(Build.VERSION.SDK_INT, greaterThanOrEqualTo(Build.VERSION_CODES.HONEYCOMB));
    realm.beginTransaction();
    AllTypesPrimaryKey obj = new AllTypesPrimaryKey();
    obj.setColumnLong(1);
    obj.setColumnString("Foo");
    realm.copyToRealm(obj);
    InputStream in = TestHelper.stringToStream("{ \"columnLong\" : 1, \"columnString\" : \"bar\" }");
    AllTypesPrimaryKey newObj = realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, in);
    realm.commitTransaction();
    assertEquals(1, realm.where(AllTypesPrimaryKey.class).count());
    assertEquals("bar", newObj.getColumnString());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Example 9 with AllTypesPrimaryKey

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

the class RealmJsonTests method createOrUpdateObjectFromJson_objectNullValues.

// Tests updating a existing object with JSON object with only primary key.
// No value should be changed.
@Test
public void createOrUpdateObjectFromJson_objectNullValues() throws IOException {
    AllTypesPrimaryKey obj = new AllTypesPrimaryKey();
    Date date = new Date(0);
    // ID
    obj.setColumnLong(1);
    obj.setColumnBinary(new byte[] { 1 });
    obj.setColumnBoolean(true);
    obj.setColumnDate(date);
    obj.setColumnDouble(1);
    obj.setColumnFloat(1);
    obj.setColumnString("1");
    realm.beginTransaction();
    realm.copyToRealm(obj);
    realm.commitTransaction();
    String json = TestHelper.streamToString(TestHelper.loadJsonFromAssets(context, "all_types_primary_key_field_only.json"));
    realm.beginTransaction();
    realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, json);
    realm.commitTransaction();
    // Checks that all primitive types are imported correctly.
    obj = realm.where(AllTypesPrimaryKey.class).findFirst();
    assertEquals("1", obj.getColumnString());
    assertEquals(1L, obj.getColumnLong());
    assertEquals(1F, obj.getColumnFloat(), 0F);
    assertEquals(1D, obj.getColumnDouble(), 0D);
    assertEquals(true, obj.isColumnBoolean());
    assertEquals(date, obj.getColumnDate());
    assertArrayEquals(new byte[] { 1 }, obj.getColumnBinary());
    assertNull(obj.getColumnRealmObject());
    assertEquals(0, obj.getColumnRealmList().size());
}
Also used : Date(java.util.Date) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Example 10 with AllTypesPrimaryKey

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

the class RealmJsonTests method createOrUpdateObjectFromJson_streamNullValues.

/**
     * Tests updating a existing object with JSON stream. Only primary key in JSON.
     * No value should be changed.
     */
@Test
public void createOrUpdateObjectFromJson_streamNullValues() throws IOException {
    assumeThat(Build.VERSION.SDK_INT, greaterThanOrEqualTo(Build.VERSION_CODES.HONEYCOMB));
    AllTypesPrimaryKey obj = new AllTypesPrimaryKey();
    Date date = new Date(0);
    // ID
    obj.setColumnLong(1);
    obj.setColumnBinary(new byte[] { 1 });
    obj.setColumnBoolean(true);
    obj.setColumnDate(date);
    obj.setColumnDouble(1);
    obj.setColumnFloat(1);
    obj.setColumnString("1");
    realm.beginTransaction();
    realm.copyToRealm(obj);
    realm.commitTransaction();
    InputStream in = TestHelper.loadJsonFromAssets(context, "all_types_primary_key_field_only.json");
    realm.beginTransaction();
    realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, in);
    realm.commitTransaction();
    in.close();
    // Checks that all primitive types are imported correctly.
    obj = realm.where(AllTypesPrimaryKey.class).findFirst();
    assertEquals("1", obj.getColumnString());
    assertEquals(1L, obj.getColumnLong());
    assertEquals(1F, obj.getColumnFloat(), 0F);
    assertEquals(1D, obj.getColumnDouble(), 0D);
    assertEquals(true, obj.isColumnBoolean());
    assertEquals(date, obj.getColumnDate());
    assertArrayEquals(new byte[] { 1 }, obj.getColumnBinary());
    assertNull(obj.getColumnRealmObject());
    assertEquals(0, obj.getColumnRealmList().size());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Date(java.util.Date) 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