formatted

This commit is contained in:
Cesare
2022-10-17 18:53:22 +02:00
parent 6133c31057
commit 44be87ef25
3 changed files with 22 additions and 17 deletions

View File

@@ -2,7 +2,6 @@ package com.baeldung.java_shallow_deep_copy.data;
public class BankAccountDeep extends BankAccount {
public BankAccountDeep(String name, String surname, Balance balance) {
super(name, surname, balance);
}

View File

@@ -9,8 +9,6 @@ import com.baeldung.java_shallow_deep_copy.data.BankAccountDeep;
public class DeepCopyTest {
@Test
void whenIsADeepCopyDoneByCopyConstructor_thenNestedObjectsAreNotTheSame() {
Balance balance = new Balance(10000, "EUR");
@@ -24,8 +22,10 @@ public class DeepCopyTest {
Balance balance = new Balance(10000, "EUR");
BankAccountDeep bankAccount = new BankAccountDeep("Hello", "World", balance);
BankAccountDeep deepCopy = new BankAccountDeep(bankAccount);
bankAccount.getBalance().setAmount(0);
float deepCopyAmount = deepCopy.getBalance().getAmount();
bankAccount.getBalance()
.setAmount(0);
float deepCopyAmount = deepCopy.getBalance()
.getAmount();
assertNotEquals(0, deepCopyAmount);
}
@@ -43,8 +43,10 @@ public class DeepCopyTest {
Balance balance = new Balance(10000, "EUR");
BankAccountDeep bankAccount = new BankAccountDeep("Hello", "World", balance);
BankAccountDeep deepCopy = (BankAccountDeep) bankAccount.clone();
bankAccount.getBalance().setAmount(0);
float deepCopyAmount = deepCopy.getBalance().getAmount();
bankAccount.getBalance()
.setAmount(0);
float deepCopyAmount = deepCopy.getBalance()
.getAmount();
assertNotEquals(0, deepCopyAmount);
}

View File

@@ -1,13 +1,13 @@
package com.baeldung.java_shallow_deep_copy.unit;
import com.baeldung.java_shallow_deep_copy.data.Balance;
import com.baeldung.java_shallow_deep_copy.data.BankAccountShallow;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.jupiter.api.Test;
import com.baeldung.java_shallow_deep_copy.data.Balance;
import com.baeldung.java_shallow_deep_copy.data.BankAccountShallow;
public class ShallowCopyTest {
@Test
@@ -28,12 +28,14 @@ public class ShallowCopyTest {
}
@Test
void whenIsAShallowCopyDoneByCopyConstructor_thenCopyShouldChange() throws CloneNotSupportedException {
void whenIsAShallowCopyDoneByCopyConstructor_thenCopyShouldChange(){
Balance balance = new Balance(10000, "EUR");
BankAccountShallow bankAccount = new BankAccountShallow("Hello", "World", balance);
BankAccountShallow shallowCopy = new BankAccountShallow(bankAccount);
bankAccount.getBalance().setAmount(0);
float shallowCopyAmount = shallowCopy.getBalance().getAmount();
bankAccount.getBalance()
.setAmount(0);
float shallowCopyAmount = shallowCopy.getBalance()
.getAmount();
assertEquals(0, shallowCopyAmount);
}
@@ -50,8 +52,10 @@ public class ShallowCopyTest {
Balance balance = new Balance(10000, "EUR");
BankAccountShallow bankAccount = new BankAccountShallow("Hello", "World", balance);
BankAccountShallow shallowCopy = (BankAccountShallow) bankAccount.clone();
bankAccount.getBalance().setAmount(0);
float shallowCopyAmount = shallowCopy.getBalance().getAmount();
bankAccount.getBalance()
.setAmount(0);
float shallowCopyAmount = shallowCopy.getBalance()
.getAmount();
assertEquals(0, shallowCopyAmount);
}