Search in sources :

Example 1 with PrimaryKeyAsBoxedByte

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

the class TestHelper method populateTestRealmWithBytePrimaryKey.

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

Example 2 with PrimaryKeyAsBoxedByte

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

the class TestHelper method addBytePrimaryKeyObjectToTestRealm.

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

Example 3 with PrimaryKeyAsBoxedByte

use of io.realm.entities.PrimaryKeyAsBoxedByte 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)

Example 4 with PrimaryKeyAsBoxedByte

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

the class RealmTests method copyToRealmOrUpdate_boxedBytePrimaryKeyFieldIsNull.

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

Example 5 with PrimaryKeyAsBoxedByte

use of io.realm.entities.PrimaryKeyAsBoxedByte 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) UiThreadTest(androidx.test.annotation.UiThreadTest)

Aggregations

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