Andrei Branza - deleted test article
This commit is contained in:
parent
5929902b1c
commit
33834c45c4
|
@ -1,33 +0,0 @@
|
|||
<?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>
|
|
@ -1,15 +0,0 @@
|
|||
package com.baeldung.cloning.deep;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode
|
||||
@ToString
|
||||
public class Address implements Cloneable {
|
||||
|
||||
private String streetName;
|
||||
private String cityName;
|
||||
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package com.baeldung.cloning.shallow;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode
|
||||
@ToString
|
||||
public class Address implements Cloneable {
|
||||
|
||||
private String streetName;
|
||||
private String cityName;
|
||||
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
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());
|
||||
}
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
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…
Reference in New Issue