Search in sources :

Example 6 with NullTypes

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

the class RealmJsonTests method createAllFromJson_nullTypesJsonWithNulls.

// Tests creating objects from Json, all nullable fields with null values or non-null values.
@Test
public void createAllFromJson_nullTypesJsonWithNulls() throws IOException, JSONException {
    String json = TestHelper.streamToString(TestHelper.loadJsonFromAssets(context, "nulltypes.json"));
    JSONArray array = new JSONArray(json);
    realm.beginTransaction();
    realm.createAllFromJson(NullTypes.class, array);
    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 : JSONArray(org.json.JSONArray) NoPrimaryKeyNullTypes(io.realm.entities.NoPrimaryKeyNullTypes) NullTypes(io.realm.entities.NullTypes) Test(org.junit.Test)

Example 7 with NullTypes

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

the class RealmObjectTests method get_set_nonNullValueOnNullableFields.

// Stores and retrieves non-null values when field can contain null strings.
@Test
public void get_set_nonNullValueOnNullableFields() {
    final String testString = "FooBar";
    final byte[] testBytes = new byte[] { 42 };
    final Date testDate = newDate(2000, 1, 1);
    realm.beginTransaction();
    NullTypes nullTypes = realm.createObject(NullTypes.class, 0);
    // 1 String
    nullTypes.setFieldStringNull(testString);
    // 2 Bytes
    nullTypes.setFieldBytesNull(testBytes);
    // 3 Boolean
    nullTypes.setFieldBooleanNull(true);
    // 4 Byte
    nullTypes.setFieldByteNull((byte) 42);
    // 5 Short
    nullTypes.setFieldShortNull((short) 42);
    // 6 Integer
    nullTypes.setFieldIntegerNull(42);
    // 7 Long
    nullTypes.setFieldLongNull(42L);
    // 8 Float
    nullTypes.setFieldFloatNull(42.42F);
    // 9 Double
    nullTypes.setFieldDoubleNull(42.42D);
    // 10 Date
    nullTypes.setFieldDateNull(testDate);
    realm.commitTransaction();
    nullTypes = realm.where(NullTypes.class).findFirst();
    // 1 String
    assertEquals(testString, nullTypes.getFieldStringNull());
    // 2 Bytes
    assertArrayEquals(testBytes, nullTypes.getFieldBytesNull());
    // 3 Boolean
    assertTrue(nullTypes.getFieldBooleanNull());
    // 4 Byte
    assertEquals((byte) 42, (byte) nullTypes.getFieldByteNull().intValue());
    // 5 Short
    assertEquals((short) 42, (short) nullTypes.getFieldShortNull().intValue());
    // 6 Integer
    assertEquals(42, nullTypes.getFieldIntegerNull().intValue());
    // 7 Long
    assertEquals(42L, nullTypes.getFieldLongNull().longValue());
    // 8 Float
    assertEquals(42.42F, nullTypes.getFieldFloatNull(), 0.0F);
    // 9 Double
    assertEquals(42.42D, nullTypes.getFieldDoubleNull(), 0.0D);
    // 10 Date
    assertEquals(testDate.getTime(), nullTypes.getFieldDateNull().getTime());
}
Also used : NullTypes(io.realm.entities.NullTypes) Date(java.util.Date) Test(org.junit.Test)

Example 8 with NullTypes

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

the class ManagedRealmListForValue_toArrayTests method setUp.

@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
    final RealmConfiguration.Builder configurationBuilder = configFactory.createConfigurationBuilder();
    configurationBuilder.schema(NullTypes.class);
    RealmConfiguration realmConfig = configurationBuilder.build();
    realm = Realm.getInstance(realmConfig);
    realm.beginTransaction();
    final NullTypes object = realm.createObject(NullTypes.class, 0);
    for (ListType type : ListType.values()) {
        for (int i = 0; i < NON_NULL_TEST_SIZE; i++) {
            switch(type) {
                case STRING_LIST:
                    {
                        final RealmList nonnull = object.getFieldStringListNotNull();
                        nonnull.add(generateValue(STRING_LIST, i));
                        final RealmList nullable = object.getFieldStringListNull();
                        nullable.add("" + i);
                        nullable.add(null);
                    }
                    break;
                case BOOLEAN_LIST:
                    {
                        final RealmList nonnull = object.getFieldBooleanListNotNull();
                        nonnull.add(generateValue(BOOLEAN_LIST, i));
                        final RealmList nullable = object.getFieldBooleanListNull();
                        nullable.add(nonnull.last());
                        nullable.add(null);
                    }
                    break;
                case BINARY_LIST:
                    {
                        final RealmList nonnull = object.getFieldBinaryListNotNull();
                        nonnull.add(generateValue(BINARY_LIST, i));
                        final RealmList nullable = object.getFieldBinaryListNull();
                        nullable.add(nonnull.last());
                        nullable.add(null);
                    }
                    break;
                case LONG_LIST:
                    {
                        final RealmList nonnull = object.getFieldLongListNotNull();
                        nonnull.add(generateValue(LONG_LIST, i));
                        final RealmList nullable = object.getFieldLongListNull();
                        nullable.add(nonnull.last());
                        nullable.add(null);
                    }
                    break;
                case INTEGER_LIST:
                    {
                        final RealmList nonnull = object.getFieldIntegerListNotNull();
                        nonnull.add(generateValue(INTEGER_LIST, i));
                        final RealmList nullable = object.getFieldIntegerListNull();
                        nullable.add(nonnull.last());
                        nullable.add(null);
                    }
                    break;
                case SHORT_LIST:
                    {
                        final RealmList nonnull = object.getFieldShortListNotNull();
                        nonnull.add(generateValue(SHORT_LIST, i));
                        final RealmList nullable = object.getFieldShortListNull();
                        nullable.add(nonnull.last());
                        nullable.add(null);
                    }
                    break;
                case BYTE_LIST:
                    {
                        final RealmList nonnull = object.getFieldByteListNotNull();
                        nonnull.add(generateValue(BYTE_LIST, i));
                        final RealmList nullable = object.getFieldByteListNull();
                        nullable.add(nonnull.last());
                        nullable.add(null);
                    }
                    break;
                case DOUBLE_LIST:
                    {
                        final RealmList nonnull = object.getFieldDoubleListNotNull();
                        nonnull.add(generateValue(DOUBLE_LIST, i));
                        final RealmList nullable = object.getFieldDoubleListNull();
                        nullable.add(nonnull.last());
                        nullable.add(null);
                    }
                    break;
                case FLOAT_LIST:
                    {
                        final RealmList nonnull = object.getFieldFloatListNotNull();
                        nonnull.add(generateValue(FLOAT_LIST, i));
                        final RealmList nullable = object.getFieldFloatListNull();
                        nullable.add(nonnull.last());
                        nullable.add(null);
                    }
                    break;
                case DATE_LIST:
                    {
                        final RealmList nonnull = object.getFieldDateListNotNull();
                        nonnull.add(generateValue(DATE_LIST, i));
                        final RealmList nullable = object.getFieldDateListNull();
                        nullable.add(nonnull.last());
                        nullable.add(null);
                    }
                    break;
                default:
                    throw new AssertionError("unexpected value type: " + listType.name());
            }
        }
    }
    realm.commitTransaction();
    list = getListFor(object, listType, typeIsNullable);
}
Also used : ListType(io.realm.ManagedRealmListForValueTests.ListType) NullTypes(io.realm.entities.NullTypes) Before(org.junit.Before)

