Search in sources :

Example 21 with NullTypes

use of io.realm.entities.NullTypes 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 22 with NullTypes

use of io.realm.entities.NullTypes in project realm-java by realm.

the class RealmCollectionTests method createEmptyCollection.

private OrderedRealmCollection<NullTypes> createEmptyCollection(Realm realm, CollectionClass collectionClass) {
    OrderedRealmCollection<NullTypes> orderedCollection;
    switch(collectionClass) {
        case REALMRESULTS_SNAPSHOT_LIST_BASE:
        case MANAGED_REALMLIST:
            realm.beginTransaction();
            NullTypes obj = realm.createObject(NullTypes.class, 0);
            realm.commitTransaction();
            orderedCollection = obj.getFieldListNull();
            break;
        case UNMANAGED_REALMLIST:
            return new RealmList<NullTypes>();
        case REALMRESULTS_SNAPSHOT_RESULTS_BASE:
        case REALMRESULTS:
            orderedCollection = realm.where(NullTypes.class).findAll();
            break;
        default:
            throw new AssertionError("Unknown collection: " + collectionClass);
    }
    if (isSnapshot(collectionClass)) {
        orderedCollection = orderedCollection.createSnapshot();
    }
    return orderedCollection;
}
Also used : NullTypes(io.realm.entities.NullTypes)

Example 23 with NullTypes

use of io.realm.entities.NullTypes in project realm-java by realm.

the class RealmJsonTests method createAllFromJson_nullTypesStreamJSONWithNulls.

// Tests creating objects form JSON stream, all nullable fields with null values or non-null values.
@Test
public void createAllFromJson_nullTypesStreamJSONWithNulls() throws IOException {
    assumeThat(Build.VERSION.SDK_INT, greaterThanOrEqualTo(Build.VERSION_CODES.HONEYCOMB));
    realm.beginTransaction();
    realm.createAllFromJson(NullTypes.class, TestHelper.loadJsonFromAssets(context, "nulltypes.json"));
    realm.commitTransaction();
    RealmResults<NullTypes> nullTypesRealmResults = realm.where(NullTypes.class).findAll();
    assertEquals(3, nullTypesRealmResults.size());
    NullTypes nullTypes1 = nullTypesRealmResults.where().equalTo("id", 1).findFirst();
    checkNullableValuesAreNull(nullTypes1);
    NullTypes nullTypes2 = nullTypesRealmResults.where().equalTo("id", 2).findFirst();
    checkNullableValuesAreNotNull(nullTypes2);
}
Also used : NoPrimaryKeyNullTypes(io.realm.entities.NoPrimaryKeyNullTypes) NullTypes(io.realm.entities.NullTypes) Test(org.junit.Test)

Aggregations

NullTypes (io.realm.entities.NullTypes)23 Test (org.junit.Test)13 Date (java.util.Date)8 NoPrimaryKeyNullTypes (io.realm.entities.NoPrimaryKeyNullTypes)4 Decimal128 (org.bson.types.Decimal128)3 ObjectId (org.bson.types.ObjectId)3 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)2 JSONArray (org.json.JSONArray)2 UiThreadTest (androidx.test.annotation.UiThreadTest)1 ListType (io.realm.ManagedRealmListForValueTests.ListType)1 JSONObject (org.json.JSONObject)1 Before (org.junit.Before)1