[BAEL-5248] Lombok Configuration System (#11599)

* [BAEL-5248] Initial code examples

* Add missing examples
This commit is contained in:
Thiago dos Santos Hora 2021-12-21 17:46:15 +01:00 committed by GitHub
parent 1a8a597fe6
commit da41d6e4f9
7 changed files with 124 additions and 1 deletions

8
lombok/lombok.config Normal file
View File

@ -0,0 +1,8 @@
import lombok_feature.config
config.stopBubbling = true
lombok.anyconstructor.addconstructorproperties=false
lombok.addLombokGeneratedAnnotation = true
lombok.addSuppressWarnings = false

View File

@ -0,0 +1 @@
lombok.experimental.flagUsage = warning

View File

@ -26,6 +26,11 @@
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>${hibernate-jpa-2.1-api.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>23.0.0</version>
</dependency>
</dependencies>
<build>
@ -70,7 +75,7 @@
<!-- various -->
<hibernate-jpa-2.1-api.version>1.0.0.Final</hibernate-jpa-2.1-api.version>
<!-- delombok maven plugin -->
<delombok-maven-plugin.version>1.18.10.0</delombok-maven-plugin.version>
<delombok-maven-plugin.version>1.18.20.0</delombok-maven-plugin.version>
</properties>
</project>

View File

@ -0,0 +1,40 @@
package com.baeldung.lombok.configexamples;
import lombok.*;
import lombok.extern.java.Log;
import java.util.logging.Level;
import static java.lang.Math.abs;
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Log
public class Account {
@NonNull
private Double balance = 0.;
@NonNull
private String accountHolder = "";
public Account addBalance(double amount) {
if (amount < 0) {
throw new IllegalArgumentException("Can not add negative amount");
}
this.balance += amount;
return this;
}
public Account withdraw(double amount) {
if (this.balance - abs(amount) < 0) {
domainLog.log(Level.INFO, String.format("Transaction denied for account holder: %s", this.accountHolder));
throw new IllegalArgumentException(String.format("Not enough balance, you have %.2f", this.balance));
}
this.balance -= abs(amount);
return this;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.lombok.configexamples;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.experimental.Accessors;
@AllArgsConstructor
@Getter
@Accessors(prefix = {"op"})
public class TransactionLog {
double amount;
String description;
}

View File

@ -0,0 +1,6 @@
clear lombok.experimental.flagUsage
lombok.anyconstructor.addconstructorproperties=true
lombok.addNullAnnotations = jetbrains
lombok.accessors.chain = true
lombok.log.fieldName = domainLog

View File

@ -0,0 +1,50 @@
package com.baeldung.lombok.configexamples;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class AccountUnitTest {
@Test
void should_initialize_account() {
Account myAccount = new Account()
.setBalance(2000.00)
.setAccountHolder("John Snow");
assertEquals(2000.00, myAccount.getBalance());
assertEquals("John Snow", myAccount.getAccountHolder());
}
@Test
void should_throw_error_when_balance_becomes_negative() {
Account myAccount = new Account()
.setBalance(20.00)
.setAccountHolder("John Snow");
assertThrows(IllegalArgumentException.class, () -> myAccount.withdraw(100.00));
}
@Test
void should_throw_no_error_when_balance_becomes_zero() {
Account myAccount = new Account()
.setBalance(20.00)
.setAccountHolder("John Snow");
myAccount.withdraw(20.00);
assertEquals(0.00, myAccount.getBalance());
}
@Test
void should_update_balance_properly() {
Account myAccount = new Account()
.setBalance(20.00)
.setAccountHolder("John Snow");
myAccount.withdraw(5.00).withdraw(10.00);
assertEquals(5.00, myAccount.getBalance());
}
}