Search in sources :

Example 1 with DefaultValueOfField

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

the class RealmJsonTests method createFromJson_respectDefaultValues.

@Test
public void createFromJson_respectDefaultValues() throws JSONException {
    final long fieldLongPrimaryKeyValue = DefaultValueOfField.FIELD_LONG_PRIMARY_KEY_DEFAULT_VALUE + 1;
    // Step 1: Prepares almost empty JSON.
    final JSONObject json = new JSONObject();
    json.put(DefaultValueOfField.FIELD_LONG_PRIMARY_KEY, fieldLongPrimaryKeyValue);
    // Step 2: Updates with almost empty JSONObject.
    realm.beginTransaction();
    final DefaultValueOfField managedObj = realm.createOrUpdateObjectFromJson(DefaultValueOfField.class, json);
    realm.commitTransaction();
    // Step 3: Checks that default values are applied.
    assertEquals(DefaultValueOfField.FIELD_IGNORED_DEFAULT_VALUE, managedObj.getFieldIgnored());
    assertEquals(DefaultValueOfField.FIELD_STRING_DEFAULT_VALUE, managedObj.getFieldString());
    assertFalse(Util.isEmptyString(managedObj.getFieldRandomString()));
    assertEquals(DefaultValueOfField.FIELD_SHORT_DEFAULT_VALUE, managedObj.getFieldShort());
    assertEquals(DefaultValueOfField.FIELD_INT_DEFAULT_VALUE, managedObj.getFieldInt());
    assertEquals(fieldLongPrimaryKeyValue, managedObj.getFieldLongPrimaryKey());
    assertEquals(DefaultValueOfField.FIELD_LONG_DEFAULT_VALUE, managedObj.getFieldLong());
    assertEquals(DefaultValueOfField.FIELD_BYTE_DEFAULT_VALUE, managedObj.getFieldByte());
    assertEquals(DefaultValueOfField.FIELD_FLOAT_DEFAULT_VALUE, managedObj.getFieldFloat(), 0f);
    assertEquals(DefaultValueOfField.FIELD_DOUBLE_DEFAULT_VALUE, managedObj.getFieldDouble(), 0d);
    assertEquals(DefaultValueOfField.FIELD_BOOLEAN_DEFAULT_VALUE, managedObj.isFieldBoolean());
    assertEquals(DefaultValueOfField.FIELD_DATE_DEFAULT_VALUE, managedObj.getFieldDate());
    assertEquals(DefaultValueOfField.FIELD_OBJECT_ID_DEFAULT_VALUE, managedObj.getFieldObjectId());
    assertEquals(DefaultValueOfField.FIELD_DECIMAL128_DEFAULT_VALUE, managedObj.getFieldDecimal128());
    assertEquals(DefaultValueOfField.FIELD_UUID_DEFAULT_VALUE, managedObj.getFieldUUID());
    assertEquals(DefaultValueOfField.FIELD_REALM_ANY_DEFAULT_VALUE, managedObj.getFieldRealmAny());
    assertArrayEquals(DefaultValueOfField.FIELD_BINARY_DEFAULT_VALUE, managedObj.getFieldBinary());
    assertArrayEquals(DefaultValueOfField.FIELD_BYTE_LIST_DEFAULT_VALUE.toArray(), managedObj.getFieldByteList().toArray());
    assertArrayEquals(DefaultValueOfField.FIELD_SHORT_LIST_DEFAULT_VALUE.toArray(), managedObj.getFieldShortList().toArray());
    assertArrayEquals(DefaultValueOfField.FIELD_INTEGER_LIST_DEFAULT_VALUE.toArray(), managedObj.getFieldIntegerList().toArray());
    assertArrayEquals(DefaultValueOfField.FIELD_LONG_LIST_DEFAULT_VALUE.toArray(), managedObj.getFieldLongList().toArray());
    assertArrayEquals(DefaultValueOfField.FIELD_BOOLEAN_LIST_DEFAULT_VALUE.toArray(), managedObj.getFieldBooleanList().toArray());
    assertArrayEquals(DefaultValueOfField.FIELD_BINARY_LIST_DEFAULT_VALUE.toArray(), managedObj.getFieldBinaryList().toArray());
    assertArrayEquals(DefaultValueOfField.FIELD_STRING_LIST_DEFAULT_VALUE.toArray(), managedObj.getFieldStringList().toArray());
    assertArrayEquals(DefaultValueOfField.FIELD_DATE_LIST_DEFAULT_VALUE.toArray(), managedObj.getFieldDateList().toArray());
    assertArrayEquals(DefaultValueOfField.FIELD_OBJECT_ID_LIST_DEFAULT_VALUE.toArray(), managedObj.getFieldObjectIdList().toArray());
    assertArrayEquals(DefaultValueOfField.FIELD_DECIMAL128_LIST_DEFAULT_VALUE.toArray(), managedObj.getFieldDecimal128List().toArray());
    assertArrayEquals(DefaultValueOfField.FIELD_UUID_LIST_DEFAULT_VALUE.toArray(), managedObj.getFieldUUIDList().toArray());
    assertArrayEquals(DefaultValueOfField.FIELD_REALM_ANY_LIST_DEFAULT_VALUE.toArray(), managedObj.getFieldRealmAnyList().toArray());
    assertEquals(RandomPrimaryKey.FIELD_INT_DEFAULT_VALUE, managedObj.getFieldObject().getFieldInt());
    assertEquals(1, managedObj.getFieldList().size());
    assertEquals(RandomPrimaryKey.FIELD_INT_DEFAULT_VALUE, managedObj.getFieldList().first().getFieldInt());
    // Makes sure that excess object by default value is not created.
    assertEquals(2, realm.where(RandomPrimaryKey.class).count());
}
Also used : JSONObject(org.json.JSONObject) DefaultValueOfField(io.realm.entities.DefaultValueOfField) Test(org.junit.Test)

