@Test public void createObject_duplicatedNullPrimaryKeyThrows() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { addPrimaryKeyObjectToTestRealm(realm); realm.beginTransaction(); try { realm.createObject(testClazz, null); fail("Null value as primary key already exists."); } catch (RealmPrimaryKeyConstraintException expected) { assertTrue("Exception message is: " + expected.getMessage(), expected.getMessage().contains("Primary key value already exists: 'null' .")); } finally { realm.cancelTransaction(); } }
@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) { assertTrue("Exception message is: " + expected.getMessage(), expected.getMessage().contains("Primary key value already exists: 'null' .")); } finally { realm.cancelTransaction(); } } }
@Test public void createObjectWithPrimaryKey_valueAlreadyExists() { realm.beginTransaction(); realm.createObject(AllJavaTypes.class, 42); try { realm.createObject(AllJavaTypes.class, 42); fail(); } catch (RealmPrimaryKeyConstraintException ignored) { } }
@Test public void primaryKey_checkPrimaryKeyOnCreate() { realm.beginTransaction(); try { realm.createObject(AnnotationTypes.class, 0); fail("Two empty objects cannot be created on the same table if a primary key is defined"); } catch (RealmPrimaryKeyConstraintException ignored) { } finally { realm.cancelTransaction(); } }
@Test public void primaryKey_errorOnInsertingSameObject() { try { realm.beginTransaction(); realm.createObject(AnnotationTypes.class, 1); realm.createObject(AnnotationTypes.class, 1); fail("Inserting two objects with same primary key should fail"); } catch (RealmPrimaryKeyConstraintException ignored) { } finally { realm.cancelTransaction(); } }
public int create(Friend friend) { mRealm.beginTransaction(); try { } catch (IllegalArgumentException | RealmPrimaryKeyConstraintException e) { mRealm.cancelTransaction(); throw e; } mRealm.commitTransaction(); return SUCCESS_OPERATION; }
public int create(Product product) { mRealm.beginTransaction(); try { } catch (IllegalArgumentException | RealmPrimaryKeyConstraintException e) { mRealm.cancelTransaction(); throw e; } mRealm.commitTransaction(); return SUCCESS_OPERATION; }
/** * Throws a properly formatted exception when multiple objects with the same primary key * value is detected. * * @param value the primary key value. */ public static void throwDuplicatePrimaryKeyException(Object value) { throw new RealmPrimaryKeyConstraintException("Value already exists: " + value); }