Andrei Branza - test article shallowCopy-vs-deepCopy
This commit is contained in:
parent
d658a3d2b8
commit
13448aa551
38
xxAndreiBranza/shallowCopy-vs-deepCopy/.gitignore
vendored
Normal file
38
xxAndreiBranza/shallowCopy-vs-deepCopy/.gitignore
vendored
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea/modules.xml
|
||||||
|
.idea/jarRepositories.xml
|
||||||
|
.idea/compiler.xml
|
||||||
|
.idea/libraries/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
33
xxAndreiBranza/shallowCopy-vs-deepCopy/pom.xml
Normal file
33
xxAndreiBranza/shallowCopy-vs-deepCopy/pom.xml
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>shallowCopy-vs-deepCopy</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>21</java.version>
|
||||||
|
<junit.version>4.13.2</junit.version>
|
||||||
|
<test.scope>test</test.scope>
|
||||||
|
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||||
|
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>${test.scope</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.30</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.cloning.deep;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@EqualsAndHashCode
|
||||||
|
@ToString
|
||||||
|
public class Address implements Cloneable
|
||||||
|
{
|
||||||
|
private String streetName;
|
||||||
|
private String cityName;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Address clone() {
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return (Address) super.clone();
|
||||||
|
} catch (CloneNotSupportedException cloneException)
|
||||||
|
{
|
||||||
|
throw new RuntimeException(cloneException);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.cloning.deep;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@EqualsAndHashCode
|
||||||
|
@ToString
|
||||||
|
public class Person implements Cloneable
|
||||||
|
{
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
private Address address;
|
||||||
|
|
||||||
|
public Person(Person personToBeCloned) {
|
||||||
|
Address addressToBeCloned = personToBeCloned.getAddress();
|
||||||
|
|
||||||
|
this.firstName = personToBeCloned.getFirstName();
|
||||||
|
this.lastName = personToBeCloned.getLastName();
|
||||||
|
this.address = new Address(addressToBeCloned.getStreetName(), addressToBeCloned.getCityName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Person clone() {
|
||||||
|
return new Person(this);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.baeldung.cloning.shallow;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@EqualsAndHashCode
|
||||||
|
@ToString
|
||||||
|
public class Address implements Cloneable
|
||||||
|
{
|
||||||
|
private String streetName;
|
||||||
|
private String cityName;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Address clone() {
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return (Address) super.clone();
|
||||||
|
} catch (CloneNotSupportedException cloneException)
|
||||||
|
{
|
||||||
|
throw new RuntimeException(cloneException);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.baeldung.cloning.shallow;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@EqualsAndHashCode
|
||||||
|
@ToString
|
||||||
|
public class Person implements Cloneable
|
||||||
|
{
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
private Address address;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Person clone() {
|
||||||
|
try {
|
||||||
|
return (Person) super.clone();
|
||||||
|
} catch (CloneNotSupportedException cloneException) {
|
||||||
|
throw new RuntimeException(cloneException);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.baeldung.cloning.deep;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
|
||||||
|
public class DeepCloningTest {
|
||||||
|
|
||||||
|
Person alex = new Person("Alex", "Jones", new Address("Main Street", "Main City"));
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingDeepCopy_thenReferencesAreNotTheSame()
|
||||||
|
{
|
||||||
|
Person constructorCopyOfAlex = new Person(alex);
|
||||||
|
|
||||||
|
assertEquals(alex, constructorCopyOfAlex);
|
||||||
|
|
||||||
|
alex.setAddress(new Address("Unknown Street", "Unknown City"));
|
||||||
|
|
||||||
|
assertNotEquals(alex.getAddress(), constructorCopyOfAlex.getAddress());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.cloning.shallow;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
public class ShallowCloningTest {
|
||||||
|
|
||||||
|
Person alex = new Person("Alex", "Jones", new Address("Main Street", "Main City"));
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingShallowCopy_ThenReferencesAreTheSame()
|
||||||
|
{
|
||||||
|
Person shallowCopyOfAlex = alex.clone();
|
||||||
|
|
||||||
|
assertEquals(alex, shallowCopyOfAlex);
|
||||||
|
|
||||||
|
alex.getAddress().setCityName("Unknown City");
|
||||||
|
|
||||||
|
assertEquals(alex.getAddress(), shallowCopyOfAlex.getAddress());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user