|
15 | 15 | */ |
16 | 16 | package org.javamoney.moneta; |
17 | 17 |
|
| 18 | +import static org.testng.Assert.assertEquals; |
| 19 | +import static org.testng.Assert.assertFalse; |
| 20 | +import static org.testng.Assert.assertNotNull; |
| 21 | +import static org.testng.Assert.assertNotSame; |
| 22 | +import static org.testng.Assert.assertTrue; |
| 23 | +import static org.testng.Assert.fail; |
| 24 | + |
| 25 | +import java.io.ByteArrayInputStream; |
| 26 | +import java.io.ByteArrayOutputStream; |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.ObjectInputStream; |
| 29 | +import java.io.ObjectOutputStream; |
| 30 | +import java.lang.invoke.MethodHandles; |
| 31 | +import java.math.BigDecimal; |
| 32 | +import java.math.BigInteger; |
| 33 | +import java.util.logging.Level; |
| 34 | +import java.util.logging.Logger; |
| 35 | + |
18 | 36 | import javax.money.CurrencyUnit; |
19 | 37 | import javax.money.Monetary; |
20 | 38 | import javax.money.MonetaryAmount; |
21 | 39 | import javax.money.MonetaryOperator; |
22 | 40 | import javax.money.MonetaryQuery; |
23 | | -import javax.money.NumberValue; |
24 | 41 |
|
25 | 42 | import org.junit.Assert; |
26 | 43 | import org.testng.annotations.Test; |
27 | 44 |
|
28 | | -import java.io.*; |
29 | | -import java.lang.invoke.MethodHandles; |
30 | | -import java.math.BigDecimal; |
31 | | -import java.math.BigInteger; |
32 | | -import java.util.logging.Level; |
33 | | -import java.util.logging.Logger; |
34 | | - |
35 | | -import static org.testng.Assert.*; |
36 | | - |
37 | 45 | /** |
38 | 46 | * @author Anatole |
39 | 47 | */ |
@@ -282,25 +290,28 @@ public void testAdd(){ |
282 | 290 | assertNotNull(moneyResult); |
283 | 291 | assertEquals(11d, moneyResult.getNumber().doubleValue(), 0d); |
284 | 292 |
|
285 | | - // This example produce a false positive |
| 293 | + // This example produce a false positive in the old version |
286 | 294 | money1 = FastMoney.of(44474249632057L, EURO); |
287 | 295 | money2 = FastMoney.of(72913073429160L, EURO); |
288 | | - moneyResult = money1.add(money2); |
289 | | - assertNotNull(moneyResult); |
290 | | - assertEquals(money1, moneyResult.subtract(money2)); |
291 | | - // Values greater 92233720368547.75807 not allowed, but we got no long overflow |
| 296 | + |
| 297 | + try { |
| 298 | + moneyResult = money1.add(money2); |
| 299 | + fail("overflow should raise ArithmeticException"); |
| 300 | + } catch (ArithmeticException e) { |
| 301 | + // should happen |
| 302 | + } |
| 303 | + |
| 304 | + // check greates 92233720368547.75807 value |
292 | 305 | long fastMoneyMax = 92233720368547L; |
293 | 306 | FastMoney money3 = FastMoney.of(fastMoneyMax, "CHF"); |
294 | | - money1=money3.add(money3); |
295 | | - assertEquals(money1, money3.multiply(2)); |
296 | 307 |
|
297 | | -// try { |
298 | | -// // the maximum value for FastMoney is 92233720368547.75807 so this should overflow |
299 | | -// money3.add(money3); |
300 | | -// fail("overflow should raise ArithmeticException"); |
301 | | -// } catch (ArithmeticException e) { |
302 | | -// // should happen |
303 | | -// } |
| 308 | + try { |
| 309 | + // the maximum value for FastMoney is 92233720368547.75807 so this should overflow |
| 310 | + money3.add(money3); |
| 311 | + fail("overflow should raise ArithmeticException"); |
| 312 | + } catch (ArithmeticException e) { |
| 313 | + // should happen |
| 314 | + } |
304 | 315 | } |
305 | 316 |
|
306 | 317 | /** |
|
0 commit comments