From 7c3cbf0d4f1af133e3dd7882358b492d003bc889 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Fri, 4 Mar 2022 03:29:27 +0000 Subject: [PATCH] [BAEL-5245] Lombok ToString annotation (#11870) --- .../com/baeldung/lombok/tostring/Account.java | 56 ++++++++++++++++ .../baeldung/lombok/tostring/AccountType.java | 9 +++ .../lombok/tostring/RewardAccount.java | 27 ++++++++ .../lombok/tostring/SavingAccount.java | 17 +++++ .../lombok/tostring/ToStringUnitTest.java | 66 +++++++++++++++++++ 5 files changed, 175 insertions(+) create mode 100644 lombok-2/src/main/java/com/baeldung/lombok/tostring/Account.java create mode 100644 lombok-2/src/main/java/com/baeldung/lombok/tostring/AccountType.java create mode 100644 lombok-2/src/main/java/com/baeldung/lombok/tostring/RewardAccount.java create mode 100644 lombok-2/src/main/java/com/baeldung/lombok/tostring/SavingAccount.java create mode 100644 lombok-2/src/test/java/com/baeldung/lombok/tostring/ToStringUnitTest.java diff --git a/lombok-2/src/main/java/com/baeldung/lombok/tostring/Account.java b/lombok-2/src/main/java/com/baeldung/lombok/tostring/Account.java new file mode 100644 index 0000000000..641ad1e1a3 --- /dev/null +++ b/lombok-2/src/main/java/com/baeldung/lombok/tostring/Account.java @@ -0,0 +1,56 @@ +package com.baeldung.lombok.tostring; + +import lombok.ToString; + +@ToString +public class Account { + + private String name; + + // render this field before any others (the highest ranked) + @ToString.Include(rank = 1) + private String id; + + @ToString.Exclude + private String accountNumber; + + // automatically excluded + private String $ignored; + + @ToString.Include + String description() { + return "Account description"; + } + + public String getAccountNumber() { + return accountNumber; + } + + public void setAccountNumber(String accountNumber) { + this.accountNumber = accountNumber; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String get$ignored() { + return $ignored; + } + + public void set$ignored(String value) { + this.$ignored = value; + } +} diff --git a/lombok-2/src/main/java/com/baeldung/lombok/tostring/AccountType.java b/lombok-2/src/main/java/com/baeldung/lombok/tostring/AccountType.java new file mode 100644 index 0000000000..7f99b5ca35 --- /dev/null +++ b/lombok-2/src/main/java/com/baeldung/lombok/tostring/AccountType.java @@ -0,0 +1,9 @@ +package com.baeldung.lombok.tostring; + +import lombok.ToString; + +@ToString +public enum AccountType { + CHECKING, + SAVING +} diff --git a/lombok-2/src/main/java/com/baeldung/lombok/tostring/RewardAccount.java b/lombok-2/src/main/java/com/baeldung/lombok/tostring/RewardAccount.java new file mode 100644 index 0000000000..9ac9c6afa6 --- /dev/null +++ b/lombok-2/src/main/java/com/baeldung/lombok/tostring/RewardAccount.java @@ -0,0 +1,27 @@ +package com.baeldung.lombok.tostring; + +import lombok.ToString; + +@ToString +public class RewardAccount extends Account { + + private String rewardAccountId; + + private Object[] relatedAccounts; + + public String getRewardAccountId() { + return rewardAccountId; + } + + public void setRewardAccountId(String rewardAccountId) { + this.rewardAccountId = rewardAccountId; + } + + public Object[] getRelatedAccounts() { + return relatedAccounts; + } + + public void setRelatedAccounts(Object[] relatedAccounts) { + this.relatedAccounts = relatedAccounts; + } +} diff --git a/lombok-2/src/main/java/com/baeldung/lombok/tostring/SavingAccount.java b/lombok-2/src/main/java/com/baeldung/lombok/tostring/SavingAccount.java new file mode 100644 index 0000000000..dfba31cf27 --- /dev/null +++ b/lombok-2/src/main/java/com/baeldung/lombok/tostring/SavingAccount.java @@ -0,0 +1,17 @@ +package com.baeldung.lombok.tostring; + +import lombok.ToString; + +@ToString(callSuper = true) +public class SavingAccount extends Account { + + private String savingAccountId; + + public String getSavingAccountId() { + return savingAccountId; + } + + public void setSavingAccountId(String savingAccountId) { + this.savingAccountId = savingAccountId; + } +} diff --git a/lombok-2/src/test/java/com/baeldung/lombok/tostring/ToStringUnitTest.java b/lombok-2/src/test/java/com/baeldung/lombok/tostring/ToStringUnitTest.java new file mode 100644 index 0000000000..cb56ad912d --- /dev/null +++ b/lombok-2/src/test/java/com/baeldung/lombok/tostring/ToStringUnitTest.java @@ -0,0 +1,66 @@ +package com.baeldung.lombok.tostring; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class ToStringUnitTest { + + @Test + void whenPrintObject_thenOutputIsCorrect() { + Account account = new Account(); + account.setId("12345"); + account.setName("An account"); + account.setAccountNumber("11111"); // should not be present in output + account.set$ignored("ignored value"); // should not be present in output + + assertThat(account.toString()) + .isEqualTo("Account(id=12345, name=An account, description=Account description)"); + } + + @Test + void whenPrintSubclassWithSuper_thenOutputIsCorrect() { + SavingAccount savingAccount = new SavingAccount(); + savingAccount.setSavingAccountId("5678"); + savingAccount.setId("12345"); + savingAccount.setName("An account"); + + assertThat(savingAccount.toString()) + .isEqualTo("SavingAccount(super=Account(id=12345, name=An account, description=Account description), savingAccountId=5678)"); + } + +@Test +void whenPrintArrays_thenOutputIsCorrect() { + RewardAccount account = new RewardAccount(); + account.setRewardAccountId("12345"); + + // circular ref, just for demonstration + Object[] relatedAccounts = new Object[2]; + relatedAccounts[0] = "54321"; + relatedAccounts[1] = relatedAccounts; + + account.setRelatedAccounts(relatedAccounts); + + assertThat(account.toString()) + .isEqualTo("RewardAccount(rewardAccountId=12345, relatedAccounts=[54321, [...]])"); +} + + @Test + void whenPrintSubclassWithoutSuper_thenOutputIsCorrect() { + RewardAccount rewardAccount = new RewardAccount(); + rewardAccount.setRewardAccountId("12345"); + + assertThat(rewardAccount.toString()) + .isEqualTo("RewardAccount(rewardAccountId=12345, relatedAccounts=null)"); + } + + @Test + void whenPrintEnum_thenOutputIsCorrect() { + assertThat(AccountType.CHECKING.toString()) + .isEqualTo("AccountType.CHECKING"); + + assertThat(AccountType.SAVING.toString()) + .isEqualTo("AccountType.SAVING"); + } + +}