11/**
2- * Copyright (c) 2012, 2022 , Werner Keil and others by the @author tag.
2+ * Copyright (c) 2012, 2024 , Werner Keil and others by the @author tag.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
55 * use this file except in compliance with the License. You may obtain a copy of
4545public class MoneyTest {
4646 // TODO break this down into smaller test classes, 1.5k LOC seems a bit large;-)
4747
48- private static final BigDecimal TEN = new BigDecimal (10.0d );
4948 protected static final CurrencyUnit EURO = Monetary .getCurrency ("EUR" );
5049 protected static final CurrencyUnit DOLLAR = Monetary .getCurrency ("USD" );
5150 protected static final CurrencyUnit BRAZILIAN_REAL = Monetary .getCurrency ("BRL" );
51+ private static final BigInteger BI_MAX_LONG = new BigInteger ("9223372036854776000" );
52+ private static final BigInteger BI_MIN_LONG = new BigInteger ("-9223372036854776000" );
53+ private static final BigDecimal BD_MAX_DOUBLE = new BigDecimal ("1.797693134862316E+308" );
5254
5355 /**
5456 * Test method for
5557 * {@link org.javamoney.moneta.Money#of(java.math.BigDecimal, javax.money.CurrencyUnit)} .
5658 */
5759 @ Test
5860 public void testOfCurrencyUnitBigDecimal () {
59- Money m = Money .of (TEN , Monetary .getCurrency ("EUR" ));
60- assertEquals (TEN , m .getNumber ().numberValue (BigDecimal .class ));
61+ Money m = Money .of (BigDecimal . TEN , Monetary .getCurrency ("EUR" ));
62+ assertEquals (BigDecimal . TEN , m .getNumber ().numberValue (BigDecimal .class ));
6163 }
6264
6365 @ Test
6466 public void testOfCurrencyUnitDouble () {
6567 Money m = Money .of (10.0d , Monetary .getCurrency ("EUR" ));
66- assertEquals (m .getNumber ().doubleValue (), TEN .doubleValue ());
68+ assertEquals (m .getNumber ().doubleValue (), BigDecimal . TEN .doubleValue ());
6769 }
6870
6971 /**
@@ -159,16 +161,6 @@ public void testGetDefaultMathContext() {
159161 assertEquals (Money .DEFAULT_MONETARY_CONTEXT , money1 .getContext ());
160162 }
161163
162- @ Test
163- public void testShouldUseDefaultMathContext () {
164- final BigDecimal amount = new BigDecimal ("1.233350000000000000001" );
165-
166- assertEquals (
167- Money .of (amount , EURO ),
168- Money .of (amount , EURO , Money .DEFAULT_MONETARY_CONTEXT )
169- );
170- }
171-
172164 /**
173165 * Test method for
174166 * {@link org.javamoney.moneta.Money#of(java.math.BigDecimal, javax.money.CurrencyUnit,
@@ -225,8 +217,8 @@ public void testOfCurrencyUnitNumber() {
225217 m = Money .of (BigInteger .valueOf (23232312321432432L ), DOLLAR );
226218 assertNotNull (m );
227219 assertEquals (DOLLAR , m .getCurrency ());
228- assertEquals (Long . valueOf ( 23232312321432432L ), m .getNumber ().numberValue (Long .class ));
229- assertEquals (BigInteger . valueOf ( 23232312321432432L ), m .getNumber ().numberValue (BigInteger .class ));
220+ assertEquals (m .getNumber ().numberValue (Long .class ), Long . valueOf ( 23232312321432430L ));
221+ assertEquals (m .getNumber ().numberValue (BigInteger .class ), BigInteger . valueOf ( 23232312321432430L ));
230222 }
231223
232224 /**
@@ -320,8 +312,8 @@ public void testOfStringNumber() {
320312 m = Money .of (BigInteger .valueOf (23232312321432432L ), "USD" );
321313 assertNotNull (m );
322314 assertEquals (DOLLAR , m .getCurrency ());
323- assertEquals (Long . valueOf ( 23232312321432432L ), m .getNumber ().numberValue (Long .class ));
324- assertEquals (BigInteger . valueOf ( 23232312321432432L ), m .getNumber ().numberValue (BigInteger .class ));
315+ assertEquals (m .getNumber ().numberValue (Long .class ), Long . valueOf ( 23232312321432430L ));
316+ assertEquals (m .getNumber ().numberValue (BigInteger .class ), BigInteger . valueOf ( 23232312321432430L ));
325317 }
326318
327319 /**
@@ -654,13 +646,13 @@ public void testRemainderNumber() {
654646 Money .of (0 , "CHF" ), Money .of (-100 , "CHF" ), Money .of (-723527.36532 , "CHF" )};
655647 for (Money m : moneys ) {
656648 assertEquals (m .getFactory ().setCurrency (m .getCurrency ()).setNumber (
657- m .getNumber ().numberValue (BigDecimal .class ).remainder (BigDecimal .valueOf (10.50 )))
649+ m .getNumber ().numberValue (BigDecimal .class ).remainder (BigDecimal .valueOf (10.50 )))
658650 .create (), m .remainder (10.50 ), "Invalid remainder of " + 10.50 );
659651 assertEquals (m .getFactory ().setCurrency (m .getCurrency ()).setNumber (
660- m .getNumber ().numberValue (BigDecimal .class ).remainder (BigDecimal .valueOf (-30.20 )))
652+ m .getNumber ().numberValue (BigDecimal .class ).remainder (BigDecimal .valueOf (-30.20 )))
661653 .create (), m .remainder (-30.20 ), "Invalid remainder of " + -30.20 );
662654 assertEquals (m .getFactory ().setCurrency (m .getCurrency ()).setNumber (
663- m .getNumber ().numberValue (BigDecimal .class ).remainder (BigDecimal .valueOf (-3 )))
655+ m .getNumber ().numberValue (BigDecimal .class ).remainder (BigDecimal .valueOf (-3 )))
664656 .create (), m .remainder (-3 ), "Invalid remainder of " + -3 );
665657 assertEquals (m .getFactory ().setCurrency (m .getCurrency ()).setNumber (
666658 m .getNumber ().numberValue (BigDecimal .class ).remainder (BigDecimal .valueOf (3 ))).create (),
@@ -832,10 +824,9 @@ public void testLongValue() {
832824 m = Money .of (-0.0 , "CHF" );
833825 assertEquals (0L , m .getNumber ().longValue (), "longValue of " + m );
834826 m = Money .of (Long .MAX_VALUE , "CHF" );
835- assertEquals (Long . MAX_VALUE , m .getNumber ().longValue () , "longValue of " + m );
827+ assertEquals (m .getNumber ().numberValue ( BigInteger . class ), BI_MAX_LONG , "longValue of " + m );
836828 m = Money .of (Long .MIN_VALUE , "CHF" );
837- assertEquals (Long .MIN_VALUE , m .getNumber ().longValue (), "longValue of " + m );
838- // try {
829+ assertEquals (m .getNumber ().numberValue (BigInteger .class ), BI_MIN_LONG , "longValue of " + m );
839830 m = Money .of (new BigDecimal ("12121762517652176251725178251872652765321876352187635217835378125" ), "CHF" );
840831 m .getNumber ().longValue ();
841832 }
@@ -854,9 +845,11 @@ public void testLongValueExact() {
854845 m = Money .of (-0.0 , "CHF" );
855846 assertEquals (0L , m .getNumber ().longValue (), "longValue of " + m );
856847 m = Money .of (Long .MAX_VALUE , "CHF" );
857- assertEquals (Long .MAX_VALUE , m .getNumber ().longValue (), "longValue of " + m );
848+ BigInteger bi = m .getNumber ().numberValueExact (BigInteger .class );
849+ assertEquals (bi , BI_MAX_LONG , "longValue of " + m );
858850 m = Money .of (Long .MIN_VALUE , "CHF" );
859- assertEquals (Long .MIN_VALUE , m .getNumber ().longValue (), "longValue of " + m );
851+ bi = m .getNumber ().numberValueExact (BigInteger .class );
852+ assertEquals (bi , BI_MIN_LONG , "longValue of " + m );
860853 try {
861854 m = Money .of (new BigDecimal ("12121762517652176251725178251872652765321876352187635217835378125" ), "CHF" );
862855 m .getNumber ().longValueExact ();
@@ -898,9 +891,9 @@ public void testDoubleValue() {
898891 m = Money .of (-0.0 , "CHF" );
899892 assertEquals (0d , m .getNumber ().doubleValue (), 0.0d , "doubleValue of " + m );
900893 m = Money .of (Double .MAX_VALUE , "CHF" );
901- assertEquals (Double . MAX_VALUE , m .getNumber ().doubleValue ( ), 0.0d , "doubleValue of " + m );
894+ assertEquals (m .getNumber ().numberValue ( BigDecimal . class ), BD_MAX_DOUBLE , "doubleValue of " + m );
902895 m = Money .of (Double .MIN_VALUE , "CHF" );
903- assertEquals (Double . MIN_VALUE , m .getNumber ().doubleValue (), 0.0d , "doubleValue of " + m );
896+ assertEquals (m .getNumber ().doubleValue (), Double . MIN_VALUE , 0.0d , "doubleValue of " + m );
904897 // try {
905898 m = Money .of (new BigDecimal ("12121762517652176251725178251872652765321876352187635217835378125" ), "CHF" );
906899 m .getNumber ().doubleValue ();
@@ -1227,61 +1220,61 @@ public void testDivide_WrongCurrency() {
12271220
12281221 @ Test (expectedExceptions = ArithmeticException .class )
12291222 public void testCreatingFromDoubleNan (){
1230- Money .of (Double .NaN , "XXX" );
1223+ Money .of (Double .NaN , "XXX" );
12311224 }
12321225
12331226 @ Test (expectedExceptions = ArithmeticException .class )
12341227 public void testCreatingFromDoublePositiveInfinity (){
1235- Money .of (Double .POSITIVE_INFINITY , "XXX" );
1228+ Money .of (Double .POSITIVE_INFINITY , "XXX" );
12361229 }
12371230
12381231 @ Test (expectedExceptions = ArithmeticException .class )
12391232 public void testCreatingFromDoubleNegativeInfinity (){
1240- Money .of (Double .NEGATIVE_INFINITY , "XXX" );
1233+ Money .of (Double .NEGATIVE_INFINITY , "XXX" );
12411234 }
12421235
12431236 @ Test (expectedExceptions = NullPointerException .class )
12441237 public void shouldReturnErrorWhenUsingZeroTheCurrencyIsNull () {
1245- Money .zero (null );
1246- fail ();
1238+ Money .zero (null );
1239+ fail ();
12471240 }
12481241
12491242 @ Test
12501243 public void shouldReturnZeroWhenUsingZero () {
1251- MonetaryAmount zero = Money .zero (BRAZILIAN_REAL );
1252- assertEquals (BigDecimal .ZERO , zero .getNumber ().numberValue (BigDecimal .class ));
1253- assertEquals (BRAZILIAN_REAL , zero .getCurrency ());
1254- }
1255-
1256- @ Test (expectedExceptions = NullPointerException .class )
1257- public void shouldReturnErrorWhenUsingOfMinorTheCurrencyIsNull () {
1258- Money .ofMinor (null , 1234L );
1259- fail ();
1260- }
1261-
1262- @ Test
1263- public void shouldReturnMonetaryAmount () {
1264- MonetaryAmount amount = Money .ofMinor (DOLLAR , 1234L );
1265- assertEquals (12.34d , amount .getNumber ().doubleValue ());
1266- assertEquals (DOLLAR , amount .getCurrency ());
1267- }
1268-
1269- @ Test (expectedExceptions = IllegalArgumentException .class )
1270- public void shouldReturnErrorWhenCurrencyIsInvalid () {
1271- Money .ofMinor (new InvalidCurrency (), 1234L );
1272- }
1273-
1274- @ Test (expectedExceptions = IllegalArgumentException .class )
1275- public void shouldReturnErrorWhenFractionDigitIsNegative () {
1276- Money .ofMinor (DOLLAR , 1234L , -2 );
1277- }
1278-
1279- @ Test
1280- public void shouldReturnMonetaryAmountUsingFractionDigits () {
1281- MonetaryAmount amount = Money .ofMinor (DOLLAR , 1234L , 3 );
1282- assertEquals (1.234d , amount .getNumber ().doubleValue ());
1283- assertEquals (DOLLAR , amount .getCurrency ());
1284- }
1244+ MonetaryAmount zero = Money .zero (BRAZILIAN_REAL );
1245+ assertEquals (BigDecimal .ZERO , zero .getNumber ().numberValue (BigDecimal .class ));
1246+ assertEquals (BRAZILIAN_REAL , zero .getCurrency ());
1247+ }
1248+
1249+ @ Test (expectedExceptions = NullPointerException .class )
1250+ public void shouldReturnErrorWhenUsingOfMinorTheCurrencyIsNull () {
1251+ Money .ofMinor (null , 1234L );
1252+ fail ();
1253+ }
1254+
1255+ @ Test
1256+ public void shouldReturnMonetaryAmount () {
1257+ MonetaryAmount amount = Money .ofMinor (DOLLAR , 1234L );
1258+ assertEquals (12.34d , amount .getNumber ().doubleValue ());
1259+ assertEquals (DOLLAR , amount .getCurrency ());
1260+ }
1261+
1262+ @ Test (expectedExceptions = IllegalArgumentException .class )
1263+ public void shouldReturnErrorWhenCurrencyIsInvalid () {
1264+ Money .ofMinor (new InvalidCurrency (), 1234L );
1265+ }
1266+
1267+ @ Test (expectedExceptions = IllegalArgumentException .class )
1268+ public void shouldReturnErrorWhenFractionDigitIsNegative () {
1269+ Money .ofMinor (DOLLAR , 1234L , -2 );
1270+ }
1271+
1272+ @ Test
1273+ public void shouldReturnMonetaryAmountUsingFractionDigits () {
1274+ MonetaryAmount amount = Money .ofMinor (DOLLAR , 1234L , 3 );
1275+ assertEquals (1.234d , amount .getNumber ().doubleValue ());
1276+ assertEquals (DOLLAR , amount .getCurrency ());
1277+ }
12851278
12861279 @ Test
12871280 public void maxScaleOnDivide () {
@@ -1301,6 +1294,4 @@ public void maxPrecisionOnDivide() {
13011294 assertEquals (quotient .getContext ().getPrecision (), 2 );
13021295 assertEquals (quotient .getNumber ().numberValue (BigDecimal .class ).longValueExact (), BigDecimal .valueOf (330 ).longValueExact ());
13031296 }
1304-
1305-
1306- }
1297+ }
0 commit comments