use of org.bson.types.Decimal128 in project realm-java by realm.
the class RealmJsonTests method createObjectFromJson_primitiveList_realmAnyValues.
@Test
public void createObjectFromJson_primitiveList_realmAnyValues() throws JSONException, IOException {
testPrimitiveListWithValues(PrimitiveListTypes.FIELD_STRING_LIST, new String[] { "a", null, "bc" });
testPrimitiveListWithValues(PrimitiveListTypes.FIELD_BOOLEAN_LIST, new Boolean[] { true, null, false });
testPrimitiveListWithValues(PrimitiveListTypes.FIELD_DOUBLE_LIST, new Double[] { 1.0d, null, 2.0d });
testPrimitiveListWithValues(PrimitiveListTypes.FIELD_FLOAT_LIST, new Float[] { 1.0f, null, 2.0f });
testPrimitiveListWithValues(PrimitiveListTypes.FIELD_BYTE_LIST, new Byte[] { 1, null, 2 });
testPrimitiveListWithValues(PrimitiveListTypes.FIELD_SHORT_LIST, new Short[] { 1, null, 2 });
testPrimitiveListWithValues(PrimitiveListTypes.FIELD_INT_LIST, new Integer[] { 1, null, 2 });
testPrimitiveListWithValues(PrimitiveListTypes.FIELD_LONG_LIST, new Long[] { 1L, null, 2L });
// Date as integer
testPrimitiveListWithValues(PrimitiveListTypes.FIELD_DATE_LIST, new Integer[] { 0, null, 1 }, new Date[] { new Date(0), null, new Date(1) });
// Date as String
testPrimitiveListWithValues(PrimitiveListTypes.FIELD_DATE_LIST, new String[] { "/Date(1000)/", null, "/Date(2000)/" }, new Date[] { new Date(1000), null, new Date(2000) });
// Date as String timezone
// Oct 03 2015 14:45.33
Calendar cal = GregorianCalendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("Australia/West"));
cal.set(2015, Calendar.OCTOBER, 3, 14, 45, 33);
cal.set(Calendar.MILLISECOND, 376);
testPrimitiveListWithValues(PrimitiveListTypes.FIELD_DATE_LIST, new String[] { "/Date(1443854733376+0800)/", null }, new Date[] { cal.getTime(), null });
testPrimitiveListWithValues(PrimitiveListTypes.FIELD_BINARY_LIST, new String[] { new String(Base64.encode(new byte[] { 1, 2, 3 }, Base64.DEFAULT), UTF_8), null, new String(Base64.encode(new byte[] { 4, 5, 6 }, Base64.DEFAULT), UTF_8) }, new byte[][] { new byte[] { 1, 2, 3 }, null, new byte[] { 4, 5, 6 } });
testPrimitiveListWithValues(PrimitiveListTypes.FIELD_OBJECT_ID_LIST, new String[] { "789ABCDEF0123456789ABCDA", null, "789ABCDEF0123456789ABCDB" }, new ObjectId[] { new ObjectId("789ABCDEF0123456789ABCDA"), null, new ObjectId("789ABCDEF0123456789ABCDB") });
testPrimitiveListWithValues(PrimitiveListTypes.FIELD_DECIMAL128_LIST, new Integer[] { 0, null, 1 }, new Decimal128[] { new Decimal128(0), null, new Decimal128(1) });
testPrimitiveListWithValues(PrimitiveListTypes.FIELD_UUID_LIST, new String[] { "027ba5ca-aa12-4afa-9219-e20cc3018599", null, "027ba5ca-aa12-4afa-9219-e20cc3018590" }, new UUID[] { UUID.fromString("027ba5ca-aa12-4afa-9219-e20cc3018599"), null, UUID.fromString("027ba5ca-aa12-4afa-9219-e20cc3018590") });
testPrimitiveListWithValues(PrimitiveListTypes.FIELD_REALM_ANY_LIST, new Object[] { 0, null, "hello world" }, new RealmAny[] { RealmAny.valueOf(0), RealmAny.nullValue(), RealmAny.valueOf("hello world") });
}
use of org.bson.types.Decimal128 in project realm-java by realm.
the class RealmJsonTests method createFromJson_defaultValuesAreIgnored.
@Test
public void createFromJson_defaultValuesAreIgnored() throws JSONException {
final long fieldLongPrimaryKeyValue = DefaultValueOfField.FIELD_LONG_PRIMARY_KEY_DEFAULT_VALUE + 1;
// Step 1: Prepares JSON.
final String fieldIgnoredValue = DefaultValueOfField.FIELD_IGNORED_DEFAULT_VALUE + ".modified";
final String fieldStringValue = DefaultValueOfField.FIELD_STRING_DEFAULT_VALUE + ".modified";
final String fieldRandomStringValue = "non-random";
final short fieldShortValue = (short) (DefaultValueOfField.FIELD_SHORT_DEFAULT_VALUE + 1);
final int fieldIntValue = DefaultValueOfField.FIELD_INT_DEFAULT_VALUE + 1;
final long fieldLongValue = DefaultValueOfField.FIELD_LONG_DEFAULT_VALUE + 1;
final byte fieldByteValue = (byte) (DefaultValueOfField.FIELD_BYTE_DEFAULT_VALUE + 1);
final float fieldFloatValue = DefaultValueOfField.FIELD_FLOAT_DEFAULT_VALUE + 1;
final double fieldDoubleValue = DefaultValueOfField.FIELD_DOUBLE_DEFAULT_VALUE + 1;
final boolean fieldBooleanValue = !DefaultValueOfField.FIELD_BOOLEAN_DEFAULT_VALUE;
final Date fieldDateValue = new Date(DefaultValueOfField.FIELD_DATE_DEFAULT_VALUE.getTime() + 1);
final ObjectId fieldObjectIdValue = new ObjectId(new Date(20));
final Decimal128 fieldDecimal128Value = new Decimal128(20);
final UUID fieldUUIDValue = UUID.randomUUID();
final RealmAny fieldRealmAnyValue = RealmAny.valueOf((float) 20);
final byte[] fieldBinaryValue = { (byte) (DefaultValueOfField.FIELD_BINARY_DEFAULT_VALUE[0] - 1) };
final int fieldObjectIntValue = RandomPrimaryKey.FIELD_INT_DEFAULT_VALUE + 1;
final int fieldListIntValue = RandomPrimaryKey.FIELD_INT_DEFAULT_VALUE + 2;
final JSONObject json = new JSONObject();
json.put(DefaultValueOfField.FIELD_LONG_PRIMARY_KEY, fieldLongPrimaryKeyValue);
json.put(DefaultValueOfField.FIELD_IGNORED, fieldIgnoredValue);
json.put(DefaultValueOfField.FIELD_STRING, fieldStringValue);
json.put(DefaultValueOfField.FIELD_RANDOM_STRING, fieldRandomStringValue);
json.put(DefaultValueOfField.FIELD_SHORT, fieldShortValue);
json.put(DefaultValueOfField.FIELD_INT, fieldIntValue);
json.put(DefaultValueOfField.FIELD_LONG, fieldLongValue);
json.put(DefaultValueOfField.FIELD_BYTE, fieldByteValue);
json.put(DefaultValueOfField.FIELD_FLOAT, fieldFloatValue);
json.put(DefaultValueOfField.FIELD_DOUBLE, fieldDoubleValue);
json.put(DefaultValueOfField.FIELD_BOOLEAN, fieldBooleanValue);
json.put(DefaultValueOfField.FIELD_DATE, getISO8601Date(fieldDateValue));
json.put(DefaultValueOfField.FIELD_OBJECT_ID, fieldObjectIdValue);
json.put(DefaultValueOfField.FIELD_DECIMAL128, fieldDecimal128Value);
json.put(DefaultValueOfField.FIELD_UUID, fieldUUIDValue);
json.put(DefaultValueOfField.FIELD_REALM_ANY, fieldRealmAnyValue);
json.put(DefaultValueOfField.FIELD_BINARY, Base64.encodeToString(fieldBinaryValue, Base64.DEFAULT));
// Value for 'fieldObject'
final JSONObject fieldObjectJson = new JSONObject();
fieldObjectJson.put(RandomPrimaryKey.FIELD_RANDOM_PRIMARY_KEY, "pk of fieldObject");
fieldObjectJson.put(RandomPrimaryKey.FIELD_INT, fieldObjectIntValue);
json.put(DefaultValueOfField.FIELD_OBJECT, fieldObjectJson);
// Value for 'fieldList'
final JSONArray fieldListArrayJson = new JSONArray();
final JSONObject fieldListItem0Json = new JSONObject();
fieldListItem0Json.put(RandomPrimaryKey.FIELD_RANDOM_PRIMARY_KEY, "pk1 of fieldList");
fieldListItem0Json.put(RandomPrimaryKey.FIELD_INT, fieldListIntValue);
fieldListArrayJson.put(fieldListItem0Json);
final JSONObject fieldListItem1Json = new JSONObject();
fieldListItem1Json.put(RandomPrimaryKey.FIELD_RANDOM_PRIMARY_KEY, "pk2 of fieldList");
fieldListItem1Json.put(RandomPrimaryKey.FIELD_INT, fieldListIntValue + 1);
fieldListArrayJson.put(fieldListItem1Json);
json.put(DefaultValueOfField.FIELD_LIST, fieldListArrayJson);
// Step 3: Updates with JSONObject.
realm.beginTransaction();
final DefaultValueOfField managedObj = realm.createOrUpdateObjectFromJson(DefaultValueOfField.class, json);
realm.commitTransaction();
// Step 4: Checks that properly created.
assertEquals(DefaultValueOfField.FIELD_IGNORED_DEFAULT_VALUE, /* not fieldIgnoredValue */
managedObj.getFieldIgnored());
assertEquals(fieldStringValue, managedObj.getFieldString());
assertEquals(fieldRandomStringValue, managedObj.getFieldRandomString());
assertEquals(fieldShortValue, managedObj.getFieldShort());
assertEquals(fieldIntValue, managedObj.getFieldInt());
assertEquals(fieldLongPrimaryKeyValue, managedObj.getFieldLongPrimaryKey());
assertEquals(fieldLongValue, managedObj.getFieldLong());
assertEquals(fieldByteValue, managedObj.getFieldByte());
assertEquals(fieldFloatValue, managedObj.getFieldFloat(), 0f);
assertEquals(fieldDoubleValue, managedObj.getFieldDouble(), 0d);
assertEquals(fieldBooleanValue, managedObj.isFieldBoolean());
assertEquals(fieldObjectIdValue, managedObj.getFieldObjectId());
assertEquals(fieldDecimal128Value, managedObj.getFieldDecimal128());
assertEquals(fieldUUIDValue, managedObj.getFieldUUID());
assertEquals(fieldRealmAnyValue, managedObj.getFieldRealmAny());
assertEquals(fieldDateValue, managedObj.getFieldDate());
assertTrue(Arrays.equals(fieldBinaryValue, managedObj.getFieldBinary()));
assertEquals(fieldObjectJson.getString(RandomPrimaryKey.FIELD_RANDOM_PRIMARY_KEY), managedObj.getFieldObject().getFieldRandomPrimaryKey());
assertEquals(fieldObjectIntValue, managedObj.getFieldObject().getFieldInt());
assertEquals(2, managedObj.getFieldList().size());
assertEquals(fieldListItem0Json.get(RandomPrimaryKey.FIELD_RANDOM_PRIMARY_KEY), managedObj.getFieldList().get(0).getFieldRandomPrimaryKey());
assertEquals(fieldListIntValue, managedObj.getFieldList().get(0).getFieldInt());
assertEquals(fieldListItem1Json.get(RandomPrimaryKey.FIELD_RANDOM_PRIMARY_KEY), managedObj.getFieldList().get(1).getFieldRandomPrimaryKey());
assertEquals(fieldListIntValue + 1, managedObj.getFieldList().get(1).getFieldInt());
// Makes sure that excess object by default value is not created.
assertEquals(3, realm.where(RandomPrimaryKey.class).count());
}
use of org.bson.types.Decimal128 in project realm-java by realm.
the class LinkingObjectsQueryTests method populateTestRealmForNullTests.
// Similar to the version in TestHelper, but with more Backlinks
// Creates 3 NullTypes objects. The objects are self-referenced (link) in
// order to test link queries.
//
// +-+--------+------+---------+--------+--------------------+----------+
// | | string | link | numeric | binary | numeric (not null) | linklist |
// +-+--------+------+---------+--------+--------------------+----------+
// |0| Fish | 0 | 1 | {0} | 1 | [0] |
// |1| null | 2 | null | null | 0 | [2] |
// |2| Horse | null | 3 | {1,2} | 3 | null |
// +-+--------+------+---------+--------+--------------------+----------+
private void populateTestRealmForNullTests(Realm testRealm) {
// 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(nullTypesArray[2]);
nullTypesArray[2].setFieldObjectNull(null);
nullTypesArray[0].getFieldListNull().add(nullTypesArray[1]);
nullTypesArray[1].getFieldListNull().add(nullTypesArray[2]);
// just to be sure
nullTypesArray[2].getFieldListNull().clear();
testRealm.commitTransaction();
}
use of org.bson.types.Decimal128 in project realm-java by realm.
the class RealmJsonTests method createObjectFromJson_decimal128AsDouble.
@Test
public void createObjectFromJson_decimal128AsDouble() throws JSONException {
JSONObject json = new JSONObject();
json.put("columnDecimal128", 0.30000001192092896D);
realm.beginTransaction();
realm.createObjectFromJson(AllTypes.class, json);
realm.commitTransaction();
AllTypes obj = realm.where(AllTypes.class).findFirst();
assertEquals(new Decimal128(new BigDecimal(0.30000001192092896D)), obj.getColumnDecimal128());
}
use of org.bson.types.Decimal128 in project realm-java by realm.
the class RealmJsonTests method createObjectFromJson_streamDecimal128AsInt.
@Test
public void createObjectFromJson_streamDecimal128AsInt() throws IOException {
assumeThat(Build.VERSION.SDK_INT, greaterThanOrEqualTo(Build.VERSION_CODES.HONEYCOMB));
InputStream in = TestHelper.loadJsonFromAssets(context, "decimal128_as_int.json");
realm.beginTransaction();
realm.createObjectFromJson(AllTypes.class, in);
realm.commitTransaction();
in.close();
AllTypes obj = realm.where(AllTypes.class).findFirst();
assertEquals(new Decimal128(-42), obj.getColumnDecimal128());
}
Aggregations