use of org.bson.types.Decimal128 in project realm-java by realm.
the class RealmResultsTests method setValue_specificType.
@Test
public void setValue_specificType() {
populateAllJavaTypes(5);
RealmResults<AllJavaTypes> collection = realm.where(AllJavaTypes.class).findAll();
realm.beginTransaction();
for (BulkSetMethods type : BulkSetMethods.values()) {
switch(type) {
case STRING:
collection.setString(AllJavaTypes.FIELD_STRING, "foo");
assertElements(collection, obj -> assertEquals("foo", obj.getFieldString()));
collection.setString(AllJavaTypes.FIELD_STRING, null);
assertElements(collection, obj -> assertEquals(null, obj.getFieldString()));
break;
case BOOLEAN:
collection.setBoolean(AllJavaTypes.FIELD_BOOLEAN, true);
assertElements(collection, obj -> assertTrue(obj.isFieldBoolean()));
break;
case BYTE:
collection.setByte(AllJavaTypes.FIELD_BYTE, (byte) 1);
assertElements(collection, obj -> assertEquals((byte) 1, obj.getFieldByte()));
break;
case SHORT:
collection.setShort(AllJavaTypes.FIELD_SHORT, (short) 2);
assertElements(collection, obj -> assertEquals((short) 2, obj.getFieldShort()));
break;
case INTEGER:
collection.setInt(AllJavaTypes.FIELD_INT, 3);
assertElements(collection, obj -> assertEquals(3, obj.getFieldInt()));
break;
case LONG:
collection.setLong(AllJavaTypes.FIELD_LONG, 4L);
assertElements(collection, obj -> assertEquals(4L, obj.getFieldLong()));
break;
case FLOAT:
collection.setFloat(AllJavaTypes.FIELD_FLOAT, 1.23F);
assertElements(collection, obj -> assertEquals(1.23F, obj.getFieldFloat(), 0F));
break;
case DOUBLE:
collection.setDouble(AllJavaTypes.FIELD_DOUBLE, 1.234);
assertElements(collection, obj -> assertEquals(1.234, obj.getFieldDouble(), 0F));
break;
case BINARY:
collection.setBlob(AllJavaTypes.FIELD_BINARY, new byte[] { 1, 2, 3 });
assertElements(collection, obj -> assertArrayEquals(new byte[] { 1, 2, 3 }, obj.getFieldBinary()));
collection.setBlob(AllJavaTypes.FIELD_BINARY, null);
assertElements(collection, obj -> assertNull(obj.getFieldBinary()));
break;
case DATE:
collection.setDate(AllJavaTypes.FIELD_DATE, new Date(1000));
assertElements(collection, obj -> assertEquals(new Date(1000), obj.getFieldDate()));
collection.setDate(AllJavaTypes.FIELD_DATE, null);
assertElements(collection, obj -> assertNull(obj.getFieldDate()));
break;
case DECIMAL128:
collection.setDecimal128(AllJavaTypes.FIELD_DECIMAL128, new Decimal128(1000));
assertElements(collection, obj -> assertEquals(new Decimal128(1000), obj.getFieldDecimal128()));
collection.setDecimal128(AllJavaTypes.FIELD_DECIMAL128, null);
assertElements(collection, obj -> assertNull(obj.getFieldDecimal128()));
break;
case OBJECT_ID:
collection.setObjectId(AllJavaTypes.FIELD_OBJECT_ID, new ObjectId(TestHelper.generateObjectIdHexString(1)));
assertElements(collection, obj -> assertEquals(new ObjectId(TestHelper.generateObjectIdHexString(1)), obj.getFieldObjectId()));
collection.setObjectId(AllJavaTypes.FIELD_OBJECT_ID, null);
assertElements(collection, obj -> assertNull(obj.getFieldObjectId()));
break;
case UUID:
collection.setUUID(AllJavaTypes.FIELD_UUID, UUID.fromString(uuid1));
assertElements(collection, obj -> assertEquals(UUID.fromString(uuid1), obj.getFieldUUID()));
collection.setUUID(AllJavaTypes.FIELD_UUID, null);
assertElements(collection, obj -> assertNull(obj.getFieldUUID()));
break;
case OBJECT:
{
AllJavaTypes childObj = realm.createObject(AllJavaTypes.class, 42);
collection.setObject(AllJavaTypes.FIELD_OBJECT, childObj);
assertElements(collection, obj -> assertEquals(childObj, obj.getFieldObject()));
collection.setObject(AllJavaTypes.FIELD_OBJECT, null);
assertElements(collection, obj -> assertNull(obj.getFieldObject()));
break;
}
case MODEL_LIST:
{
AllJavaTypes childObj = realm.createObject(AllJavaTypes.class, 43);
collection.setList(AllJavaTypes.FIELD_LIST, new RealmList<>(childObj));
assertElements(collection, obj -> {
assertEquals(1, obj.getFieldList().size());
assertEquals(childObj, obj.getFieldList().first());
});
break;
}
case STRING_VALUE_LIST:
{
RealmList<String> list = new RealmList<>("Foo", "Bar");
collection.setList(AllJavaTypes.FIELD_STRING_LIST, list);
assertElements(collection, obj -> {
assertEquals("Foo", obj.getFieldStringList().first());
assertEquals("Bar", obj.getFieldStringList().last());
});
break;
}
case BOOLEAN_VALUE_LIST:
{
RealmList<Boolean> list = new RealmList<>(true, false);
collection.setList(AllJavaTypes.FIELD_BOOLEAN_LIST, list);
assertElements(collection, obj -> {
assertTrue(obj.getFieldBooleanList().first());
assertFalse(obj.getFieldBooleanList().last());
});
break;
}
case BYTE_VALUE_LIST:
{
RealmList<Byte> list = new RealmList<>((byte) 1, (byte) 2);
collection.setList(AllJavaTypes.FIELD_BYTE_LIST, list);
assertElements(collection, obj -> {
assertEquals(Byte.valueOf((byte) 1), obj.getFieldByteList().first());
assertEquals(Byte.valueOf((byte) 2), obj.getFieldByteList().last());
});
break;
}
case SHORT_VALUE_LIST:
{
RealmList<Short> list = new RealmList<>((short) 1, (short) 2);
collection.setList(AllJavaTypes.FIELD_SHORT_LIST, list);
assertElements(collection, obj -> {
assertEquals(Short.valueOf((short) 1), obj.getFieldShortList().first());
assertEquals(Short.valueOf((short) 2), obj.getFieldShortList().last());
});
break;
}
case INTEGER_VALUE_LIST:
{
RealmList<Integer> list = new RealmList<>(1, 2);
collection.setList(AllJavaTypes.FIELD_INTEGER_LIST, list);
assertElements(collection, obj -> {
assertEquals(Integer.valueOf(1), obj.getFieldIntegerList().first());
assertEquals(Integer.valueOf(2), obj.getFieldIntegerList().last());
});
break;
}
case LONG_VALUE_LIST:
{
RealmList<Long> list = new RealmList<>(1L, 2L);
collection.setList(AllJavaTypes.FIELD_LONG_LIST, list);
assertElements(collection, obj -> {
assertEquals(Long.valueOf(1), obj.getFieldLongList().first());
assertEquals(Long.valueOf(2), obj.getFieldLongList().last());
});
break;
}
case FLOAT_VALUE_LIST:
{
RealmList<Float> list = new RealmList<>(1.1F, 2.2F);
collection.setList(AllJavaTypes.FIELD_FLOAT_LIST, list);
assertElements(collection, obj -> {
assertEquals(1.1F, obj.getFieldFloatList().first(), 0F);
assertEquals(2.2F, obj.getFieldFloatList().last(), 0F);
});
break;
}
case DOUBLE_VALUE_LIST:
{
RealmList<Double> list = new RealmList<>(1.1D, 2.2D);
collection.setList(AllJavaTypes.FIELD_DOUBLE_LIST, list);
assertElements(collection, obj -> {
assertEquals(1.1D, obj.getFieldDoubleList().first(), 0D);
assertEquals(2.2D, obj.getFieldDoubleList().last(), 0D);
});
break;
}
case BINARY_VALUE_LIST:
{
RealmList<byte[]> list = new RealmList<>(new byte[] { 1, 2, 3 }, new byte[] { 2, 3, 4 });
collection.setList(AllJavaTypes.FIELD_BINARY_LIST, list);
assertElements(collection, obj -> {
assertArrayEquals(new byte[] { 1, 2, 3 }, obj.getFieldBinaryList().first());
assertArrayEquals(new byte[] { 2, 3, 4 }, obj.getFieldBinaryList().last());
});
break;
}
case DATE_VALUE_LIST:
{
RealmList<Date> list = new RealmList<>(new Date(1000), new Date(2000));
collection.setList(AllJavaTypes.FIELD_DATE_LIST, list);
assertElements(collection, obj -> {
assertEquals(new Date(1000), obj.getFieldDateList().first());
assertEquals(new Date(2000), obj.getFieldDateList().last());
});
break;
}
case DECIMAL128_VALUE_LIST:
{
RealmList<Decimal128> list = new RealmList<>(new Decimal128(1000), new Decimal128(2000));
collection.setList(AllJavaTypes.FIELD_DECIMAL128_LIST, list);
assertElements(collection, obj -> {
assertEquals(new Decimal128(1000), obj.getFieldDecimal128List().first());
assertEquals(new Decimal128(2000), obj.getFieldDecimal128List().last());
});
break;
}
case OBJECT_ID_VALUE_LIST:
{
String hex1 = TestHelper.randomObjectIdHexString();
String hex2 = TestHelper.randomObjectIdHexString();
RealmList<ObjectId> list = new RealmList<>(new ObjectId(hex1), new ObjectId(hex2));
collection.setList(AllJavaTypes.FIELD_OBJECT_ID_LIST, list);
assertElements(collection, obj -> {
assertEquals(new ObjectId(hex1), obj.getFieldObjectIdList().first());
assertEquals(new ObjectId(hex2), obj.getFieldObjectIdList().last());
});
break;
}
case UUID_VALUE_LIST:
{
String uuid1 = UUID.randomUUID().toString();
String uuid2 = UUID.randomUUID().toString();
RealmList<UUID> list = new RealmList<>(UUID.fromString(uuid1), UUID.fromString(uuid2));
collection.setList(AllJavaTypes.FIELD_UUID_LIST, list);
assertElements(collection, obj -> {
assertEquals(UUID.fromString(uuid1), obj.getFieldUUIDList().first());
assertEquals(UUID.fromString(uuid2), obj.getFieldUUIDList().last());
});
break;
}
default:
fail("Unknown type: " + type);
}
}
}
use of org.bson.types.Decimal128 in project realm-java by realm.
the class RealmQueryTests method populateForDistinctAllTypes.
private void populateForDistinctAllTypes(Realm realm, long numberOfBlocks, long numberOfObjects) {
realm.beginTransaction();
for (int i = 0; i < numberOfBlocks; i++) {
Dog dog = realm.createObject(Dog.class);
for (int j = 0; j < numberOfObjects; j++) {
AllTypes obj = realm.createObject(AllTypes.class);
obj.setColumnBinary(new byte[j]);
obj.setColumnString("Test " + j);
obj.setColumnLong(j);
obj.setColumnFloat(j / 1000f);
obj.setColumnDouble(j / 1000d);
obj.setColumnBoolean(j % 2 == 0);
obj.setColumnDate(new Date(1000L * j));
obj.setColumnDecimal128(new Decimal128(j));
obj.setColumnObjectId(new ObjectId(j, j));
obj.setColumnUUID(UUID.fromString(TestHelper.generateUUIDString(j)));
obj.setColumnMutableRealmInteger(j);
obj.setColumnRealmLink(obj);
obj.setColumnRealmObject(dog);
obj.setColumnRealmAny(RealmAny.valueOf(i));
}
}
realm.commitTransaction();
}
use of org.bson.types.Decimal128 in project realm-java by realm.
the class TestHelper method populateTestRealmForNullTests.
public static void populateTestRealmForNullTests(Realm testRealm) {
// Creates 3 NullTypes objects. The objects are self-referenced (link) in
// order to test link queries.
//
// +-+--------+------+---------+--------+--------------------+
// | | string | link | numeric | binary | numeric (not null) |
// +-+--------+------+---------+--------+--------------------+
// |0| Fish | 0 | 1 | {0} | 1 |
// |1| null | null | null | null | 0 |
// |2| Horse | 1 | 3 | {1,2} | 3 |
// +-+--------+------+---------+--------+--------------------+
// 1 String
String[] words = { "Fish", null, "Horse" };
// 2 Bytes
byte[][] binaries = { new byte[] { 0 }, null, new byte[] { 1, 2 } };
// 3 Boolean
Boolean[] booleans = { false, null, true };
// Numeric fields will be 1, 0/null, 3
// 10 Date
Date[] dates = { new Date(0), null, new Date(10000) };
NullTypes[] nullTypesArray = new NullTypes[3];
Decimal128[] decimals = { new Decimal128(BigDecimal.TEN), null, new Decimal128(BigDecimal.ONE) };
ObjectId[] ids = { new ObjectId(TestHelper.generateObjectIdHexString(10)), null, new ObjectId(TestHelper.generateObjectIdHexString(1)) };
testRealm.beginTransaction();
for (int i = 0; i < 3; i++) {
NullTypes nullTypes = new NullTypes();
nullTypes.setId(i + 1);
// 1 String
nullTypes.setFieldStringNull(words[i]);
if (words[i] != null) {
nullTypes.setFieldStringNotNull(words[i]);
}
// 2 Bytes
nullTypes.setFieldBytesNull(binaries[i]);
if (binaries[i] != null) {
nullTypes.setFieldBytesNotNull(binaries[i]);
}
// 3 Boolean
nullTypes.setFieldBooleanNull(booleans[i]);
if (booleans[i] != null) {
nullTypes.setFieldBooleanNotNull(booleans[i]);
}
if (i != 1) {
int n = i + 1;
// 4 Byte
nullTypes.setFieldByteNull((byte) n);
nullTypes.setFieldByteNotNull((byte) n);
// 5 Short
nullTypes.setFieldShortNull((short) n);
nullTypes.setFieldShortNotNull((short) n);
// 6 Integer
nullTypes.setFieldIntegerNull(n);
nullTypes.setFieldIntegerNotNull(n);
// 7 Long
nullTypes.setFieldLongNull((long) n);
nullTypes.setFieldLongNotNull((long) n);
// 8 Float
nullTypes.setFieldFloatNull((float) n);
nullTypes.setFieldFloatNotNull((float) n);
// 9 Double
nullTypes.setFieldDoubleNull((double) n);
nullTypes.setFieldDoubleNotNull((double) n);
}
// 10 Date
nullTypes.setFieldDateNull(dates[i]);
if (dates[i] != null) {
nullTypes.setFieldDateNotNull(dates[i]);
}
nullTypes.setFieldDecimal128Null(decimals[i]);
nullTypes.setFieldObjectIdNull(ids[i]);
nullTypesArray[i] = testRealm.copyToRealm(nullTypes);
}
nullTypesArray[0].setFieldObjectNull(nullTypesArray[0]);
nullTypesArray[1].setFieldObjectNull(null);
nullTypesArray[2].setFieldObjectNull(nullTypesArray[1]);
testRealm.commitTransaction();
}
use of org.bson.types.Decimal128 in project realm-java by realm.
the class RealmJsonTests method createObjectFromJson_allSimpleObjectAllTypes.
@Test
public void createObjectFromJson_allSimpleObjectAllTypes() throws JSONException {
JSONObject json = new JSONObject();
ObjectId objectId = new ObjectId(new Date(20));
Decimal128 decimal128 = new Decimal128(300);
UUID uuid = UUID.randomUUID();
RealmAny realmAny = RealmAny.valueOf("Hello world");
json.put("columnString", "String");
json.put("columnLong", 1L);
json.put("columnFloat", 1.23F);
json.put("columnDouble", 1.23D);
json.put("columnBoolean", true);
json.put("columnBinary", new String(Base64.encode(new byte[] { 1, 2, 3 }, Base64.DEFAULT), UTF_8));
json.put("columnObjectId", objectId);
json.put("columnDecimal128", decimal128);
json.put("columnUUID", uuid);
json.put("columnRealmAny", realmAny);
realm.beginTransaction();
realm.createObjectFromJson(AllTypes.class, json);
realm.commitTransaction();
AllTypes obj = realm.where(AllTypes.class).findFirst();
// Checks that all primitive types are imported correctly.
assertEquals("String", obj.getColumnString());
assertEquals(1L, obj.getColumnLong());
assertEquals(1.23F, obj.getColumnFloat(), 0F);
assertEquals(1.23D, obj.getColumnDouble(), 0D);
assertEquals(true, obj.isColumnBoolean());
assertArrayEquals(new byte[] { 1, 2, 3 }, obj.getColumnBinary());
assertEquals(objectId, obj.getColumnObjectId());
assertEquals(decimal128, obj.getColumnDecimal128());
assertEquals(uuid, obj.getColumnUUID());
assertEquals(realmAny, obj.getColumnRealmAny());
}
use of org.bson.types.Decimal128 in project realm-java by realm.
the class RealmJsonTests method checkNullableValuesAreNull.
// Checks the imported object from nulltyps.json[0].
private void checkNullableValuesAreNull(NullTypes nullTypes1) {
// 1 String
assertNull(nullTypes1.getFieldStringNull());
assertEquals("", nullTypes1.getFieldStringNotNull());
// 2 Bytes
assertNull(nullTypes1.getFieldBytesNull());
assertTrue(Arrays.equals(new byte[0], nullTypes1.getFieldBytesNotNull()));
// 3 Boolean
assertNull(nullTypes1.getFieldBooleanNull());
assertFalse(nullTypes1.getFieldBooleanNotNull());
// 4 Byte
assertNull(nullTypes1.getFieldByteNull());
assertEquals(0, nullTypes1.getFieldByteNotNull().byteValue());
// 5 Short
assertNull(nullTypes1.getFieldShortNull());
assertEquals(0, nullTypes1.getFieldShortNotNull().shortValue());
// 6 Integer
assertNull(nullTypes1.getFieldIntegerNull());
assertEquals(0, nullTypes1.getFieldIntegerNotNull().intValue());
// 7 Long
assertNull(nullTypes1.getFieldLongNull());
assertEquals(0, nullTypes1.getFieldLongNotNull().longValue());
// 8 Float
assertNull(nullTypes1.getFieldFloatNull());
assertEquals((Float) 0F, nullTypes1.getFieldFloatNotNull());
// 9 Double
assertNull(nullTypes1.getFieldDoubleNull());
assertEquals((Double) 0D, nullTypes1.getFieldDoubleNotNull());
// 10 Date
assertNull(nullTypes1.getFieldDateNull());
assertEquals(new Date(0), nullTypes1.getFieldDateNotNull());
// 11 RealmObject
assertNull(nullTypes1.getFieldObjectNull());
// 12 ObjectId
assertNull(nullTypes1.getFieldObjectIdNull());
assertEquals(new ObjectId("789ABCDEF0123456789ABCDE"), nullTypes1.getFieldObjectIdNotNull());
// 13 Decimal128
assertNull(nullTypes1.getFieldDecimal128Null());
assertEquals(new Decimal128(-42), nullTypes1.getFieldDecimal128NotNull());
// 14 UUID
assertNull(nullTypes1.getFieldUUIDNull());
assertEquals(UUID.fromString("027ba5ca-aa12-4afa-9219-e20cc3018599"), nullTypes1.getFieldUUIDNotNull());
}
Aggregations