Example 9 with NullTypes

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

the class BulkInsertTests method insertOrUpdate_nullTypes.

@Test
public void insertOrUpdate_nullTypes() {
    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));
    nullTypes1.setFieldDecimal128Null(new Decimal128(BigDecimal.TEN));
    nullTypes1.setFieldObjectIdNull(new ObjectId(TestHelper.generateObjectIdHexString(7)));
    nullTypes1.setFieldUUIDNull(UUID.fromString(TestHelper.generateUUIDString(7)));
    realm.beginTransaction();
    realm.insert(nullTypes1);
    realm.commitTransaction();
    NullTypes first = realm.where(NullTypes.class).findFirst();
    assertNotNull(first);
    assertEquals(nullTypes1.getId(), first.getId());
    assertEquals(nullTypes1.getFieldIntegerNull(), first.getFieldIntegerNull());
    assertEquals(nullTypes1.getFieldFloatNull(), first.getFieldFloatNull());
    assertEquals(nullTypes1.getFieldDoubleNull(), first.getFieldDoubleNull());
    assertEquals(nullTypes1.getFieldBooleanNull(), first.getFieldBooleanNull());
    assertEquals(nullTypes1.getFieldStringNull(), first.getFieldStringNull());
    assertEquals(nullTypes1.getFieldDateNull(), first.getFieldDateNull());
    assertEquals(nullTypes1.getFieldDecimal128Null(), first.getFieldDecimal128Null());
    assertEquals(nullTypes1.getFieldObjectIdNull(), first.getFieldObjectIdNull());
    assertEquals(nullTypes1.getFieldUUIDNull(), first.getFieldUUIDNull());
    NullTypes nullTypes2 = new NullTypes();
    nullTypes2.setId(2);
    NullTypes nullTypes3 = new NullTypes();
    nullTypes3.setId(3);
    nullTypes1 = new NullTypes();
    nullTypes1.setId(1);
    nullTypes1.setFieldIntegerNull(null);
    nullTypes1.setFieldFloatNull(null);
    nullTypes1.setFieldDoubleNull(null);
    nullTypes1.setFieldBooleanNull(null);
    nullTypes1.setFieldStringNull(null);
    nullTypes1.setFieldDateNull(null);
    nullTypes1.setFieldDecimal128Null(null);
    nullTypes1.setFieldObjectIdNull(null);
    nullTypes1.setFieldUUIDNull(null);
    nullTypes1.setFieldListNull(new RealmList<NullTypes>());
    nullTypes1.getFieldListNull().add(nullTypes2);
    nullTypes1.getFieldListNull().add(nullTypes3);
    OrderedRealmCollection<NullTypes> collection = new RealmList<NullTypes>();
    collection.add(nullTypes2);
    collection.add(nullTypes1);
    collection.add(nullTypes3);
    realm.beginTransaction();
    realm.insertOrUpdate(collection);
    realm.commitTransaction();
    first = realm.where(NullTypes.class).equalTo("id", 1).findFirst();
    assertNotNull(first);
    assertEquals(nullTypes1.getId(), first.getId());
    assertNull(first.getFieldIntegerNull());
    assertNull(first.getFieldFloatNull());
    assertNull(first.getFieldDoubleNull());
    assertNull(first.getFieldBooleanNull());
    assertNull(first.getFieldStringNull());
    assertNull(first.getFieldDateNull());
    assertNull(first.getFieldDecimal128Null());
    assertNull(first.getFieldObjectIdNull());
    assertNull(first.getFieldUUIDNull());
    assertEquals(2, first.getFieldListNull().size());
    assertEquals(2, first.getFieldListNull().get(0).getId());
    assertEquals(3, first.getFieldListNull().get(1).getId());
}
Also used : ObjectId(org.bson.types.ObjectId) NullTypes(io.realm.entities.NullTypes) Decimal128(org.bson.types.Decimal128) Date(java.util.Date) Test(org.junit.Test)