Example 2 with DefaultValueOfField

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

the class RealmJsonTests method createFromJson_defaultValuesAreIgnored.

@Test
public void createFromJson_defaultValuesAreIgnored() throws JSONException {
    final long fieldLongPrimaryKeyValue = DefaultValueOfField.FIELD_LONG_PRIMARY_KEY_DEFAULT_VALUE + 1;
    // Step 1: Prepares JSON.
    final String fieldIgnoredValue = DefaultValueOfField.FIELD_IGNORED_DEFAULT_VALUE + ".modified";
    final String fieldStringValue = DefaultValueOfField.FIELD_STRING_DEFAULT_VALUE + ".modified";
    final String fieldRandomStringValue = "non-random";
    final short fieldShortValue = (short) (DefaultValueOfField.FIELD_SHORT_DEFAULT_VALUE + 1);
    final int fieldIntValue = DefaultValueOfField.FIELD_INT_DEFAULT_VALUE + 1;
    final long fieldLongValue = DefaultValueOfField.FIELD_LONG_DEFAULT_VALUE + 1;
    final byte fieldByteValue = (byte) (DefaultValueOfField.FIELD_BYTE_DEFAULT_VALUE + 1);
    final float fieldFloatValue = DefaultValueOfField.FIELD_FLOAT_DEFAULT_VALUE + 1;
    final double fieldDoubleValue = DefaultValueOfField.FIELD_DOUBLE_DEFAULT_VALUE + 1;
    final boolean fieldBooleanValue = !DefaultValueOfField.FIELD_BOOLEAN_DEFAULT_VALUE;
    final Date fieldDateValue = new Date(DefaultValueOfField.FIELD_DATE_DEFAULT_VALUE.getTime() + 1);
    final ObjectId fieldObjectIdValue = new ObjectId(new Date(20));
    final Decimal128 fieldDecimal128Value = new Decimal128(20);
    final UUID fieldUUIDValue = UUID.randomUUID();
    final RealmAny fieldRealmAnyValue = RealmAny.valueOf((float) 20);
    final byte[] fieldBinaryValue = { (byte) (DefaultValueOfField.FIELD_BINARY_DEFAULT_VALUE[0] - 1) };
    final int fieldObjectIntValue = RandomPrimaryKey.FIELD_INT_DEFAULT_VALUE + 1;
    final int fieldListIntValue = RandomPrimaryKey.FIELD_INT_DEFAULT_VALUE + 2;
    final JSONObject json = new JSONObject();
    json.put(DefaultValueOfField.FIELD_LONG_PRIMARY_KEY, fieldLongPrimaryKeyValue);
    json.put(DefaultValueOfField.FIELD_IGNORED, fieldIgnoredValue);
    json.put(DefaultValueOfField.FIELD_STRING, fieldStringValue);
    json.put(DefaultValueOfField.FIELD_RANDOM_STRING, fieldRandomStringValue);
    json.put(DefaultValueOfField.FIELD_SHORT, fieldShortValue);
    json.put(DefaultValueOfField.FIELD_INT, fieldIntValue);
    json.put(DefaultValueOfField.FIELD_LONG, fieldLongValue);
    json.put(DefaultValueOfField.FIELD_BYTE, fieldByteValue);
    json.put(DefaultValueOfField.FIELD_FLOAT, fieldFloatValue);
    json.put(DefaultValueOfField.FIELD_DOUBLE, fieldDoubleValue);
    json.put(DefaultValueOfField.FIELD_BOOLEAN, fieldBooleanValue);
    json.put(DefaultValueOfField.FIELD_DATE, getISO8601Date(fieldDateValue));
    json.put(DefaultValueOfField.FIELD_OBJECT_ID, fieldObjectIdValue);
    json.put(DefaultValueOfField.FIELD_DECIMAL128, fieldDecimal128Value);
    json.put(DefaultValueOfField.FIELD_UUID, fieldUUIDValue);
    json.put(DefaultValueOfField.FIELD_REALM_ANY, fieldRealmAnyValue);
    json.put(DefaultValueOfField.FIELD_BINARY, Base64.encodeToString(fieldBinaryValue, Base64.DEFAULT));
    // Value for 'fieldObject'
    final JSONObject fieldObjectJson = new JSONObject();
    fieldObjectJson.put(RandomPrimaryKey.FIELD_RANDOM_PRIMARY_KEY, "pk of fieldObject");
    fieldObjectJson.put(RandomPrimaryKey.FIELD_INT, fieldObjectIntValue);
    json.put(DefaultValueOfField.FIELD_OBJECT, fieldObjectJson);
    // Value for 'fieldList'
    final JSONArray fieldListArrayJson = new JSONArray();
    final JSONObject fieldListItem0Json = new JSONObject();
    fieldListItem0Json.put(RandomPrimaryKey.FIELD_RANDOM_PRIMARY_KEY, "pk1 of fieldList");
    fieldListItem0Json.put(RandomPrimaryKey.FIELD_INT, fieldListIntValue);
    fieldListArrayJson.put(fieldListItem0Json);
    final JSONObject fieldListItem1Json = new JSONObject();
    fieldListItem1Json.put(RandomPrimaryKey.FIELD_RANDOM_PRIMARY_KEY, "pk2 of fieldList");
    fieldListItem1Json.put(RandomPrimaryKey.FIELD_INT, fieldListIntValue + 1);
    fieldListArrayJson.put(fieldListItem1Json);
    json.put(DefaultValueOfField.FIELD_LIST, fieldListArrayJson);
    // Step 3: Updates with JSONObject.
    realm.beginTransaction();
    final DefaultValueOfField managedObj = realm.createOrUpdateObjectFromJson(DefaultValueOfField.class, json);
    realm.commitTransaction();
    // Step 4: Checks that properly created.
    assertEquals(DefaultValueOfField.FIELD_IGNORED_DEFAULT_VALUE, /* not fieldIgnoredValue */
    managedObj.getFieldIgnored());
    assertEquals(fieldStringValue, managedObj.getFieldString());
    assertEquals(fieldRandomStringValue, managedObj.getFieldRandomString());
    assertEquals(fieldShortValue, managedObj.getFieldShort());
    assertEquals(fieldIntValue, managedObj.getFieldInt());
    assertEquals(fieldLongPrimaryKeyValue, managedObj.getFieldLongPrimaryKey());
    assertEquals(fieldLongValue, managedObj.getFieldLong());
    assertEquals(fieldByteValue, managedObj.getFieldByte());
    assertEquals(fieldFloatValue, managedObj.getFieldFloat(), 0f);
    assertEquals(fieldDoubleValue, managedObj.getFieldDouble(), 0d);
    assertEquals(fieldBooleanValue, managedObj.isFieldBoolean());
    assertEquals(fieldObjectIdValue, managedObj.getFieldObjectId());
    assertEquals(fieldDecimal128Value, managedObj.getFieldDecimal128());
    assertEquals(fieldUUIDValue, managedObj.getFieldUUID());
    assertEquals(fieldRealmAnyValue, managedObj.getFieldRealmAny());
    assertEquals(fieldDateValue, managedObj.getFieldDate());
    assertTrue(Arrays.equals(fieldBinaryValue, managedObj.getFieldBinary()));
    assertEquals(fieldObjectJson.getString(RandomPrimaryKey.FIELD_RANDOM_PRIMARY_KEY), managedObj.getFieldObject().getFieldRandomPrimaryKey());
    assertEquals(fieldObjectIntValue, managedObj.getFieldObject().getFieldInt());
    assertEquals(2, managedObj.getFieldList().size());
    assertEquals(fieldListItem0Json.get(RandomPrimaryKey.FIELD_RANDOM_PRIMARY_KEY), managedObj.getFieldList().get(0).getFieldRandomPrimaryKey());
    assertEquals(fieldListIntValue, managedObj.getFieldList().get(0).getFieldInt());
    assertEquals(fieldListItem1Json.get(RandomPrimaryKey.FIELD_RANDOM_PRIMARY_KEY), managedObj.getFieldList().get(1).getFieldRandomPrimaryKey());
    assertEquals(fieldListIntValue + 1, managedObj.getFieldList().get(1).getFieldInt());
    // Makes sure that excess object by default value is not created.
    assertEquals(3, realm.where(RandomPrimaryKey.class).count());
}
Also used : PrimaryKeyAsObjectId(io.realm.entities.PrimaryKeyAsObjectId) ObjectId(org.bson.types.ObjectId) JSONArray(org.json.JSONArray) Decimal128(org.bson.types.Decimal128) Date(java.util.Date) JSONObject(org.json.JSONObject) UUID(java.util.UUID) DefaultValueOfField(io.realm.entities.DefaultValueOfField) Test(org.junit.Test)

