Search in sources :

Example 11 with PrimaryKeyAsString

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

PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)11 Test (org.junit.Test)9 PrimaryKeyAsBoxedShort (io.realm.entities.PrimaryKeyAsBoxedShort)3 JSONObject (org.json.JSONObject)3 PrimaryKeyAsBoxedByte (io.realm.entities.PrimaryKeyAsBoxedByte)2 PrimaryKeyAsBoxedInteger (io.realm.entities.PrimaryKeyAsBoxedInteger)2 PrimaryKeyAsBoxedLong (io.realm.entities.PrimaryKeyAsBoxedLong)2 Table (io.realm.internal.Table)2 PrimaryKeyRequiredAsString (io.realm.entities.PrimaryKeyRequiredAsString)1 RealmPrimaryKeyConstraintException (io.realm.exceptions.RealmPrimaryKeyConstraintException)1