Example 10 with NullTypes

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

the class DynamicRealmObjectTests method setter_nullOnRequiredFieldsThrows.

@Test
public void setter_nullOnRequiredFieldsThrows() {
    realm.beginTransaction();
    NullTypes obj = realm.createObject(NullTypes.class, 0);
    DynamicRealmObject dObj = new DynamicRealmObject(obj);
    try {
        for (SupportedType type : SupportedType.values()) {
            String fieldName = null;
            try {
                switch(type) {
                    // Ignore
                    case OBJECT:
                        continue;
                    case LIST:
                        fieldName = NullTypes.FIELD_LIST_NULL;
                        break;
                    case LIST_INTEGER:
                        fieldName = NullTypes.FIELD_INTEGER_LIST_NULL;
                        break;
                    case LIST_STRING:
                        fieldName = NullTypes.FIELD_STRING_LIST_NULL;
                        break;
                    case LIST_BOOLEAN:
                        fieldName = NullTypes.FIELD_BOOLEAN_LIST_NULL;
                        break;
                    case LIST_FLOAT:
                        fieldName = NullTypes.FIELD_FLOAT_LIST_NULL;
                        break;
                    case LIST_DOUBLE:
                        fieldName = NullTypes.FIELD_DOUBLE_LIST_NULL;
                        break;
                    case LIST_BINARY:
                        fieldName = NullTypes.FIELD_BINARY_LIST_NULL;
                        break;
                    case LIST_DATE:
                        fieldName = NullTypes.FIELD_DATE_LIST_NULL;
                        break;
                    case LIST_DECIMAL128:
                        fieldName = NullTypes.FIELD_DECIMAL128_LIST_NULL;
                        break;
                    case LIST_OBJECT_ID:
                        fieldName = NullTypes.FIELD_OBJECT_ID_LIST_NULL;
                        break;
                    case LIST_UUID:
                        fieldName = NullTypes.FIELD_UUID_LIST_NULL;
                        break;
                    case BOOLEAN:
                        fieldName = NullTypes.FIELD_BOOLEAN_NOT_NULL;
                        break;
                    case BYTE:
                        fieldName = NullTypes.FIELD_BYTE_NOT_NULL;
                        break;
                    case SHORT:
                        fieldName = NullTypes.FIELD_SHORT_NOT_NULL;
                        break;
                    case INT:
                        fieldName = NullTypes.FIELD_INTEGER_NOT_NULL;
                        break;
                    case LONG:
                        fieldName = NullTypes.FIELD_LONG_NOT_NULL;
                        break;
                    case FLOAT:
                        fieldName = NullTypes.FIELD_FLOAT_NOT_NULL;
                        break;
                    case DOUBLE:
                        fieldName = NullTypes.FIELD_DOUBLE_NOT_NULL;
                        break;
                    case STRING:
                        fieldName = NullTypes.FIELD_STRING_NOT_NULL;
                        break;
                    case BINARY:
                        fieldName = NullTypes.FIELD_BYTES_NOT_NULL;
                        break;
                    case DATE:
                        fieldName = NullTypes.FIELD_DATE_NOT_NULL;
                        break;
                    case DECIMAL128:
                        fieldName = NullTypes.FIELD_DECIMAL128_NOT_NULL;
                        break;
                    case OBJECT_ID:
                        fieldName = NullTypes.FIELD_OBJECT_ID_NOT_NULL;
                        break;
                    case UUID:
                        fieldName = NullTypes.FIELD_UUID_NOT_NULL;
                        break;
                    default:
                        fail("Unknown type: " + type);
                }
                dObj.setNull(fieldName);
                fail("Setting value to null should throw: " + type);
            } catch (IllegalArgumentException ignored) {
                assertTrue(ignored.getMessage().contains(fieldName));
            }
        }
    } finally {
        realm.cancelTransaction();
    }
}
Also used : NullTypes(io.realm.entities.NullTypes) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) 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