Example 3 with DefaultValueOfField

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

the class RealmTests method copyToRealm_defaultValuesAreIgnored.

@Test
public void copyToRealm_defaultValuesAreIgnored() {
    final String fieldIgnoredValue = DefaultValueOfField.FIELD_IGNORED_DEFAULT_VALUE + ".modified";
    final String fieldStringValue = DefaultValueOfField.FIELD_STRING_DEFAULT_VALUE + ".modified";
    final String fieldRandomStringValue = "non-random";
    final short fieldShortValue = (short) (DefaultValueOfField.FIELD_SHORT_DEFAULT_VALUE + 1);
    final int fieldIntValue = DefaultValueOfField.FIELD_INT_DEFAULT_VALUE + 1;
    final long fieldLongPrimaryKeyValue = DefaultValueOfField.FIELD_LONG_PRIMARY_KEY_DEFAULT_VALUE + 1;
    final long fieldLongValue = DefaultValueOfField.FIELD_LONG_DEFAULT_VALUE + 1;
    final byte fieldByteValue = (byte) (DefaultValueOfField.FIELD_BYTE_DEFAULT_VALUE + 1);
    final float fieldFloatValue = DefaultValueOfField.FIELD_FLOAT_DEFAULT_VALUE + 1;
    final double fieldDoubleValue = DefaultValueOfField.FIELD_DOUBLE_DEFAULT_VALUE + 1;
    final boolean fieldBooleanValue = !DefaultValueOfField.FIELD_BOOLEAN_DEFAULT_VALUE;
    final Date fieldDateValue = new Date(DefaultValueOfField.FIELD_DATE_DEFAULT_VALUE.getTime() + 1);
    final byte[] fieldBinaryValue = { (byte) (DefaultValueOfField.FIELD_BINARY_DEFAULT_VALUE[0] - 1) };
    final int fieldObjectIntValue = RandomPrimaryKey.FIELD_INT_DEFAULT_VALUE + 1;
    final int fieldListIntValue = RandomPrimaryKey.FIELD_INT_DEFAULT_VALUE + 2;
    final DefaultValueOfField managedObj;
    realm.beginTransaction();
    {
        final DefaultValueOfField obj = new DefaultValueOfField();
        obj.setFieldIgnored(fieldIgnoredValue);
        obj.setFieldString(fieldStringValue);
        obj.setFieldRandomString(fieldRandomStringValue);
        obj.setFieldShort(fieldShortValue);
        obj.setFieldInt(fieldIntValue);
        obj.setFieldLongPrimaryKey(fieldLongPrimaryKeyValue);
        obj.setFieldLong(fieldLongValue);
        obj.setFieldByte(fieldByteValue);
        obj.setFieldFloat(fieldFloatValue);
        obj.setFieldDouble(fieldDoubleValue);
        obj.setFieldBoolean(fieldBooleanValue);
        obj.setFieldDate(fieldDateValue);
        obj.setFieldBinary(fieldBinaryValue);
        final RandomPrimaryKey fieldObjectValue = new RandomPrimaryKey();
        fieldObjectValue.setFieldInt(fieldObjectIntValue);
        obj.setFieldObject(fieldObjectValue);
        final RealmList<RandomPrimaryKey> list = new RealmList<RandomPrimaryKey>();
        final RandomPrimaryKey listItem = new RandomPrimaryKey();
        listItem.setFieldInt(fieldListIntValue);
        list.add(listItem);
        obj.setFieldList(list);
        obj.setFieldStringList(new RealmList<>("2", "3"));
        obj.setFieldBooleanList(new RealmList<>(true, false));
        obj.setFieldBinaryList(new RealmList<>(new byte[] { 2 }, new byte[] { 3 }));
        obj.setFieldLongList(new RealmList<>(2L, 3L));
        obj.setFieldIntegerList(new RealmList<>(2, 3));
        obj.setFieldShortList(new RealmList<>((short) 2, (short) 3));
        obj.setFieldByteList(new RealmList<>((byte) 2, (byte) 3));
        obj.setFieldDoubleList(new RealmList<>(2D, 3D));
        obj.setFieldFloatList(new RealmList<>(2F, 3F));
        obj.setFieldDateList(new RealmList<>(new Date(2L), new Date(3L)));
        managedObj = realm.copyToRealm(obj);
    }
    realm.commitTransaction();
    assertEquals(DefaultValueOfField.FIELD_IGNORED_DEFAULT_VALUE, /*not fieldIgnoredValue*/
    managedObj.getFieldIgnored());
    assertEquals(fieldStringValue, managedObj.getFieldString());
    assertEquals(fieldRandomStringValue, managedObj.getFieldRandomString());
    assertEquals(fieldShortValue, managedObj.getFieldShort());
    assertEquals(fieldIntValue, managedObj.getFieldInt());
    assertEquals(fieldLongPrimaryKeyValue, managedObj.getFieldLongPrimaryKey());
    assertEquals(fieldLongValue, managedObj.getFieldLong());
    assertEquals(fieldByteValue, managedObj.getFieldByte());
    assertEquals(fieldFloatValue, managedObj.getFieldFloat(), 0F);
    assertEquals(fieldDoubleValue, managedObj.getFieldDouble(), 0D);
    assertEquals(fieldBooleanValue, managedObj.isFieldBoolean());
    assertEquals(fieldDateValue, managedObj.getFieldDate());
    assertArrayEquals(fieldBinaryValue, managedObj.getFieldBinary());
    assertEquals(fieldObjectIntValue, managedObj.getFieldObject().getFieldInt());
    assertEquals(1, managedObj.getFieldList().size());
    assertEquals(fieldListIntValue, managedObj.getFieldList().first().getFieldInt());
    assertEquals(2, managedObj.getFieldStringList().size());
    assertEquals("2", managedObj.getFieldStringList().get(0));
    assertEquals("3", managedObj.getFieldStringList().get(1));
    assertEquals(2, managedObj.getFieldBooleanList().size());
    assertEquals(true, managedObj.getFieldBooleanList().get(0));
    assertEquals(false, managedObj.getFieldBooleanList().get(1));
    assertEquals(2, managedObj.getFieldBinaryList().size());
    assertArrayEquals(new byte[] { 2 }, managedObj.getFieldBinaryList().get(0));
    assertArrayEquals(new byte[] { 3 }, managedObj.getFieldBinaryList().get(1));
    assertEquals(2, managedObj.getFieldLongList().size());
    assertEquals((Long) 2L, managedObj.getFieldLongList().get(0));
    assertEquals((Long) 3L, managedObj.getFieldLongList().get(1));
    assertEquals(2, managedObj.getFieldIntegerList().size());
    assertEquals((Integer) 2, managedObj.getFieldIntegerList().get(0));
    assertEquals((Integer) 3, managedObj.getFieldIntegerList().get(1));
    assertEquals(2, managedObj.getFieldShortList().size());
    assertEquals((Short) (short) 2, managedObj.getFieldShortList().get(0));
    assertEquals((Short) (short) 3, managedObj.getFieldShortList().get(1));
    assertEquals(2, managedObj.getFieldByteList().size());
    assertEquals((Byte) (byte) 2, managedObj.getFieldByteList().get(0));
    assertEquals((Byte) (byte) 3, managedObj.getFieldByteList().get(1));
    assertEquals(2, managedObj.getFieldDoubleList().size());
    assertEquals((Double) 2D, managedObj.getFieldDoubleList().get(0));
    assertEquals((Double) 3D, managedObj.getFieldDoubleList().get(1));
    assertEquals(2, managedObj.getFieldFloatList().size());
    assertEquals((Float) 2F, managedObj.getFieldFloatList().get(0));
    assertEquals((Float) 3F, managedObj.getFieldFloatList().get(1));
    assertEquals(2, managedObj.getFieldDateList().size());
    assertEquals(new Date(2L), managedObj.getFieldDateList().get(0));
    assertEquals(new Date(3L), managedObj.getFieldDateList().get(1));
    // Makes sure that excess object by default value is not created.
    assertEquals(2, realm.where(RandomPrimaryKey.class).count());
}
Also used : PrimaryKeyRequiredAsString(io.realm.entities.PrimaryKeyRequiredAsString) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Date(java.util.Date) RandomPrimaryKey(io.realm.entities.RandomPrimaryKey) DefaultValueOfField(io.realm.entities.DefaultValueOfField) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 4 with DefaultValueOfField

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

