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