Search in sources :

Example 81 with Decimal128

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

the class JNITableTest method findFirst.

@Test
public void findFirst() {
    final int TEST_SIZE = 10;
    Table t = TestHelper.createTableWithAllColumnTypes(sharedRealm);
    long colKey1 = t.getColumnKey("binary");
    long colKey2 = t.getColumnKey("boolean");
    long colKey3 = t.getColumnKey("date");
    long colKey4 = t.getColumnKey("double");
    long colKey5 = t.getColumnKey("float");
    long colKey6 = t.getColumnKey("long");
    long colKey7 = t.getColumnKey("string");
    long colKey8 = t.getColumnKey("decimal128");
    long colKey9 = t.getColumnKey("object_id");
    sharedRealm.beginTransaction();
    for (int i = 0; i < TEST_SIZE; i++) {
        TestHelper.addRowWithValues(t, new long[] { colKey1, colKey2, colKey3, colKey4, colKey5, colKey6, colKey7, colKey8, colKey9 }, new Object[] { new byte[] { 1, 2, 3 }, true, new Date(i), (double) i, (float) i, i, "string " + i, new Decimal128(i), new ObjectId(TestHelper.generateObjectIdHexString(i)) });
    }
    TestHelper.addRowWithValues(t, new long[] { colKey1, colKey2, colKey3, colKey4, colKey5, colKey6, colKey7, colKey8, colKey9 }, new Object[] { new byte[] { 1, 2, 3 }, true, new Date(TEST_SIZE), (double) TEST_SIZE, (float) TEST_SIZE, TEST_SIZE, "", new Decimal128(TEST_SIZE), new ObjectId(TestHelper.generateObjectIdHexString(TEST_SIZE)) });
    sharedRealm.commitTransaction();
    assertEquals(0, t.findFirstBoolean(colKey2, true));
    for (int i = 0; i < TEST_SIZE; i++) {
        assertEquals(i, t.findFirstDate(colKey3, new Date(i)));
        assertEquals(i, t.findFirstDouble(colKey4, (double) i));
        assertEquals(i, t.findFirstFloat(colKey5, (float) i));
        assertEquals(i, t.findFirstLong(colKey6, i));
        assertEquals(i, t.findFirstDecimal128(colKey8, new Decimal128(i)));
        assertEquals(i, t.findFirstObjectId(colKey9, new ObjectId(TestHelper.generateObjectIdHexString(i))));
    }
    try {
        t.findFirstString(colKey7, null);
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    try {
        t.findFirstDate(colKey3, null);
        fail();
    } catch (IllegalArgumentException ignored) {
    }
}
Also used : ObjectId(org.bson.types.ObjectId) Decimal128(org.bson.types.Decimal128) Date(java.util.Date) Test(org.junit.Test)

Example 82 with Decimal128

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

the class JNITableTest method findFirstNonExisting.

@Test
public void findFirstNonExisting() {
    Table t = TestHelper.createTableWithAllColumnTypes(sharedRealm);
    long colKey1 = t.getColumnKey("binary");
    long colKey2 = t.getColumnKey("boolean");
    long colKey3 = t.getColumnKey("date");
    long colKey4 = t.getColumnKey("double");
    long colKey5 = t.getColumnKey("float");
    long colKey6 = t.getColumnKey("long");
    long colKey7 = t.getColumnKey("string");
    long colKey8 = t.getColumnKey("decimal128");
    long colKey9 = t.getColumnKey("object_id");
    sharedRealm.beginTransaction();
    TestHelper.addRowWithValues(t, new long[] { colKey1, colKey2, colKey3, colKey4, colKey5, colKey6, colKey7, colKey8, colKey9 }, new Object[] { new byte[] { 1, 2, 3 }, true, new Date(1384423149761L), 4.5D, 5.7F, 100, "string", new Decimal128(0), new ObjectId(TestHelper.generateObjectIdHexString(0)) });
    sharedRealm.commitTransaction();
    assertEquals(-1, t.findFirstBoolean(colKey2, false));
    assertEquals(-1, t.findFirstDate(colKey3, new Date(138442314986L)));
    assertEquals(-1, t.findFirstDouble(colKey4, 1.0D));
    assertEquals(-1, t.findFirstFloat(colKey5, 1.0F));
    assertEquals(-1, t.findFirstLong(colKey6, 50));
    assertEquals(-1, t.findFirstString(colKey7, "anotherstring"));
    assertEquals(-1, t.findFirstDecimal128(colKey8, new Decimal128(1)));
    assertEquals(-1, t.findFirstObjectId(colKey9, new ObjectId(TestHelper.generateObjectIdHexString(1))));
}
Also used : ObjectId(org.bson.types.ObjectId) Decimal128(org.bson.types.Decimal128) Date(java.util.Date) Test(org.junit.Test)

Example 83 with Decimal128

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

the class RealmResultsTests method setValue_specificType_internalNameOnDynamicRealms.

@Test
public void setValue_specificType_internalNameOnDynamicRealms() {
    populateMappedAllJavaTypes(5);
    DynamicRealm dynamicRealm = DynamicRealm.getInstance(realm.getConfiguration());
    dynamicRealm.beginTransaction();
    try {
        RealmResults<DynamicRealmObject> collection = dynamicRealm.where("MappedAllJavaTypes").findAll();
        for (BulkSetMethods type : BulkSetMethods.values()) {
            switch(type) {
                case STRING:
                    collection.setString("field_string", "foo");
                    assertElements(collection, obj -> assertEquals("foo", obj.getString("field_string")));
                    break;
                case BOOLEAN:
                    collection.setBoolean("field_boolean", true);
                    assertElements(collection, obj -> assertTrue(obj.getBoolean("field_boolean")));
                    break;
                case BYTE:
                    collection.setByte("field_byte", (byte) 1);
                    assertElements(collection, obj -> assertEquals((byte) 1, obj.getByte("field_byte")));
                    break;
                case SHORT:
                    collection.setShort("field_short", (short) 2);
                    assertElements(collection, obj -> assertEquals((short) 2, obj.getShort("field_short")));
                    break;
                case INTEGER:
                    collection.setInt("field_int", 3);
                    assertElements(collection, obj -> assertEquals(3, obj.getInt("field_int")));
                    break;
                case LONG:
                    collection.setLong("field_long", 4L);
                    assertElements(collection, obj -> assertEquals(4L, obj.getLong("field_long")));
                    break;
                case FLOAT:
                    collection.setFloat("field_float", 1.23F);
                    assertElements(collection, obj -> assertEquals(1.23F, obj.getFloat("field_float"), 0F));
                    break;
                case DOUBLE:
                    collection.setDouble("field_double", 1.234);
                    assertElements(collection, obj -> assertEquals(1.234, obj.getDouble("field_double"), 0F));
                    break;
                case BINARY:
                    collection.setBlob("field_binary", new byte[] { 1, 2, 3 });
                    assertElements(collection, obj -> assertArrayEquals(new byte[] { 1, 2, 3 }, obj.getBlob("field_binary")));
                    break;
                case DATE:
                    collection.setDate("field_date", new Date(1000));
                    assertElements(collection, obj -> assertEquals(new Date(1000), obj.getDate("field_date")));
                    break;
                case DECIMAL128:
                    collection.setDecimal128("field_decimal128", new Decimal128(1000));
                    assertElements(collection, obj -> assertEquals(new Decimal128(1000), obj.getDecimal128("field_decimal128")));
                    break;
                case OBJECT_ID:
                    {
                        String hex = TestHelper.randomObjectIdHexString();
                        collection.setObjectId("field_object_id", new ObjectId(hex));
                        assertElements(collection, obj -> assertEquals(new ObjectId(hex), obj.getObjectId("field_object_id")));
                        break;
                    }
                case UUID:
                    {
                        String uuid = UUID.randomUUID().toString();
                        collection.setUUID("field_uuid", UUID.fromString(uuid));
                        assertElements(collection, obj -> assertEquals(UUID.fromString(uuid), obj.getUUID("field_uuid")));
                        break;
                    }
                case OBJECT:
                    {
                        DynamicRealmObject childObj = dynamicRealm.createObject("MappedAllJavaTypes", 42);
                        collection.setObject("field_object", childObj);
                        assertElements(collection, obj -> assertEquals(childObj, obj.getObject("field_object")));
                        break;
                    }
                case MODEL_LIST:
                    {
                        DynamicRealmObject childObj = dynamicRealm.createObject("MappedAllJavaTypes", 43);
                        collection.setList("field_list", new RealmList<>(childObj));
                        assertElements(collection, obj -> {
                            RealmList<DynamicRealmObject> list = obj.getList("field_list");
                            assertEquals(1, list.size());
                            assertEquals(childObj, list.first());
                        });
                        break;
                    }
                case STRING_VALUE_LIST:
                    collection.setList("field_string_list", new RealmList<>("Foo"));
                    assertElements(collection, obj -> {
                        RealmList<String> list = obj.getList("field_string_list", String.class);
                        assertEquals(1, list.size());
                        assertEquals("Foo", list.first());
                    });
                    break;
                case BOOLEAN_VALUE_LIST:
                    collection.setList("field_boolean_list", new RealmList<>(true));
                    assertElements(collection, obj -> {
                        RealmList<Boolean> list = obj.getList("field_boolean_list", Boolean.class);
                        assertEquals(1, list.size());
                        assertEquals(true, list.first());
                    });
                    break;
                case BYTE_VALUE_LIST:
                    collection.setList("field_byte_list", new RealmList<>((byte) 1));
                    assertElements(collection, obj -> {
                        RealmList<Byte> list = obj.getList("field_byte_list", Byte.class);
                        assertEquals(1, list.size());
                        assertEquals(Byte.valueOf((byte) 1), list.first());
                    });
                    break;
                case SHORT_VALUE_LIST:
                    collection.setList("field_short_list", new RealmList<>((short) 1));
                    assertElements(collection, obj -> {
                        RealmList<Short> list = obj.getList("field_short_list", Short.class);
                        assertEquals(1, list.size());
                        assertEquals(Short.valueOf((short) 1), list.first());
                    });
                    break;
                case INTEGER_VALUE_LIST:
                    collection.setList("field_integer_list", new RealmList<>(1));
                    assertElements(collection, obj -> {
                        RealmList<Integer> list = obj.getList("field_integer_list", Integer.class);
                        assertEquals(1, list.size());
                        assertEquals(Integer.valueOf(1), list.first());
                    });
                    break;
                case LONG_VALUE_LIST:
                    collection.setList("field_long_list", new RealmList<>(1L));
                    assertElements(collection, obj -> {
                        RealmList<Long> list = obj.getList("field_long_list", Long.class);
                        assertEquals(1, list.size());
                        assertEquals(Long.valueOf((byte) 1), list.first());
                    });
                    break;
                case FLOAT_VALUE_LIST:
                    collection.setList("field_float_list", new RealmList<>(1.1F));
                    assertElements(collection, obj -> {
                        RealmList<Float> list = obj.getList("field_float_list", Float.class);
                        assertEquals(1, list.size());
                        assertEquals(1.1F, list.first(), 0F);
                    });
                    break;
                case DOUBLE_VALUE_LIST:
                    collection.setList("field_double_list", new RealmList<>(1.1D));
                    assertElements(collection, obj -> {
                        RealmList<Double> list = obj.getList("field_double_list", Double.class);
                        assertEquals(1, list.size());
                        assertEquals(1.1D, list.first(), 0F);
                    });
                    break;
                case BINARY_VALUE_LIST:
                    collection.setList("field_binary_list", new RealmList<>(new byte[] { 1, 2, 3 }));
                    assertElements(collection, obj -> {
                        RealmList<byte[]> list = obj.getList("field_binary_list", byte[].class);
                        assertEquals(1, list.size());
                        assertArrayEquals(new byte[] { 1, 2, 3 }, list.first());
                    });
                    break;
                case DATE_VALUE_LIST:
                    collection.setList("field_date_list", new RealmList<>(new Date(1000)));
                    assertElements(collection, obj -> {
                        RealmList<Date> list = obj.getList("field_date_list", Date.class);
                        assertEquals(1, list.size());
                        assertEquals(new Date(1000), list.first());
                    });
                    break;
                case DECIMAL128_VALUE_LIST:
                    collection.setList("field_decimal_list", new RealmList<>(new Decimal128(1000)));
                    assertElements(collection, obj -> {
                        RealmList<Decimal128> list = obj.getList("field_decimal_list", Decimal128.class);
                        assertEquals(1, list.size());
                        assertEquals(new Decimal128(1000), list.first());
                    });
                    break;
                case OBJECT_ID_VALUE_LIST:
                    {
                        String hex = TestHelper.randomObjectIdHexString();
                        collection.setList("field_object_id_list", new RealmList<>(new ObjectId(hex)));
                        assertElements(collection, obj -> {
                            RealmList<ObjectId> list = obj.getList("field_object_id_list", ObjectId.class);
                            assertEquals(1, list.size());
                            assertEquals(new ObjectId(hex), list.first());
                        });
                        break;
                    }
                case UUID_VALUE_LIST:
                    {
                        String uuid = UUID.randomUUID().toString();
                        collection.setList("field_uuid_list", new RealmList<>(UUID.fromString(uuid)));
                        assertElements(collection, obj -> {
                            RealmList<UUID> list = obj.getList("field_uuid_list", UUID.class);
                            assertEquals(1, list.size());
                            assertEquals(UUID.fromString(uuid), list.first());
                        });
                        break;
                    }
                default:
                    fail("Unknown type: " + type);
            }
        }
    } finally {
        dynamicRealm.close();
    }
}
Also used : Arrays(java.util.Arrays) AllJavaTypes(io.realm.entities.AllJavaTypes) Date(java.util.Date) Dog(io.realm.entities.Dog) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) BigDecimal(java.math.BigDecimal) JSONException(org.json.JSONException) RunInLooperThread(io.realm.rule.RunInLooperThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StringOnly(io.realm.entities.StringOnly) After(org.junit.After) Assert.fail(org.junit.Assert.fail) DefaultValueOfField(io.realm.entities.DefaultValueOfField) NonLatinFieldNames(io.realm.entities.NonLatinFieldNames) TimeZone(java.util.TimeZone) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) RandomPrimaryKey(io.realm.entities.RandomPrimaryKey) RunWith(org.junit.runner.RunWith) SimpleDateFormat(java.text.SimpleDateFormat) JSONAssert(org.skyscreamer.jsonassert.JSONAssert) MappedAllJavaTypes(io.realm.entities.MappedAllJavaTypes) Calendar(java.util.Calendar) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) ExpectedException(org.junit.rules.ExpectedException) Decimal128(org.bson.types.Decimal128) Before(org.junit.Before) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) PrimaryKeyAsLong(io.realm.entities.PrimaryKeyAsLong) RealmLog(io.realm.log.RealmLog) Assert.assertNotNull(org.junit.Assert.assertNotNull) AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) CyclicType(io.realm.entities.CyclicType) TimeUnit(java.util.concurrent.TimeUnit) OsResults(io.realm.internal.OsResults) Mockito(org.mockito.Mockito) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) Owner(io.realm.entities.Owner) UiThreadTest(androidx.test.annotation.UiThreadTest) ObjectId(org.bson.types.ObjectId) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) UUID(java.util.UUID) ObjectId(org.bson.types.ObjectId) Decimal128(org.bson.types.Decimal128) Date(java.util.Date) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PrimaryKeyAsLong(io.realm.entities.PrimaryKeyAsLong) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 84 with Decimal128

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);
        }
    }
}
Also used : Arrays(java.util.Arrays) AllJavaTypes(io.realm.entities.AllJavaTypes) Date(java.util.Date) Dog(io.realm.entities.Dog) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) BigDecimal(java.math.BigDecimal) JSONException(org.json.JSONException) RunInLooperThread(io.realm.rule.RunInLooperThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StringOnly(io.realm.entities.StringOnly) After(org.junit.After) Assert.fail(org.junit.Assert.fail) DefaultValueOfField(io.realm.entities.DefaultValueOfField) NonLatinFieldNames(io.realm.entities.NonLatinFieldNames) TimeZone(java.util.TimeZone) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) RandomPrimaryKey(io.realm.entities.RandomPrimaryKey) RunWith(org.junit.runner.RunWith) SimpleDateFormat(java.text.SimpleDateFormat) JSONAssert(org.skyscreamer.jsonassert.JSONAssert) MappedAllJavaTypes(io.realm.entities.MappedAllJavaTypes) Calendar(java.util.Calendar) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) ExpectedException(org.junit.rules.ExpectedException) Decimal128(org.bson.types.Decimal128) Before(org.junit.Before) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) PrimaryKeyAsLong(io.realm.entities.PrimaryKeyAsLong) RealmLog(io.realm.log.RealmLog) Assert.assertNotNull(org.junit.Assert.assertNotNull) AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) CyclicType(io.realm.entities.CyclicType) TimeUnit(java.util.concurrent.TimeUnit) OsResults(io.realm.internal.OsResults) Mockito(org.mockito.Mockito) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) Owner(io.realm.entities.Owner) UiThreadTest(androidx.test.annotation.UiThreadTest) ObjectId(org.bson.types.ObjectId) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ObjectId(org.bson.types.ObjectId) Decimal128(org.bson.types.Decimal128) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) AllJavaTypes(io.realm.entities.AllJavaTypes) MappedAllJavaTypes(io.realm.entities.MappedAllJavaTypes) Date(java.util.Date) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 85 with Decimal128

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();
}
Also used : ObjectId(org.bson.types.ObjectId) AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) Decimal128(org.bson.types.Decimal128) Dog(io.realm.entities.Dog) Date(java.util.Date)

Aggregations

Decimal128 (org.bson.types.Decimal128)95 ObjectId (org.bson.types.ObjectId)53 Test (org.junit.Test)48 Date (java.util.Date)47 BigDecimal (java.math.BigDecimal)24 AllTypes (io.realm.entities.AllTypes)22 UUID (java.util.UUID)18 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 List (java.util.List)9 BsonDocument (org.bson.BsonDocument)9 PrimaryKeyAsObjectId (io.realm.entities.PrimaryKeyAsObjectId)8 JSONObject (org.json.JSONObject)8 MappedAllJavaTypes (io.realm.entities.MappedAllJavaTypes)7 BsonDecimal128 (org.bson.BsonDecimal128)7 Document (org.bson.Document)7 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)6