← joda-money  /  src/test/java/org/joda/money/TestMoney.java

1
/*
2
 *  Copyright 2009-present, Stephen Colebourne
3
 *
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *  you may not use this file except in compliance with the License.
6
 *  You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *  Unless required by applicable law or agreed to in writing, software
11
 *  distributed under the License is distributed on an "AS IS" BASIS,
12
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *  See the License for the specific language governing permissions and
14
 *  limitations under the License.
15
 */
16
package org.joda.money;
17
18
import static org.assertj.core.api.Assertions.assertThat;
19
import static org.assertj.core.api.Assertions.fail;
20
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
21
import static org.junit.jupiter.api.Assertions.assertEquals;
22
23
import java.io.ByteArrayInputStream;
24
import java.io.ByteArrayOutputStream;
25
import java.io.IOException;
26
import java.io.InvalidObjectException;
27
import java.io.ObjectInputStream;
28
import java.io.ObjectOutputStream;
29
import java.lang.reflect.InvocationTargetException;
30
import java.lang.reflect.Modifier;
31
import java.math.BigDecimal;
32
import java.math.RoundingMode;
33
import java.util.Arrays;
34
import java.util.Collections;
35
36
import org.junit.jupiter.api.Test;
37
import org.junit.jupiter.params.ParameterizedTest;
38
import org.junit.jupiter.params.provider.MethodSource;
39
40
/**
41
 * Test Money.
42
 */
