Added test class for a simple shallow copy and deep copy
This commit is contained in:
@@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung.java_shallow_deep_copy.data;
|
||||||
|
|
||||||
|
public class Balance implements Cloneable {
|
||||||
|
|
||||||
|
private float amount;
|
||||||
|
private String currency;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object clone() throws CloneNotSupportedException {
|
||||||
|
return super.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Balance(float amount, String currency) {
|
||||||
|
this.amount = amount;
|
||||||
|
this.currency = currency;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Copy constructor
|
||||||
|
public Balance(Balance balance) {
|
||||||
|
this(balance.getAmount(), balance.getCurrency());
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getAmount() {
|
||||||
|
return amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAmount(float amount) {
|
||||||
|
this.amount = amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCurrency() {
|
||||||
|
return currency;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCurrency(String currency) {
|
||||||
|
this.currency = currency;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung.java_shallow_deep_copy.data;
|
||||||
|
|
||||||
|
public class BankAccount implements Cloneable {
|
||||||
|
|
||||||
|
protected String name;
|
||||||
|
protected String surname;
|
||||||
|
protected Balance balance;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSurname() {
|
||||||
|
return surname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSurname(String surname) {
|
||||||
|
this.surname = surname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Balance getBalance() {
|
||||||
|
return balance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBalance(Balance balance) {
|
||||||
|
this.balance = balance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BankAccount(String name, String surname, Balance balance) {
|
||||||
|
this.name = name;
|
||||||
|
this.surname = surname;
|
||||||
|
this.balance = balance;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object clone() throws CloneNotSupportedException {
|
||||||
|
return new BankAccountDeep(name, surname, (Balance) balance.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
public BankAccountDeep(BankAccountDeep bankAccountDeep) {
|
||||||
|
this(bankAccountDeep.name, bankAccountDeep.surname, new Balance(bankAccountDeep.balance));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.java_shallow_deep_copy.data;
|
||||||
|
|
||||||
|
public class BankAccountShallow extends BankAccount {
|
||||||
|
|
||||||
|
public BankAccountShallow(String name, String surname, Balance balance) {
|
||||||
|
super(name, surname, balance);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BankAccountShallow(BankAccountShallow shallow) {
|
||||||
|
this(shallow.name, shallow.surname, shallow.balance);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object clone() throws CloneNotSupportedException {
|
||||||
|
return super.clone();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
package com.baeldung.java_shallow_deep_copy.data;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class Person {
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
private String surname;
|
|
||||||
private List<String> addresses;
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getSurname() {
|
|
||||||
return surname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSurname(String surname) {
|
|
||||||
this.surname = surname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getAddresses() {
|
|
||||||
return addresses;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAddresses(List<String> addresses) {
|
|
||||||
this.addresses = addresses;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
package com.baeldung.java_shallow_deep_copy.data;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class PersonDeep extends Person{
|
|
||||||
|
|
||||||
public PersonDeep(String name, String surname, List<String> addresses) {
|
|
||||||
this.setName(name);
|
|
||||||
this.setSurname(surname);
|
|
||||||
List<String> deepCopyList = new ArrayList<>();
|
|
||||||
deepCopyList.addAll(addresses);
|
|
||||||
this.setAddresses(deepCopyList);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
package com.baeldung.java_shallow_deep_copy.data;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class PersonShallow extends Person {
|
|
||||||
|
|
||||||
public PersonShallow(String name, String surname, List<String> addresses) {
|
|
||||||
this.setName(name);
|
|
||||||
this.setSurname(surname);
|
|
||||||
this.setAddresses(addresses);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,26 +1,53 @@
|
|||||||
package com.baeldung.java_shallow_deep_copy.unit;
|
package com.baeldung.java_shallow_deep_copy.unit;
|
||||||
|
|
||||||
import com.baeldung.java_shallow_deep_copy.data.PersonDeep;
|
import com.baeldung.java_shallow_deep_copy.data.*;
|
||||||
import com.baeldung.java_shallow_deep_copy.data.PersonShallow;
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class DeepCopyTest {
|
public class DeepCopyTest {
|
||||||
|
|
||||||
|
private static final String NAME = "Hello";
|
||||||
|
private static final String SURNAME = "World";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whenTheParameterIsNotAnImmutableObject_thenShouldDoADeepCopy (){
|
void whenIsADeepCopyDoneByCopyConstructor_thenNestedObjectShouldNotHaveSameReference() {
|
||||||
|
Balance balance = new Balance(10000, "EUR");
|
||||||
|
BankAccountDeep bankAccount = new BankAccountDeep(NAME, SURNAME, balance);
|
||||||
|
BankAccountDeep deepCopy = new BankAccountDeep(bankAccount);
|
||||||
|
assertNotEquals(bankAccount.getBalance(), deepCopy.getBalance());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenIsADeepCopyDoneByCopyConstructor_thenCopyValueShouldNotChange() {
|
||||||
|
Balance balance = new Balance(10000, "EUR");
|
||||||
|
BankAccountDeep bankAccount = new BankAccountDeep(NAME, SURNAME, balance);
|
||||||
|
BankAccountDeep deepCopy = new BankAccountDeep(bankAccount);
|
||||||
|
balance.setAmount(0);
|
||||||
|
assertNotEquals(bankAccount.getBalance()
|
||||||
|
.getAmount(), deepCopy.getBalance()
|
||||||
|
.getAmount());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenIsADeepCopyDoneByCloneMethod_thenNestedObjectShouldNotHaveSameReference() throws CloneNotSupportedException {
|
||||||
String name = "Hello";
|
String name = "Hello";
|
||||||
String surname = "World";
|
String surname = "World";
|
||||||
List<String> addresses = new ArrayList<>();
|
Balance balance = new Balance(10000, "EUR");
|
||||||
addresses.add("Standford street");
|
BankAccountDeep bankAccount = new BankAccountDeep(NAME, SURNAME, balance);
|
||||||
PersonDeep personShallow = new PersonDeep(name,surname,addresses);
|
BankAccountDeep deepCopy = (BankAccountDeep) bankAccount.clone();
|
||||||
personShallow.getAddresses().forEach(System.out :: println);
|
assertNotEquals(bankAccount.getBalance(), deepCopy.getBalance());
|
||||||
Assertions.assertEquals(addresses, personShallow.getAddresses());
|
}
|
||||||
addresses.add("Oxford street");
|
|
||||||
personShallow.getAddresses().forEach(System.out :: println);
|
@Test
|
||||||
Assertions.assertNotEquals(addresses, personShallow.getAddresses());
|
void whenIsADeepCopyDoneByCloneMethod_thenCopyValueShouldNotChange() throws CloneNotSupportedException {
|
||||||
|
|
||||||
|
Balance balance = new Balance(10000, "EUR");
|
||||||
|
BankAccountDeep bankAccount = new BankAccountDeep(NAME, SURNAME, balance);
|
||||||
|
BankAccountDeep deepCopy = (BankAccountDeep) bankAccount.clone();
|
||||||
|
balance.setAmount(0);
|
||||||
|
assertNotEquals(balance.getAmount(), deepCopy.getBalance()
|
||||||
|
.getAmount());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,38 +1,62 @@
|
|||||||
package com.baeldung.java_shallow_deep_copy.unit;
|
package com.baeldung.java_shallow_deep_copy.unit;
|
||||||
|
|
||||||
import com.baeldung.java_shallow_deep_copy.data.PersonShallow;
|
import com.baeldung.java_shallow_deep_copy.data.Balance;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import com.baeldung.java_shallow_deep_copy.data.BankAccountShallow;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import java.util.List;
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
|
||||||
|
|
||||||
public class ShallowCopyTest {
|
public class ShallowCopyTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whenTheParameterIsAnImmutableObject_thenShouldNotChangeTheValue (){
|
void whenIsAShallowCopyDoneByCopyConstructor_thenImmutableObjectWillNotChange() {
|
||||||
String name = "Hello";
|
String name = "Hello";
|
||||||
String surname = "World";
|
String surname = "World";
|
||||||
PersonShallow personShallow = new PersonShallow(name,surname,null);
|
BankAccountShallow personShallow = new BankAccountShallow(name, surname, null);
|
||||||
surname = "Pluto";
|
surname = "Pluto";
|
||||||
Assertions.assertNotEquals(surname, personShallow.getSurname());
|
assertNotEquals(surname, personShallow.getSurname());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whenTheParameterIsNotAnImmutableObject_thenShouldDoAShallowCopy (){
|
void whenIsAShallowCopyDoneByCopyConstructor_thenNestedObjectsAreTheSame() {
|
||||||
String name = "Hello";
|
Balance balance = new Balance(10000, "EUR");
|
||||||
String surname = "World";
|
BankAccountShallow bankAccount = new BankAccountShallow("Hello", "World", balance);
|
||||||
List<String> addresses = new ArrayList<>();
|
BankAccountShallow shallowCopy = new BankAccountShallow(bankAccount.getName(), bankAccount.getSurname(), bankAccount.getBalance());
|
||||||
addresses.add("Standford street");
|
assertEquals(bankAccount.getBalance(), shallowCopy.getBalance());
|
||||||
PersonShallow personShallow = new PersonShallow(name,surname,addresses);
|
|
||||||
personShallow.getAddresses().forEach(System.out :: println);
|
|
||||||
Assertions.assertEquals(addresses, personShallow.getAddresses());
|
|
||||||
addresses.add("Oxford street");
|
|
||||||
personShallow.getAddresses().forEach(System.out :: println);
|
|
||||||
Assertions.assertEquals(addresses, personShallow.getAddresses());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenIsAShallowCopyDoneByCopyConstructor_thenCopyValueShouldChange() throws CloneNotSupportedException {
|
||||||
|
Balance balance = new Balance(10000, "EUR");
|
||||||
|
BankAccountShallow bankAccount = new BankAccountShallow("Hello", "World", balance);
|
||||||
|
BankAccountShallow shallowCopy = new BankAccountShallow(bankAccount);
|
||||||
|
float newBalance = 0;
|
||||||
|
balance.setAmount(newBalance);
|
||||||
|
assertEquals(newBalance, bankAccount.getBalance()
|
||||||
|
.getAmount());
|
||||||
|
assertEquals(newBalance, shallowCopy.getBalance()
|
||||||
|
.getAmount());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenIsAShallowCopyDoneByCloneMethod_thenNestedObjectsAreTheSame() throws CloneNotSupportedException {
|
||||||
|
Balance balance = new Balance(10000, "EUR");
|
||||||
|
BankAccountShallow bankAccount = new BankAccountShallow("Hello", "World", balance);
|
||||||
|
BankAccountShallow shallowCopy = (BankAccountShallow) bankAccount.clone();
|
||||||
|
assertEquals(bankAccount.getBalance(), shallowCopy.getBalance());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenIsAShallowCopyDoneByCloneMethod__thenCopyValueShouldChange() throws CloneNotSupportedException {
|
||||||
|
Balance balance = new Balance(10000, "EUR");
|
||||||
|
BankAccountShallow bankAccount = new BankAccountShallow("Hello", "World", balance);
|
||||||
|
BankAccountShallow shallowCopy = (BankAccountShallow) bankAccount.clone();
|
||||||
|
float newBalance = 0;
|
||||||
|
balance.setAmount(newBalance);
|
||||||
|
assertEquals(newBalance, shallowCopy.getBalance()
|
||||||
|
.getAmount());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user