Search in sources :

Example 86 with Decimal128

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

Example 87 with Decimal128

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());
}
Also used : JSONObject(org.json.JSONObject) PrimaryKeyAsObjectId(io.realm.entities.PrimaryKeyAsObjectId) ObjectId(org.bson.types.ObjectId) AllTypes(io.realm.entities.AllTypes) Decimal128(org.bson.types.Decimal128) UUID(java.util.UUID) Date(java.util.Date) Test(org.junit.Test)

Example 88 with Decimal128

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

Example 89 with Decimal128

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

the class RealmJsonTests method createObjectFromJson_streamDecimal128AsLong.

@Test
public void createObjectFromJson_streamDecimal128AsLong() throws IOException {
    assumeThat(Build.VERSION.SDK_INT, greaterThanOrEqualTo(Build.VERSION_CODES.HONEYCOMB));
    InputStream in = TestHelper.loadJsonFromAssets(context, "decimal128_as_long.json");
    realm.beginTransaction();
    realm.createObjectFromJson(AllTypes.class, in);
    realm.commitTransaction();
    in.close();
    AllTypes obj = realm.where(AllTypes.class).findFirst();
    assertEquals(new Decimal128(-32361122672259149L), obj.getColumnDecimal128());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AllTypes(io.realm.entities.AllTypes) Decimal128(org.bson.types.Decimal128) Test(org.junit.Test)

Example 90 with Decimal128

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

the class RealmJsonTests method assertAllTypesPrimaryKeyUpdated.

// Asserts that the list of AllTypesPrimaryKey objects where inserted and updated properly.
private void assertAllTypesPrimaryKeyUpdated() {
    assertEquals(1, realm.where(AllTypesPrimaryKey.class).count());
    AllTypesPrimaryKey obj = realm.where(AllTypesPrimaryKey.class).findFirst();
    assertEquals("Bar", obj.getColumnString());
    assertEquals(2.23F, obj.getColumnFloat(), 0F);
    assertEquals(2.234D, obj.getColumnDouble(), 0D);
    assertEquals(true, obj.isColumnBoolean());
    assertArrayEquals(new byte[] { 1, 2, 3 }, obj.getColumnBinary());
    assertEquals(new Date(2000), obj.getColumnDate());
    assertEquals(new ObjectId("789ABCDEF0123456789ABCDA"), obj.getColumnObjectId());
    assertEquals(new Decimal128(-43), obj.getColumnDecimal128());
    assertEquals(UUID.fromString("027ba5ca-aa12-4afa-9219-e20cc3018590"), obj.getColumnUUID());
    assertEquals(RealmAny.valueOf("hello world"), obj.getColumnRealmAny());
    assertEquals("Dog4", obj.getColumnRealmObject().getName());
    assertEquals(2, obj.getColumnRealmList().size());
    assertEquals("Dog5", obj.getColumnRealmList().get(0).getName());
}
Also used : PrimaryKeyAsObjectId(io.realm.entities.PrimaryKeyAsObjectId) ObjectId(org.bson.types.ObjectId) Decimal128(org.bson.types.Decimal128) Date(java.util.Date) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey)

Aggregations

Decimal128 (org.bson.types.Decimal128)96 ObjectId (org.bson.types.ObjectId)53 Test (org.junit.Test)48 Date (java.util.Date)47 BigDecimal (java.math.BigDecimal)25 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 Document (org.bson.Document)8 JSONObject (org.json.JSONObject)8 MappedAllJavaTypes (io.realm.entities.MappedAllJavaTypes)7 BsonDecimal128 (org.bson.BsonDecimal128)7 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)6