[improve-lombok-accessor] add final accessors (#13703)
* [improve-lombok-accessor] add final accessors * [improve-lombok-accessor] rename test methods; remove the unused class * [improve-lombok-accessor] formatting
This commit is contained in:
parent
b2f92bce02
commit
750c00416f
@ -1,24 +0,0 @@
|
||||
package com.baeldung.lombok.accessors.model;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class BasicAccount {
|
||||
private String name;
|
||||
private BigDecimal balance;
|
||||
|
||||
public BigDecimal getBalance() {
|
||||
return this.balance;
|
||||
}
|
||||
|
||||
public void setBalance(BigDecimal newBalance) {
|
||||
this.balance = newBalance;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String accountName) {
|
||||
this.name = accountName;
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.baeldung.lombok.accessors.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Accessors(makeFinal = true)
|
||||
@Getter
|
||||
@Setter
|
||||
public class FinalAccount {
|
||||
private String name;
|
||||
private BigDecimal balance;
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.baeldung.lombok.accessors.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Accessors(makeFinal = true, fluent = true, chain = true)
|
||||
@Getter
|
||||
@Setter
|
||||
public class FinalChainedFluentAccount {
|
||||
private String name;
|
||||
private BigDecimal balance;
|
||||
}
|
@ -1,26 +1,36 @@
|
||||
package com.baeldung.lombok.accessors;
|
||||
|
||||
import com.baeldung.lombok.accessors.model.*;
|
||||
import com.baeldung.lombok.accessors.model.ChainedAccount;
|
||||
import com.baeldung.lombok.accessors.model.ChainedFluentAccount;
|
||||
import com.baeldung.lombok.accessors.model.FinalAccount;
|
||||
import com.baeldung.lombok.accessors.model.FinalChainedFluentAccount;
|
||||
import com.baeldung.lombok.accessors.model.FluentAccount;
|
||||
import com.baeldung.lombok.accessors.model.PrefixedAccount;
|
||||
import com.baeldung.lombok.accessors.model.PrefixedFluentAccount;
|
||||
import com.baeldung.lombok.accessors.model.StandardAccount;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class AccessorsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenBasicAccount_thenUseBasicAccessors() {
|
||||
BasicAccount account = new BasicAccount();
|
||||
account.setName("Basic Accessors");
|
||||
public void whenStandardAccount_thenHaveStandardAccessors() {
|
||||
StandardAccount account = new StandardAccount();
|
||||
account.setName("Standard Accessors");
|
||||
account.setBalance(BigDecimal.TEN);
|
||||
|
||||
assertEquals("Basic Accessors", account.getName());
|
||||
assertEquals("Standard Accessors", account.getName());
|
||||
assertEquals(BigDecimal.TEN, account.getBalance());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFluentAccount_thenUseFluentAccessors() {
|
||||
public void whenFluentAccount_thenHaveFluentAccessors() {
|
||||
FluentAccount account = new FluentAccount();
|
||||
account.name("Fluent Account");
|
||||
account.balance(BigDecimal.TEN);
|
||||
@ -30,7 +40,7 @@ public class AccessorsUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenChainedAccount_thenUseChainedAccessors() {
|
||||
public void whenChainedAccount_thenHaveChainedAccessors() {
|
||||
ChainedAccount account = new ChainedAccount();
|
||||
account.setName("Chained Account").setBalance(BigDecimal.TEN);
|
||||
|
||||
@ -39,7 +49,7 @@ public class AccessorsUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenChainedFluentAccount_thenUseChainedFluentAccessors() {
|
||||
public void whenChainedFluentAccount_thenHaveChainedFluentAccessors() {
|
||||
ChainedFluentAccount account = new ChainedFluentAccount()
|
||||
.name("Fluent Account")
|
||||
.balance(BigDecimal.TEN);
|
||||
@ -49,7 +59,7 @@ public class AccessorsUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPrefixedAccount_thenRemovePrefixFromAccessors() {
|
||||
public void whenPrefixedAccount_thenRemovePrefixFromAccessors() {
|
||||
PrefixedAccount account = new PrefixedAccount();
|
||||
account.setName("Prefixed Fields");
|
||||
account.setBalance(BigDecimal.TEN);
|
||||
@ -61,7 +71,7 @@ public class AccessorsUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPrefixedFluentAccount_thenRemovePrefixFromAccessors() {
|
||||
public void whenPrefixedFluentAccount_thenRemovePrefixFromAccessors() {
|
||||
PrefixedFluentAccount account = new PrefixedFluentAccount();
|
||||
account
|
||||
.name("Prefixed Fluent Fields")
|
||||
@ -71,4 +81,38 @@ public class AccessorsUnitTest {
|
||||
assertEquals(BigDecimal.TEN, account.balance());
|
||||
}
|
||||
|
||||
}
|
||||
@Test
|
||||
public void whenFinalAccount_thenHaveFinalAccessors() {
|
||||
FinalAccount account = new FinalAccount();
|
||||
account.setName("Final Account");
|
||||
account.setBalance(BigDecimal.TEN);
|
||||
|
||||
assertEquals("Final Account", account.getName());
|
||||
assertEquals(BigDecimal.TEN, account.getBalance());
|
||||
|
||||
//verify if all getters and setters are final methods
|
||||
boolean getterSettersAreFinal = Arrays.stream(FinalAccount.class.getMethods())
|
||||
.filter(method -> method.getName().matches("^(get|set)(Name|Balance)$"))
|
||||
.allMatch(method -> Modifier.isFinal(method.getModifiers()));
|
||||
assertTrue(getterSettersAreFinal);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFinalChainedFluentAccount_thenHaveFinalAccessors() {
|
||||
FinalChainedFluentAccount account = new FinalChainedFluentAccount();
|
||||
account
|
||||
.name("Final Chained Fluent Account")
|
||||
.balance(BigDecimal.TEN);
|
||||
|
||||
assertEquals("Final Chained Fluent Account", account.name());
|
||||
assertEquals(BigDecimal.TEN, account.balance());
|
||||
|
||||
//verify if all getters and setters are final methods
|
||||
boolean getterSettersAreFinal = Arrays.stream(FinalAccount.class.getMethods())
|
||||
.filter(method -> method.getName().matches("^(name|balance)$"))
|
||||
.allMatch(method -> Modifier.isFinal(method.getModifiers()));
|
||||
assertTrue(getterSettersAreFinal);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user