43
class TestMoney {
44
45
    private static final CurrencyUnit GBP = CurrencyUnit.of("GBP");
46
    private static final CurrencyUnit EUR = CurrencyUnit.of("EUR");
47
    private static final CurrencyUnit USD = CurrencyUnit.of("USD");
48
    private static final CurrencyUnit JPY = CurrencyUnit.of("JPY");
49
    private static final BigDecimal BIGDEC_2_3 = new BigDecimal("2.3");
50
    private static final BigDecimal BIGDEC_2_34 = new BigDecimal("2.34");
51
    private static final BigDecimal BIGDEC_2_345 = new BigDecimal("2.345");
52
    private static final BigDecimal BIGDEC_M5_78 = new BigDecimal("-5.78");
53
54
    private static final Money GBP_0_00 = Money.parse("GBP 0.00");
55
    private static final Money GBP_1_23 = Money.parse("GBP 1.23");
56
    private static final Money GBP_2_33 = Money.parse("GBP 2.33");
57
    private static final Money GBP_2_34 = Money.parse("GBP 2.34");
58
    private static final Money GBP_2_35 = Money.parse("GBP 2.35");
59
    private static final Money GBP_2_36 = Money.parse("GBP 2.36");
60
    private static final Money GBP_5_78 = Money.parse("GBP 5.78");
61
    private static final Money GBP_M1_23 = Money.parse("GBP -1.23");
62
    private static final Money GBP_M5_78 = Money.parse("GBP -5.78");
63
    private static final Money GBP_INT_MAX_PLUS1 = Money.ofMinor(GBP, ((long) Integer.MAX_VALUE) + 1);
64
    private static final Money GBP_INT_MIN_MINUS1 = Money.ofMinor(GBP, ((long) Integer.MIN_VALUE) - 1);
65
    private static final Money GBP_INT_MAX_MAJOR_PLUS1 = Money.ofMinor(GBP, (((long) Integer.MAX_VALUE) + 1) * 100);
66
    private static final Money GBP_INT_MIN_MAJOR_MINUS1 = Money.ofMinor(GBP, (((long) Integer.MIN_VALUE) - 1) * 100);
67
    private static final Money GBP_LONG_MAX_PLUS1 = Money.of(GBP, BigDecimal.valueOf(Long.MAX_VALUE).add(BigDecimal.ONE));
68
    private static final Money GBP_LONG_MIN_MINUS1 =
69
            Money.of(GBP, BigDecimal.valueOf(Long.MIN_VALUE).subtract(BigDecimal.ONE));
70
    private static final Money GBP_LONG_MAX_MAJOR_PLUS1 = Money.of(
71
            GBP,
72
            BigDecimal.valueOf(Long.MAX_VALUE).add(BigDecimal.ONE).multiply(BigDecimal.valueOf(100)));
73
    private static final Money GBP_LONG_MIN_MAJOR_MINUS1 = Money.of(
74
            GBP,
75
            BigDecimal.valueOf(Long.MIN_VALUE).subtract(BigDecimal.ONE).multiply(BigDecimal.valueOf(100)));
76
    private static final Money JPY_423 = Money.parse("JPY 423");
77
    private static final Money USD_1_23 = Money.parse("USD 1.23");
78
    private static final Money USD_2_34 = Money.parse("USD 2.34");
79
    private static final Money USD_2_35 = Money.parse("USD 2.35");
80
81
    //-----------------------------------------------------------------------
82
    // of(Currency,BigDecimal)
83
    //-----------------------------------------------------------------------
84
    @Test
85
    void test_factory_of_Currency_BigDecimal() {
86
        var test = Money.of(GBP, BIGDEC_2_34);
87
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
88
        assertThat(test.getAmountMinorInt()).isEqualTo(234);
89
        assertThat(test.getAmount().scale()).isEqualTo(2);
90
    }
91
92
    @Test
93
    void test_factory_of_Currency_BigDecimal_correctScale() {
94
        var test = Money.of(GBP, BIGDEC_2_3);
95
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
96
        assertThat(test.getAmountMinorInt()).isEqualTo(230);
97
        assertThat(test.getAmount().scale()).isEqualTo(2);
98
    }
99
100
    @Test
101
    void test_factory_of_Currency_BigDecimal_invalidScaleGBP() {
102
        assertThatExceptionOfType(ArithmeticException.class)
103
            .isThrownBy(() -> Money.of(GBP, BIGDEC_2_345));
104
    }
105
106
    @Test
107
    void test_factory_of_Currency_BigDecimal_invalidScaleJPY() {
108
        assertThatExceptionOfType(ArithmeticException.class)
109
            .isThrownBy(() -> Money.of(JPY, BIGDEC_2_3));
110
    }
111
112
    @Test
113
    void test_factory_of_Currency_BigDecimal_nullCurrency() {
114
        assertThatExceptionOfType(NullPointerException.class)
115
            .isThrownBy(() -> Money.of((CurrencyUnit) null, BIGDEC_2_34));
116
    }
117
118
    @Test
119
    void test_factory_of_Currency_BigDecimal_nullBigDecimal() {
120
        assertThatExceptionOfType(NullPointerException.class)
121
            .isThrownBy(() -> Money.of(GBP, (BigDecimal) null));
122
    }
123
124
    //-----------------------------------------------------------------------
125
    // of(Currency,BigDecimal,RoundingMode)
126
    //-----------------------------------------------------------------------
127
    @Test
128
    void test_factory_of_Currency_BigDecimal_GBP_RoundingMode_DOWN() {
129
        var test = Money.of(GBP, BIGDEC_2_34, RoundingMode.DOWN);
130
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
131
        assertThat(test.getAmountMinorInt()).isEqualTo(234);
132
        assertThat(test.getAmount().scale()).isEqualTo(2);
133
    }
134
135
    @Test
136
    void test_factory_of_Currency_BigDecimal_JPY_RoundingMode_DOWN() {
137
        var test = Money.of(JPY, BIGDEC_2_34, RoundingMode.DOWN);
138
        assertThat(test.getCurrencyUnit()).isEqualTo(JPY);
139
        assertThat(test.getAmountMinorInt()).isEqualTo(2);
140
        assertThat(test.getAmount().scale()).isEqualTo(0);
141
    }
142
143
    @Test
144
    void test_factory_of_Currency_BigDecimal_JPY_RoundingMode_UP() {
145
        var test = Money.of(JPY, BIGDEC_2_34, RoundingMode.UP);
146
        assertThat(test.getCurrencyUnit()).isEqualTo(JPY);
147
        assertThat(test.getAmountMinorInt()).isEqualTo(3);
148
        assertThat(test.getAmount().scale()).isEqualTo(0);
149
    }
150
151
    @Test
152
    void test_factory_of_Currency_BigDecimal_RoundingMode_UNNECESSARY() {
153
        assertThatExceptionOfType(ArithmeticException.class)
154
            .isThrownBy(() -> Money.of(JPY, BIGDEC_2_34, RoundingMode.UNNECESSARY));
155
    }
156
157
    @Test
158
    void test_factory_of_Currency_BigDecimal_RoundingMode_nullCurrency() {
159
        assertThatExceptionOfType(NullPointerException.class)
160
            .isThrownBy(() -> Money.of((CurrencyUnit) null, BIGDEC_2_34, RoundingMode.DOWN));
161
    }
162
163
    @Test
164
    void test_factory_of_Currency_BigDecimal_RoundingMode_nullBigDecimal() {
165
        assertThatExceptionOfType(NullPointerException.class)
166
            .isThrownBy(() -> Money.of(GBP, (BigDecimal) null, RoundingMode.DOWN));
167
    }
168
169
    @Test
170
    void test_factory_of_Currency_BigDecimal_RoundingMode_nullRoundingMode() {
171
        assertThatExceptionOfType(NullPointerException.class)
172
            .isThrownBy(() -> Money.of(GBP, BIGDEC_2_34, (RoundingMode) null));
173
    }
174
175
    //-----------------------------------------------------------------------
176
    // of(Currency,double)
177
    //-----------------------------------------------------------------------
178
    @Test
179
    void test_factory_of_Currency_double() {
180
        var test = Money.of(GBP, 2.34d);
181
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
182
        assertThat(test.getAmountMinorInt()).isEqualTo(234);
183
        assertThat(test.getScale()).isEqualTo(2);
184
    }
185
186
    @Test
187
    void test_factory_of_Currency_double_correctScale() {
188
        var test = Money.of(GBP, 2.3d);
189
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
190
        assertThat(test.getAmountMinorInt()).isEqualTo(230);
191
        assertThat(test.getScale()).isEqualTo(2);
192
    }
193
194
    @Test
195
    void test_factory_of_Currency_double_trailingZero1() {
196
        var test = Money.of(GBP, 1.230d);
197
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
198
        assertThat(test.getAmount()).isEqualTo(BigDecimal.valueOf(123L, 2));
199
        assertThat(test.getScale()).isEqualTo(2);
200
    }
201
202
    @Test
203
    void test_factory_of_Currency_double_trailingZero2() {
204
        var test = Money.of(GBP, 1.20d);
205
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
206
        assertThat(test.getAmount()).isEqualTo(BigDecimal.valueOf(120L, 2));
207
        assertThat(test.getScale()).isEqualTo(2);
208
    }
209
210
    @Test
211
    void test_factory_of_Currency_double_medium() {
212
        var test = Money.of(GBP, 2000d);
213
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
214
        assertThat(test.getAmount()).isEqualTo(BigDecimal.valueOf(200000L, 2));
215
        assertThat(test.getScale()).isEqualTo(2);
216
    }
217
218
    @Test
219
    void test_factory_of_Currency_double_big() {
220
        var test = Money.of(GBP, 200000000d);
221
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
222
        assertThat(test.getAmount()).isEqualTo(BigDecimal.valueOf(20000000000L, 2));
223
        assertThat(test.getScale()).isEqualTo(2);
224
    }
225
226
    @Test
227
    void test_factory_of_Currency_double_invalidScaleGBP() {
228
        assertThatExceptionOfType(ArithmeticException.class)
229
            .isThrownBy(() -> Money.of(GBP, 2.345d));
230
    }
231
232
    @Test
233
    void test_factory_of_Currency_double_invalidScaleJPY() {
234
        assertThatExceptionOfType(ArithmeticException.class)
235
            .isThrownBy(() -> Money.of(JPY, 2.3d));
236
    }
237
238
    @Test
239
    void test_factory_of_Currency_double_nullCurrency() {
240
        assertThatExceptionOfType(NullPointerException.class)
241
            .isThrownBy(() -> Money.of((CurrencyUnit) null, BIGDEC_2_34));
242
    }
243
244
    //-----------------------------------------------------------------------
245
    // of(Currency,double,RoundingMode)
246
    //-----------------------------------------------------------------------
247
    @Test
248
    void test_factory_of_Currency_double_GBP_RoundingMode_DOWN() {
249
        var test = Money.of(GBP, 2.34d, RoundingMode.DOWN);
250
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
251
        assertThat(test.getAmountMinorInt()).isEqualTo(234);
252
        assertThat(test.getAmount().scale()).isEqualTo(2);
253
    }
254
255
    @Test
256
    void test_factory_of_Currency_double_JPY_RoundingMode_DOWN() {
257
        var test = Money.of(JPY, 2.34d, RoundingMode.DOWN);
258
        assertThat(test.getCurrencyUnit()).isEqualTo(JPY);
259
        assertThat(test.getAmountMinorInt()).isEqualTo(2);
260
        assertThat(test.getAmount().scale()).isEqualTo(0);
261
    }
262
263
    @Test
264
    void test_factory_of_Currency_double_JPY_RoundingMode_UP() {
265
        var test = Money.of(JPY, 2.34d, RoundingMode.UP);
266
        assertThat(test.getCurrencyUnit()).isEqualTo(JPY);
267
        assertThat(test.getAmountMinorInt()).isEqualTo(3);
268
        assertThat(test.getAmount().scale()).isEqualTo(0);
269
    }
270
271
    @Test
272
    void test_factory_of_Currency_double_RoundingMode_UNNECESSARY() {
273
        assertThatExceptionOfType(ArithmeticException.class)
274
            .isThrownBy(() -> Money.of(JPY, 2.34d, RoundingMode.UNNECESSARY));
275
    }
276
277
    @Test
278
    void test_factory_of_Currency_double_RoundingMode_nullCurrency() {
279
        assertThatExceptionOfType(NullPointerException.class)
280
            .isThrownBy(() -> Money.of((CurrencyUnit) null, 2.34d, RoundingMode.DOWN));
281
    }
282
283
    @Test
284
    void test_factory_of_Currency_double_RoundingMode_nullRoundingMode() {
285
        assertThatExceptionOfType(NullPointerException.class)
286
            .isThrownBy(() -> Money.of(GBP, 2.34d, (RoundingMode) null));
287
    }
288
289
    //-----------------------------------------------------------------------
290
    // ofMajor(Currency,long)
291
    //-----------------------------------------------------------------------
292
    @Test
293
    void test_factory_ofMajor_Currency_long() {
294
        var test = Money.ofMajor(GBP, 234);
295
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
296
        assertThat(test.getAmountMinorInt()).isEqualTo(23400);
297
        assertThat(test.getAmount().scale()).isEqualTo(2);
298
    }
299
300
    @Test
301
    void test_factory_ofMajor_Currency_long_nullCurrency() {
302
        assertThatExceptionOfType(NullPointerException.class)
303
            .isThrownBy(() -> Money.ofMajor((CurrencyUnit) null, 234));
304
    }
305
306
    //-----------------------------------------------------------------------
307
    // ofMinor(Currency,long)
308
    //-----------------------------------------------------------------------
309
    @Test
310
    void test_factory_ofMinor_Currency_long() {
311
        var test = Money.ofMinor(GBP, 234);
312
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
313
        assertThat(test.getAmountMinorInt()).isEqualTo(234);
314
        assertThat(test.getAmount().scale()).isEqualTo(2);
315
    }
316
317
    @Test
318
    void test_factory_ofMinor_Currency_long_nullCurrency() {
319
        assertThatExceptionOfType(NullPointerException.class)
320
            .isThrownBy(() -> Money.ofMinor((CurrencyUnit) null, 234));
321
    }
322
323
    //-----------------------------------------------------------------------
324
    // zero(Currency)
325
    //-----------------------------------------------------------------------
326
    @Test
327
    void test_factory_zero_Currency() {
328
        var test = Money.zero(GBP);
329
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
330
        assertThat(test.getAmountMinorInt()).isEqualTo(0);
331
        assertThat(test.getAmount().scale()).isEqualTo(2);
332
    }
333
334
    @Test
335
    void test_factory_zero_Currency_nullCurrency() {
336
        assertThatExceptionOfType(NullPointerException.class)
337
            .isThrownBy(() -> Money.zero((CurrencyUnit) null));
338
    }
339
340
    //-----------------------------------------------------------------------
341
    // from(BigMoneyProvider)
342
    //-----------------------------------------------------------------------
343
    @Test
344
    void test_factory_from_BigMoneyProvider() {
345
        var test = Money.of(BigMoney.parse("GBP 104.23"));
346
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
347
        assertThat(test.getAmountMinorInt()).isEqualTo(10423);
348
        assertThat(test.getAmount().scale()).isEqualTo(2);
349
    }
350
351
    @Test
352
    void test_factory_from_BigMoneyProvider_fixScale() {
353
        var test = Money.of(BigMoney.parse("GBP 104.2"));
354
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
355
        assertThat(test.getAmountMinorInt()).isEqualTo(10420);
356
        assertThat(test.getAmount().scale()).isEqualTo(2);
357
    }
358
359
    @Test
360
    void test_factory_from_BigMoneyProvider_invalidCurrencyScale() {
361
        assertThatExceptionOfType(ArithmeticException.class)
362
            .isThrownBy(() -> Money.of(BigMoney.parse("GBP 104.235")));
363
    }
364
365
    @Test
366
    void test_factory_from_BigMoneyProvider_nullBigMoneyProvider() {
367
        assertThatExceptionOfType(NullPointerException.class)
368
            .isThrownBy(() -> Money.of((BigMoneyProvider) null));
369
    }
370
371
    //-----------------------------------------------------------------------
372
    // from(BigMoneyProvider,RoundingMode)
373
    //-----------------------------------------------------------------------
374
    @Test
375
    void test_factory_from_BigMoneyProvider_RoundingMode() {
376
        var test = Money.of(BigMoney.parse("GBP 104.235"), RoundingMode.HALF_EVEN);
377
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
378
        assertThat(test.getAmountMinorInt()).isEqualTo(10424);
379
        assertThat(test.getAmount().scale()).isEqualTo(2);
380
    }
381
382
    @Test
383
    void test_factory_from_BigMoneyProvider_RoundingMode_nullBigMoneyProvider() {
384
        assertThatExceptionOfType(NullPointerException.class)
385
            .isThrownBy(() -> Money.of((BigMoneyProvider) null, RoundingMode.DOWN));
386
    }
387
388
    @Test
389
    void test_factory_from_BigMoneyProvider_RoundingMode_nullRoundingMode() {
390
        assertThatExceptionOfType(NullPointerException.class)
391
            .isThrownBy(() -> Money.of(BigMoney.parse("GBP 104.235"), (RoundingMode) null));
392
    }
393
394
    //-----------------------------------------------------------------------
395
    // total(Money...)
396
    //-----------------------------------------------------------------------
397
    @Test
398
    void test_factory_total_varargs_1() {
399
        var test = Money.total(GBP_1_23);
400
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
401
        assertThat(test.getAmountMinorInt()).isEqualTo(123);
402
    }
403
404
    @Test
405
    void test_factory_total_array_1() {
406
        var array = new Money[] {GBP_1_23};
407
        var test = Money.total(array);
408
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
409
        assertThat(test.getAmountMinorInt()).isEqualTo(123);
410
    }
411
412
    @Test
413
    void test_factory_total_varargs_3() {
414
        var test = Money.total(GBP_1_23, GBP_2_33, GBP_2_36);
415
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
416
        assertThat(test.getAmountMinorInt()).isEqualTo(592);
417
    }
418
419
    @Test
420
    void test_factory_total_array_3() {
421
        var array = new Money[] {GBP_1_23, GBP_2_33, GBP_2_36};
422
        var test = Money.total(array);
423
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
424
        assertThat(test.getAmountMinorInt()).isEqualTo(592);
425
    }
426
427
    @Test
428
    void test_factory_total_varargs_empty() {
429
        assertThatExceptionOfType(IllegalArgumentException.class)
430
            .isThrownBy(() -> Money.total());
431
    }
432
433
    @Test
434
    void test_factory_total_array_empty() {
435
        var array = new Money[0];
436
        assertThatExceptionOfType(IllegalArgumentException.class)
437
            .isThrownBy(() -> Money.total(array));
438
    }
439
440
    @Test
441
    void test_factory_total_varargs_currenciesDiffer() {
442
        assertThatExceptionOfType(CurrencyMismatchException.class)
443
            .isThrownBy(() -> {
444
                try {
445
                    Money.total(GBP_2_33, JPY_423);
446
                } catch (CurrencyMismatchException ex) {
447
                    assertEquals(GBP, ex.getFirstCurrency());
448
                    assertEquals(JPY, ex.getSecondCurrency());
449
                    throw ex;
450
                }
451
            });
452
    }
453
454
    @Test
455
    void test_factory_total_array_currenciesDiffer() {
456
        assertThatExceptionOfType(CurrencyMismatchException.class)
457
            .isThrownBy(() -> {
458
                try {
459
                    var array = new Money[] {GBP_2_33, JPY_423};
460
                    Money.total(array);
461
                } catch (CurrencyMismatchException ex) {
462
                    assertEquals(GBP, ex.getFirstCurrency());
463
                    assertEquals(JPY, ex.getSecondCurrency());
464
                    throw ex;
465
                }
466
            });
467
    }
468
469
    @Test
470
    void test_factory_total_varargs_nullFirst() {
471
        assertThatExceptionOfType(NullPointerException.class)
472
            .isThrownBy(() -> Money.total((Money) null, GBP_2_33, GBP_2_36));
473
    }
474
475
    @Test
476
    void test_factory_total_array_nullFirst() {
477
        var array = new Money[] {null, GBP_2_33, GBP_2_36};
478
        assertThatExceptionOfType(NullPointerException.class)
479
            .isThrownBy(() -> Money.total(array));
480
    }
481
482
    @Test
483
    void test_factory_total_varargs_nullNotFirst() {
484
        assertThatExceptionOfType(NullPointerException.class)
485
            .isThrownBy(() -> Money.total(GBP_2_33, null, GBP_2_36));
486
    }
487
488
    @Test
489
    void test_factory_total_array_nullNotFirst() {
490
        var array = new Money[] {GBP_2_33, null, GBP_2_36};
491
        assertThatExceptionOfType(NullPointerException.class)
492
            .isThrownBy(() -> Money.total(array));
493
    }
494
495
    //-----------------------------------------------------------------------
496
    // total(Iterable)
497
    //-----------------------------------------------------------------------
498
    @Test
499
    void test_factory_total_Iterable() {
500
        Iterable<Money> iterable = Arrays.asList(GBP_1_23, GBP_2_33, GBP_2_36);
501
        var test = Money.total(iterable);
502
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
503
        assertThat(test.getAmountMinorInt()).isEqualTo(592);
504
    }
505
506
    @Test
507
    void test_factory_total_Iterable_empty() {
508
        Iterable<Money> iterable = Collections.emptyList();
509
        assertThatExceptionOfType(IllegalArgumentException.class)
510
            .isThrownBy(() -> Money.total(iterable));
511
    }
512
513
    @Test
514
    void test_factory_total_Iterable_currenciesDiffer() {
515
        assertThatExceptionOfType(CurrencyMismatchException.class)
516
            .isThrownBy(() -> {
517
                try {
518
                    Iterable<Money> iterable = Arrays.asList(GBP_2_33, JPY_423);
519
                    Money.total(iterable);
520
                } catch (CurrencyMismatchException ex) {
521
                    assertEquals(GBP, ex.getFirstCurrency());
522
                    assertEquals(JPY, ex.getSecondCurrency());
523
                    throw ex;
524
                }
525
            });
526
    }
527
528
    @Test
529
    void test_factory_total_Iterable_nullFirst() {
530
        Iterable<Money> iterable = Arrays.asList(null, GBP_2_33, GBP_2_36);
531
        assertThatExceptionOfType(NullPointerException.class)
532
            .isThrownBy(() -> Money.total(iterable));
533
    }
534
535
    @Test
536
    void test_factory_total_Iterable_nullNotFirst() {
537
        Iterable<Money> iterable = Arrays.asList(GBP_2_33, null, GBP_2_36);
538
        assertThatExceptionOfType(NullPointerException.class)
539
            .isThrownBy(() -> Money.total(iterable));
540
    }
541
542
    //-----------------------------------------------------------------------
543
    // total(CurrencyUnit,Money...)
544
    //-----------------------------------------------------------------------
545
    @Test
546
    void test_factory_total_CurrencyUnitVarargs_1() {
547
        var test = Money.total(GBP, GBP_1_23);
548
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
549
        assertThat(test.getAmountMinorInt()).isEqualTo(123);
550
    }
551
552
    @Test
553
    void test_factory_total_CurrencyUnitArray_1() {
554
        var array = new Money[] {GBP_1_23};
555
        var test = Money.total(GBP, array);
556
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
557
        assertThat(test.getAmountMinorInt()).isEqualTo(123);
558
    }
559
560
    @Test
561
    void test_factory_total_CurrencyUnitVarargs_3() {
562
        var test = Money.total(GBP, GBP_1_23, GBP_2_33, GBP_2_36);
563
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
564
        assertThat(test.getAmountMinorInt()).isEqualTo(592);
565
    }
566
567
    @Test
568
    void test_factory_total_CurrencyUnitArray_3() {
569
        var array = new Money[] {GBP_1_23, GBP_2_33, GBP_2_36};
570
        var test = Money.total(GBP, array);
571
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
572
        assertThat(test.getAmountMinorInt()).isEqualTo(592);
573
    }
574
575
    @Test
576
    void test_factory_total_CurrencyUnitVarargs_empty() {
577
        var test = Money.total(GBP);
578
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
579
        assertThat(test.getAmountMinorInt()).isEqualTo(0);
580
    }
581
582
    @Test
583
    void test_factory_total_CurrencyUnitArray_empty() {
584
        var array = new Money[0];
585
        var test = Money.total(GBP, array);
586
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
587
        assertThat(test.getAmountMinorInt()).isEqualTo(0);
588
    }
589
590
    @Test
591
    void test_factory_total_CurrencyUnitVarargs_currenciesDiffer() {
592
        assertThatExceptionOfType(CurrencyMismatchException.class)
593
            .isThrownBy(() -> {
594
                try {
595
                    Money.total(GBP, JPY_423);
596
                } catch (CurrencyMismatchException ex) {
597
                    assertEquals(GBP, ex.getFirstCurrency());
598
                    assertEquals(JPY, ex.getSecondCurrency());
599
                    throw ex;
600
                }
601
            });
602
    }
603
604
    @Test
605
    void test_factory_total_CurrencyUnitArray_currenciesDiffer() {
606
        var array = new Money[] {JPY_423};
607
        assertThatExceptionOfType(CurrencyMismatchException.class)
608
            .isThrownBy(() -> {
609
                try {
610
                    Money.total(GBP, array);
611
                } catch (CurrencyMismatchException ex) {
612
                    assertEquals(GBP, ex.getFirstCurrency());
613
                    assertEquals(JPY, ex.getSecondCurrency());
614
                    throw ex;
615
                }
616
            });
617
    }
618
619
    @Test
620
    void test_factory_total_CurrencyUnitVarargs_currenciesDifferInArray() {
621
        assertThatExceptionOfType(CurrencyMismatchException.class)
622
            .isThrownBy(() -> {
623
                try {
624
                    Money.total(GBP, GBP_2_33, JPY_423);
625
                } catch (CurrencyMismatchException ex) {
626
                    assertEquals(GBP, ex.getFirstCurrency());
627
                    assertEquals(JPY, ex.getSecondCurrency());
628
                    throw ex;
629
                }
630
            });
631
    }
632
633
    @Test
634
    void test_factory_total_CurrencyUnitArray_currenciesDifferInArray() {
635
        var array = new Money[] {GBP_2_33, JPY_423};
636
        assertThatExceptionOfType(CurrencyMismatchException.class)
637
            .isThrownBy(() -> {
638
                try {
639
                    Money.total(GBP, array);
640
                } catch (CurrencyMismatchException ex) {
641
                    assertEquals(GBP, ex.getFirstCurrency());
642
                    assertEquals(JPY, ex.getSecondCurrency());
643
                    throw ex;
644
                }
645
            });
646
    }
647
648
    @Test
649
    void test_factory_total_CurrencyUnitVarargs_nullFirst() {
650
        assertThatExceptionOfType(NullPointerException.class)
651
            .isThrownBy(() -> Money.total(GBP, null, GBP_2_33, GBP_2_36));
652
    }
653
654
    @Test
655
    void test_factory_total_CurrencyUnitArray_nullFirst() {
656
        var array = new Money[] {null, GBP_2_33, GBP_2_36};
657
        assertThatExceptionOfType(NullPointerException.class)
658
            .isThrownBy(() -> Money.total(GBP, array));
659
    }
660
661
    @Test
662
    void test_factory_total_CurrencyUnitVarargs_nullNotFirst() {
663
        assertThatExceptionOfType(NullPointerException.class)
664
            .isThrownBy(() -> Money.total(GBP, GBP_2_33, null, GBP_2_36));
665
    }
666
667
    @Test
668
    void test_factory_total_CurrencyUnitArray_nullNotFirst() {
669
        var array = new Money[] {GBP_2_33, null, GBP_2_36};
670
        assertThatExceptionOfType(NullPointerException.class)
671
            .isThrownBy(() -> Money.total(GBP, array));
672
    }
673
674
    //-----------------------------------------------------------------------
675
    // total(CurrencyUnit,Iterable)
676
    //-----------------------------------------------------------------------
677
    @Test
678
    void test_factory_total_CurrencyUnitIterable() {
679
        Iterable<Money> iterable = Arrays.asList(GBP_1_23, GBP_2_33, GBP_2_36);
680
        var test = Money.total(GBP, iterable);
681
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
682
        assertThat(test.getAmountMinorInt()).isEqualTo(592);
683
    }
684
685
    @Test
686
    void test_factory_total_CurrencyUnitIterable_empty() {
687
        Iterable<Money> iterable = Collections.emptyList();
688
        var test = Money.total(GBP, iterable);
689
        assertThat(test.getCurrencyUnit()).isEqualTo(GBP);
690
        assertThat(test.getAmountMinorInt()).isEqualTo(0);
691
    }
692
693
    @Test
694
    void test_factory_total_CurrencyUnitIterable_currenciesDiffer() {
695
        Iterable<Money> iterable = Arrays.asList(JPY_423);
696
        assertThatExceptionOfType(CurrencyMismatchException.class)
697
            .isThrownBy(() -> {
698
                try {
699
                    Money.total(GBP, iterable);
700
                } catch (CurrencyMismatchException ex) {
701
                    assertEquals(GBP, ex.getFirstCurrency());
702
                    assertEquals(JPY, ex.getSecondCurrency());
703
                    throw ex;
704
                }
705
            });
706
    }
707
708
    @Test
709
    void test_factory_total_CurrencyUnitIterable_currenciesDifferInIterable() {
710
        Iterable<Money> iterable = Arrays.asList(GBP_2_33, JPY_423);
711
        assertThatExceptionOfType(CurrencyMismatchException.class)
712
            .isThrownBy(() -> {
713
                try {
714
                    Money.total(GBP, iterable);
715
                } catch (CurrencyMismatchException ex) {
716
                    assertEquals(GBP, ex.getFirstCurrency());
717
                    assertEquals(JPY, ex.getSecondCurrency());
718
                    throw ex;
719
                }
720
            });
721
    }
722
723
    @Test
724
    void test_factory_total_CurrencyUnitIterable_nullFirst() {
725
        Iterable<Money> iterable = Arrays.asList(null, GBP_2_33, GBP_2_36);
726
        assertThatExceptionOfType(NullPointerException.class)
727
            .isThrownBy(() -> Money.total(GBP, iterable));
728
    }
729
730
    @Test
731
    void test_factory_total_CurrencyUnitIterable_nullNotFirst() {
732
        Iterable<Money> iterable = Arrays.asList(GBP_2_33, null, GBP_2_36);
733
        assertThatExceptionOfType(NullPointerException.class)
734
            .isThrownBy(() -> Money.total(GBP, iterable));
735
    }
736
737
    //-----------------------------------------------------------------------
738
    // parse(String)
739
    //-----------------------------------------------------------------------
740
    public static Object[][] data_parse() {
741
        return new Object[][] {
742
            {"GBP 2.43", GBP, 243},
743
            {"GBP +12.57", GBP, 1257},
744
            {"GBP -5.87", GBP, -587},
745
            {"GBP 0.99", GBP, 99},
746
            {"GBP .99", GBP, 99},
747
            {"GBP +.99", GBP, 99},
748
            {"GBP +0.99", GBP, 99},
749
            {"GBP -.99", GBP, -99},
750
            {"GBP -0.99", GBP, -99},
751
            {"GBP 0", GBP, 0},
752
            {"GBP 2", GBP, 200},
753
            {"GBP 123.", GBP, 12300},
754
            {"GBP3", GBP, 300},
755
            {"GBP3.10", GBP, 310},
756
            {"GBP  3.10", GBP, 310},
757
            {"GBP   3.10", GBP, 310},
758
            {"GBP                           3.10", GBP, 310},
759
        };
760
    }
761
762
    @ParameterizedTest
763
    @MethodSource("data_parse")
764
    void test_factory_parse(String str, CurrencyUnit currency, int amount) {
765
        var test = Money.parse(str);
766
        assertThat(test.getCurrencyUnit()).isEqualTo(currency);
767
        assertThat(test.getAmountMinorInt()).isEqualTo(amount);
768
    }
769
770
    @Test
771
    void test_factory_parse_String_tooShort() {
772
        assertThatExceptionOfType(IllegalArgumentException.class)
773
            .isThrownBy(() -> Money.parse("GBP "));
774
    }
775
776
    @Test
777
    void test_factory_parse_String_badCurrency() {
778
        assertThatExceptionOfType(IllegalArgumentException.class)
779
            .isThrownBy(() -> Money.parse("GBX 2.34"));
780
    }
781
782
    @Test
783
    void test_factory_parse_String_nullString() {
784
        assertThatExceptionOfType(NullPointerException.class)
785
            .isThrownBy(() -> Money.parse((String) null));
786
    }
787
788
    //-----------------------------------------------------------------------
789
    // constructor
790
    //-----------------------------------------------------------------------
791
    @Test
792
    void test_constructor_null1() throws Exception {
793
        var con = Money.class.getDeclaredConstructor(BigMoney.class);
794
        assertThat(Modifier.isPublic(con.getModifiers())).isFalse();
795
        assertThat(Modifier.isProtected(con.getModifiers())).isFalse();
796
        try {
797
            con.setAccessible(true);
798
            con.newInstance(new Object[] {null});
799
            fail("");
800
        } catch (InvocationTargetException ex) {
801
            assertThat(ex.getCause().getClass()).isEqualTo(AssertionError.class);
802
        }
803
    }
804
805
    @Test
806
    void test_constructor_scale() throws Exception {
807
        var con = Money.class.getDeclaredConstructor(BigMoney.class);
808
        try {
809
            con.setAccessible(true);
810
            con.newInstance(new Object[] {BigMoney.of(GBP, BIGDEC_2_3)});
811
            fail("");
812
        } catch (InvocationTargetException ex) {
813
            assertThat(ex.getCause().getClass()).isEqualTo(AssertionError.class);
814
        }
815
    }
816
817
    //-----------------------------------------------------------------------
818
    // serialization
819
    //-----------------------------------------------------------------------
820
    @Test
821
    void test_serialization() throws Exception {
822
        var a = GBP_2_34;
823
        var baos = new ByteArrayOutputStream();
824
        try (var oos = new ObjectOutputStream(baos)) {
825
            oos.writeObject(a);
826
            oos.close();
827
            var ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
828
            var input = (Money) ois.readObject();
829
            assertThat(input).isEqualTo(a);
830
        }
831
    }
832
833
    @Test
834
    void test_serialization_invalidNumericCode() throws IOException {
835
        var cu = new CurrencyUnit("GBP", (short) 234, (short) 2);
836
        var m = Money.of(cu, 123.43d);
837
        var baos = new ByteArrayOutputStream();
838
        try (var oos = new ObjectOutputStream(baos)) {
839
            oos.writeObject(m);
840
            oos.close();
841
            var ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
842
            assertThatExceptionOfType(InvalidObjectException.class)
843
                .isThrownBy(() -> ois.readObject());
844
        }
845
    }
846
847
    @Test
848
    void test_serialization_invalidDecimalPlaces() throws IOException {
849
        var cu = new CurrencyUnit("GBP", (short) 826, (short) 3);
850
        var m = Money.of(cu, 123.43d);
851
        var baos = new ByteArrayOutputStream();
852
        try (var oos = new ObjectOutputStream(baos)) {
853
            oos.writeObject(m);
854
            oos.close();
855
            var ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
856
            assertThatExceptionOfType(InvalidObjectException.class)
857
                .isThrownBy(() -> ois.readObject());
858
        }
859
    }
860
861
    //-----------------------------------------------------------------------
862
    // getCurrencyUnit()
863
    //-----------------------------------------------------------------------
864
    @Test
865
    void test_getCurrencyUnit_GBP() {
866
        assertThat(GBP_2_34.getCurrencyUnit()).isEqualTo(GBP);
867
    }
868
869
    @Test
870
    void test_getCurrencyUnit_EUR() {
871
        assertThat(Money.parse("EUR -5.78").getCurrencyUnit()).isEqualTo(EUR);
872
    }
873
874
    //-----------------------------------------------------------------------
875
    // withCurrencyUnit(Currency)
876
    //-----------------------------------------------------------------------
877
    @Test
878
    void test_withCurrencyUnit_Currency() {
879
        var test = GBP_2_34.withCurrencyUnit(USD);
880
        assertThat(test).hasToString("USD 2.34");
881
    }
882
883
    @Test
884
    void test_withCurrencyUnit_Currency_same() {
885
        var test = GBP_2_34.withCurrencyUnit(GBP);
886
        assertThat(test).isSameAs(GBP_2_34);
887
    }
888
889
    @Test
890
    void test_withCurrencyUnit_Currency_scaleProblem() {
891
        assertThatExceptionOfType(ArithmeticException.class)
892
            .isThrownBy(() -> GBP_2_34.withCurrencyUnit(JPY));
893
    }
894
895
    @Test
896
    void test_withCurrencyUnit_Currency_nullCurrency() {
897
        assertThatExceptionOfType(NullPointerException.class)
898
            .isThrownBy(() -> GBP_2_34.withCurrencyUnit((CurrencyUnit) null));
899
    }
900
901
    //-----------------------------------------------------------------------
902
    // withCurrencyUnit(Currency,RoundingMode)
903
    //-----------------------------------------------------------------------
904
    @Test
905
    void test_withCurrencyUnit_CurrencyRoundingMode_DOWN() {
906
        var test = GBP_2_34.withCurrencyUnit(JPY, RoundingMode.DOWN);
907
        assertThat(test).hasToString("JPY 2");
908
    }
909
910
    @Test
911
    void test_withCurrencyUnit_CurrencyRoundingMode_UP() {
912
        var test = GBP_2_34.withCurrencyUnit(JPY, RoundingMode.UP);
913
        assertThat(test).hasToString("JPY 3");
914
    }
915
916
    @Test
917
    void test_withCurrencyUnit_CurrencyRoundingMode_same() {
918
        var test = GBP_2_34.withCurrencyUnit(GBP, RoundingMode.DOWN);
919
        assertThat(test).isSameAs(GBP_2_34);
920
    }
921
922
    @Test
923
    void test_withCurrencyUnit_CurrencyRoundingMode_UNECESSARY() {
924
        assertThatExceptionOfType(ArithmeticException.class)
925
            .isThrownBy(() -> GBP_2_34.withCurrencyUnit(JPY, RoundingMode.UNNECESSARY));
926
    }
927
928
    @Test
929
    void test_withCurrencyUnit_CurrencyRoundingMode_nullCurrency() {
930
        assertThatExceptionOfType(NullPointerException.class)
931
            .isThrownBy(() -> GBP_2_34.withCurrencyUnit((CurrencyUnit) null, RoundingMode.UNNECESSARY));
932
    }
933
934
    //-----------------------------------------------------------------------
935
    // getScale()
936
    //-----------------------------------------------------------------------
937
    @Test
938
    void test_getScale_GBP() {
939
        assertThat(GBP_2_34.getScale()).isEqualTo(2);
940
    }
941
942
    @Test
943
    void test_getScale_JPY() {
944
        assertThat(JPY_423.getScale()).isEqualTo(0);
945
    }
946
947
    //-----------------------------------------------------------------------
948
    // getAmount()
949
    //-----------------------------------------------------------------------
950
    @Test
951
    void test_getAmount_positive() {
952
        assertThat(GBP_2_34.getAmount()).isEqualTo(BIGDEC_2_34);
953
    }
954
955
    @Test
956
    void test_getAmount_negative() {
957
        assertThat(GBP_M5_78.getAmount()).isEqualTo(BIGDEC_M5_78);
958
    }
959
960
    //-----------------------------------------------------------------------
961
    // getAmountMajor()
962
    //-----------------------------------------------------------------------
963
    @Test
964
    void test_getAmountMajor_positive() {
965
        assertThat(GBP_2_34.getAmountMajor()).isEqualTo(BigDecimal.valueOf(2));
966
    }
967
968
    @Test
969
    void test_getAmountMajor_negative() {
970
        assertThat(GBP_M5_78.getAmountMajor()).isEqualTo(BigDecimal.valueOf(-5));
971
    }
972
973
    //-----------------------------------------------------------------------
974
    // getAmountMajorLong()
975
    //-----------------------------------------------------------------------
976
    @Test
977
    void test_getAmountMajorLong_positive() {
978
        assertThat(GBP_2_34.getAmountMajorLong()).isEqualTo(2L);
979
    }
980
981
    @Test
982
    void test_getAmountMajorLong_negative() {
983
        assertThat(GBP_M5_78.getAmountMajorLong()).isEqualTo(-5L);
984
    }
985
986
    @Test
987
    void test_getAmountMajorLong_tooBigPositive() {
988
        assertThatExceptionOfType(ArithmeticException.class)
989
            .isThrownBy(() -> GBP_LONG_MAX_MAJOR_PLUS1.getAmountMajorLong());
990
    }
991
992
    @Test
993
    void test_getAmountMajorLong_tooBigNegative() {
994
        assertThatExceptionOfType(ArithmeticException.class)
995
            .isThrownBy(() -> GBP_LONG_MIN_MAJOR_MINUS1.getAmountMajorLong());
996
    }
997
998
    //-----------------------------------------------------------------------
999
    // getAmountMajorInt()
1000
    //-----------------------------------------------------------------------
1001
    @Test
1002
    void test_getAmountMajorInt_positive() {
1003
        assertThat(GBP_2_34.getAmountMajorInt()).isEqualTo(2);
1004
    }
1005
1006
    @Test
1007
    void test_getAmountMajorInt_negative() {
1008
        assertThat(GBP_M5_78.getAmountMajorInt()).isEqualTo(-5);
1009
    }
1010
1011
    @Test
1012
    void test_getAmountMajorInt_tooBigPositive() {
1013
        assertThatExceptionOfType(ArithmeticException.class)
1014
            .isThrownBy(() -> GBP_INT_MAX_MAJOR_PLUS1.getAmountMajorInt());
1015
    }
1016
1017
    @Test
1018
    void test_getAmountMajorInt_tooBigNegative() {
1019
        assertThatExceptionOfType(ArithmeticException.class)
1020
            .isThrownBy(() -> GBP_INT_MIN_MAJOR_MINUS1.getAmountMajorInt());
1021
    }
1022
1023
    //-----------------------------------------------------------------------
1024
    // getAmountMinor()
1025
    //-----------------------------------------------------------------------
1026
    @Test
1027
    void test_getAmountMinor_positive() {
1028
        assertThat(GBP_2_34.getAmountMinor()).isEqualTo(BigDecimal.valueOf(234));
1029
    }
1030
1031
    @Test
1032
    void test_getAmountMinor_negative() {
1033
        assertThat(GBP_M5_78.getAmountMinor()).isEqualTo(BigDecimal.valueOf(-578));
1034
    }
1035
1036
    //-----------------------------------------------------------------------
1037
    // getAmountMinorLong()
1038
    //-----------------------------------------------------------------------
1039
    @Test
1040
    void test_getAmountMinorLong_positive() {
1041
        assertThat(GBP_2_34.getAmountMinorLong()).isEqualTo(234L);
1042
    }
1043
1044
    @Test
1045
    void test_getAmountMinorLong_negative() {
1046
        assertThat(GBP_M5_78.getAmountMinorLong()).isEqualTo(-578L);
1047
    }
1048
1049
    @Test
1050
    void test_getAmountMinorLong_tooBigPositive() {
1051
        assertThatExceptionOfType(ArithmeticException.class)
1052
            .isThrownBy(() -> GBP_LONG_MAX_PLUS1.getAmountMinorLong());
1053
    }
1054
1055
    @Test
1056
    void test_getAmountMinorLong_tooBigNegative() {
1057
        assertThatExceptionOfType(ArithmeticException.class)
1058
            .isThrownBy(() -> GBP_LONG_MIN_MINUS1.getAmountMinorLong());
1059
    }
1060
1061
    //-----------------------------------------------------------------------
1062
    // getAmountMinorInt()
1063
    //-----------------------------------------------------------------------
1064
    @Test
1065
    void test_getAmountMinorInt_positive() {
1066
        assertThat(GBP_2_34.getAmountMinorInt()).isEqualTo(234);
1067
    }
1068
1069
    @Test
1070
    void test_getAmountMinorInt_negative() {
1071
        assertThat(GBP_M5_78.getAmountMinorInt()).isEqualTo(-578);
1072
    }
1073
1074
    @Test
1075
    void test_getAmountMinorInt_tooBigPositive() {
1076
        assertThatExceptionOfType(ArithmeticException.class)
1077
            .isThrownBy(() -> GBP_INT_MAX_PLUS1.getAmountMinorInt());
1078
    }
1079
1080
    @Test
1081
    void test_getAmountMinorInt_tooBigNegative() {
1082
        assertThatExceptionOfType(ArithmeticException.class)
1083
            .isThrownBy(() -> GBP_INT_MIN_MINUS1.getAmountMinorInt());
1084
    }
1085
1086
    //-----------------------------------------------------------------------
1087
    // getMinorPart()
1088
    //-----------------------------------------------------------------------
1089
    @Test
1090
    void test_getMinorPart_positive() {
1091
        assertThat(GBP_2_34.getMinorPart()).isEqualTo(34);
1092
    }
1093
1094
    @Test
1095
    void test_getMinorPart_negative() {
1096
        assertThat(GBP_M5_78.getMinorPart()).isEqualTo(-78);
1097
    }
1098
1099
    //-----------------------------------------------------------------------
1100
    // isZero()
1101
    //-----------------------------------------------------------------------
1102
    @Test
1103
    void test_isZero() {
1104
        assertThat(GBP_0_00.isZero()).isTrue();
1105
        assertThat(GBP_2_34.isZero()).isFalse();
1106
        assertThat(GBP_M5_78.isZero()).isFalse();
1107
    }
1108
1109
    //-----------------------------------------------------------------------
1110
    // isPositive()
1111
    //-----------------------------------------------------------------------
1112
    @Test
1113
    void test_isPositive() {
1114
        assertThat(GBP_0_00.isPositive()).isFalse();
1115
        assertThat(GBP_2_34.isPositive()).isTrue();
1116
        assertThat(GBP_M5_78.isPositive()).isFalse();
1117
    }
1118
1119
    //-----------------------------------------------------------------------
1120
    // isPositiveOrZero()
1121
    //-----------------------------------------------------------------------
1122
    @Test
1123
    void test_isPositiveOrZero() {
1124
        assertThat(GBP_0_00.isPositiveOrZero()).isTrue();
1125
        assertThat(GBP_2_34.isPositiveOrZero()).isTrue();
1126
        assertThat(GBP_M5_78.isPositiveOrZero()).isFalse();
1127
    }
1128
1129
    //-----------------------------------------------------------------------
1130
    // isNegative()
1131
    //-----------------------------------------------------------------------
1132
    @Test
1133
    void test_isNegative() {
1134
        assertThat(GBP_0_00.isNegative()).isFalse();
1135
        assertThat(GBP_2_34.isNegative()).isFalse();
1136
        assertThat(GBP_M5_78.isNegative()).isTrue();
1137
    }
1138
1139
    //-----------------------------------------------------------------------
1140
    // isNegativeOrZero()
1141
    //-----------------------------------------------------------------------
1142
    @Test
1143
    void test_isNegativeOrZero() {
1144
        assertThat(GBP_0_00.isNegativeOrZero()).isTrue();
1145
        assertThat(GBP_2_34.isNegativeOrZero()).isFalse();
1146
        assertThat(GBP_M5_78.isNegativeOrZero()).isTrue();
1147
    }
1148
1149
    //-----------------------------------------------------------------------
1150
    // withAmount(BigDecimal)
1151
    //-----------------------------------------------------------------------
1152
    @Test
1153
    void test_withAmount_BigDecimal() {
1154
        var test = GBP_2_34.withAmount(BIGDEC_M5_78);
1155
        assertThat(test).hasToString("GBP -5.78");
1156
    }
1157
1158
    @Test
1159
    void test_withAmount_BigDecimal_same() {
1160
        var test = GBP_2_34.withAmount(BIGDEC_2_34);
1161
        assertThat(test).isSameAs(GBP_2_34);
1162
    }
1163
1164
    @Test
1165
    void test_withAmount_BigDecimal_invalidScale() {
1166
        assertThatExceptionOfType(ArithmeticException.class)
1167
            .isThrownBy(() -> GBP_2_34.withAmount(new BigDecimal("2.345")));
1168
    }
1169
1170
    @Test
1171
    void test_withAmount_BigDecimal_nullBigDecimal() {
1172
        assertThatExceptionOfType(NullPointerException.class)
1173
            .isThrownBy(() -> GBP_2_34.withAmount((BigDecimal) null));
1174
    }
1175
1176
    //-----------------------------------------------------------------------
1177
    // withAmount(BigDecimal,RoundingMode)
1178
    //-----------------------------------------------------------------------
1179
    @Test
1180
    void test_withAmount_BigDecimalRoundingMode() {
1181
        var test = GBP_2_34.withAmount(BIGDEC_M5_78, RoundingMode.UNNECESSARY);
1182
        assertThat(test).hasToString("GBP -5.78");
1183
    }
1184
1185
    @Test
1186
    void test_withAmount_BigDecimalRoundingMode_same() {
1187
        var test = GBP_2_34.withAmount(BIGDEC_2_34, RoundingMode.UNNECESSARY);
1188
        assertThat(test).isSameAs(GBP_2_34);
1189
    }
1190
1191
    @Test
1192
    void test_withAmount_BigDecimalRoundingMode_roundDown() {
1193
        var test = GBP_2_34.withAmount(new BigDecimal("2.355"), RoundingMode.DOWN);
1194
        assertThat(test).isEqualTo(GBP_2_35);
1195
    }
1196
1197
    @Test
1198
    void test_withAmount_BigDecimalRoundingMode_roundUnecessary() {
1199
        assertThatExceptionOfType(ArithmeticException.class)
1200
            .isThrownBy(() -> GBP_2_34.withAmount(new BigDecimal("2.345"), RoundingMode.UNNECESSARY));
1201
    }
1202
1203
    @Test
1204
    void test_withAmount_BigDecimalRoundingMode_nullBigDecimal() {
1205
        assertThatExceptionOfType(NullPointerException.class)
1206
            .isThrownBy(() -> GBP_2_34.withAmount((BigDecimal) null, RoundingMode.UNNECESSARY));
1207
    }
1208
1209
    @Test
1210
    void test_withAmount_BigDecimalRoundingMode_nullRoundingMode() {
1211
        assertThatExceptionOfType(NullPointerException.class)
1212
            .isThrownBy(() -> GBP_2_34.withAmount(BIGDEC_2_34, (RoundingMode) null));
1213
    }
1214
1215
    //-----------------------------------------------------------------------
1216
    // withAmount(double)
1217
    //-----------------------------------------------------------------------
1218
    @Test
1219
    void test_withAmount_double() {
1220
        var test = GBP_2_34.withAmount(-5.78d);
1221
        assertThat(test).hasToString("GBP -5.78");
1222
    }
1223
1224
    @Test
1225
    void test_withAmount_double_same() {
1226
        var test = GBP_2_34.withAmount(2.34d);
1227
        assertThat(test).isSameAs(GBP_2_34);
1228
    }
1229
1230
    @Test
1231
    void test_withAmount_double_invalidScale() {
1232
        assertThatExceptionOfType(ArithmeticException.class)
1233
            .isThrownBy(() -> GBP_2_34.withAmount(2.345d));
1234
    }
1235
1236
    //-----------------------------------------------------------------------
1237
    // withAmount(double,RoundingMode)
1238
    //-----------------------------------------------------------------------
1239
    @Test
1240
    void test_withAmount_doubleRoundingMode() {
1241
        var test = GBP_2_34.withAmount(-5.78d, RoundingMode.UNNECESSARY);
1242
        assertThat(test).hasToString("GBP -5.78");
1243
    }
1244
1245
    @Test
1246
    void test_withAmount_doubleRoundingMode_same() {
1247
        var test = GBP_2_34.withAmount(2.34d, RoundingMode.UNNECESSARY);
1248
        assertThat(test).isSameAs(GBP_2_34);
1249
    }
1250
1251
    @Test
1252
    void test_withAmount_doubleRoundingMode_roundDown() {
1253
        var test = GBP_2_34.withAmount(2.355d, RoundingMode.DOWN);
1254
        assertThat(test).isEqualTo(GBP_2_35);
1255
    }
1256
1257
    @Test
1258
    void test_withAmount_doubleRoundingMode_roundUnecessary() {
1259
        assertThatExceptionOfType(ArithmeticException.class)
1260
            .isThrownBy(() -> GBP_2_34.withAmount(2.345d, RoundingMode.UNNECESSARY));
1261
    }
1262
1263
    @Test
1264
    void test_withAmount_doubleRoundingMode_nullRoundingMode() {
1265
        assertThatExceptionOfType(NullPointerException.class)
1266
            .isThrownBy(() -> GBP_2_34.withAmount(BIGDEC_2_34, (RoundingMode) null));
1267
    }
1268
1269
    //-----------------------------------------------------------------------
1270
    // plus(Iterable)
1271
    //-----------------------------------------------------------------------
1272
    @Test
1273
    void test_plus_Iterable() {
1274
        Iterable<Money> iterable = Arrays.asList(GBP_2_33, GBP_1_23);
1275
        var test = GBP_2_34.plus(iterable);
1276
        assertThat(test).hasToString("GBP 5.90");
1277
    }
1278
1279
    @Test
1280
    void test_plus_Iterable_zero() {
1281
        Iterable<Money> iterable = Arrays.asList(GBP_0_00);
1282
        var test = GBP_2_34.plus(iterable);
1283
        assertThat(test).isSameAs(GBP_2_34);
1284
    }
1285
1286
    @Test
1287
    void test_plus_Iterable_currencyMismatch() {
1288
        assertThatExceptionOfType(CurrencyMismatchException.class)
1289
            .isThrownBy(() -> {
1290
                try {
1291
                    Iterable<Money> iterable = Arrays.asList(GBP_2_33, JPY_423);
1292
                    GBP_M5_78.plus(iterable);
1293
                } catch (CurrencyMismatchException ex) {
1294
                    assertEquals(GBP, ex.getFirstCurrency());
1295
                    assertEquals(JPY, ex.getSecondCurrency());
1296
                    throw ex;
1297
                }
1298
            });
1299
    }
1300
1301
    @Test
1302
    void test_plus_Iterable_nullEntry() {
1303
        Iterable<Money> iterable = Arrays.asList(GBP_2_33, null);
1304
        assertThatExceptionOfType(NullPointerException.class)
1305
            .isThrownBy(() -> GBP_M5_78.plus(iterable));
1306
    }
1307
1308
    @Test
1309
    void test_plus_Iterable_nullIterable() {
1310
        assertThatExceptionOfType(NullPointerException.class)
1311
            .isThrownBy(() -> GBP_M5_78.plus((Iterable<Money>) null));
1312
    }
1313
1314
    //-----------------------------------------------------------------------
1315
    // plus(Money)
1316
    //-----------------------------------------------------------------------
1317
    @Test
1318
    void test_plus_Money_zero() {
1319
        var test = GBP_2_34.plus(GBP_0_00);
1320
        assertThat(test).isSameAs(GBP_2_34);
1321
    }
1322
1323
    @Test
1324
    void test_plus_Money_positive() {
1325
        var test = GBP_2_34.plus(GBP_1_23);
1326
        assertThat(test).hasToString("GBP 3.57");
1327
    }
1328
1329
    @Test
1330
    void test_plus_Money_negative() {
1331
        var test = GBP_2_34.plus(GBP_M1_23);
1332
        assertThat(test).hasToString("GBP 1.11");
1333
    }
1334
1335
    @Test
1336
    void test_plus_Money_currencyMismatch() {
1337
        assertThatExceptionOfType(CurrencyMismatchException.class)
1338
            .isThrownBy(() -> {
1339
                try {
1340
                    GBP_M5_78.plus(USD_1_23);
1341
                } catch (CurrencyMismatchException ex) {
1342
                    assertEquals(GBP, ex.getFirstCurrency());
1343
                    assertEquals(USD, ex.getSecondCurrency());
1344
                    throw ex;
1345
                }
1346
            });
1347
    }
1348
1349
    @Test
1350
    void test_plus_Money_nullMoney() {
1351
        assertThatExceptionOfType(NullPointerException.class)
1352
            .isThrownBy(() -> GBP_M5_78.plus((Money) null));
1353
    }
1354
1355
    //-----------------------------------------------------------------------
1356
    // plus(BigDecimal)
1357
    //-----------------------------------------------------------------------
1358
    @Test
1359
    void test_plus_BigDecimal_zero() {
1360
        var test = GBP_2_34.plus(BigDecimal.ZERO);
1361
        assertThat(test).isSameAs(GBP_2_34);
1362
    }
1363
1364
    @Test
1365
    void test_plus_BigDecimal_positive() {
1366
        var test = GBP_2_34.plus(new BigDecimal("1.23"));
1367
        assertThat(test).hasToString("GBP 3.57");
1368
    }
1369
1370
    @Test
1371
    void test_plus_BigDecimal_negative() {
1372
        var test = GBP_2_34.plus(new BigDecimal("-1.23"));
1373
        assertThat(test).hasToString("GBP 1.11");
1374
    }
1375
1376
    @Test
1377
    void test_plus_BigDecimal_invalidScale() {
1378
        assertThatExceptionOfType(ArithmeticException.class)
1379
            .isThrownBy(() -> GBP_2_34.plus(new BigDecimal("1.235")));
1380
    }
1381
1382
    @Test
1383
    void test_plus_BigDecimal_nullBigDecimal() {
1384
        assertThatExceptionOfType(NullPointerException.class)
1385
            .isThrownBy(() -> GBP_M5_78.plus((BigDecimal) null));
1386
    }
1387
1388
    //-----------------------------------------------------------------------
1389
    // plus(BigDecimal,RoundingMode)
1390
    //-----------------------------------------------------------------------
1391
    @Test
1392
    void test_plus_BigDecimalRoundingMode_zero() {
1393
        var test = GBP_2_34.plus(BigDecimal.ZERO, RoundingMode.UNNECESSARY);
1394
        assertThat(test).isSameAs(GBP_2_34);
1395
    }
1396
1397
    @Test
1398
    void test_plus_BigDecimalRoundingMode_positive() {
1399
        var test = GBP_2_34.plus(new BigDecimal("1.23"), RoundingMode.UNNECESSARY);
1400
        assertThat(test).hasToString("GBP 3.57");
1401
    }
1402
1403
    @Test
1404
    void test_plus_BigDecimalRoundingMode_negative() {
1405
        var test = GBP_2_34.plus(new BigDecimal("-1.23"), RoundingMode.UNNECESSARY);
1406
        assertThat(test).hasToString("GBP 1.11");
1407
    }
1408
1409
    @Test
1410
    void test_plus_BigDecimalRoundingMode_roundDown() {
1411
        var test = GBP_2_34.plus(new BigDecimal("1.235"), RoundingMode.DOWN);
1412
        assertThat(test).hasToString("GBP 3.57");
1413
    }
1414
1415
    @Test
1416
    void test_plus_BigDecimalRoundingMode_roundUnecessary() {
1417
        assertThatExceptionOfType(ArithmeticException.class)
1418
            .isThrownBy(() -> GBP_2_34.plus(new BigDecimal("1.235"), RoundingMode.UNNECESSARY));
1419
    }
1420
1421
    @Test
1422
    void test_plus_BigDecimalRoundingMode_nullBigDecimal() {
1423
        assertThatExceptionOfType(NullPointerException.class)
1424
            .isThrownBy(() -> GBP_M5_78.plus((BigDecimal) null, RoundingMode.UNNECESSARY));
1425
    }
1426
1427
    @Test
1428
    void test_plus_BigDecimalRoundingMode_nullRoundingMode() {
1429
        assertThatExceptionOfType(NullPointerException.class)
1430
            .isThrownBy(() -> GBP_M5_78.plus(BIGDEC_2_34, (RoundingMode) null));
1431
    }
1432
1433
    //-----------------------------------------------------------------------
1434
    // plus(double)
1435
    //-----------------------------------------------------------------------
1436
    @Test
1437
    void test_plus_double_zero() {
1438
        var test = GBP_2_34.plus(0d);
1439
        assertThat(test).isSameAs(GBP_2_34);
1440
    }
1441
1442
    @Test
1443
    void test_plus_double_positive() {
1444
        var test = GBP_2_34.plus(1.23d);
1445
        assertThat(test).hasToString("GBP 3.57");
1446
    }
1447
1448
    @Test
1449
    void test_plus_double_negative() {
1450
        var test = GBP_2_34.plus(-1.23d);
1451
        assertThat(test).hasToString("GBP 1.11");
1452
    }
1453
1454
    @Test
1455
    void test_plus_double_invalidScale() {
1456
        assertThatExceptionOfType(ArithmeticException.class)
1457
            .isThrownBy(() -> GBP_2_34.plus(1.235d));
1458
    }
1459
1460
    //-----------------------------------------------------------------------
1461
    // plus(double,RoundingMode)
1462
    //-----------------------------------------------------------------------
1463
    @Test
1464
    void test_plus_doubleRoundingMode_zero() {
1465
        var test = GBP_2_34.plus(0d, RoundingMode.UNNECESSARY);
1466
        assertThat(test).isSameAs(GBP_2_34);
1467
    }
1468
1469
    @Test
1470
    void test_plus_doubleRoundingMode_positive() {
1471
        var test = GBP_2_34.plus(1.23d, RoundingMode.UNNECESSARY);
1472
        assertThat(test).hasToString("GBP 3.57");
1473
    }
1474
1475
    @Test
1476
    void test_plus_doubleRoundingMode_negative() {
1477
        var test = GBP_2_34.plus(-1.23d, RoundingMode.UNNECESSARY);
1478
        assertThat(test).hasToString("GBP 1.11");
1479
    }
1480
1481
    @Test
1482
    void test_plus_doubleRoundingMode_roundDown() {
1483
        var test = GBP_2_34.plus(1.235d, RoundingMode.DOWN);
1484
        assertThat(test).hasToString("GBP 3.57");
1485
    }
1486
1487
    @Test
1488
    void test_plus_doubleRoundingMode_roundUnecessary() {
1489
        assertThatExceptionOfType(ArithmeticException.class)
1490
            .isThrownBy(() -> GBP_2_34.plus(1.235d, RoundingMode.UNNECESSARY));
1491
    }
1492
1493
    @Test
1494
    void test_plus_doubleRoundingMode_nullRoundingMode() {
1495
        assertThatExceptionOfType(NullPointerException.class)
1496
            .isThrownBy(() -> GBP_M5_78.plus(2.34d, (RoundingMode) null));
1497
    }
1498
1499
    //-----------------------------------------------------------------------
1500
    // plusMajor(long)
1501
    //-----------------------------------------------------------------------
1502
    @Test
1503
    void test_plusMajor_zero() {
1504
        var test = GBP_2_34.plusMajor(0);
1505
        assertThat(test).isSameAs(GBP_2_34);
1506
    }
1507
1508
    @Test
1509
    void test_plusMajor_positive() {
1510
        var test = GBP_2_34.plusMajor(123);
1511
        assertThat(test).hasToString("GBP 125.34");
1512
    }
1513
1514
    @Test
1515
    void test_plusMajor_negative() {
1516
        var test = GBP_2_34.plusMajor(-123);
1517
        assertThat(test).hasToString("GBP -120.66");
1518
    }
1519
1520
    //-----------------------------------------------------------------------
1521
    // plusMinor(long)
1522
    //-----------------------------------------------------------------------
1523
    @Test
1524
    void test_plusMinor_zero() {
1525
        var test = GBP_2_34.plusMinor(0);
1526
        assertThat(test).isSameAs(GBP_2_34);
1527
    }
1528
1529
    @Test
1530
    void test_plusMinor_positive() {
1531
        var test = GBP_2_34.plusMinor(123);
1532
        assertThat(test).hasToString("GBP 3.57");
1533
    }
1534
1535
    @Test
1536
    void test_plusMinor_negative() {
1537
        var test = GBP_2_34.plusMinor(-123);
1538
        assertThat(test).hasToString("GBP 1.11");
1539
    }
1540
1541
    //-----------------------------------------------------------------------
1542
    // minus(Iterable)
1543
    //-----------------------------------------------------------------------
1544
    @Test
1545
    void test_minus_Iterable() {
1546
        Iterable<Money> iterable = Arrays.asList(GBP_2_33, GBP_1_23);
1547
        var test = GBP_2_34.minus(iterable);
1548
        assertThat(test).hasToString("GBP -1.22");
1549
    }
1550
1551
    @Test
1552
    void test_minus_Iterable_zero() {
1553
        Iterable<Money> iterable = Arrays.asList(GBP_0_00);
1554
        var test = GBP_2_34.minus(iterable);
1555
        assertThat(test).isSameAs(GBP_2_34);
1556
    }
1557
1558
    @Test
1559
    void test_minus_Iterable_currencyMismatch() {
1560
        assertThatExceptionOfType(CurrencyMismatchException.class)
1561
            .isThrownBy(() -> {
1562
                try {
1563
                    Iterable<Money> iterable = Arrays.asList(GBP_2_33, JPY_423);
1564
                    GBP_M5_78.minus(iterable);
1565
                } catch (CurrencyMismatchException ex) {
1566
                    assertEquals(GBP, ex.getFirstCurrency());
1567
                    assertEquals(JPY, ex.getSecondCurrency());
1568
                    throw ex;
1569
                }
1570
            });
1571
    }
1572
1573
    @Test
1574
    void test_minus_Iterable_nullEntry() {
1575
        Iterable<Money> iterable = Arrays.asList(GBP_2_33, null);
1576
        assertThatExceptionOfType(NullPointerException.class)
1577
            .isThrownBy(() -> GBP_M5_78.minus(iterable));
1578
    }
1579
1580
    @Test
1581
    void test_minus_Iterable_nullIterable() {
1582
        assertThatExceptionOfType(NullPointerException.class)
1583
            .isThrownBy(() -> GBP_M5_78.minus((Iterable<Money>) null));
1584
    }
1585
1586
    //-----------------------------------------------------------------------
1587
    // minus(Money)
1588
    //-----------------------------------------------------------------------
1589
    @Test
1590
    void test_minus_Money_zero() {
1591
        var test = GBP_2_34.minus(GBP_0_00);
1592
        assertThat(test).isSameAs(GBP_2_34);
1593
    }
1594
1595
    @Test
1596
    void test_minus_Money_positive() {
1597
        var test = GBP_2_34.minus(GBP_1_23);
1598
        assertThat(test).hasToString("GBP 1.11");
1599
    }
1600
1601
    @Test
1602
    void test_minus_Money_negative() {
1603
        var test = GBP_2_34.minus(GBP_M1_23);
1604
        assertThat(test).hasToString("GBP 3.57");
1605
    }
1606
1607
    @Test
1608
    void test_minus_Money_currencyMismatch() {
1609
        assertThatExceptionOfType(CurrencyMismatchException.class)
1610
            .isThrownBy(() -> {
1611
                try {
1612
                    GBP_M5_78.minus(USD_1_23);
1613
                } catch (CurrencyMismatchException ex) {
1614
                    assertEquals(GBP, ex.getFirstCurrency());
1615
                    assertEquals(USD, ex.getSecondCurrency());
1616
                    throw ex;
1617
                }
1618
            });
1619
    }
1620
1621
    @Test
1622
    void test_minus_Money_nullMoney() {
1623
        assertThatExceptionOfType(NullPointerException.class)
1624
            .isThrownBy(() -> GBP_M5_78.minus((Money) null));
1625
    }
1626
1627
    //-----------------------------------------------------------------------
1628
    // minus(BigDecimal)
1629
    //-----------------------------------------------------------------------
1630
    @Test
1631
    void test_minus_BigDecimal_zero() {
1632
        var test = GBP_2_34.minus(BigDecimal.ZERO);
1633
        assertThat(test).isSameAs(GBP_2_34);
1634
    }
1635
1636
    @Test
1637
    void test_minus_BigDecimal_positive() {
1638
        var test = GBP_2_34.minus(new BigDecimal("1.23"));
1639
        assertThat(test).hasToString("GBP 1.11");
1640
    }
1641
1642
    @Test
1643
    void test_minus_BigDecimal_negative() {
1644
        var test = GBP_2_34.minus(new BigDecimal("-1.23"));
1645
        assertThat(test).hasToString("GBP 3.57");
1646
    }
1647
1648
    @Test
1649
    void test_minus_BigDecimal_invalidScale() {
1650
        assertThatExceptionOfType(ArithmeticException.class)
1651
            .isThrownBy(() -> GBP_2_34.minus(new BigDecimal("1.235")));
1652
    }
1653
1654
    @Test
1655
    void test_minus_BigDecimal_nullBigDecimal() {
1656
        assertThatExceptionOfType(NullPointerException.class)
1657
            .isThrownBy(() -> GBP_M5_78.minus((BigDecimal) null));
1658
    }
1659
1660
    //-----------------------------------------------------------------------
1661
    // minus(BigDecimal,RoundingMode)
1662
    //-----------------------------------------------------------------------
1663
    @Test
1664
    void test_minus_BigDecimalRoundingMode_zero() {
1665
        var test = GBP_2_34.minus(BigDecimal.ZERO, RoundingMode.UNNECESSARY);
1666
        assertThat(test).isSameAs(GBP_2_34);
1667
    }
1668
1669
    @Test
1670
    void test_minus_BigDecimalRoundingMode_positive() {
1671
        var test = GBP_2_34.minus(new BigDecimal("1.23"), RoundingMode.UNNECESSARY);
1672
        assertThat(test).hasToString("GBP 1.11");
1673
    }
1674
1675
    @Test
1676
    void test_minus_BigDecimalRoundingMode_negative() {
1677
        var test = GBP_2_34.minus(new BigDecimal("-1.23"), RoundingMode.UNNECESSARY);
1678
        assertThat(test).hasToString("GBP 3.57");
1679
    }
1680
1681
    @Test
1682
    void test_minus_BigDecimalRoundingMode_roundDown() {
1683
        var test = GBP_2_34.minus(new BigDecimal("1.235"), RoundingMode.DOWN);
1684
        assertThat(test).hasToString("GBP 1.10");
1685
    }
1686
1687
    @Test
1688
    void test_minus_BigDecimalRoundingMode_roundUnecessary() {
1689
        assertThatExceptionOfType(ArithmeticException.class)
1690
            .isThrownBy(() -> GBP_2_34.minus(new BigDecimal("1.235"), RoundingMode.UNNECESSARY));
1691
    }
1692
1693
    @Test
1694
    void test_minus_BigDecimalRoundingMode_nullBigDecimal() {
1695
        assertThatExceptionOfType(NullPointerException.class)
1696
            .isThrownBy(() -> GBP_M5_78.minus((BigDecimal) null, RoundingMode.UNNECESSARY));
1697
    }
1698
1699
    @Test
1700
    void test_minus_BigDecimalRoundingMode_nullRoundingMode() {
1701
        assertThatExceptionOfType(NullPointerException.class)
1702
            .isThrownBy(() -> GBP_M5_78.minus(BIGDEC_2_34, (RoundingMode) null));
1703
    }
1704
1705
    //-----------------------------------------------------------------------
1706
    // minus(double)
1707
    //-----------------------------------------------------------------------
1708
    @Test
1709
    void test_minus_double_zero() {
1710
        var test = GBP_2_34.minus(0d);
1711
        assertThat(test).isSameAs(GBP_2_34);
1712
    }
1713
1714
    @Test
1715
    void test_minus_double_positive() {
1716
        var test = GBP_2_34.minus(1.23d);
1717
        assertThat(test).hasToString("GBP 1.11");
1718
    }
1719
1720
    @Test
1721
    void test_minus_double_negative() {
1722
        var test = GBP_2_34.minus(-1.23d);
1723
        assertThat(test).hasToString("GBP 3.57");
1724
    }
1725
1726
    @Test
1727
    void test_minus_double_invalidScale() {
1728
        assertThatExceptionOfType(ArithmeticException.class)
1729
            .isThrownBy(() -> GBP_2_34.minus(1.235d));
1730
    }
1731
1732
    //-----------------------------------------------------------------------
1733
    // minus(double,RoundingMode)
1734
    //-----------------------------------------------------------------------
1735
    @Test
1736
    void test_minus_doubleRoundingMode_zero() {
1737
        var test = GBP_2_34.minus(0d, RoundingMode.UNNECESSARY);
1738
        assertThat(test).isSameAs(GBP_2_34);
1739
    }
1740
1741
    @Test
1742
    void test_minus_doubleRoundingMode_positive() {
1743
        var test = GBP_2_34.minus(1.23d, RoundingMode.UNNECESSARY);
1744
        assertThat(test).hasToString("GBP 1.11");
1745
    }
1746
1747
    @Test
1748
    void test_minus_doubleRoundingMode_negative() {
1749
        var test = GBP_2_34.minus(-1.23d, RoundingMode.UNNECESSARY);
1750
        assertThat(test).hasToString("GBP 3.57");
1751
    }
1752
1753
    @Test
1754
    void test_minus_doubleRoundingMode_roundDown() {
1755
        var test = GBP_2_34.minus(1.235d, RoundingMode.DOWN);
1756
        assertThat(test).hasToString("GBP 1.10");
1757
    }
1758
1759
    @Test
1760
    void test_minus_doubleRoundingMode_roundUnecessary() {
1761
        assertThatExceptionOfType(ArithmeticException.class)
1762
            .isThrownBy(() -> GBP_2_34.minus(1.235d, RoundingMode.UNNECESSARY));
1763
    }
1764
1765
    @Test
1766
    void test_minus_doubleRoundingMode_nullRoundingMode() {
1767
        assertThatExceptionOfType(NullPointerException.class)
1768
            .isThrownBy(() -> GBP_M5_78.minus(2.34d, (RoundingMode) null));
1769
    }
1770
1771
    //-----------------------------------------------------------------------
1772
    // minusMajor(long)
1773
    //-----------------------------------------------------------------------
1774
    @Test
1775
    void test_minusMajor_zero() {
1776
        var test = GBP_2_34.minusMajor(0);
1777
        assertThat(test).isSameAs(GBP_2_34);
1778
    }
1779
1780
    @Test
1781
    void test_minusMajor_positive() {
1782
        var test = GBP_2_34.minusMajor(123);
1783
        assertThat(test).hasToString("GBP -120.66");
1784
    }
1785
1786
    @Test
1787
    void test_minusMajor_negative() {
1788
        var test = GBP_2_34.minusMajor(-123);
1789
        assertThat(test).hasToString("GBP 125.34");
1790
    }
1791
1792
    //-----------------------------------------------------------------------
1793
    // minusMinor(long)
1794
    //-----------------------------------------------------------------------
1795
    @Test
1796
    void test_minusMinor_zero() {
1797
        var test = GBP_2_34.minusMinor(0);
1798
        assertThat(test).isSameAs(GBP_2_34);
1799
    }
1800
1801
    @Test
1802
    void test_minusMinor_positive() {
1803
        var test = GBP_2_34.minusMinor(123);
1804
        assertThat(test).hasToString("GBP 1.11");
1805
    }
1806
1807
    @Test
1808
    void test_minusMinor_negative() {
1809
        var test = GBP_2_34.minusMinor(-123);
1810
        assertThat(test).hasToString("GBP 3.57");
1811
    }
1812
1813
    //-----------------------------------------------------------------------
1814
    // multipliedBy(BigDecimal,RoundingMode)
1815
    //-----------------------------------------------------------------------
1816
    @Test
1817
    void test_multipliedBy_BigDecimalRoundingMode_one() {
1818
        var test = GBP_2_34.multipliedBy(BigDecimal.ONE, RoundingMode.DOWN);
1819
        assertThat(test).isSameAs(GBP_2_34);
1820
    }
1821
1822
    @Test
1823
    void test_multipliedBy_BigDecimalRoundingMode_positive() {
1824
        var test = GBP_2_33.multipliedBy(new BigDecimal("2.5"), RoundingMode.DOWN);
1825
        assertThat(test).hasToString("GBP 5.82");
1826
    }
1827
1828
    @Test
1829
    void test_multipliedBy_BigDecimalRoundingMode_positive_halfUp() {
1830
        var test = GBP_2_33.multipliedBy(new BigDecimal("2.5"), RoundingMode.HALF_UP);
1831
        assertThat(test).hasToString("GBP 5.83");
1832
    }
1833
1834
    @Test
1835
    void test_multipliedBy_BigDecimalRoundingMode_negative() {
1836
        var test = GBP_2_33.multipliedBy(new BigDecimal("-2.5"), RoundingMode.FLOOR);
1837
        assertThat(test).hasToString("GBP -5.83");
1838
    }
1839
1840
    @Test
1841
    void test_multipliedBy_BigDecimalRoundingMode_nullBigDecimal() {
1842
        assertThatExceptionOfType(NullPointerException.class)
1843
            .isThrownBy(() -> GBP_5_78.multipliedBy((BigDecimal) null, RoundingMode.DOWN));
1844
    }
1845
1846
    @Test
1847
    void test_multipliedBy_BigDecimalRoundingMode_nullRoundingMode() {
1848
        assertThatExceptionOfType(NullPointerException.class)
1849
            .isThrownBy(() -> GBP_5_78.multipliedBy(new BigDecimal("2.5"), (RoundingMode) null));
1850
    }
1851
1852
    //-----------------------------------------------------------------------
1853
    // multipliedBy(double,RoundingMode)
1854
    //-----------------------------------------------------------------------
1855
    @Test
1856
    void test_multipliedBy_doubleRoundingMode_one() {
1857
        var test = GBP_2_34.multipliedBy(1d, RoundingMode.DOWN);
1858
        assertThat(test).isSameAs(GBP_2_34);
1859
    }
1860
1861
    @Test
1862
    void test_multipliedBy_doubleRoundingMode_positive() {
1863
        var test = GBP_2_33.multipliedBy(2.5d, RoundingMode.DOWN);
1864
        assertThat(test).hasToString("GBP 5.82");
1865
    }
1866
1867
    @Test
1868
    void test_multipliedBy_doubleRoundingMode_positive_halfUp() {
1869
        var test = GBP_2_33.multipliedBy(2.5d, RoundingMode.HALF_UP);
1870
        assertThat(test).hasToString("GBP 5.83");
1871
    }
1872
1873
    @Test
1874
    void test_multipliedBy_doubleRoundingMode_negative() {
1875
        var test = GBP_2_33.multipliedBy(-2.5d, RoundingMode.FLOOR);
1876
        assertThat(test).hasToString("GBP -5.83");
1877
    }
1878
1879
    @Test
1880
    void test_multipliedBy_doubleRoundingMode_nullRoundingMode() {
1881
        assertThatExceptionOfType(NullPointerException.class)
1882
            .isThrownBy(() -> GBP_5_78.multipliedBy(2.5d, (RoundingMode) null));
1883
    }
1884
1885
    //-----------------------------------------------------------------------
1886
    // multipliedBy(long)
1887
    //-----------------------------------------------------------------------
1888
    @Test
1889
    void test_multipliedBy_long_one() {
1890
        var test = GBP_2_34.multipliedBy(1);
1891
        assertThat(test).isSameAs(GBP_2_34);
1892
    }
1893
1894
    @Test
1895
    void test_multipliedBy_long_positive() {
1896
        var test = GBP_2_34.multipliedBy(3);
1897
        assertThat(test).hasToString("GBP 7.02");
1898
    }
1899
1900
    @Test
1901
    void test_multipliedBy_long_negative() {
1902
        var test = GBP_2_34.multipliedBy(-3);
1903
        assertThat(test).hasToString("GBP -7.02");
1904
    }
1905
1906
    //-----------------------------------------------------------------------
1907
    // dividedBy(BigDecimal,RoundingMode)
1908
    //-----------------------------------------------------------------------
1909
    @Test
1910
    void test_dividedBy_BigDecimalRoundingMode_one() {
1911
        var test = GBP_2_34.dividedBy(BigDecimal.ONE, RoundingMode.DOWN);
1912
        assertThat(test).isSameAs(GBP_2_34);
1913
    }
1914
1915
    @Test
1916
    void test_dividedBy_BigDecimalRoundingMode_positive() {
1917
        var test = GBP_2_34.dividedBy(new BigDecimal("2.5"), RoundingMode.DOWN);
1918
        assertThat(test).hasToString("GBP 0.93");
1919
    }
1920
1921
    @Test
1922
    void test_dividedBy_BigDecimalRoundingMode_positive_halfUp() {
1923
        var test = GBP_2_34.dividedBy(new BigDecimal("2.5"), RoundingMode.HALF_UP);
1924
        assertThat(test).hasToString("GBP 0.94");
1925
    }
1926
1927
    @Test
1928
    void test_dividedBy_BigDecimalRoundingMode_negative() {
1929
        var test = GBP_2_34.dividedBy(new BigDecimal("-2.5"), RoundingMode.FLOOR);
1930
        assertThat(test).hasToString("GBP -0.94");
1931
    }
1932
1933
    @Test
1934
    void test_dividedBy_BigDecimalRoundingMode_nullBigDecimal() {
1935
        assertThatExceptionOfType(NullPointerException.class)
1936
            .isThrownBy(() -> GBP_5_78.dividedBy((BigDecimal) null, RoundingMode.DOWN));
1937
    }
1938
1939
    @Test
1940
    void test_dividedBy_BigDecimalRoundingMode_nullRoundingMode() {
1941
        assertThatExceptionOfType(NullPointerException.class)
1942
            .isThrownBy(() -> GBP_5_78.dividedBy(new BigDecimal("2.5"), (RoundingMode) null));
1943
    }
1944
1945
    //-----------------------------------------------------------------------
1946
    // dividedBy(double,RoundingMode)
1947
    //-----------------------------------------------------------------------
1948
    @Test
1949
    void test_dividedBy_doubleRoundingMode_one() {
1950
        var test = GBP_2_34.dividedBy(1d, RoundingMode.DOWN);
1951
        assertThat(test).isSameAs(GBP_2_34);
1952
    }
1953
1954
    @Test
1955
    void test_dividedBy_doubleRoundingMode_positive() {
1956
        var test = GBP_2_34.dividedBy(2.5d, RoundingMode.DOWN);
1957
        assertThat(test).hasToString("GBP 0.93");
1958
    }
1959
1960
    @Test
1961
    void test_dividedBy_doubleRoundingMode_positive_halfUp() {
1962
        var test = GBP_2_34.dividedBy(2.5d, RoundingMode.HALF_UP);
1963
        assertThat(test).hasToString("GBP 0.94");
1964
    }
1965
1966
    @Test
1967
    void test_dividedBy_doubleRoundingMode_negative() {
1968
        var test = GBP_2_34.dividedBy(-2.5d, RoundingMode.FLOOR);
1969
        assertThat(test).hasToString("GBP -0.94");
1970
    }
1971
1972
    @Test
1973
    void test_dividedBy_doubleRoundingMode_nullRoundingMode() {
1974
        assertThatExceptionOfType(NullPointerException.class)
1975
            .isThrownBy(() -> GBP_5_78.dividedBy(2.5d, (RoundingMode) null));
1976
    }
1977
1978
    //-----------------------------------------------------------------------
1979
    // dividedBy(long,RoundingMode)
1980
    //-----------------------------------------------------------------------
1981
    @Test
1982
    void test_dividedBy_long_one() {
1983
        var test = GBP_2_34.dividedBy(1, RoundingMode.DOWN);
1984
        assertThat(test).isSameAs(GBP_2_34);
1985
    }
1986
1987
    @Test
1988
    void test_dividedBy_long_positive() {
1989
        var test = GBP_2_34.dividedBy(3, RoundingMode.DOWN);
1990
        assertThat(test).hasToString("GBP 0.78");
1991
    }
1992
1993
    @Test
1994
    void test_dividedBy_long_positive_roundDown() {
1995
        var test = GBP_2_35.dividedBy(3, RoundingMode.DOWN);
1996
        assertThat(test).hasToString("GBP 0.78");
1997
    }
1998
1999
    @Test
2000
    void test_dividedBy_long_positive_roundUp() {
2001
        var test = GBP_2_35.dividedBy(3, RoundingMode.UP);
2002
        assertThat(test).hasToString("GBP 0.79");
2003
    }
2004
2005
    @Test
2006
    void test_dividedBy_long_negative() {
2007
        var test = GBP_2_34.dividedBy(-3, RoundingMode.DOWN);
2008
        assertThat(test).hasToString("GBP -0.78");
2009
    }
2010
2011
    //-----------------------------------------------------------------------
2012
    // negated()
2013
    //-----------------------------------------------------------------------
2014
    @Test
2015
    void test_negated_positive() {
2016
        var test = GBP_2_34.negated();
2017
        assertThat(test).hasToString("GBP -2.34");
2018
    }
2019
2020
    @Test
2021
    void test_negated_negative() {
2022
        var test = Money.parse("GBP -2.34").negated();
2023
        assertThat(test).hasToString("GBP 2.34");
2024
    }
2025
2026
    //-----------------------------------------------------------------------
2027
    // abs()
2028
    //-----------------------------------------------------------------------
2029
    @Test
2030
    void test_abs_positive() {
2031
        var test = GBP_2_34.abs();
2032
        assertThat(test).isSameAs(GBP_2_34);
2033
    }
2034
2035
    @Test
2036
    void test_abs_negative() {
2037
        var test = Money.parse("GBP -2.34").abs();
2038
        assertThat(test).hasToString("GBP 2.34");
2039
    }
2040
2041
    //-----------------------------------------------------------------------
2042
    // rounded()
2043
    //-----------------------------------------------------------------------
2044
    @Test
2045
    void test_round_2down() {
2046
        var test = GBP_2_34.rounded(2, RoundingMode.DOWN);
2047
        assertThat(test).isSameAs(GBP_2_34);
2048
    }
2049
2050
    @Test
2051
    void test_round_2up() {
2052
        var test = GBP_2_34.rounded(2, RoundingMode.DOWN);
2053
        assertThat(test).isSameAs(GBP_2_34);
2054
    }
2055
2056
    @Test
2057
    void test_round_1down() {
2058
        var test = GBP_2_34.rounded(1, RoundingMode.DOWN);
2059
        assertThat(test).hasToString("GBP 2.30");
2060
    }
2061
2062
    @Test
2063
    void test_round_1up() {
2064
        var test = GBP_2_34.rounded(1, RoundingMode.UP);
2065
        assertThat(test).hasToString("GBP 2.40");
2066
    }
2067
2068
    @Test
2069
    void test_round_0down() {
2070
        var test = GBP_2_34.rounded(0, RoundingMode.DOWN);
2071
        assertThat(test).hasToString("GBP 2.00");
2072
    }
2073
2074
    @Test
2075
    void test_round_0up() {
2076
        var test = GBP_2_34.rounded(0, RoundingMode.UP);
2077
        assertThat(test).hasToString("GBP 3.00");
2078
    }
2079
2080
    @Test
2081
    void test_round_M1down() {
2082
        var test = Money.parse("GBP 432.34").rounded(-1, RoundingMode.DOWN);
2083
        assertThat(test).hasToString("GBP 430.00");
2084
    }
2085
2086
    @Test
2087
    void test_round_M1up() {
2088
        var test = Money.parse("GBP 432.34").rounded(-1, RoundingMode.UP);
2089
        assertThat(test).hasToString("GBP 440.00");
2090
    }
2091
2092
    @Test
2093
    void test_round_3() {
2094
        var test = GBP_2_34.rounded(3, RoundingMode.DOWN);
2095
        assertThat(test).isSameAs(GBP_2_34);
2096
    }
2097
2098
    //-----------------------------------------------------------------------
2099
    // convertedTo(BigDecimal,RoundingMode)
2100
    //-----------------------------------------------------------------------
2101
    @Test
2102
    void test_convertedTo_BigDecimalRoundingMode_positive() {
2103
        var test = GBP_2_33.convertedTo(EUR, new BigDecimal("2.5"), RoundingMode.DOWN);
2104
        assertThat(test).hasToString("EUR 5.82");
2105
    }
2106
2107
    @Test
2108
    void test_convertedTo_BigDecimalRoundingMode_positive_halfUp() {
2109
        var test = GBP_2_33.convertedTo(EUR, new BigDecimal("2.5"), RoundingMode.HALF_UP);
2110
        assertThat(test).hasToString("EUR 5.83");
2111
    }
2112
2113
    @Test
2114
    void test_convertedTo_BigDecimalRoundingMode_negative() {
2115
        assertThatExceptionOfType(IllegalArgumentException.class)
2116
            .isThrownBy(() -> GBP_2_33.convertedTo(EUR, new BigDecimal("-2.5"), RoundingMode.FLOOR));
2117
    }
2118
2119
    @Test
2120
    void test_convertedTo_BigDecimalRoundingMode_sameCurrency() {
2121
        assertThatExceptionOfType(IllegalArgumentException.class)
2122
            .isThrownBy(() -> GBP_2_33.convertedTo(GBP, new BigDecimal("2.5"), RoundingMode.DOWN));
2123
    }
2124
2125
    @Test
2126
    void test_convertedTo_BigDecimalRoundingMode_nullCurrency() {
2127
        assertThatExceptionOfType(NullPointerException.class)
2128
            .isThrownBy(() -> GBP_5_78.convertedTo((CurrencyUnit) null, new BigDecimal("2"), RoundingMode.DOWN));
2129
    }
2130
2131
    @Test
2132
    void test_convertedTo_BigDecimalRoundingMode_nullBigDecimal() {
2133
        assertThatExceptionOfType(NullPointerException.class)
2134
            .isThrownBy(() -> GBP_5_78.convertedTo(EUR, (BigDecimal) null, RoundingMode.DOWN));
2135
    }
2136
2137
    @Test
2138
    void test_convertedTo_BigDecimalRoundingMode_nullRoundingMode() {
2139
        assertThatExceptionOfType(NullPointerException.class)
2140
            .isThrownBy(() -> GBP_5_78.convertedTo(EUR, new BigDecimal("2.5"), (RoundingMode) null));
2141
    }
2142
2143
    //-----------------------------------------------------------------------
2144
    // toMoney()
2145
    //-----------------------------------------------------------------------
2146
    @Test
2147
    void test_toBigMoney() {
2148
        assertThat(GBP_2_34.toBigMoney()).isEqualTo(BigMoney.ofMinor(GBP, 234));
2149
    }
2150
2151
    //-----------------------------------------------------------------------
2152
    // isSameCurrency(Money)
2153
    //-----------------------------------------------------------------------
2154
    @Test
2155
    void test_isSameCurrency_Money_same() {
2156
        assertThat(GBP_2_34.isSameCurrency(GBP_2_35)).isTrue();
2157
    }
2158
2159
    @Test
2160
    void test_isSameCurrency_Money_different() {
2161
        assertThat(GBP_2_34.isSameCurrency(USD_2_34)).isFalse();
2162
    }
2163
2164
    @Test
2165
    void test_isSameCurrency_BigMoney_same() {
2166
        assertThat(GBP_2_34.isSameCurrency(BigMoney.parse("GBP 2"))).isTrue();
2167
    }
2168
2169
    @Test
2170
    void test_isSameCurrency_BigMoney_different() {
2171
        assertThat(GBP_2_34.isSameCurrency(BigMoney.parse("USD 2"))).isFalse();
2172
    }
2173
2174
    @Test
2175
    void test_isSameCurrency_Money_nullMoney() {
2176
        assertThatExceptionOfType(NullPointerException.class)
2177
            .isThrownBy(() -> GBP_2_34.isSameCurrency((Money) null));
2178
    }
2179
2180
    //-----------------------------------------------------------------------
2181
    // compareTo()
2182
    //-----------------------------------------------------------------------
2183
    @Test
2184
    void test_compareTo_Money() {
2185
        var a = GBP_2_34;
2186
        var b = GBP_2_35;
2187
        var c = GBP_2_36;
2188
        assertThat(a.compareTo(a)).isEqualTo(0);
2189
        assertThat(b.compareTo(b)).isEqualTo(0);
2190
        assertThat(c.compareTo(c)).isEqualTo(0);
2191
2192
        assertThat(a.compareTo(b)).isEqualTo(-1);
2193
        assertThat(b.compareTo(a)).isEqualTo(1);
2194
2195
        assertThat(a.compareTo(c)).isEqualTo(-1);
2196
        assertThat(c.compareTo(a)).isEqualTo(1);
2197
2198
        assertThat(b.compareTo(c)).isEqualTo(-1);
2199
        assertThat(c.compareTo(b)).isEqualTo(1);
2200
    }
2201
2202
    @Test
2203
    void test_compareTo_BigMoney() {
2204
        var t = GBP_2_35;
2205
        var a = BigMoney.ofMinor(GBP, 234);
2206
        var b = BigMoney.ofMinor(GBP, 235);
2207
        var c = BigMoney.ofMinor(GBP, 236);
2208
        assertThat(t.compareTo(a)).isEqualTo(1);
2209
        assertThat(t.compareTo(b)).isEqualTo(0);
2210
        assertThat(t.compareTo(c)).isEqualTo(-1);
2211
    }
2212
2213
    @Test
2214
    void test_compareTo_currenciesDiffer() {
2215
        var a = GBP_2_34;
2216
        var b = USD_2_35;
2217
        assertThatExceptionOfType(CurrencyMismatchException.class)
2218
            .isThrownBy(() -> a.compareTo(b));
2219
    }
2220
2221
    @Test
2222
    @SuppressWarnings({"unchecked", "rawtypes"})
2223
    void test_compareTo_wrongType() {
2224
        Comparable a = GBP_2_34;
2225
        assertThatExceptionOfType(ClassCastException.class)
2226
            .isThrownBy(() -> a.compareTo("NotRightType"));
2227
    }
2228
2229
    //-----------------------------------------------------------------------
2230
    // isEqual()
2231
    //-----------------------------------------------------------------------
2232
    @Test
2233
    void test_isEqual() {
2234
        var a = GBP_2_34;
2235
        var b = GBP_2_35;
2236
        var c = GBP_2_36;
2237
        assertThat(a.isEqual(a)).isTrue();
2238
        assertThat(b.isEqual(b)).isTrue();
2239
        assertThat(c.isEqual(c)).isTrue();
2240
2241
        assertThat(a.isEqual(b)).isFalse();
2242
        assertThat(b.isEqual(a)).isFalse();
2243
2244
        assertThat(a.isEqual(c)).isFalse();
2245
        assertThat(c.isEqual(a)).isFalse();
2246
2247
        assertThat(b.isEqual(c)).isFalse();
2248
        assertThat(c.isEqual(b)).isFalse();
2249
    }
2250
2251
    @Test
2252
    void test_isEqual_Money() {
2253
        var a = GBP_2_34;
2254
        var b = BigMoney.ofMinor(GBP, 234);
2255
        assertThat(a.isEqual(b)).isTrue();
2256
    }
2257
2258
    @Test
2259
    void test_isEqual_currenciesDiffer() {
2260
        var a = GBP_2_34;
2261
        var b = USD_2_35;
2262
        assertThatExceptionOfType(CurrencyMismatchException.class)
2263
            .isThrownBy(() -> a.isEqual(b));
2264
    }
2265
2266
    //-----------------------------------------------------------------------
2267
    // isGreaterThan()
2268
    //-----------------------------------------------------------------------
2269
    @Test
2270
    void test_isGreaterThan() {
2271
        var a = GBP_2_34;
2272
        var b = GBP_2_35;
2273
        var c = GBP_2_36;
2274
        assertThat(a.isGreaterThan(a)).isFalse();
2275
        assertThat(a.isGreaterThan(b)).isFalse();
2276
        assertThat(a.isGreaterThan(c)).isFalse();
2277
2278
        assertThat(b.isGreaterThan(a)).isTrue();
2279
        assertThat(b.isGreaterThan(b)).isFalse();
2280
        assertThat(b.isGreaterThan(c)).isFalse();
2281
2282
        assertThat(c.isGreaterThan(a)).isTrue();
2283
        assertThat(c.isGreaterThan(b)).isTrue();
2284
        assertThat(c.isGreaterThan(c)).isFalse();
2285
    }
2286
2287
    @Test
2288
    void test_isGreaterThan_currenciesDiffer() {
2289
        var a = GBP_2_34;
2290
        var b = USD_2_35;
2291
        assertThatExceptionOfType(CurrencyMismatchException.class)
2292
            .isThrownBy(() -> a.isGreaterThan(b));
2293
    }
2294
2295
    //-----------------------------------------------------------------------
2296
    // isGreaterThanOrEqual()
2297
    //-----------------------------------------------------------------------
2298
    @Test
2299
    void test_isGreaterThanOrEqual() {
2300
        var a = GBP_2_34;
2301
        var b = GBP_2_35;
2302
        var c = GBP_2_36;
2303
        assertThat(a.isGreaterThanOrEqual(a)).isTrue();
2304
        assertThat(a.isGreaterThanOrEqual(b)).isFalse();
2305
        assertThat(a.isGreaterThanOrEqual(c)).isFalse();
2306
2307
        assertThat(b.isGreaterThanOrEqual(a)).isTrue();
2308
        assertThat(b.isGreaterThanOrEqual(b)).isTrue();
2309
        assertThat(b.isGreaterThanOrEqual(c)).isFalse();
2310
2311
        assertThat(c.isGreaterThanOrEqual(a)).isTrue();
2312
        assertThat(c.isGreaterThanOrEqual(b)).isTrue();
2313
        assertThat(c.isGreaterThanOrEqual(c)).isTrue();
2314
    }
2315
2316
    @Test
2317
    void test_isGreaterThanOrEqual_currenciesDiffer() {
2318
        var a = GBP_2_34;
2319
        var b = USD_2_35;
2320
        assertThatExceptionOfType(CurrencyMismatchException.class)
2321
            .isThrownBy(() -> a.isGreaterThanOrEqual(b));
2322
    }
2323
2324
    //-----------------------------------------------------------------------
2325
    // isLessThan()
2326
    //-----------------------------------------------------------------------
2327
    @Test
2328
    void test_isLessThan() {
2329
        var a = GBP_2_34;
2330
        var b = GBP_2_35;
2331
        var c = GBP_2_36;
2332
        assertThat(a.isLessThan(a)).isFalse();
2333
        assertThat(a.isLessThan(b)).isTrue();
2334
        assertThat(a.isLessThan(c)).isTrue();
2335
2336
        assertThat(b.isLessThan(a)).isFalse();
2337
        assertThat(b.isLessThan(b)).isFalse();
2338
        assertThat(b.isLessThan(c)).isTrue();
2339
2340
        assertThat(c.isLessThan(a)).isFalse();
2341
        assertThat(c.isLessThan(b)).isFalse();
2342
        assertThat(c.isLessThan(c)).isFalse();
2343
    }
2344
2345
    @Test
2346
    void test_isLessThan_currenciesDiffer() {
2347
        var a = GBP_2_34;
2348
        var b = USD_2_35;
2349
        assertThatExceptionOfType(CurrencyMismatchException.class)
2350
            .isThrownBy(() -> a.isLessThan(b));
2351
    }
2352
2353
    //-----------------------------------------------------------------------
2354
    // isLessThanOrEqual()
2355
    //-----------------------------------------------------------------------
2356
    @Test
2357
    void test_isLessThanOrEqual() {
2358
        var a = GBP_2_34;
2359
        var b = GBP_2_35;
2360
        var c = GBP_2_36;
2361
        assertThat(a.isLessThanOrEqual(a)).isTrue();
2362
        assertThat(a.isLessThanOrEqual(b)).isTrue();
2363
        assertThat(a.isLessThanOrEqual(c)).isTrue();
2364
2365
        assertThat(b.isLessThanOrEqual(a)).isFalse();
2366
        assertThat(b.isLessThanOrEqual(b)).isTrue();
2367
        assertThat(b.isLessThanOrEqual(c)).isTrue();
2368
2369
        assertThat(c.isLessThanOrEqual(a)).isFalse();
2370
        assertThat(c.isLessThanOrEqual(b)).isFalse();
2371
        assertThat(c.isLessThanOrEqual(c)).isTrue();
2372
    }
2373
2374
    @Test
2375
    void test_isLessThanOrEqual_currenciesDiffer() {
2376
        var a = GBP_2_34;
2377
        var b = USD_2_35;
2378
        assertThatExceptionOfType(CurrencyMismatchException.class)
2379
            .isThrownBy(() -> a.isLessThanOrEqual(b));
2380
    }
2381
2382
    //-----------------------------------------------------------------------
2383
    // equals() hashCode()
2384
    //-----------------------------------------------------------------------
2385
    @Test
2386
    void test_equals_hashCode_positive() {
2387
        var a = GBP_2_34;
2388
        var b = GBP_2_34;
2389
        var c = GBP_2_35;
2390
        assertThat(a).isEqualTo(a);
2391
        assertThat(b).isEqualTo(b);
2392
        assertThat(c).isEqualTo(c);
2393
2394
        assertThat(b).isEqualTo(a);
2395
        assertThat(a).isEqualTo(b);
2396
        assertThat(b.hashCode()).isEqualTo(a.hashCode());
2397
2398
        assertThat(c).isNotEqualTo(a);
2399
        assertThat(c).isNotEqualTo(b);
2400
    }
2401
2402
    @Test
2403
    void test_equals_false() {
2404
        var a = GBP_2_34;
2405
        assertThat(a).isNotEqualTo(null);
2406
        assertThat(new Object()).isNotEqualTo(a);
2407
    }
2408
2409
    //-----------------------------------------------------------------------
2410
    // toString()
2411
    //-----------------------------------------------------------------------
2412
    @Test
2413
    void test_toString_positive() {
2414
        var test = Money.of(GBP, BIGDEC_2_34);
2415
        assertThat(test).hasToString("GBP 2.34");
2416
    }
2417
2418
    @Test
2419
    void test_toString_negative() {
2420
        var test = Money.of(EUR, BIGDEC_M5_78);
2421
        assertThat(test).hasToString("EUR -5.78");
2422
    }
2423
2424
}
2425