use of org.bson.types.Decimal128 in project realm-java by realm.
the class RealmJsonTests method createObjectFromJson_decimal128AsInt.
@Test
public void createObjectFromJson_decimal128AsInt() throws JSONException {
JSONObject json = new JSONObject();
json.put("columnDecimal128", -42);
realm.beginTransaction();
realm.createObjectFromJson(AllTypes.class, json);
realm.commitTransaction();
AllTypes obj = realm.where(AllTypes.class).findFirst();
assertEquals(new Decimal128(-42), obj.getColumnDecimal128());
}
use of org.bson.types.Decimal128 in project realm-java by realm.
the class RealmTests method copyToRealm.
@Test
public void copyToRealm() {
Date date = new Date();
Dog dog = new Dog();
dog.setName("Fido");
RealmList<Dog> list = new RealmList<Dog>();
list.add(dog);
AllTypes allTypes = new AllTypes();
allTypes.setColumnString("String");
allTypes.setColumnLong(1L);
allTypes.setColumnFloat(1F);
allTypes.setColumnDouble(1D);
allTypes.setColumnBoolean(true);
allTypes.setColumnDate(date);
allTypes.setColumnBinary(new byte[] { 1, 2, 3 });
allTypes.setColumnDecimal128(new Decimal128(new BigDecimal("12345")));
allTypes.setColumnObjectId(new ObjectId(TestHelper.generateObjectIdHexString(7)));
allTypes.setColumnUUID(UUID.fromString(TestHelper.generateUUIDString(7)));
allTypes.setColumnRealmAny(RealmAny.valueOf(UUID.fromString(TestHelper.generateUUIDString(6))));
allTypes.setColumnRealmObject(dog);
allTypes.setColumnRealmList(list);
allTypes.setColumnStringList(new RealmList<String>("1"));
allTypes.setColumnBinaryList(new RealmList<byte[]>(new byte[] { 1 }));
allTypes.setColumnBooleanList(new RealmList<Boolean>(true));
allTypes.setColumnLongList(new RealmList<Long>(1L));
allTypes.setColumnDoubleList(new RealmList<Double>(1D));
allTypes.setColumnFloatList(new RealmList<Float>(1F));
allTypes.setColumnDateList(new RealmList<Date>(new Date(1L)));
allTypes.setColumnDecimal128List(new RealmList<Decimal128>(new Decimal128(new BigDecimal("54321"))));
allTypes.setColumnObjectIdList(new RealmList<ObjectId>(new ObjectId(TestHelper.generateObjectIdHexString(5))));
allTypes.setColumnUUIDList(new RealmList<>(UUID.fromString(TestHelper.generateUUIDString(5))));
allTypes.setColumnRealmAnyList(new RealmList<>(RealmAny.valueOf(UUID.fromString(TestHelper.generateUUIDString(7)))));
realm.beginTransaction();
AllTypes realmTypes = realm.copyToRealm(allTypes);
realm.commitTransaction();
// Objects should not be considered equal.
assertNotSame(allTypes, realmTypes);
// But they contain the same data.
assertEquals(allTypes.getColumnString(), realmTypes.getColumnString());
assertEquals(allTypes.getColumnLong(), realmTypes.getColumnLong());
assertEquals(allTypes.getColumnFloat(), realmTypes.getColumnFloat(), 0);
assertEquals(allTypes.getColumnDouble(), realmTypes.getColumnDouble(), 0);
assertEquals(allTypes.isColumnBoolean(), realmTypes.isColumnBoolean());
assertEquals(allTypes.getColumnDate(), realmTypes.getColumnDate());
assertArrayEquals(allTypes.getColumnBinary(), realmTypes.getColumnBinary());
assertEquals(allTypes.getColumnDecimal128(), realmTypes.getColumnDecimal128());
assertEquals(allTypes.getColumnObjectId(), realmTypes.getColumnObjectId());
assertEquals(allTypes.getColumnUUID(), realmTypes.getColumnUUID());
assertEquals(allTypes.getColumnRealmAny(), realmTypes.getColumnRealmAny());
assertEquals(allTypes.getColumnRealmObject().getName(), dog.getName());
assertEquals(list.size(), realmTypes.getColumnRealmList().size());
// noinspection ConstantConditions
assertEquals(list.get(0).getName(), realmTypes.getColumnRealmList().get(0).getName());
assertEquals(1, realmTypes.getColumnStringList().size());
assertEquals("1", realmTypes.getColumnStringList().get(0));
assertEquals(1, realmTypes.getColumnBooleanList().size());
assertEquals(true, realmTypes.getColumnBooleanList().get(0));
assertEquals(1, realmTypes.getColumnBinaryList().size());
assertArrayEquals(new byte[] { 1 }, realmTypes.getColumnBinaryList().get(0));
assertEquals(1, realmTypes.getColumnLongList().size());
assertEquals((Long) 1L, realmTypes.getColumnLongList().get(0));
assertEquals(1, realmTypes.getColumnDoubleList().size());
assertEquals((Double) 1D, realmTypes.getColumnDoubleList().get(0));
assertEquals(1, realmTypes.getColumnFloatList().size());
assertEquals((Float) 1F, realmTypes.getColumnFloatList().get(0));
assertEquals(1, realmTypes.getColumnDateList().size());
assertEquals(new Date(1), realmTypes.getColumnDateList().get(0));
assertEquals(1, realmTypes.getColumnDecimal128List().size());
assertEquals(new Decimal128(new BigDecimal("54321")), realmTypes.getColumnDecimal128List().get(0));
assertEquals(1, realmTypes.getColumnObjectIdList().size());
assertEquals(new ObjectId(TestHelper.generateObjectIdHexString(5)), realmTypes.getColumnObjectIdList().get(0));
assertEquals(1, realmTypes.getColumnUUIDList().size());
assertEquals(UUID.fromString(TestHelper.generateUUIDString(5)), realmTypes.getColumnUUIDList().get(0));
assertEquals(1, realmTypes.getColumnRealmAnyList().size());
assertEquals(RealmAny.valueOf(UUID.fromString(TestHelper.generateUUIDString(7))), realmTypes.getColumnRealmAnyList().get(0));
}
use of org.bson.types.Decimal128 in project realm-java by realm.
the class RealmTests method where_equalTo_wrongFieldTypeAsInput.
// TODO Move to RealmQueryTests?
@Test
public void where_equalTo_wrongFieldTypeAsInput() throws IOException {
populateTestRealm();
for (int i = 0; i < columnData.size(); i++) {
// Realm queries applies coercion on numerical values
boolean NON_NUMERICAL_COLUMN = (i > 4) && (i != 10);
// Realm queries applies coercion on objectid and date
boolean NON_OBJECT_OR_DATE = ((i <= 4) || (i > 6)) && (i != 10);
// Realm queries applies coercion on string and binary
boolean NON_STRING_OR_BINARY = ((i <= 6) || (i > 8)) && (i != 10);
try {
realm.where(AllTypes.class).equalTo(columnData.get(i), 13.37D).findAll();
if (NON_NUMERICAL_COLUMN) {
fail("Realm.where should fail with illegal argument");
}
} catch (IllegalArgumentException ignored) {
}
try {
realm.where(AllTypes.class).equalTo(columnData.get(i), 13.3711F).findAll();
if (NON_NUMERICAL_COLUMN) {
fail("Realm.where should fail with illegal argument");
}
} catch (IllegalArgumentException ignored) {
}
try {
realm.where(AllTypes.class).equalTo(columnData.get(i), 1337).findAll();
if (NON_NUMERICAL_COLUMN) {
fail("Realm.where should fail with illegal argument");
}
} catch (IllegalArgumentException ignored) {
}
try {
realm.where(AllTypes.class).equalTo(columnData.get(i), new Decimal128(new BigDecimal(i + "12345"))).findAll();
if (NON_NUMERICAL_COLUMN) {
fail("Realm.where should fail with illegal argument");
}
} catch (IllegalArgumentException ignored) {
}
try {
realm.where(AllTypes.class).equalTo(columnData.get(i), true).findAll();
if (NON_NUMERICAL_COLUMN) {
fail("Realm.where should fail with illegal argument");
}
} catch (IllegalArgumentException ignored) {
}
try {
realm.where(AllTypes.class).equalTo(columnData.get(i), new Date()).findAll();
if (NON_OBJECT_OR_DATE) {
fail("Realm.where should fail with illegal argument");
}
} catch (IllegalArgumentException ignored) {
}
try {
realm.where(AllTypes.class).equalTo(columnData.get(i), new ObjectId(TestHelper.generateObjectIdHexString(i))).findAll();
if (NON_OBJECT_OR_DATE) {
fail("Realm.where should fail with illegal argument");
}
} catch (IllegalArgumentException ignored) {
}
try {
realm.where(AllTypes.class).equalTo(columnData.get(i), "test").findAll();
if (NON_STRING_OR_BINARY) {
fail("Realm.where should fail with illegal argument");
}
} catch (IllegalArgumentException ignored) {
}
try {
realm.where(AllTypes.class).equalTo(columnData.get(i), new byte[] { 1, 2, 3 }).findAll();
if (NON_STRING_OR_BINARY) {
fail("Realm.where should fail with illegal argument");
}
} catch (IllegalArgumentException ignored) {
}
try {
realm.where(AllTypes.class).equalTo(columnData.get(i), UUID.fromString(TestHelper.generateUUIDString(i))).findAll();
if ((i != 9) && (i != 10)) {
fail("Realm.where should fail with illegal argument");
}
} catch (IllegalArgumentException ignored) {
}
}
}
use of org.bson.types.Decimal128 in project realm-java by realm.
the class RealmResultsTests method setValue_specificType_modelClassNameOnTypedRealms.
@Test
public void setValue_specificType_modelClassNameOnTypedRealms() {
populateMappedAllJavaTypes(5);
RealmResults<MappedAllJavaTypes> collection = realm.where(MappedAllJavaTypes.class).findAll();
realm.beginTransaction();
for (BulkSetMethods type : BulkSetMethods.values()) {
switch(type) {
case STRING:
collection.setString("fieldString", "foo");
assertElements(collection, obj -> assertEquals("foo", obj.fieldString));
break;
case BOOLEAN:
collection.setBoolean("fieldBoolean", true);
assertElements(collection, obj -> assertTrue(obj.fieldBoolean));
break;
case BYTE:
collection.setByte("fieldByte", (byte) 1);
assertElements(collection, obj -> assertEquals((byte) 1, obj.fieldByte));
break;
case SHORT:
collection.setShort("fieldShort", (short) 2);
assertElements(collection, obj -> assertEquals((short) 2, obj.fieldShort));
break;
case INTEGER:
collection.setInt("fieldInt", 3);
assertElements(collection, obj -> assertEquals(3, obj.fieldInt));
break;
case LONG:
collection.setLong("fieldLong", 4L);
assertElements(collection, obj -> assertEquals(4L, obj.fieldLong));
break;
case FLOAT:
collection.setFloat("fieldFloat", 1.23F);
assertElements(collection, obj -> assertEquals(1.23F, obj.fieldFloat, 0F));
break;
case DOUBLE:
collection.setDouble("fieldDouble", 1.234);
assertElements(collection, obj -> assertEquals(1.234, obj.fieldDouble, 0F));
break;
case BINARY:
collection.setBlob("fieldBinary", new byte[] { 1, 2, 3 });
assertElements(collection, obj -> assertArrayEquals(new byte[] { 1, 2, 3 }, obj.fieldBinary));
break;
case DATE:
collection.setDate("fieldDate", new Date(1000));
assertElements(collection, obj -> assertEquals(new Date(1000), obj.fieldDate));
break;
case DECIMAL128:
collection.setDecimal128("fieldDecimal128", new Decimal128(1000));
assertElements(collection, obj -> assertEquals(new Decimal128(1000), obj.fieldDecimal128));
break;
case OBJECT_ID:
{
String hex = TestHelper.randomObjectIdHexString();
collection.setObjectId("fieldObjectId", new ObjectId(hex));
assertElements(collection, obj -> assertEquals(new ObjectId(hex), obj.fieldObjectId));
break;
}
case UUID:
{
String uuid = UUID.randomUUID().toString();
collection.setUUID("fieldUUID", UUID.fromString(uuid));
assertElements(collection, obj -> assertEquals(UUID.fromString(uuid), obj.fieldUUID));
break;
}
case OBJECT:
{
MappedAllJavaTypes childObj = realm.createObject(MappedAllJavaTypes.class, 42);
collection.setObject("fieldObject", childObj);
assertElements(collection, obj -> assertEquals(childObj, obj.fieldObject));
break;
}
case MODEL_LIST:
{
MappedAllJavaTypes childObj = realm.createObject(MappedAllJavaTypes.class, 43);
collection.setList("fieldList", new RealmList<>(childObj));
assertElements(collection, obj -> {
assertEquals(1, obj.fieldList.size());
assertEquals(childObj, obj.fieldList.first());
});
break;
}
case STRING_VALUE_LIST:
collection.setList("fieldStringList", new RealmList<>("Foo"));
assertElements(collection, obj -> {
assertEquals(1, obj.fieldStringList.size());
assertEquals("Foo", obj.fieldStringList.first());
});
break;
case BOOLEAN_VALUE_LIST:
collection.setList("fieldBooleanList", new RealmList<>(true));
assertElements(collection, obj -> {
assertEquals(1, obj.fieldBooleanList.size());
assertEquals(true, obj.fieldBooleanList.first());
});
break;
case BYTE_VALUE_LIST:
collection.setList("fieldByteList", new RealmList<>((byte) 1));
assertElements(collection, obj -> {
assertEquals(1, obj.fieldByteList.size());
assertEquals(Byte.valueOf((byte) 1), obj.fieldByteList.first());
});
break;
case SHORT_VALUE_LIST:
collection.setList("fieldShortList", new RealmList<>((short) 1));
assertElements(collection, obj -> {
assertEquals(1, obj.fieldShortList.size());
assertEquals(Short.valueOf((short) 1), obj.fieldShortList.first());
});
break;
case INTEGER_VALUE_LIST:
collection.setList("fieldIntegerList", new RealmList<>(1));
assertElements(collection, obj -> {
assertEquals(1, obj.fieldIntegerList.size());
assertEquals(Integer.valueOf(1), obj.fieldIntegerList.first());
});
break;
case LONG_VALUE_LIST:
collection.setList("fieldLongList", new RealmList<>(1L));
assertElements(collection, obj -> {
assertEquals(1, obj.fieldLongList.size());
assertEquals(Long.valueOf((byte) 1), obj.fieldLongList.first());
});
break;
case FLOAT_VALUE_LIST:
collection.setList("fieldFloatList", new RealmList<>(1.1F));
assertElements(collection, obj -> {
assertEquals(1, obj.fieldFloatList.size());
assertEquals(1.1F, obj.fieldFloatList.first(), 0F);
});
break;
case DOUBLE_VALUE_LIST:
collection.setList("fieldDoubleList", new RealmList<>(1.1D));
assertElements(collection, obj -> {
assertEquals(1, obj.fieldDoubleList.size());
assertEquals(1.1D, obj.fieldDoubleList.first(), 0F);
});
break;
case BINARY_VALUE_LIST:
collection.setList("fieldBinaryList", new RealmList<>(new byte[] { 1, 2, 3 }));
assertElements(collection, obj -> {
assertEquals(1, obj.fieldBinaryList.size());
assertArrayEquals(new byte[] { 1, 2, 3 }, obj.fieldBinaryList.first());
});
break;
case DATE_VALUE_LIST:
collection.setList("fieldDateList", new RealmList<>(new Date(1000)));
assertElements(collection, obj -> {
assertEquals(1, obj.fieldDateList.size());
assertEquals(new Date(1000), obj.fieldDateList.first());
});
break;
case DECIMAL128_VALUE_LIST:
collection.setList("fieldDecimalList", new RealmList<>(new Decimal128(1000)));
assertElements(collection, obj -> {
assertEquals(1, obj.fieldDecimalList.size());
assertEquals(new Decimal128(1000), obj.fieldDecimalList.first());
});
break;
case OBJECT_ID_VALUE_LIST:
{
String hex = TestHelper.randomObjectIdHexString();
collection.setList("fieldObjectIdList", new RealmList<>(new ObjectId(hex)));
assertElements(collection, obj -> {
assertEquals(1, obj.fieldObjectIdList.size());
assertEquals(new ObjectId(hex), obj.fieldObjectIdList.first());
});
break;
}
case UUID_VALUE_LIST:
{
String uuid = UUID.randomUUID().toString();
collection.setList("fieldUUIDList", new RealmList<>(UUID.fromString(uuid)));
assertElements(collection, obj -> {
assertEquals(1, obj.fieldUUIDList.size());
assertEquals(UUID.fromString(uuid), obj.fieldUUIDList.first());
});
break;
}
default:
fail("Unknown type: " + type);
}
}
}
use of org.bson.types.Decimal128 in project realm-java by realm.
the class RealmResultsTests method setValue_specificType_wrongFieldNameThrows.
@Test
public void setValue_specificType_wrongFieldNameThrows() {
populateAllJavaTypes(5);
RealmResults<AllTypes> collection = realm.where(AllTypes.class).findAll();
realm.beginTransaction();
for (BulkSetMethods type : BulkSetMethods.values()) {
try {
switch(type) {
case STRING:
collection.setString("foo", "bar");
break;
case BOOLEAN:
collection.setBoolean("foo", true);
break;
case BYTE:
collection.setByte("foo", (byte) 1);
break;
case SHORT:
collection.setShort("foo", (short) 2);
break;
case INTEGER:
collection.setInt("foo", 3);
break;
case LONG:
collection.setLong("foo", 4L);
break;
case FLOAT:
collection.setFloat("foo", 1.23F);
break;
case DOUBLE:
collection.setDouble("foo", 1.234);
break;
case BINARY:
collection.setBlob("foo", new byte[] { 1, 2, 3 });
break;
case DATE:
collection.setDate("foo", new Date(1000));
break;
case DECIMAL128:
collection.setDecimal128("foo", new Decimal128(1000));
break;
case OBJECT_ID:
collection.setObjectId("foo", new ObjectId(TestHelper.randomObjectIdHexString()));
break;
case UUID:
collection.setUUID("foo", UUID.randomUUID());
break;
case OBJECT:
collection.setObject("foo", realm.createObject(AllTypes.class));
break;
case MODEL_LIST:
collection.setList("foo", new RealmList<>());
break;
case STRING_VALUE_LIST:
collection.setList("foo", new RealmList<>("Foo"));
break;
case BOOLEAN_VALUE_LIST:
collection.setList("foo", new RealmList<>(true));
break;
case BYTE_VALUE_LIST:
collection.setList("foo", new RealmList<>((byte) 1));
break;
case SHORT_VALUE_LIST:
collection.setList("foo", new RealmList<>((short) 1));
break;
case INTEGER_VALUE_LIST:
collection.setList("foo", new RealmList<>(1));
break;
case LONG_VALUE_LIST:
collection.setList("foo", new RealmList<>(1L));
break;
case FLOAT_VALUE_LIST:
collection.setList("foo", new RealmList<>(1.1F));
break;
case DOUBLE_VALUE_LIST:
collection.setList("foo", new RealmList<>(1.1D));
break;
case BINARY_VALUE_LIST:
collection.setList("foo", new RealmList<>(new byte[] {}));
break;
case DATE_VALUE_LIST:
collection.setList("foo", new RealmList<>(new Date()));
break;
case DECIMAL128_VALUE_LIST:
collection.setList("foo", new RealmList<>(new Decimal128(1000)));
break;
case OBJECT_ID_VALUE_LIST:
collection.setList("foo", new RealmList<>(new ObjectId(TestHelper.randomObjectIdHexString())));
break;
case UUID_VALUE_LIST:
collection.setList("foo", new RealmList<>(UUID.randomUUID()));
break;
default:
fail("Unknown type: " + type);
}
fail(type + " should have thrown an exception");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("does not exist"));
}
}
}
Aggregations