Added test class for a simple shallow copy and deep copy

This commit is contained in:
Cesare
2022-10-13 18:16:51 +02:00
parent 12fee8e6d0
commit f43312e2c1
7 changed files with 172 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
<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>org.example</groupId>
<artifactId>java-shallow-deep-copy</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>java-shallow-deep-copy</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,13 @@
package com.baeldung.java_shallow_deep_copy;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
}
}

View File

@@ -0,0 +1,34 @@
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;
}
}

View File

@@ -0,0 +1,15 @@
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);
}
}

View File

@@ -0,0 +1,14 @@
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);
}
}

View File

@@ -0,0 +1,26 @@
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.PersonShallow;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
public class DeepCopyTest {
@Test
void whenTheParameterIsNotAnImmutableObject_thenShouldDoADeepCopy (){
String name = "Hello";
String surname = "World";
List<String> addresses = new ArrayList<>();
addresses.add("Standford street");
PersonDeep personShallow = new PersonDeep(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.assertNotEquals(addresses, personShallow.getAddresses());
}
}

View File

@@ -0,0 +1,38 @@
package com.baeldung.java_shallow_deep_copy.unit;
import com.baeldung.java_shallow_deep_copy.data.PersonShallow;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
public class ShallowCopyTest {
@Test
void whenTheParameterIsAnImmutableObject_thenShouldNotChangeTheValue (){
String name = "Hello";
String surname = "World";
PersonShallow personShallow = new PersonShallow(name,surname,null);
surname = "Pluto";
Assertions.assertNotEquals(surname, personShallow.getSurname());
}
@Test
void whenTheParameterIsNotAnImmutableObject_thenShouldDoAShallowCopy (){
String name = "Hello";
String surname = "World";
List<String> addresses = new ArrayList<>();
addresses.add("Standford street");
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());
}
}