Search in sources :

Example 1 with PrimaryKeyAsBoxedLong

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();
}
Also used : PrimaryKeyAsBoxedLong(io.realm.entities.PrimaryKeyAsBoxedLong)

Example 2 with PrimaryKeyAsBoxedLong

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;
}
Also used : PrimaryKeyAsBoxedLong(io.realm.entities.PrimaryKeyAsBoxedLong)

Example 3 with PrimaryKeyAsBoxedLong

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());
}
Also used : PrimaryKeyRequiredAsString(io.realm.entities.PrimaryKeyRequiredAsString) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) PrimaryKeyAsBoxedLong(io.realm.entities.PrimaryKeyAsBoxedLong) Test(org.junit.Test)

Example 4 with PrimaryKeyAsBoxedLong

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());
}
Also used : PrimaryKeyAsBoxedShort(io.realm.entities.PrimaryKeyAsBoxedShort) PrimaryKeyAsBoxedByte(io.realm.entities.PrimaryKeyAsBoxedByte) PrimaryKeyAsBoxedInteger(io.realm.entities.PrimaryKeyAsBoxedInteger) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) PrimaryKeyAsBoxedLong(io.realm.entities.PrimaryKeyAsBoxedLong) Test(org.junit.Test)

Example 5 with PrimaryKeyAsBoxedLong

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

Aggregations

PrimaryKeyAsBoxedLong (io.realm.entities.PrimaryKeyAsBoxedLong)5 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)3 Test (org.junit.Test)3 PrimaryKeyAsBoxedByte (io.realm.entities.PrimaryKeyAsBoxedByte)2 PrimaryKeyAsBoxedInteger (io.realm.entities.PrimaryKeyAsBoxedInteger)2 PrimaryKeyAsBoxedShort (io.realm.entities.PrimaryKeyAsBoxedShort)2 PrimaryKeyRequiredAsString (io.realm.entities.PrimaryKeyRequiredAsString)2 RealmPrimaryKeyConstraintException (io.realm.exceptions.RealmPrimaryKeyConstraintException)1