Search in sources :

Example 1 with PrimaryKeyAsBoxedShort

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

the class TestHelper method populateTestRealmWithShortPrimaryKey.

/**
 * Populates a realm with Short 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 populateTestRealmWithShortPrimaryKey(Realm testRealm, Short primaryFieldValue, String secondaryFieldValue, int numberOfPopulation, int iteratorBeginValue) {
    testRealm.beginTransaction();
    PrimaryKeyAsBoxedShort userObj = new PrimaryKeyAsBoxedShort();
    userObj.setId(primaryFieldValue);
    userObj.setName(secondaryFieldValue);
    testRealm.copyToRealm(userObj);
    short idValue = (short) iteratorBeginValue;
    for (int i = 0; i < numberOfPopulation - 1; ++i, ++idValue) {
        PrimaryKeyAsBoxedShort obj = new PrimaryKeyAsBoxedShort();
        obj.setId(idValue);
        obj.setName(String.valueOf(idValue));
        testRealm.copyToRealm(obj);
    }
    testRealm.commitTransaction();
}
Also used : PrimaryKeyAsBoxedShort(io.realm.entities.PrimaryKeyAsBoxedShort)

Example 2 with PrimaryKeyAsBoxedShort

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

the class BulkInsertTests method insert_nullPrimaryKey.

@Test
public void insert_nullPrimaryKey() {
    PrimaryKeyAsString primaryKeyAsString = new PrimaryKeyAsString();
    primaryKeyAsString.setId(19);
    realm.beginTransaction();
    realm.insertOrUpdate(primaryKeyAsString);
    realm.commitTransaction();
    primaryKeyAsString = realm.where(PrimaryKeyAsString.class).isNull("name").findFirst();
    assertNotNull(primaryKeyAsString);
    assertNull(primaryKeyAsString.getName());
    assertEquals(19, primaryKeyAsString.getId());
    PrimaryKeyAsBoxedShort primaryKeyAsShort = new PrimaryKeyAsBoxedShort();
    primaryKeyAsShort.setName("42");
    realm.beginTransaction();
    realm.insertOrUpdate(primaryKeyAsShort);
    realm.commitTransaction();
    primaryKeyAsShort = realm.where(PrimaryKeyAsBoxedShort.class).isNull("id").findFirst();
    assertNotNull(primaryKeyAsShort);
    assertNull(primaryKeyAsShort.getId());
    assertEquals("42", primaryKeyAsShort.getName());
}
Also used : PrimaryKeyAsBoxedShort(io.realm.entities.PrimaryKeyAsBoxedShort) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Test(org.junit.Test)

Example 3 with PrimaryKeyAsBoxedShort

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

the class RealmTests method copyToRealmOrUpdate_boxedShortPrimaryKeyFieldIsNull.

@Test
public void copyToRealmOrUpdate_boxedShortPrimaryKeyFieldIsNull() {
    final String SECONDARY_FIELD_VALUE = "nullShortPrimaryKeyObj";
    final String SECONDARY_FIELD_UPDATED = "nullShortPrimaryKeyObjUpdated";
    PrimaryKeyAsBoxedShort nullPrimaryKeyObj = TestHelper.addShortPrimaryKeyObjectToTestRealm(realm, (Short) null, SECONDARY_FIELD_VALUE);
    RealmResults<PrimaryKeyAsBoxedShort> result = realm.where(PrimaryKeyAsBoxedShort.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(PrimaryKeyAsBoxedShort.class).findFirst().getName());
}
Also used : PrimaryKeyAsBoxedShort(io.realm.entities.PrimaryKeyAsBoxedShort) PrimaryKeyRequiredAsString(io.realm.entities.PrimaryKeyRequiredAsString) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 4 with PrimaryKeyAsBoxedShort

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

the class TestHelper method addShortPrimaryKeyObjectToTestRealm.

/**
 * Adds a Short type PrimaryKey object to a realm with values for id field (PrimaryKey) and name field
 */
public static PrimaryKeyAsBoxedShort addShortPrimaryKeyObjectToTestRealm(Realm testRealm, Short primaryFieldValue, String secondaryFieldValue) {
    testRealm.beginTransaction();
    PrimaryKeyAsBoxedShort obj = new PrimaryKeyAsBoxedShort();
    obj.setId(primaryFieldValue);
    obj.setName(secondaryFieldValue);
    testRealm.copyToRealm(obj);
    testRealm.commitTransaction();
    return obj;
}
Also used : PrimaryKeyAsBoxedShort(io.realm.entities.PrimaryKeyAsBoxedShort)

Example 5 with PrimaryKeyAsBoxedShort

use of io.realm.entities.PrimaryKeyAsBoxedShort 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, but wasn't detected correctly");
        } catch (RealmPrimaryKeyConstraintException expected) {
            assertTrue("Exception message is: " + expected.getMessage(), expected.getMessage().contains("with an existing primary key value 'null'"));
        } finally {
            realm.cancelTransaction();
        }
    }
}
Also used : RealmPrimaryKeyConstraintException(io.realm.exceptions.RealmPrimaryKeyConstraintException) PrimaryKeyAsBoxedShort(io.realm.entities.PrimaryKeyAsBoxedShort) PrimaryKeyAsBoxedByte(io.realm.entities.PrimaryKeyAsBoxedByte) PrimaryKeyAsBoxedInteger(io.realm.entities.PrimaryKeyAsBoxedInteger) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) PrimaryKeyRequiredAsString(io.realm.entities.PrimaryKeyRequiredAsString) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) PrimaryKeyAsBoxedLong(io.realm.entities.PrimaryKeyAsBoxedLong) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Aggregations

PrimaryKeyAsBoxedShort (io.realm.entities.PrimaryKeyAsBoxedShort)6 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)4 Test (org.junit.Test)4 UiThreadTest (androidx.test.annotation.UiThreadTest)3 PrimaryKeyAsBoxedByte (io.realm.entities.PrimaryKeyAsBoxedByte)2 PrimaryKeyAsBoxedInteger (io.realm.entities.PrimaryKeyAsBoxedInteger)2 PrimaryKeyAsBoxedLong (io.realm.entities.PrimaryKeyAsBoxedLong)2 PrimaryKeyRequiredAsString (io.realm.entities.PrimaryKeyRequiredAsString)2 RealmPrimaryKeyConstraintException (io.realm.exceptions.RealmPrimaryKeyConstraintException)1