Search in sources :

Example 26 with Decimal128

use of org.bson.types.Decimal128 in project realm-java by realm.

the class RealmJsonTests method createUsingJsonStream_decimal128.

@Test
public void createUsingJsonStream_decimal128() throws JSONException {
    JSONObject json = new JSONObject();
    json.put("columnDecimal128", new Decimal128(BigDecimal.TEN));
    realm.beginTransaction();
    realm.createObjectFromJson(AllTypes.class, json);
    realm.commitTransaction();
    AllTypes obj = realm.where(AllTypes.class).findFirst();
    assertEquals(new Decimal128(BigDecimal.TEN), obj.getColumnDecimal128());
}
Also used : JSONObject(org.json.JSONObject) AllTypes(io.realm.entities.AllTypes) Decimal128(org.bson.types.Decimal128) Test(org.junit.Test)

Example 27 with Decimal128

use of org.bson.types.Decimal128 in project realm-java by realm.

the class RealmJsonTests method createObjectFromJson_decimal128AsLong.

@Test
public void createObjectFromJson_decimal128AsLong() throws JSONException {
    JSONObject json = new JSONObject();
    json.put("columnDecimal128", -32361122672259149L);
    realm.beginTransaction();
    realm.createObjectFromJson(AllTypes.class, json);
    realm.commitTransaction();
    AllTypes obj = realm.where(AllTypes.class).findFirst();
    assertEquals(new Decimal128(-32361122672259149L), obj.getColumnDecimal128());
}
Also used : JSONObject(org.json.JSONObject) AllTypes(io.realm.entities.AllTypes) Decimal128(org.bson.types.Decimal128) Test(org.junit.Test)

Example 28 with Decimal128

use of org.bson.types.Decimal128 in project realm-java by realm.

the class RealmJsonTests method createOrUpdateObjectFromJson_streamNullValues.

/**
 * Tests updating a existing object with JSON stream. Only primary key in JSON.
 * No value should be changed.
 */
@Test
public void createOrUpdateObjectFromJson_streamNullValues() throws IOException {
    assumeThat(Build.VERSION.SDK_INT, greaterThanOrEqualTo(Build.VERSION_CODES.HONEYCOMB));
    AllTypesPrimaryKey obj = new AllTypesPrimaryKey();
    Date date = new Date(0);
    ObjectId objectId = new ObjectId(new Date(20));
    Decimal128 decimal128 = new Decimal128(300);
    UUID uuid = UUID.randomUUID();
    RealmAny realmAny = RealmAny.valueOf("Hello world");
    // ID
    obj.setColumnLong(1);
    obj.setColumnBinary(new byte[] { 1 });
    obj.setColumnBoolean(true);
    obj.setColumnDate(date);
    obj.setColumnDouble(1);
    obj.setColumnFloat(1);
    obj.setColumnString("1");
    obj.setColumnObjectId(objectId);
    obj.setColumnDecimal128(decimal128);
    obj.setColumnUUID(uuid);
    obj.setColumnRealmAny(realmAny);
    realm.beginTransaction();
    realm.copyToRealm(obj);
    realm.commitTransaction();
    InputStream in = TestHelper.loadJsonFromAssets(context, "all_types_primary_key_field_only.json");
    realm.beginTransaction();
    realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, in);
    realm.commitTransaction();
    in.close();
    // Checks that all primitive types are imported correctly.
    obj = realm.where(AllTypesPrimaryKey.class).findFirst();
    assertEquals("1", obj.getColumnString());
    assertEquals(1L, obj.getColumnLong());
    assertEquals(1F, obj.getColumnFloat(), 0F);
    assertEquals(1D, obj.getColumnDouble(), 0D);
    assertEquals(true, obj.isColumnBoolean());
    assertEquals(date, obj.getColumnDate());
    assertEquals(objectId, obj.getColumnObjectId());
    assertEquals(decimal128, obj.getColumnDecimal128());
    assertEquals(uuid, obj.getColumnUUID());
    assertEquals(realmAny, obj.getColumnRealmAny());
    assertArrayEquals(new byte[] { 1 }, obj.getColumnBinary());
    assertNull(obj.getColumnRealmObject());
    assertEquals(0, obj.getColumnRealmList().size());
}
Also used : PrimaryKeyAsObjectId(io.realm.entities.PrimaryKeyAsObjectId) ObjectId(org.bson.types.ObjectId) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Decimal128(org.bson.types.Decimal128) UUID(java.util.UUID) Date(java.util.Date) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Example 29 with Decimal128

use of org.bson.types.Decimal128 in project realm-java by realm.

the class RealmJsonTests method createOrUpdateObjectFromJson_objectNullValues.

