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());
}
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();
}
}
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());
}
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());
}
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());
}
Aggregations