Skip to content

Commit c090635

Browse files
committed
Add missing examples
1 parent 0f67664 commit c090635

7 files changed

Lines changed: 122 additions & 4 deletions

File tree

lombok/lombok.config

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import lombok_feature.config
2+
13
config.stopBubbling = true
2-
lombok.anyconstructor.addconstructorproperties=true
4+
lombok.anyconstructor.addconstructorproperties=false
35
lombok.addLombokGeneratedAnnotation = true
4-
lombok.experimental.flagUsage = WARNING
6+
lombok.addSuppressWarnings = false
7+
8+

lombok/lombok_feature.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lombok.experimental.flagUsage = warning

lombok/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
<artifactId>hibernate-jpa-2.1-api</artifactId>
2727
<version>${hibernate-jpa-2.1-api.version}</version>
2828
</dependency>
29+
<dependency>
30+
<groupId>org.jetbrains</groupId>
31+
<artifactId>annotations</artifactId>
32+
<version>23.0.0</version>
33+
</dependency>
2934
</dependencies>
3035

3136
<build>
@@ -70,7 +75,7 @@
7075
<!-- various -->
7176
<hibernate-jpa-2.1-api.version>1.0.0.Final</hibernate-jpa-2.1-api.version>
7277
<!-- delombok maven plugin -->
73-
<delombok-maven-plugin.version>1.18.10.0</delombok-maven-plugin.version>
78+
<delombok-maven-plugin.version>1.18.20.0</delombok-maven-plugin.version>
7479
</properties>
7580

7681
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.lombok.configexamples;
2+
3+
import lombok.*;
4+
import lombok.extern.java.Log;
5+
6+
import java.util.logging.Level;
7+
8+
import static java.lang.Math.abs;
9+
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
@Getter
13+
@Setter
14+
@Log
15+
public class Account {
16+
17+
@NonNull
18+
private Double balance = 0.;
19+
@NonNull
20+
private String accountHolder = "";
21+
22+
public Account addBalance(double amount) {
23+
if (amount < 0) {
24+
throw new IllegalArgumentException("Can not add negative amount");
25+
}
26+
27+
this.balance += amount;
28+
return this;
29+
}
30+
31+
public Account withdraw(double amount) {
32+
if (this.balance - abs(amount) < 0) {
33+
domainLog.log(Level.INFO, String.format("Transaction denied for account holder: %s", this.accountHolder));
34+
throw new IllegalArgumentException(String.format("Not enough balance, you have %.2f", this.balance));
35+
}
36+
37+
this.balance -= abs(amount);
38+
return this;
39+
}
40+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.lombok.configexamples;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.experimental.Accessors;
6+
7+
@AllArgsConstructor
8+
@Getter
9+
@Accessors(prefix = {"op"})
10+
public class TransactionLog {
11+
double amount;
12+
String description;
13+
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
lombok.anyconstructor.addconstructorproperties=false
1+
clear lombok.experimental.flagUsage
2+
3+
lombok.anyconstructor.addconstructorproperties=true
4+
lombok.addNullAnnotations = jetbrains
5+
lombok.accessors.chain = true
6+
lombok.log.fieldName = domainLog
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.lombok.configexamples;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
7+
8+
class AccountUnitTest {
9+
10+
@Test
11+
void should_initialize_account() {
12+
Account myAccount = new Account()
13+
.setBalance(2000.00)
14+
.setAccountHolder("John Snow");
15+
16+
assertEquals(2000.00, myAccount.getBalance());
17+
assertEquals("John Snow", myAccount.getAccountHolder());
18+
}
19+
20+
@Test
21+
void should_throw_error_when_balance_becomes_negative() {
22+
Account myAccount = new Account()
23+
.setBalance(20.00)
24+
.setAccountHolder("John Snow");
25+
26+
assertThrows(IllegalArgumentException.class, () -> myAccount.withdraw(100.00));
27+
}
28+
29+
@Test
30+
void should_throw_no_error_when_balance_becomes_zero() {
31+
Account myAccount = new Account()
32+
.setBalance(20.00)
33+
.setAccountHolder("John Snow");
34+
35+
myAccount.withdraw(20.00);
36+
37+
assertEquals(0.00, myAccount.getBalance());
38+
}
39+
40+
@Test
41+
void should_update_balance_properly() {
42+
Account myAccount = new Account()
43+
.setBalance(20.00)
44+
.setAccountHolder("John Snow");
45+
46+
myAccount.withdraw(5.00).withdraw(10.00);
47+
48+
assertEquals(5.00, myAccount.getBalance());
49+
}
50+
}

0 commit comments

Comments
 (0)