// Tests updating a existing object with JSON object with only primary key.
// No value should be changed.
@Test
public void createOrUpdateObjectFromJson_objectNullValues() throws IOException {
    AllTypesPrimaryKey obj = new AllTypesPrimaryKey();
    Date date = new Date(0);
    ObjectId objectId = new ObjectId(new Date(20));
    Decimal128 decimal128 = new Decimal128(300);
    UUID uuid = UUID.randomUUID();
    RealmAny realmAny = RealmAny.valueOf("Hello world");
    // ID
    obj.setColumnLong(1);
    obj.setColumnBinary(new byte[] { 1 });
    obj.setColumnBoolean(true);
    obj.setColumnDate(date);
    obj.setColumnDouble(1);
    obj.setColumnFloat(1);
    obj.setColumnString("1");
    obj.setColumnObjectId(objectId);
    obj.setColumnDecimal128(decimal128);
    obj.setColumnUUID(uuid);
    obj.setColumnRealmAny(realmAny);
    realm.beginTransaction();
    realm.copyToRealm(obj);
    realm.commitTransaction();
    String json = TestHelper.streamToString(TestHelper.loadJsonFromAssets(context, "all_types_primary_key_field_only.json"));
    realm.beginTransaction();
    realm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, json);
    realm.commitTransaction();
    // Checks that all primitive types are imported correctly.
    obj = realm.where(AllTypesPrimaryKey.class).findFirst();
    assertEquals("1", obj.getColumnString());
    assertEquals(1L, obj.getColumnLong());
    assertEquals(1F, obj.getColumnFloat(), 0F);
    assertEquals(1D, obj.getColumnDouble(), 0D);
    assertEquals(true, obj.isColumnBoolean());
    assertEquals(date, obj.getColumnDate());
    assertEquals(objectId, obj.getColumnObjectId());
    assertEquals(decimal128, obj.getColumnDecimal128());
    assertEquals(uuid, obj.getColumnUUID());
    assertEquals(realmAny, obj.getColumnRealmAny());
    assertArrayEquals(new byte[] { 1 }, obj.getColumnBinary());
    assertNull(obj.getColumnRealmObject());
    assertEquals(0, obj.getColumnRealmList().size());
}
Also used : PrimaryKeyAsObjectId(io.realm.entities.PrimaryKeyAsObjectId) ObjectId(org.bson.types.ObjectId) Decimal128(org.bson.types.Decimal128) UUID(java.util.UUID) Date(java.util.Date) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey) Test(org.junit.Test)

Example 30 with Decimal128

use of org.bson.types.Decimal128 in project realm-java by realm.

the class QueryTests method createIsNotEmptyDataSet.