the class RealmTests method copyFromRealm_defaultValuesAreIgnored.

@Test
public void copyFromRealm_defaultValuesAreIgnored() {
    final DefaultValueOfField managedObj;
    realm.beginTransaction();
    {
        final DefaultValueOfField obj = new DefaultValueOfField();
        obj.setFieldIgnored(DefaultValueOfField.FIELD_IGNORED_DEFAULT_VALUE + ".modified");
        obj.setFieldString(DefaultValueOfField.FIELD_STRING_DEFAULT_VALUE + ".modified");
        obj.setFieldRandomString("non-random");
        obj.setFieldShort((short) (DefaultValueOfField.FIELD_SHORT_DEFAULT_VALUE + 1));
        obj.setFieldInt(DefaultValueOfField.FIELD_INT_DEFAULT_VALUE + 1);
        obj.setFieldLongPrimaryKey(DefaultValueOfField.FIELD_LONG_PRIMARY_KEY_DEFAULT_VALUE + 1);
        obj.setFieldLong(DefaultValueOfField.FIELD_LONG_DEFAULT_VALUE + 1);
        obj.setFieldByte((byte) (DefaultValueOfField.FIELD_BYTE_DEFAULT_VALUE + 1));
        obj.setFieldFloat(DefaultValueOfField.FIELD_FLOAT_DEFAULT_VALUE + 1);
        obj.setFieldDouble(DefaultValueOfField.FIELD_DOUBLE_DEFAULT_VALUE + 1);
        obj.setFieldBoolean(!DefaultValueOfField.FIELD_BOOLEAN_DEFAULT_VALUE);
        obj.setFieldDate(new Date(DefaultValueOfField.FIELD_DATE_DEFAULT_VALUE.getTime() + 1));
        obj.setFieldBinary(new byte[] { (byte) (DefaultValueOfField.FIELD_BINARY_DEFAULT_VALUE[0] - 1) });
        final RandomPrimaryKey fieldObjectValue = new RandomPrimaryKey();
        fieldObjectValue.setFieldInt(RandomPrimaryKey.FIELD_INT_DEFAULT_VALUE + 1);
        obj.setFieldObject(fieldObjectValue);
        final RealmList<RandomPrimaryKey> list = new RealmList<RandomPrimaryKey>();
        final RandomPrimaryKey listItem = new RandomPrimaryKey();
        listItem.setFieldInt(RandomPrimaryKey.FIELD_INT_DEFAULT_VALUE + 2);
        list.add(listItem);
        obj.setFieldList(list);
        obj.setFieldStringList(new RealmList<>("2", "3"));
        obj.setFieldBooleanList(new RealmList<>(true, false));
        obj.setFieldBinaryList(new RealmList<>(new byte[] { 2 }, new byte[] { 3 }));
        obj.setFieldLongList(new RealmList<>(2L, 3L));
        obj.setFieldIntegerList(new RealmList<>(2, 3));
        obj.setFieldShortList(new RealmList<>((short) 2, (short) 3));
        obj.setFieldByteList(new RealmList<>((byte) 2, (byte) 3));
        obj.setFieldDoubleList(new RealmList<>(2D, 3D));
        obj.setFieldFloatList(new RealmList<>(2F, 3F));
        obj.setFieldDateList(new RealmList<>(new Date(2L), new Date(3L)));
        managedObj = realm.copyToRealm(obj);
    }
    realm.commitTransaction();
    final DefaultValueOfField copy = realm.copyFromRealm(managedObj);
    assertEquals(DefaultValueOfField.FIELD_IGNORED_DEFAULT_VALUE, copy.getFieldIgnored());
    assertEquals(managedObj.getFieldString(), copy.getFieldString());
    assertEquals(managedObj.getFieldRandomString(), copy.getFieldRandomString());
    assertEquals(managedObj.getFieldShort(), copy.getFieldShort());
    assertEquals(managedObj.getFieldInt(), copy.getFieldInt());
    assertEquals(managedObj.getFieldLongPrimaryKey(), copy.getFieldLongPrimaryKey());
    assertEquals(managedObj.getFieldLong(), copy.getFieldLong());
    assertEquals(managedObj.getFieldByte(), copy.getFieldByte());
    assertEquals(managedObj.getFieldFloat(), copy.getFieldFloat(), 0F);
    assertEquals(managedObj.getFieldDouble(), copy.getFieldDouble(), 0D);
    assertEquals(managedObj.isFieldBoolean(), copy.isFieldBoolean());
    assertEquals(managedObj.getFieldDate(), copy.getFieldDate());
    assertArrayEquals(managedObj.getFieldBinary(), copy.getFieldBinary());
    assertEquals(managedObj.getFieldObject().getFieldInt(), copy.getFieldObject().getFieldInt());
    assertEquals(1, copy.getFieldList().size());
    // noinspection ConstantConditions
    assertEquals(managedObj.getFieldList().first().getFieldInt(), copy.getFieldList().first().getFieldInt());
    assertEquals(2, managedObj.getFieldStringList().size());
    assertEquals("2", managedObj.getFieldStringList().get(0));
    assertEquals("3", managedObj.getFieldStringList().get(1));
    assertEquals(2, managedObj.getFieldBooleanList().size());
    assertEquals(true, managedObj.getFieldBooleanList().get(0));
    assertEquals(false, managedObj.getFieldBooleanList().get(1));
    assertEquals(2, managedObj.getFieldBinaryList().size());
    assertArrayEquals(new byte[] { 2 }, managedObj.getFieldBinaryList().get(0));
    assertArrayEquals(new byte[] { 3 }, managedObj.getFieldBinaryList().get(1));
    assertEquals(2, managedObj.getFieldLongList().size());
    assertEquals((Long) 2L, managedObj.getFieldLongList().get(0));
    assertEquals((Long) 3L, managedObj.getFieldLongList().get(1));
    assertEquals(2, managedObj.getFieldIntegerList().size());
    assertEquals((Integer) 2, managedObj.getFieldIntegerList().get(0));
    assertEquals((Integer) 3, managedObj.getFieldIntegerList().get(1));
    assertEquals(2, managedObj.getFieldShortList().size());
    assertEquals((Short) (short) 2, managedObj.getFieldShortList().get(0));
    assertEquals((Short) (short) 3, managedObj.getFieldShortList().get(1));
    assertEquals(2, managedObj.getFieldByteList().size());
    assertEquals((Byte) (byte) 2, managedObj.getFieldByteList().get(0));
    assertEquals((Byte) (byte) 3, managedObj.getFieldByteList().get(1));
    assertEquals(2, managedObj.getFieldDoubleList().size());
    assertEquals((Double) 2D, managedObj.getFieldDoubleList().get(0));
    assertEquals((Double) 3D, managedObj.getFieldDoubleList().get(1));
    assertEquals(2, managedObj.getFieldFloatList().size());
    assertEquals((Float) 2F, managedObj.getFieldFloatList().get(0));
    assertEquals((Float) 3F, managedObj.getFieldFloatList().get(1));
    assertEquals(2, managedObj.getFieldDateList().size());
    assertEquals(new Date(2L), managedObj.getFieldDateList().get(0));
    assertEquals(new Date(3L), managedObj.getFieldDateList().get(1));
}
Also used : RandomPrimaryKey(io.realm.entities.RandomPrimaryKey) DefaultValueOfField(io.realm.entities.DefaultValueOfField) Date(java.util.Date) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 5 with DefaultValueOfField

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

