1+ package pl .mperor .lab .java .clean .code .ddd .value .object ;
2+
3+ import org .junit .jupiter .api .Assertions ;
4+ import org .junit .jupiter .api .Test ;
5+
6+ import java .math .BigDecimal ;
7+ import java .math .RoundingMode ;
8+ import java .time .LocalDateTime ;
9+
10+
11+ class ValueObjectTest {
12+
13+ @ Test
14+ void shouldAllowToUseMoneyAsValueObject () {
15+ var money = Money .of (BigDecimal .ZERO );
16+ Assertions .assertEquals (BigDecimal .ZERO .setScale (2 , RoundingMode .HALF_EVEN ), money .amount ());
17+
18+ Assertions .assertThrows (IllegalArgumentException .class , () -> new Money (null ));
19+ Assertions .assertThrows (IllegalArgumentException .class , () -> new Money (new BigDecimal ("-1" )));
20+
21+ var three = Money .of (BigDecimal .ONE ).add (Money .of (BigDecimal .TWO ));
22+ Assertions .assertEquals (new BigDecimal ("3.00" ), three .amount ());
23+ Assertions .assertEquals (new BigDecimal ("2.00" ), three .subtract (Money .of (BigDecimal .ONE )).amount ());
24+ }
25+
26+ @ Test
27+ void shouldAllowToUseEmailAddressAsValueObject () {
28+ var email = new EmailAddress ("john.doe@example.com" );
29+ Assertions .assertEquals ("john.doe@example.com" , email .email ());
30+ Assertions .assertThrows (IllegalArgumentException .class , () -> new EmailAddress (null ));
31+ Assertions .assertThrows (IllegalArgumentException .class , () -> new EmailAddress ("not an email @.wtf" ));
32+ }
33+
34+ @ Test
35+ void shouldAllowToUseDateTimeRangeAsValueObject () {
36+ Assertions .assertTrue (new DateTimeRange (LocalDateTime .MIN , LocalDateTime .MAX )
37+ .isWithinRange (LocalDateTime .now ()));
38+ Assertions .assertTrue (new DateTimeRange (LocalDateTime .MIN , LocalDateTime .MAX )
39+ .isWithinRange (LocalDateTime .MIN ));
40+ Assertions .assertTrue (new DateTimeRange (LocalDateTime .MIN , LocalDateTime .MAX )
41+ .isWithinRange (LocalDateTime .MAX ));
42+ }
43+ }
0 commit comments