Search in sources :

Example 11 with NullTypes

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

the class DynamicRealmObjectTests method setter_null.

@Test
public void setter_null() {
    realm.beginTransaction();
    NullTypes obj = realm.createObject(NullTypes.class, 0);
    DynamicRealmObject dObj = new DynamicRealmObject(obj);
    try {
        for (SupportedType type : SupportedType.values()) {
            switch(type) {
                case OBJECT:
                    NullTypes childObj = new NullTypes();
                    childObj.setId(1);
                    DynamicRealmObject dynamicChildObject = new DynamicRealmObject(realm.copyToRealm(childObj));
                    dObj.setObject(NullTypes.FIELD_OBJECT_NULL, dynamicChildObject);
                    assertNotNull(dObj.getObject(NullTypes.FIELD_OBJECT_NULL));
                    dObj.setNull(NullTypes.FIELD_OBJECT_NULL);
                    assertNull(dObj.getObject(NullTypes.FIELD_OBJECT_NULL));
                    break;
                case LIST:
                    try {
                        dObj.setNull(NullTypes.FIELD_LIST_NULL);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case LIST_INTEGER:
                    try {
                        dObj.setNull(NullTypes.FIELD_INTEGER_LIST_NULL);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case LIST_STRING:
                    try {
                        dObj.setNull(NullTypes.FIELD_STRING_LIST_NULL);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case LIST_BOOLEAN:
                    try {
                        dObj.setNull(NullTypes.FIELD_BOOLEAN_LIST_NULL);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case LIST_FLOAT:
                    try {
                        dObj.setNull(NullTypes.FIELD_FLOAT_LIST_NULL);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case LIST_DOUBLE:
                    try {
                        dObj.setNull(NullTypes.FIELD_DOUBLE_LIST_NULL);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case LIST_BINARY:
                    try {
                        dObj.setNull(NullTypes.FIELD_BINARY_LIST_NULL);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case LIST_DATE:
                    try {
                        dObj.setNull(NullTypes.FIELD_DATE_LIST_NULL);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case LIST_DECIMAL128:
                    try {
                        dObj.setNull(NullTypes.FIELD_DECIMAL128_LIST_NULL);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case LIST_OBJECT_ID:
                    try {
                        dObj.setNull(NullTypes.FIELD_OBJECT_ID_LIST_NULL);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case LIST_UUID:
                    try {
                        dObj.setNull(NullTypes.FIELD_UUID_LIST_NULL);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case BOOLEAN:
                    dObj.setNull(NullTypes.FIELD_BOOLEAN_NULL);
                    assertTrue(dObj.isNull(NullTypes.FIELD_BOOLEAN_NULL));
                    break;
                case BYTE:
                    dObj.setNull(NullTypes.FIELD_BYTE_NULL);
                    assertTrue(dObj.isNull(NullTypes.FIELD_BYTE_NULL));
                    break;
                case SHORT:
                    dObj.setNull(NullTypes.FIELD_SHORT_NULL);
                    assertTrue(dObj.isNull(NullTypes.FIELD_SHORT_NULL));
                    break;
                case INT:
                    dObj.setNull(NullTypes.FIELD_INTEGER_NULL);
                    assertTrue(dObj.isNull(NullTypes.FIELD_INTEGER_NULL));
                    break;
                case LONG:
                    dObj.setNull(NullTypes.FIELD_LONG_NULL);
                    assertTrue(dObj.isNull(NullTypes.FIELD_LONG_NULL));
                    break;
                case FLOAT:
                    dObj.setNull(NullTypes.FIELD_FLOAT_NULL);
                    assertTrue(dObj.isNull(NullTypes.FIELD_FLOAT_NULL));
                    break;
                case DOUBLE:
                    dObj.setNull(NullTypes.FIELD_DOUBLE_NULL);
                    assertTrue(dObj.isNull(NullTypes.FIELD_DOUBLE_NULL));
                    break;
                case STRING:
                    dObj.setNull(NullTypes.FIELD_STRING_NULL);
                    assertTrue(dObj.isNull(NullTypes.FIELD_STRING_NULL));
                    break;
                case BINARY:
                    dObj.setNull(NullTypes.FIELD_BYTES_NULL);
                    assertTrue(dObj.isNull(NullTypes.FIELD_BYTES_NULL));
                    break;
                case DATE:
                    dObj.setNull(NullTypes.FIELD_DATE_NULL);
                    assertTrue(dObj.isNull(NullTypes.FIELD_DATE_NULL));
                    break;
                case DECIMAL128:
                    dObj.setNull(NullTypes.FIELD_DECIMAL128_NULL);
                    assertTrue(dObj.isNull(NullTypes.FIELD_DECIMAL128_NULL));
                    break;
                case OBJECT_ID:
                    dObj.setNull(NullTypes.FIELD_OBJECT_ID_NULL);
                    assertTrue(dObj.isNull(NullTypes.FIELD_OBJECT_ID_NULL));
                    break;
                case UUID:
                    dObj.setNull(NullTypes.FIELD_UUID_NULL);
                    assertTrue(dObj.isNull(NullTypes.FIELD_UUID_NULL));
                    break;
                default:
                    fail("Unknown type: " + type);
            }
        }
    } finally {
        realm.cancelTransaction();
    }
}
Also used : NullTypes(io.realm.entities.NullTypes) Test(org.junit.Test)

Example 12 with NullTypes

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

the class DynamicRealmObjectTests method typedSetter_null.

// Tests types where you can set null using the typed setter instead of using setNull().
@Test
public void typedSetter_null() {
    realm.beginTransaction();
    NullTypes obj = realm.createObject(NullTypes.class, 0);
    DynamicRealmObject dObj = new DynamicRealmObject(obj);
    try {
        for (SupportedType type : SupportedType.values()) {
            switch(type) {
                case OBJECT:
                    dObj.setObject(NullTypes.FIELD_OBJECT_NULL, null);
                    assertNull(dObj.getObject(NullTypes.FIELD_OBJECT_NULL));
                    break;
                case LIST:
                    try {
                        dObj.setList(NullTypes.FIELD_LIST_NULL, null);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case LIST_INTEGER:
                    try {
                        dObj.setList(NullTypes.FIELD_INTEGER_LIST_NULL, null);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case LIST_STRING:
                    try {
                        dObj.setList(NullTypes.FIELD_STRING_LIST_NULL, null);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case LIST_BOOLEAN:
                    try {
                        dObj.setList(NullTypes.FIELD_BOOLEAN_LIST_NULL, null);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case LIST_FLOAT:
                    try {
                        dObj.setList(NullTypes.FIELD_FLOAT_LIST_NULL, null);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case LIST_DOUBLE:
                    try {
                        dObj.setList(NullTypes.FIELD_DOUBLE_LIST_NULL, null);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case LIST_BINARY:
                    try {
                        dObj.setList(NullTypes.FIELD_BINARY_LIST_NULL, null);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case LIST_DATE:
                    try {
                        dObj.setList(NullTypes.FIELD_DATE_LIST_NULL, null);
                        fail();
                    } catch (IllegalArgumentException ignored) {
                    }
                    break;
                case DATE:
                    dObj.setDate(NullTypes.FIELD_DATE_NULL, null);
                    assertNull(dObj.getDate(NullTypes.FIELD_DATE_NULL));
                    break;
                case STRING:
                    dObj.setString(NullTypes.FIELD_STRING_NULL, null);
                    assertNull(dObj.getString(NullTypes.FIELD_STRING_NULL));
                    break;
                case BINARY:
                    dObj.setBlob(NullTypes.FIELD_BYTES_NULL, null);
                    assertNull(dObj.getBlob(NullTypes.FIELD_BYTES_NULL));
                    break;
                case BOOLEAN:
                case SHORT:
                case INT:
                case LONG:
                case FLOAT:
                case DOUBLE:
                default:
            }
        }
    } finally {
        realm.cancelTransaction();
    }
}
Also used : NullTypes(io.realm.entities.NullTypes) Test(org.junit.Test)

Example 13 with NullTypes

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

the class CollectionTests method populatePartialNullRowsForNumericTesting.

// Creates a number of objects that mix null and real values for number type fields.
protected void populatePartialNullRowsForNumericTesting(Realm realm) {
    NullTypes nullTypes1 = new NullTypes();
    nullTypes1.setId(1);
    nullTypes1.setFieldIntegerNull(1);
    nullTypes1.setFieldFloatNull(2F);
    nullTypes1.setFieldDoubleNull(3D);
    nullTypes1.setFieldBooleanNull(true);
    nullTypes1.setFieldStringNull("4");
    nullTypes1.setFieldDateNull(new Date(12345));
    NullTypes nullTypes2 = new NullTypes();
    nullTypes2.setId(2);
    NullTypes nullTypes3 = new NullTypes();
    nullTypes3.setId(3);
    nullTypes3.setFieldIntegerNull(0);
    nullTypes3.setFieldFloatNull(0F);
    nullTypes3.setFieldDoubleNull(0D);
    nullTypes3.setFieldBooleanNull(false);
    nullTypes3.setFieldStringNull("0");
    nullTypes3.setFieldDateNull(new Date(0));
    realm.beginTransaction();
    realm.copyToRealm(nullTypes1);
    realm.copyToRealm(nullTypes2);
    realm.copyToRealm(nullTypes3);
    realm.commitTransaction();
}
Also used : NullTypes(io.realm.entities.NullTypes) Date(java.util.Date)

Example 14 with NullTypes

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

the class ManagedOrderedRealmCollectionTests method createEmptyCollection.

private OrderedRealmCollection<NullTypes> createEmptyCollection(Realm realm, ManagedCollection 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 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 15 with NullTypes

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

the class ManagedOrderedRealmCollectionTests method sort_rowsWithPartialNullValues.

@Test
public void sort_rowsWithPartialNullValues() {
    if (isSnapshot(collectionClass)) {
        thrown.expect(UnsupportedOperationException.class);
    }
    populatePartialNullRowsForNumericTesting(realm);
    OrderedRealmCollection<NullTypes> original;
    OrderedRealmCollection<NullTypes> copy;
    switch(collectionClass) {
        case REALMRESULTS_SNAPSHOT_LIST_BASE:
        case MANAGED_REALMLIST:
            realm.beginTransaction();
            RealmResults<NullTypes> objects = realm.where(NullTypes.class).findAll();
            NullTypes parent = realm.createObject(NullTypes.class, 0);
            for (int i = 0; i < objects.size(); i++) {
                NullTypes object = objects.get(i);
                if (object.getId() != 0) {
                    parent.getFieldListNull().add(object);
                }
            }
            realm.commitTransaction();
            original = parent.getFieldListNull().where().findAll();
            copy = parent.getFieldListNull();
            break;
        case REALMRESULTS_SNAPSHOT_RESULTS_BASE:
        case REALMRESULTS:
            original = realm.where(NullTypes.class).findAll();
            copy = realm.where(NullTypes.class).findAll();
            break;
        default:
            throw new AssertionError("Unknown collection class: " + collectionClass);
    }
    if (isSnapshot(collectionClass)) {
        copy = copy.createSnapshot();
    }
    // 1 String
    doTestSortOnColumnWithPartialNullValues(NullTypes.FIELD_STRING_NULL, original, copy);
    // 3 Boolean
    doTestSortOnColumnWithPartialNullValues(NullTypes.FIELD_BOOLEAN_NULL, original, copy);
    // 6 Integer
    doTestSortOnColumnWithPartialNullValues(NullTypes.FIELD_INTEGER_NULL, original, copy);
    // 7 Float
    doTestSortOnColumnWithPartialNullValues(NullTypes.FIELD_FLOAT_NULL, original, copy);
    // 8 Double
    doTestSortOnColumnWithPartialNullValues(NullTypes.FIELD_DOUBLE_NULL, original, copy);
    // 10 Date
    doTestSortOnColumnWithPartialNullValues(NullTypes.FIELD_DATE_NULL, original, copy);
}
Also used : 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