BAEL-5138: Implementation code for Lombok @With Methods (#11494)
* BAEL-5138: Implementation code for Lombok @With Methods * BAEL-5138: Implementation code for Lombok @With Methods * Fixed a type issue in HolderUnitTest * Removed a constructor from User to avoid confusion with article
This commit is contained in:
parent
49d32777d1
commit
9f418cd996
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.lombok.with;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.With;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public abstract class Device {
|
||||
private final String serial;
|
||||
@With
|
||||
private final boolean isInspected;
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.lombok.with;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.With;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class Holder {
|
||||
@With
|
||||
private String variableA;
|
||||
@With
|
||||
private String _variableB;
|
||||
@With
|
||||
private String $variableC;
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.lombok.with;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.NonNull;
|
||||
import lombok.With;
|
||||
|
||||
@With
|
||||
@AllArgsConstructor
|
||||
public class ImprovedUser {
|
||||
@NonNull
|
||||
private final String username;
|
||||
@NonNull
|
||||
private final String emailAddress;
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.lombok.with;
|
||||
|
||||
public class KioskDevice extends Device {
|
||||
|
||||
public KioskDevice(String serial, boolean isInspected) {
|
||||
super(serial, isInspected);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Device withInspected(boolean isInspected) {
|
||||
return new KioskDevice(getSerial(), isInspected);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.lombok.with;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.With;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class Stock {
|
||||
@With
|
||||
private String sku;
|
||||
private int stockCount;
|
||||
|
||||
public Stock withSku(String sku) {
|
||||
return new Stock("mod-" + sku, stockCount);
|
||||
}
|
||||
|
||||
public Stock withSKU(String... sku) {
|
||||
return sku == null || sku.length == 0 ?
|
||||
new Stock("unknown", stockCount) :
|
||||
new Stock("mod-" + sku[0], stockCount);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.lombok.with;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.With;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class User {
|
||||
private final String username;
|
||||
private final String emailAddress;
|
||||
@With
|
||||
private final boolean isAuthenticated;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.lombok.with;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
|
||||
public class HolderUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenWithMethodsGenerated_thenUsable() {
|
||||
Holder value = new Holder("a", "b");
|
||||
|
||||
Holder valueModifiedA = value.withVariableA("mod-a");
|
||||
Holder valueModifiedB = value.with_variableB("mod-b");
|
||||
// Holder valueModifiedC = value.with$VariableC("mod-c"); not possible
|
||||
|
||||
assertNotSame(valueModifiedA, value);
|
||||
assertNotSame(valueModifiedB, value);
|
||||
assertEquals("mod-a", valueModifiedA.getVariableA());
|
||||
assertEquals("mod-b", valueModifiedB.get_variableB());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.lombok.with;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class ImprovedUserUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUsernameNull_thenException() {
|
||||
ImprovedUser user = new ImprovedUser("testuser", "test@mail.com");
|
||||
|
||||
assertThrows(NullPointerException.class, () -> user.withUsername(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenEmailNull_thenException() {
|
||||
ImprovedUser user = new ImprovedUser("testuser", "test@mail.com");
|
||||
|
||||
assertThrows(NullPointerException.class, () -> user.withEmailAddress(null));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.lombok.with;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class KioskDeviceWithUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenDeviceInspected_thenClonedAndUpdated() {
|
||||
KioskDevice device = new KioskDevice("S-001", false);
|
||||
|
||||
Device inspectedDevice = device.withInspected(true);
|
||||
|
||||
assertNotSame(inspectedDevice, device);
|
||||
assertFalse(device.isInspected());
|
||||
assertTrue(inspectedDevice.isInspected());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.lombok.with;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class StockWithUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenWithManuallyProvided_whenSkuChanged_thenManualMethodUsed() {
|
||||
Stock stock = new Stock("sku-001", 5);
|
||||
|
||||
Stock modifiedStock = stock.withSku("sku-002");
|
||||
Stock anotherModifiedStock = stock.withSKU("sku-003", "sku-004");
|
||||
|
||||
assertNotSame(modifiedStock, stock);
|
||||
assertNotSame(anotherModifiedStock, stock);
|
||||
assertTrue(modifiedStock.getSku().startsWith("mod"));
|
||||
assertTrue(anotherModifiedStock.getSku().startsWith("mod"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.lombok.with;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class UserWithUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUserAuthenticated_thenClonedAndUpdated() {
|
||||
User immutableUser = new User("testuser", "test@mail.com", false);
|
||||
|
||||
User authenticatedUser = immutableUser.withAuthenticated(true);
|
||||
|
||||
assertNotSame(immutableUser, authenticatedUser);
|
||||
assertFalse(immutableUser.isAuthenticated());
|
||||
assertTrue(authenticatedUser.isAuthenticated());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue