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