the class RealmTests method createObject_defaultValueFromModelField.

@Test
public void createObject_defaultValueFromModelField() {
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            // Creates a DefaultValueOfField with non-default primary key value.
            realm.createObject(DefaultValueOfField.class, DefaultValueOfField.FIELD_LONG_PRIMARY_KEY_DEFAULT_VALUE * 3);
        }
    });
    final String createdRandomString = DefaultValueOfField.lastRandomStringValue;
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_STRING, DefaultValueOfField.FIELD_STRING_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_RANDOM_STRING, createdRandomString);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_SHORT, DefaultValueOfField.FIELD_SHORT_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_INT, DefaultValueOfField.FIELD_INT_DEFAULT_VALUE);
    // Default value for pk must be ignored.
    testNoObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_LONG_PRIMARY_KEY, DefaultValueOfField.FIELD_LONG_PRIMARY_KEY_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_LONG_PRIMARY_KEY, DefaultValueOfField.FIELD_LONG_PRIMARY_KEY_DEFAULT_VALUE * 3);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_LONG, DefaultValueOfField.FIELD_LONG_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_BYTE, DefaultValueOfField.FIELD_BYTE_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_FLOAT, DefaultValueOfField.FIELD_FLOAT_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_DOUBLE, DefaultValueOfField.FIELD_DOUBLE_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_BOOLEAN, DefaultValueOfField.FIELD_BOOLEAN_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_DATE, DefaultValueOfField.FIELD_DATE_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_BINARY, DefaultValueOfField.FIELD_BINARY_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_OBJECT + "." + RandomPrimaryKey.FIELD_INT, RandomPrimaryKey.FIELD_INT_DEFAULT_VALUE);
    testOneObjectFound(realm, DefaultValueOfField.class, DefaultValueOfField.FIELD_LIST + "." + RandomPrimaryKey.FIELD_INT, RandomPrimaryKey.FIELD_INT_DEFAULT_VALUE);
}
Also used : PrimaryKeyRequiredAsString(io.realm.entities.PrimaryKeyRequiredAsString) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) DefaultValueOfField(io.realm.entities.DefaultValueOfField) OsSharedRealm(io.realm.internal.OsSharedRealm) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Aggregations

DefaultValueOfField (io.realm.entities.DefaultValueOfField)7 Test (org.junit.Test)7 Date (java.util.Date)5 UiThreadTest (androidx.test.annotation.UiThreadTest)4 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)3 RandomPrimaryKey (io.realm.entities.RandomPrimaryKey)3 JSONObject (org.json.JSONObject)3 PrimaryKeyRequiredAsString (io.realm.entities.PrimaryKeyRequiredAsString)2 JSONArray (org.json.JSONArray)2 PrimaryKeyAsObjectId (io.realm.entities.PrimaryKeyAsObjectId)1 OsSharedRealm (io.realm.internal.OsSharedRealm)1 UUID (java.util.UUID)1 Decimal128 (org.bson.types.Decimal128)1 ObjectId (org.bson.types.ObjectId)1