Search in sources :

Example 1 with RealmPrimaryKeyConstraintException

use of io.realm.exceptions.RealmPrimaryKeyConstraintException in project realm-java by realm.

the class BulkInsertTests method insert_duplicatedPrimaryKeyFails.

@Test
public void insert_duplicatedPrimaryKeyFails() {
    // Single object with 2 references to two objects with the same ID
    AllJavaTypes obj = new AllJavaTypes(2);
    obj.setFieldList(new RealmList<AllJavaTypes>(new AllJavaTypes(1), new AllJavaTypes(1)));
    realm.beginTransaction();
    try {
        realm.insert(obj);
        fail();
    } catch (RealmPrimaryKeyConstraintException ignored) {
    } finally {
        realm.cancelTransaction();
    }
    // Two objects with the same ID in a list
    realm.beginTransaction();
    try {
        realm.insert(Arrays.asList(new AllJavaTypes(1), new AllJavaTypes(1)));
        fail();
    } catch (RealmPrimaryKeyConstraintException ignored) {
    } finally {
        realm.cancelTransaction();
    }
}
Also used : RealmPrimaryKeyConstraintException(io.realm.exceptions.RealmPrimaryKeyConstraintException) AllJavaTypes(io.realm.entities.AllJavaTypes) Test(org.junit.Test)

Example 2 with RealmPrimaryKeyConstraintException

use of io.realm.exceptions.RealmPrimaryKeyConstraintException 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 3 with RealmPrimaryKeyConstraintException

use of io.realm.exceptions.RealmPrimaryKeyConstraintException in project realm-java by realm.

the class RealmTests method copyToRealm_duplicatedPrimaryKeyThrows.

@Test
public void copyToRealm_duplicatedPrimaryKeyThrows() {
    final String[] PRIMARY_KEY_TYPES = { "String", "BoxedLong", "long" };
    for (String className : PRIMARY_KEY_TYPES) {
        String expectedKey = null;
        try {
            realm.beginTransaction();
            switch(className) {
                case "String":
                    {
                        expectedKey = "foo";
                        PrimaryKeyAsString obj = new PrimaryKeyAsString("foo");
                        realm.copyToRealm(obj);
                        realm.copyToRealm(obj);
                        break;
                    }
                case "BoxedLong":
                    {
                        expectedKey = Long.toString(Long.MIN_VALUE);
                        PrimaryKeyAsBoxedLong obj = new PrimaryKeyAsBoxedLong(Long.MIN_VALUE, "boxedlong");
                        realm.copyToRealm(obj);
                        realm.copyToRealm(obj);
                        break;
                    }
                case "long":
                    expectedKey = Long.toString(Long.MAX_VALUE);
                    PrimaryKeyAsLong obj = new PrimaryKeyAsLong(Long.MAX_VALUE);
                    realm.copyToRealm(obj);
                    realm.copyToRealm(obj);
                    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 '" + expectedKey + "'"));
        } finally {
            realm.cancelTransaction();
        }
    }
}
Also used : RealmPrimaryKeyConstraintException(io.realm.exceptions.RealmPrimaryKeyConstraintException) PrimaryKeyAsLong(io.realm.entities.PrimaryKeyAsLong) 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

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