use of io.realm.entities.PrimaryKeyAsBoxedLong in project realm-java by realm.
the class TestHelper method populateTestRealmWithLongPrimaryKey.
/**
* Populates a realm with Long type Primarykey objects for a number of numberOfPopulation - 1,
* starting with iteratorBeginValue. One object is setup to have given values from parameters.
*/
public static void populateTestRealmWithLongPrimaryKey(Realm testRealm, Long primaryFieldValue, String secondaryFieldValue, long numberOfPopulation, long iteratorBeginValue) {
testRealm.beginTransaction();
PrimaryKeyAsBoxedLong userObj = new PrimaryKeyAsBoxedLong();
userObj.setId(primaryFieldValue);
userObj.setName(secondaryFieldValue);
testRealm.copyToRealm(userObj);
long idValue = iteratorBeginValue;
for (long i = 0; i < numberOfPopulation - 1; ++i, ++idValue) {
PrimaryKeyAsBoxedLong obj = new PrimaryKeyAsBoxedLong();
obj.setId(idValue);
obj.setName(String.valueOf(idValue));
testRealm.copyToRealm(obj);
}
testRealm.commitTransaction();
}
use of io.realm.entities.PrimaryKeyAsBoxedLong in project realm-java by realm.
the class TestHelper method addLongPrimaryKeyObjectToTestRealm.
/**
* Adds a Long type PrimaryKey object to a realm with values for id field (PrimaryKey) and name field
*/
public static PrimaryKeyAsBoxedLong addLongPrimaryKeyObjectToTestRealm(Realm testRealm, Long primaryFieldValue, String secondaryFieldValue) {
testRealm.beginTransaction();
PrimaryKeyAsBoxedLong obj = new PrimaryKeyAsBoxedLong();
obj.setId(primaryFieldValue);
obj.setName(secondaryFieldValue);
testRealm.copyToRealm(obj);
testRealm.commitTransaction();
return obj;
}
use of io.realm.entities.PrimaryKeyAsBoxedLong in project realm-java by realm.
the class RealmTests method copyToRealmOrUpdate_boxedLongPrimaryKeyFieldIsNull.
@Test
public void copyToRealmOrUpdate_boxedLongPrimaryKeyFieldIsNull() {
final String SECONDARY_FIELD_VALUE = "nullLongPrimaryKeyObj";
final String SECONDARY_FIELD_UPDATED = "nullLongPrimaryKeyObjUpdated";
PrimaryKeyAsBoxedLong nullPrimaryKeyObj = TestHelper.addLongPrimaryKeyObjectToTestRealm(realm, (Long) null, SECONDARY_FIELD_VALUE);
RealmResults<PrimaryKeyAsBoxedLong> result = realm.where(PrimaryKeyAsBoxedLong.class).findAll();
assertEquals(1, result.size());
assertEquals(SECONDARY_FIELD_VALUE, result.first().getName());
assertEquals(null, result.first().getId());
// Updates objects.
realm.beginTransaction();
nullPrimaryKeyObj.setName(SECONDARY_FIELD_UPDATED);
realm.copyToRealmOrUpdate(nullPrimaryKeyObj);
realm.commitTransaction();
assertEquals(SECONDARY_FIELD_UPDATED, realm.where(PrimaryKeyAsBoxedLong.class).findFirst().getName());
}
use of io.realm.entities.PrimaryKeyAsBoxedLong in project realm-java by realm.
the class RealmTests method createObjectWithPrimaryKey_null.
@Test
public void createObjectWithPrimaryKey_null() {
// Byte
realm.beginTransaction();
PrimaryKeyAsBoxedByte primaryKeyAsBoxedByte = realm.createObject(PrimaryKeyAsBoxedByte.class, null);
realm.commitTransaction();
assertEquals(1, realm.where(PrimaryKeyAsBoxedByte.class).count());
assertNull(primaryKeyAsBoxedByte.getId());
// Short
realm.beginTransaction();
PrimaryKeyAsBoxedShort primaryKeyAsBoxedShort = realm.createObject(PrimaryKeyAsBoxedShort.class, null);
realm.commitTransaction();
assertEquals(1, realm.where(PrimaryKeyAsBoxedShort.class).count());
assertNull(primaryKeyAsBoxedShort.getId());
// Integer
realm.beginTransaction();
PrimaryKeyAsBoxedInteger primaryKeyAsBoxedInteger = realm.createObject(PrimaryKeyAsBoxedInteger.class, null);
realm.commitTransaction();
assertEquals(1, realm.where(PrimaryKeyAsBoxedInteger.class).count());
assertNull(primaryKeyAsBoxedInteger.getId());
// Long
realm.beginTransaction();
PrimaryKeyAsBoxedLong primaryKeyAsBoxedLong = realm.createObject(PrimaryKeyAsBoxedLong.class, null);
realm.commitTransaction();
assertEquals(1, realm.where(PrimaryKeyAsBoxedLong.class).count());
assertNull(primaryKeyAsBoxedLong.getId());
// String
realm.beginTransaction();
PrimaryKeyAsString primaryKeyAsString = realm.createObject(PrimaryKeyAsString.class, null);
realm.commitTransaction();
assertEquals(1, realm.where(PrimaryKeyAsString.class).count());
assertNull(primaryKeyAsString.getName());
}
use of io.realm.entities.PrimaryKeyAsBoxedLong in project realm-java by realm.
the class RealmTests method copyToRealm_duplicatedNullPrimaryKeyThrows.
@Test
public void copyToRealm_duplicatedNullPrimaryKeyThrows() {
final String[] PRIMARY_KEY_TYPES = { "String", "BoxedByte", "BoxedShort", "BoxedInteger", "BoxedLong" };
TestHelper.addStringPrimaryKeyObjectToTestRealm(realm, (String) null, 0);
TestHelper.addBytePrimaryKeyObjectToTestRealm(realm, (Byte) null, (String) null);
TestHelper.addShortPrimaryKeyObjectToTestRealm(realm, (Short) null, (String) null);
TestHelper.addIntegerPrimaryKeyObjectToTestRealm(realm, (Integer) null, (String) null);
TestHelper.addLongPrimaryKeyObjectToTestRealm(realm, (Long) null, (String) null);
for (String className : PRIMARY_KEY_TYPES) {
try {
realm.beginTransaction();
switch(className) {
case "String":
realm.copyToRealm(new PrimaryKeyAsString());
break;
case "BoxedByte":
realm.copyToRealm(new PrimaryKeyAsBoxedByte());
break;
case "BoxedShort":
realm.copyToRealm(new PrimaryKeyAsBoxedShort());
break;
case "BoxedInteger":
realm.copyToRealm(new PrimaryKeyAsBoxedInteger());
break;
case "BoxedLong":
realm.copyToRealm(new PrimaryKeyAsBoxedLong());
break;
default:
}
fail("Null value as primary key already exists.");
} catch (RealmPrimaryKeyConstraintException expected) {
assertEquals("Value already exists: null", expected.getMessage());
} finally {
realm.cancelTransaction();
}
}
}
Aggregations