@@ -847,12 +847,13 @@ public void testAdd_ExceedsCapabilities(){
847847 }
848848 if (maxCtx .getMaxScale () >= 0 ){
849849 MonetaryContext tgtContext =
850- new MonetaryContext .Builder ().setMaxScale (maxCtx .getMaxScale () + 1 ).create ();
850+ new MonetaryContext .Builder (maxCtx ).setMaxScale (maxCtx .getMaxScale () + 1 ).create ();
851851 Class <? extends MonetaryAmount > exceedingType = null ;
852852 try {
853853 exceedingType = MonetaryAmounts .queryAmountType (tgtContext );
854854 assertNotNull (exceedingType );
855- MonetaryAmountFactory <? extends MonetaryAmount > bigFactory = MonetaryAmounts .getAmountFactory (exceedingType );
855+ MonetaryAmountFactory <? extends MonetaryAmount > bigFactory =
856+ MonetaryAmounts .getAmountFactory (exceedingType );
856857 mAmount2 = bigFactory .setCurrency ("CHF" ).setNumber (createNumberWithScale (f , maxCtx .getMaxScale ()))
857858 .create ();
858859 }
@@ -869,17 +870,17 @@ public void testAdd_ExceedsCapabilities(){
869870 }
870871
871872 private BigDecimal createNumberWithScale (MonetaryAmountFactory f , int scale ){
872- StringBuilder b = new StringBuilder (scale + 2 );
873+ StringBuilder b = new StringBuilder (scale + 2 );
873874 b .append ("1." );
874- for (int i = 0 ; i < scale ;i ++){
875+ for (int i = 0 ; i < scale ; i ++){
875876 b .append (String .valueOf (i % 10 ));
876877 }
877878 return new BigDecimal (b .toString (), MathContext .UNLIMITED );
878879 }
879880
880881 private BigDecimal createNumberWithPrecision (MonetaryAmountFactory f , int precision ){
881- StringBuilder b = new StringBuilder (precision + 1 );
882- for (int i = 0 ; i < precision ;i ++){
882+ StringBuilder b = new StringBuilder (precision + 1 );
883+ for (int i = 0 ; i < precision ; i ++){
883884 b .append (String .valueOf (i % 10 ));
884885 }
885886 return new BigDecimal (b .toString (), MathContext .UNLIMITED );
@@ -907,7 +908,16 @@ public void testAdd_Null(){
907908 @ SpecAssertion (section = "4.2.2" , id = "422-D6" )
908909 @ Test
909910 public void testSubtractPositiveIntegers (){
910- fail ();
911+ for (Class type : MonetaryAmounts .getAmountTypes ()){
912+ MonetaryAmount mAmount1 =
913+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (10 ).create ();
914+ MonetaryAmount mAmount2 =
915+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (20 ).create ();
916+ MonetaryAmount mActualResult = mAmount1 .subtract (mAmount2 );
917+ MonetaryAmount mExpectedResult =
918+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (-10 ).create ();
919+ Assert .assertEquals ("Subtracting two simple ammounts" , mExpectedResult , mActualResult );
920+ }
911921 }
912922
913923 /**
@@ -916,7 +926,16 @@ public void testSubtractPositiveIntegers(){
916926 @ SpecAssertion (section = "4.2.2" , id = "422-D6" )
917927 @ Test
918928 public void testSubtractNegativeIntegers (){
919- fail ();
929+ for (Class type : MonetaryAmounts .getAmountTypes ()){
930+ MonetaryAmount mAmount1 =
931+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (-10 ).create ();
932+ MonetaryAmount mAmount2 =
933+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (-20 ).create ();
934+ MonetaryAmount mActualResult = mAmount1 .subtract (mAmount2 );
935+ MonetaryAmount mExpectedResult =
936+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (10 ).create ();
937+ Assert .assertEquals ("Subtracting two simple ammounts" , mExpectedResult , mActualResult );
938+ }
920939 }
921940
922941 /**
@@ -925,7 +944,16 @@ public void testSubtractNegativeIntegers(){
925944 @ SpecAssertion (section = "4.2.2" , id = "422-D6" )
926945 @ Test
927946 public void testSubtractPositiveFractions (){
928- fail ();
947+ for (Class type : MonetaryAmounts .getAmountTypes ()){
948+ MonetaryAmount mAmount1 =
949+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (1.5 ).create ();
950+ MonetaryAmount mAmount2 =
951+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (2.85 ).create ();
952+ MonetaryAmount mActualResult = mAmount1 .subtract (mAmount2 );
953+ MonetaryAmount mExpectedResult =
954+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (-1.35 ).create ();
955+ Assert .assertEquals ("Adding two simple ammounts" , mExpectedResult , mActualResult );
956+ }
929957 }
930958
931959 /**
@@ -934,46 +962,36 @@ public void testSubtractPositiveFractions(){
934962 @ SpecAssertion (section = "4.2.2" , id = "422-D6" )
935963 @ Test
936964 public void testSubtractMixedIntegers (){
937- // for(Class type : MonetaryAmounts.getAmountTypes()){
938- // MonetaryAmount mAmount1 =
939- // MonetaryAmounts.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(-10)
940- // .create();
941- // MonetaryAmount mAmount2 =
942- // MonetaryAmounts.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(20)
943- // .create();
944- // MonetaryAmount mActualResult = mAmount1.add(mAmount2);
945- // MonetaryAmount mExpectedResult =
946- // MonetaryAmounts.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(10)
947- // .create();
948- // Assert.assertEquals("Adding two simple ammounts", mExpectedResult, mActualResult);
949- // }
950- // for(Class type : MonetaryAmounts.getAmountTypes()){
951- // MonetaryAmount mAmount1 =
952- // MonetaryAmounts.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(-20)
953- // .create();
954- // MonetaryAmount mAmount2 =
955- // MonetaryAmounts.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(10)
956- // .create();
957- // MonetaryAmount mActualResult = mAmount1.add(mAmount2);
958- // MonetaryAmount mExpectedResult =
959- // MonetaryAmounts.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(-10)
960- // .create();
961- // Assert.assertEquals("Adding two simple ammounts", mExpectedResult, mActualResult);
962- // }
963- // for(Class type : MonetaryAmounts.getAmountTypes()){
964- // MonetaryAmount mAmount1 =
965- // MonetaryAmounts.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(-10)
966- // .create();
967- // MonetaryAmount mAmount2 =
968- // MonetaryAmounts.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(10)
969- // .create();
970- // MonetaryAmount mActualResult = mAmount1.add(mAmount2);
971- // MonetaryAmount mExpectedResult =
972- // MonetaryAmounts.getAmountFactory(type).setCurrency(DEFAULT_CURRENCY).setNumber(0)
973- // .create();
974- // Assert.assertEquals("Adding two simple ammounts", mExpectedResult, mActualResult);
975- // }
976- fail ();
965+ for (Class type : MonetaryAmounts .getAmountTypes ()){
966+ MonetaryAmount mAmount1 =
967+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (20 ).create ();
968+ MonetaryAmount mAmount2 =
969+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (10 ).create ();
970+ MonetaryAmount mActualResult = mAmount1 .subtract (mAmount2 );
971+ MonetaryAmount mExpectedResult =
972+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (10 ).create ();
973+ Assert .assertEquals ("Adding two simple ammounts" , mExpectedResult , mActualResult );
974+ }
975+ for (Class type : MonetaryAmounts .getAmountTypes ()){
976+ MonetaryAmount mAmount1 =
977+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (10 ).create ();
978+ MonetaryAmount mAmount2 =
979+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (10 ).create ();
980+ MonetaryAmount mActualResult = mAmount1 .subtract (mAmount2 );
981+ MonetaryAmount mExpectedResult =
982+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (0 ).create ();
983+ Assert .assertEquals ("Adding two simple ammounts" , mExpectedResult , mActualResult );
984+ }
985+ for (Class type : MonetaryAmounts .getAmountTypes ()){
986+ MonetaryAmount mAmount1 =
987+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (-10 ).create ();
988+ MonetaryAmount mAmount2 =
989+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (10 ).create ();
990+ MonetaryAmount mActualResult = mAmount1 .subtract (mAmount2 );
991+ MonetaryAmount mExpectedResult =
992+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (-20 ).create ();
993+ Assert .assertEquals ("Adding two simple ammounts" , mExpectedResult , mActualResult );
994+ }
977995 }
978996
979997 /**
@@ -982,7 +1000,36 @@ public void testSubtractMixedIntegers(){
9821000 @ SpecAssertion (section = "4.2.2" , id = "422-D6" )
9831001 @ Test
9841002 public void testSubtractMixedFractions (){
985- fail ();
1003+ for (Class type : MonetaryAmounts .getAmountTypes ()){
1004+ MonetaryAmount mAmount1 =
1005+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (1.5 ).create ();
1006+ MonetaryAmount mAmount2 =
1007+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (-2.85 ).create ();
1008+ MonetaryAmount mActualResult = mAmount1 .subtract (mAmount2 );
1009+ MonetaryAmount mExpectedResult =
1010+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (4.35 ).create ();
1011+ Assert .assertEquals ("Adding two simple ammounts" , mExpectedResult , mActualResult );
1012+ }
1013+ for (Class type : MonetaryAmounts .getAmountTypes ()){
1014+ MonetaryAmount mAmount1 =
1015+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (-1.5 ).create ();
1016+ MonetaryAmount mAmount2 =
1017+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (+2.85 ).create ();
1018+ MonetaryAmount mActualResult = mAmount1 .subtract (mAmount2 );
1019+ MonetaryAmount mExpectedResult =
1020+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (-4.35 ).create ();
1021+ Assert .assertEquals ("Adding two simple ammounts" , mExpectedResult , mActualResult );
1022+ }
1023+ for (Class type : MonetaryAmounts .getAmountTypes ()){
1024+ MonetaryAmount mAmount1 =
1025+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (2.85 ).create ();
1026+ MonetaryAmount mAmount2 =
1027+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (+2.85 ).create ();
1028+ MonetaryAmount mActualResult = mAmount1 .subtract (mAmount2 );
1029+ MonetaryAmount mExpectedResult =
1030+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (0 ).create ();
1031+ Assert .assertEquals ("Adding two simple ammounts" , mExpectedResult , mActualResult );
1032+ }
9861033 }
9871034
9881035 /**
@@ -1025,7 +1072,37 @@ public void testSubtract_Zero(){
10251072 @ SpecAssertion (section = "4.2.2" , id = "422-D9" )
10261073 @ Test (expected = MonetaryException .class )
10271074 public void testSubtract_ExceedsCapabilities (){
1028- fail ("Not yet implemented" );
1075+ for (Class type : MonetaryAmounts .getAmountTypes ()){
1076+ MonetaryAmountFactory <MonetaryAmount > f = MonetaryAmounts .getAmountFactory (type );
1077+ f .setCurrency ("CHF" );
1078+ MonetaryAmount mAmount1 = f .setNumber (0 ).create ();
1079+ MonetaryContext maxCtx = f .getMaximalMonetaryContext ();
1080+ MonetaryAmount mAmount2 = null ;
1081+ if (maxCtx .getPrecision () > 0 ){
1082+ mAmount2 = f .setNumber (createNumberWithPrecision (f , maxCtx .getPrecision ())).create ();
1083+ }
1084+ if (maxCtx .getMaxScale () >= 0 ){
1085+ MonetaryContext tgtContext =
1086+ new MonetaryContext .Builder (maxCtx ).setMaxScale (maxCtx .getMaxScale () + 1 ).create ();
1087+ Class <? extends MonetaryAmount > exceedingType = null ;
1088+ try {
1089+ exceedingType = MonetaryAmounts .queryAmountType (tgtContext );
1090+ assertNotNull (exceedingType );
1091+ MonetaryAmountFactory <? extends MonetaryAmount > bigFactory =
1092+ MonetaryAmounts .getAmountFactory (exceedingType );
1093+ mAmount2 = bigFactory .setCurrency ("CHF" ).setNumber (createNumberWithScale (f , maxCtx .getMaxScale ()))
1094+ .create ();
1095+ }
1096+ catch (MonetaryException e ){
1097+ // we have to abort the test...
1098+ }
1099+
1100+ }
1101+ if (mAmount2 != null ){
1102+ mAmount1 .subtract (mAmount2 );
1103+ fail ("Exception expected" );
1104+ }
1105+ }
10291106 }
10301107
10311108 /**
@@ -1049,7 +1126,7 @@ public void testSubtract_Null(){
10491126 @ SpecAssertion (section = "4.2.2" , id = "422-D11" )
10501127 @ Test
10511128 public void testMultiply (){
1052- fail ("Not yet implemented " );
1129+ fail ("Exception expected " );
10531130 }
10541131
10551132 /**
@@ -1058,7 +1135,16 @@ public void testMultiply(){
10581135 @ SpecAssertion (section = "4.2.2" , id = "422-D12" )
10591136 @ Test
10601137 public void testMultiplyOne (){
1061- fail ("Not yet implemented" );
1138+ for (Class type : MonetaryAmounts .getAmountTypes ()){
1139+ MonetaryAmount mAmount1 =
1140+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (10 ).create ();
1141+ MonetaryAmount mActualResult = mAmount1 .multiply (1 );
1142+ assertTrue (mActualResult == mAmount1 );
1143+ mActualResult = mAmount1 .multiply (1.0 );
1144+ assertTrue (mActualResult == mAmount1 );
1145+ mActualResult = mAmount1 .multiply (BigDecimal .ONE );
1146+ assertTrue (mActualResult == mAmount1 );
1147+ }
10621148 }
10631149
10641150
@@ -1079,7 +1165,16 @@ public void testMultiplyExceedsCapabilities(){
10791165 @ SpecAssertion (section = "4.2.2" , id = "422-D14" )
10801166 @ Test
10811167 public void testMultiplyNull (){
1082- fail ("Not yet implemented" );
1168+ for (Class type : MonetaryAmounts .getAmountTypes ()){
1169+ MonetaryAmount mAmount1 =
1170+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (10 ).create ();
1171+ try {
1172+ MonetaryAmount mActualResult = mAmount1 .multiply (null );
1173+ }
1174+ catch (NullPointerException e ){
1175+ // expected
1176+ }
1177+ }
10831178 }
10841179
10851180 /**
@@ -1106,7 +1201,16 @@ public void testDivideZero(){
11061201 @ SpecAssertion (section = "4.2.2" , id = "422-D17" )
11071202 @ Test
11081203 public void testDivideOne (){
1109- fail ("Not yet implemented" );
1204+ for (Class type : MonetaryAmounts .getAmountTypes ()){
1205+ MonetaryAmount mAmount1 =
1206+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (10 ).create ();
1207+ MonetaryAmount mActualResult = mAmount1 .divide (1 );
1208+ assertTrue (mActualResult == mAmount1 );
1209+ mActualResult = mAmount1 .divide (1.0 );
1210+ assertTrue (mActualResult == mAmount1 );
1211+ mActualResult = mAmount1 .divide (BigDecimal .ONE );
1212+ assertTrue (mActualResult == mAmount1 );
1213+ }
11101214 }
11111215
11121216 /**
@@ -1115,7 +1219,16 @@ public void testDivideOne(){
11151219 @ SpecAssertion (section = "4.2.2" , id = "422-D18" )
11161220 @ Test
11171221 public void testDivideNull (){
1118- fail ("Not yet implemented" );
1222+ for (Class type : MonetaryAmounts .getAmountTypes ()){
1223+ MonetaryAmount mAmount1 =
1224+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (10 ).create ();
1225+ try {
1226+ MonetaryAmount mActualResult = mAmount1 .divide (null );
1227+ }
1228+ catch (NullPointerException e ){
1229+ // expected
1230+ }
1231+ }
11191232 }
11201233
11211234 /**
@@ -1142,7 +1255,16 @@ public void testRemainderZero(){
11421255 @ SpecAssertion (section = "4.2.2" , id = "422-D21" )
11431256 @ Test
11441257 public void testRemainderNull (){
1145- fail ("Not yet implemented" );
1258+ for (Class type : MonetaryAmounts .getAmountTypes ()){
1259+ MonetaryAmount mAmount1 =
1260+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (10 ).create ();
1261+ try {
1262+ MonetaryAmount mActualResult = mAmount1 .remainder (null );
1263+ }
1264+ catch (NullPointerException e ){
1265+ // expected
1266+ }
1267+ }
11461268 }
11471269
11481270 /**
@@ -1169,7 +1291,16 @@ public void testDivideAndRemainderZero(){
11691291 @ SpecAssertion (section = "4.2.2" , id = "422-D24" )
11701292 @ Test
11711293 public void testDivideAndRemainderNull (){
1172- fail ("Not yet implemented" );
1294+ for (Class type : MonetaryAmounts .getAmountTypes ()){
1295+ MonetaryAmount mAmount1 =
1296+ MonetaryAmounts .getAmountFactory (type ).setCurrency (DEFAULT_CURRENCY ).setNumber (10 ).create ();
1297+ try {
1298+ MonetaryAmount [] mActualResult = mAmount1 .divideAndRemainder (null );
1299+ }
1300+ catch (NullPointerException e ){
1301+ // expected
1302+ }
1303+ }
11731304 }
11741305
11751306 /**
@@ -1196,7 +1327,19 @@ public void testScaleByPowerOfTen(){
11961327 @ SpecAssertion (section = "4.2.2" , id = "422-D27" )
11971328 @ Test
11981329 public void testAbsolute (){
1199- fail ("Not yet implemented" );
1330+ for (Class type : MonetaryAmounts .getAmountTypes ()){
1331+ MonetaryAmountFactory <MonetaryAmount > f = MonetaryAmounts .getAmountFactory (type );
1332+ f .setCurrency ("CHF" );
1333+ MonetaryAmount m = f .setNumber (10 ).create ();
1334+ assertEquals (m , m .abs ());
1335+ assertTrue (m == m .abs ());
1336+ m = f .setNumber (0 ).create ();
1337+ assertEquals (m , m .abs ());
1338+ assertTrue (m == m .abs ());
1339+ m = f .setNumber (-10 ).create ();
1340+ assertEquals (m .negate (), m .abs ());
1341+ assertTrue (m != m .abs ());
1342+ }
12001343 }
12011344
12021345 /**
@@ -1205,7 +1348,14 @@ public void testAbsolute(){
12051348 @ SpecAssertion (section = "4.2.2" , id = "422-D28" )
12061349 @ Test
12071350 public void testNegate (){
1208- fail ("Not yet implemented" );
1351+ for (Class type : MonetaryAmounts .getAmountTypes ()){
1352+ MonetaryAmountFactory <MonetaryAmount > f = MonetaryAmounts .getAmountFactory (type );
1353+ f .setCurrency ("CHF" );
1354+ MonetaryAmount m = f .setNumber (100 ).create ();
1355+ assertEquals (f .setNumber (-100 ), m .negate ());
1356+ m = f .setNumber (-123.234 ).create ();
1357+ assertEquals (f .setNumber (123.234 ), m .negate ());
1358+ }
12091359 }
12101360
12111361 /**
0 commit comments