@SuppressWarnings("unchecked")
protected final void createIsNotEmptyDataSet(Realm realm) {
    realm.beginTransaction();
    AllJavaTypesUnsupportedTypes emptyValues = new AllJavaTypesUnsupportedTypes();
    emptyValues.setFieldId(1);
    emptyValues.setFieldString("");
    emptyValues.setFieldBinary(new byte[0]);
    emptyValues.setFieldObject(emptyValues);
    emptyValues.setFieldList(new RealmList<>());
    realm.copyToRealm(emptyValues);
    AllJavaTypesUnsupportedTypes notEmpty = new AllJavaTypesUnsupportedTypes();
    notEmpty.setFieldId(2);
    notEmpty.setFieldString("Foo");
    notEmpty.setFieldBinary(new byte[] { 1, 2, 3 });
    notEmpty.setFieldObject(notEmpty);
    notEmpty.setFieldList(new RealmList<>(emptyValues));
    notEmpty.setFieldIntegerList(new RealmList<>(1));
    notEmpty.setFieldBooleanList(new RealmList<>(true));
    notEmpty.setFieldStringList(new RealmList<>("hello"));
    notEmpty.setFieldBinaryList(new RealmList<>(new byte[1]));
    notEmpty.setFieldDateList(new RealmList<>(new Date()));
    notEmpty.setFieldFloatList(new RealmList<>(1F));
    notEmpty.setFieldDoubleList(new RealmList<>(1.0));
    notEmpty.setFieldDecimal128List(new RealmList<>(new Decimal128(1L)));
    notEmpty.setFieldObjectIdList(new RealmList<>(new ObjectId()));
    notEmpty.setFieldUUIDList(new RealmList<>(UUID.randomUUID()));
    notEmpty.setFieldRealmAnyList(new RealmList<>(RealmAny.valueOf(1)));
    notEmpty.setColumnRealmDictionary((RealmDictionary<AllJavaTypesUnsupportedTypes>) getDictionary(RealmFieldType.STRING_TO_LINK_MAP, notEmpty));
    notEmpty.setColumnIntegerDictionary((RealmDictionary<Integer>) getDictionary(RealmFieldType.STRING_TO_INTEGER_MAP));
    notEmpty.setColumnBooleanDictionary((RealmDictionary<Boolean>) getDictionary(RealmFieldType.STRING_TO_BOOLEAN_MAP));
    notEmpty.setColumnStringDictionary((RealmDictionary<String>) getDictionary(RealmFieldType.STRING_TO_STRING_MAP));
    notEmpty.setColumnBinaryDictionary((RealmDictionary<byte[]>) getDictionary(RealmFieldType.STRING_TO_BINARY_MAP));
    notEmpty.setColumnDateDictionary((RealmDictionary<Date>) getDictionary(RealmFieldType.STRING_TO_DATE_MAP));
    notEmpty.setColumnFloatDictionary((RealmDictionary<Float>) getDictionary(RealmFieldType.STRING_TO_FLOAT_MAP));
    notEmpty.setColumnDoubleDictionary((RealmDictionary<Double>) getDictionary(RealmFieldType.STRING_TO_DOUBLE_MAP));
    notEmpty.setColumnDecimal128Dictionary((RealmDictionary<Decimal128>) getDictionary(RealmFieldType.STRING_TO_DECIMAL128_MAP));
    notEmpty.setColumnObjectIdDictionary((RealmDictionary<ObjectId>) getDictionary(RealmFieldType.STRING_TO_OBJECT_ID_MAP));
    notEmpty.setColumnUUIDDictionary((RealmDictionary<UUID>) getDictionary(RealmFieldType.STRING_TO_UUID_MAP));
    notEmpty.setColumnRealmAnyDictionary((RealmDictionary<RealmAny>) getDictionary(RealmFieldType.STRING_TO_MIXED_MAP));
    notEmpty.setColumnRealmSet((RealmSet<AllJavaTypesUnsupportedTypes>) getSet(RealmFieldType.LINK_SET, notEmpty));
    notEmpty.setColumnIntegerSet((RealmSet<Integer>) getSet(RealmFieldType.INTEGER_SET));
    notEmpty.setColumnBooleanSet((RealmSet<Boolean>) getSet(RealmFieldType.BOOLEAN_SET));
    notEmpty.setColumnStringSet((RealmSet<String>) getSet(RealmFieldType.STRING_SET));
    notEmpty.setColumnBinarySet((RealmSet<byte[]>) getSet(RealmFieldType.BINARY_SET));
    notEmpty.setColumnDateSet((RealmSet<Date>) getSet(RealmFieldType.DATE_SET));
    notEmpty.setColumnFloatSet((RealmSet<Float>) getSet(RealmFieldType.FLOAT_SET));
    notEmpty.setColumnDoubleSet((RealmSet<Double>) getSet(RealmFieldType.DOUBLE_SET));
    notEmpty.setColumnDecimal128Set((RealmSet<Decimal128>) getSet(RealmFieldType.DECIMAL128_SET));
    notEmpty.setColumnObjectIdSet((RealmSet<ObjectId>) getSet(RealmFieldType.OBJECT_ID_SET));
    notEmpty.setColumnUUIDSet((RealmSet<UUID>) getSet(RealmFieldType.UUID_SET));
    notEmpty.setColumnRealmAnySet((RealmSet<RealmAny>) getSet(RealmFieldType.MIXED_SET));
    realm.copyToRealmOrUpdate(notEmpty);
    realm.commitTransaction();
}
Also used : ObjectId(org.bson.types.ObjectId) AllJavaTypesUnsupportedTypes(io.realm.entities.AllJavaTypesUnsupportedTypes) Decimal128(org.bson.types.Decimal128) UUID(java.util.UUID) Date(java.util.Date)

Aggregations

Decimal128 (org.bson.types.Decimal128)83 ObjectId (org.bson.types.ObjectId)50 Date (java.util.Date)46 Test (org.junit.Test)46 AllTypes (io.realm.entities.AllTypes)22 BigDecimal (java.math.BigDecimal)20 UUID (java.util.UUID)17 UiThreadTest (androidx.test.annotation.UiThreadTest)12 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)11 AllJavaTypes (io.realm.entities.AllJavaTypes)10 DictionaryAllTypes (io.realm.entities.DictionaryAllTypes)10 Dog (io.realm.entities.Dog)9 PrimaryKeyAsObjectId (io.realm.entities.PrimaryKeyAsObjectId)8 JSONObject (org.json.JSONObject)8 MappedAllJavaTypes (io.realm.entities.MappedAllJavaTypes)7 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)6 PrimaryKeyAsLong (io.realm.entities.PrimaryKeyAsLong)6 SimpleDateFormat (java.text.SimpleDateFormat)6 Calendar (java.util.Calendar)6 DefaultValueOfField (io.realm.entities.DefaultValueOfField)5