use of org.bson.types.Decimal128 in project mongo-java-driver by mongodb.
the class JsonReaderTest method testDecimal128Int64Constructor.
@Test
public void testDecimal128Int64Constructor() {
String json = "NumberDecimal(" + Long.MAX_VALUE + ")";
testStringAndStream(json, bsonReader -> {
assertEquals(BsonType.DECIMAL128, bsonReader.readBsonType());
assertEquals(new Decimal128(Long.MAX_VALUE), bsonReader.readDecimal128());
assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
return null;
});
}
use of org.bson.types.Decimal128 in project immutables by immutables.
the class TypeConversionTest method decimal128.
@Test
void decimal128() throws IOException {
BsonDecimal128 value = new BsonDecimal128(new Decimal128(BigDecimal.valueOf(1.1)));
check(Parsers.parserAt(value).getCurrentToken()).is(JsonToken.VALUE_NUMBER_FLOAT);
check(Parsers.parserAt(value).getNumberType()).is(JsonParser.NumberType.BIG_DECIMAL);
check(Parsers.parserAt(value).getDoubleValue()).is(1.1);
check(Parsers.parserAt(value).getDecimalValue()).is(BigDecimal.valueOf(1.1));
}
use of org.bson.types.Decimal128 in project immutables by immutables.
the class BsonDecimal128Test method read.
@Test
public void read() throws Exception {
BsonDocument doc = new BsonDocument();
doc.put("int", new BsonDecimal128(Decimal128.parse(Integer.toString(Integer.MAX_VALUE))));
doc.put("long", new BsonDecimal128(new Decimal128(Long.MAX_VALUE)));
doc.put("double", new BsonDecimal128(Decimal128.parse("12.111")));
JsonReader reader = Jsons.asGsonReader(doc);
reader.beginObject();
check(reader.nextName()).is("int");
check(reader.peek()).is(JsonToken.NUMBER);
check(reader.nextInt()).is(Integer.MAX_VALUE);
check(reader.nextName()).is("long");
check(reader.peek()).is(JsonToken.NUMBER);
check(reader.nextLong()).is(Long.MAX_VALUE);
check(reader.nextName()).is("double");
check(reader.peek()).is(JsonToken.NUMBER);
check(reader.nextDouble()).is(12.111D);
reader.endObject();
reader.close();
}
use of org.bson.types.Decimal128 in project realm-java by realm.
the class DynamicRealmObjectTests method untypedSetter_usingStringConversion.
@Test
public void untypedSetter_usingStringConversion() {
realm.beginTransaction();
AllJavaTypes obj = realm.createObject(AllJavaTypes.class, 0);
DynamicRealmObject dObj = new DynamicRealmObject(obj);
try {
for (SupportedType type : SupportedType.values()) {
switch(type) {
case BOOLEAN:
dObj.set(AllJavaTypes.FIELD_BOOLEAN, "true");
assertTrue(dObj.getBoolean(AllJavaTypes.FIELD_BOOLEAN));
break;
case SHORT:
dObj.set(AllJavaTypes.FIELD_SHORT, "42");
assertEquals((short) 42, dObj.getShort(AllJavaTypes.FIELD_SHORT));
break;
case INT:
dObj.set(AllJavaTypes.FIELD_INT, "42");
assertEquals(42, dObj.getInt(AllJavaTypes.FIELD_INT));
break;
case LONG:
dObj.set(AllJavaTypes.FIELD_LONG, "42");
assertEquals((long) 42, dObj.getLong(AllJavaTypes.FIELD_LONG));
break;
case FLOAT:
dObj.set(AllJavaTypes.FIELD_FLOAT, "1.23");
assertEquals(1.23f, dObj.getFloat(AllJavaTypes.FIELD_FLOAT), 0f);
break;
case DOUBLE:
dObj.set(AllJavaTypes.FIELD_DOUBLE, "1.234");
assertEquals(1.234d, dObj.getDouble(AllJavaTypes.FIELD_DOUBLE), 0f);
break;
case DATE:
dObj.set(AllJavaTypes.FIELD_DATE, "1000");
assertEquals(new Date(1000), dObj.getDate(AllJavaTypes.FIELD_DATE));
break;
case DECIMAL128:
dObj.set(AllJavaTypes.FIELD_DECIMAL128, "1");
assertEquals(new Decimal128(BigDecimal.ONE), dObj.get(AllJavaTypes.FIELD_DECIMAL128));
break;
case OBJECT_ID:
dObj.set(AllJavaTypes.FIELD_OBJECT_ID, TestHelper.generateObjectIdHexString(7));
assertEquals(new ObjectId(TestHelper.generateObjectIdHexString(7)), dObj.get(AllJavaTypes.FIELD_OBJECT_ID));
break;
case UUID:
String uuid = UUID.randomUUID().toString();
dObj.set(AllJavaTypes.FIELD_UUID, UUID.fromString(uuid));
assertEquals(UUID.fromString(uuid), dObj.get(AllJavaTypes.FIELD_UUID));
break;
// These types don't have a string representation that can be parsed.
case OBJECT:
case LIST:
case LIST_INTEGER:
case LIST_STRING:
case LIST_BOOLEAN:
case LIST_FLOAT:
case LIST_DOUBLE:
case LIST_BINARY:
case LIST_DATE:
case LIST_DECIMAL128:
case LIST_OBJECT_ID:
case LIST_UUID:
case STRING:
case BINARY:
case BYTE:
break;
default:
fail("Unknown type: " + type);
break;
}
}
} finally {
realm.cancelTransaction();
}
}
use of org.bson.types.Decimal128 in project realm-java by realm.
the class DynamicRealmObjectTests method setUp.
@Before
public void setUp() {
RealmConfiguration realmConfig = configFactory.createConfiguration();
realm = Realm.getInstance(realmConfig);
realm.beginTransaction();
typedObj = realm.createObject(AllJavaTypes.class, 1);
typedObj.setFieldString("str");
typedObj.setFieldShort((short) 1);
typedObj.setFieldInt(1);
typedObj.setFieldLong(1);
typedObj.setFieldByte((byte) 4);
typedObj.setFieldFloat(1.23f);
typedObj.setFieldDouble(1.234d);
typedObj.setFieldBinary(new byte[] { 1, 2, 3 });
typedObj.setFieldBoolean(true);
typedObj.setFieldDate(new Date(1000));
typedObj.setFieldDecimal128(new Decimal128(BigDecimal.TEN));
typedObj.setFieldObjectId(new ObjectId(TestHelper.generateObjectIdHexString(7)));
typedObj.setFieldUUID(UUID.randomUUID());
typedObj.setFieldObject(typedObj);
typedObj.getFieldList().add(typedObj);
typedObj.getFieldIntegerList().add(1);
typedObj.getFieldStringList().add("str");
typedObj.getFieldBooleanList().add(true);
typedObj.getFieldFloatList().add(1.23F);
typedObj.getFieldDoubleList().add(1.234D);
typedObj.getFieldBinaryList().add(new byte[] { 1, 2, 3 });
typedObj.getFieldDateList().add(new Date(1000));
dObjTyped = new DynamicRealmObject(typedObj);
realm.commitTransaction();
dynamicRealm = DynamicRealm.getInstance(realm.getConfiguration());
dObjDynamic = dynamicRealm.where(AllJavaTypes.CLASS_NAME).findFirst();
}
Aggregations