Modified code for Test
This commit is contained in:
parent
601d051f32
commit
fc07da9eda
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.shallowdeepcopy;
|
||||||
|
|
||||||
|
class School {
|
||||||
|
private String schoolName;
|
||||||
|
public School(School sc) {
|
||||||
|
this(sc.getSchoolName());
|
||||||
|
}
|
||||||
|
public School(String schoolName) {
|
||||||
|
this.schoolName = schoolName;
|
||||||
|
}
|
||||||
|
// standard getters and setters
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object clone() {
|
||||||
|
try {
|
||||||
|
return (School) super.clone();
|
||||||
|
} catch (CloneNotSupportedException e) {
|
||||||
|
return new School(this.getSchoolName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -14,3 +14,14 @@ class Student {
|
|||||||
} // standard getters and setters
|
} // standard getters and setters
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object clone() {
|
||||||
|
Student student = null;
|
||||||
|
try {
|
||||||
|
student = (Student) super.clone();
|
||||||
|
} catch (CloneNotSupportedException e) {
|
||||||
|
student = new Student(this.getName(), this.getAge(), this.getSchool());
|
||||||
|
}
|
||||||
|
student.school= (School) this.school.clone();
|
||||||
|
return student;
|
||||||
|
}
|
@ -6,29 +6,36 @@ import org.junit.jupiter.api.Test;
|
|||||||
public class ShallowDeepCopyExampleTest {
|
public class ShallowDeepCopyExampleTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testShallowCopy() {
|
public void whenPerformShallowCopy_thenObjectsNotSame() {
|
||||||
Student originalStudent = new Student("John", 20);
|
School school = new School("Baeldung School");
|
||||||
Student shallowCopy = originalStudent;
|
Student originalStudent = new Student("John", 20, school);
|
||||||
|
Student shallowCopy = new Student(originalStudent.getName(), originalStudent.getAge(), originalStudent.getSchool());
|
||||||
|
assertNotSame(shallowCopy, originalStudent);
|
||||||
|
}
|
||||||
|
|
||||||
shallowCopy.setName("Baeldung");
|
@Test public void whenModifyingActualObject_thenCopyAlsoChange() {
|
||||||
shallowCopy.setRollno(10);
|
School school = new School("Baeldung School");
|
||||||
|
Student originalStudent = new Student("John", 20, school);
|
||||||
assertEquals("Baeldung", originalStudent.getName());
|
Student shallowCopy = new Student(originalStudent.getName(), originalStudent.getAge(), originalStudent.getSchool());
|
||||||
assertEquals(10, originalStudent.getRollno());
|
school.setSchoolName("New Baeldung School");
|
||||||
|
assertEquals(shallowCopy.getSchool().getSchoolName(), originalStudent.getSchool().getSchoolName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testDeepCopy() {
|
public void whenModifyingActualObject_thenCloneCopyNotChange() {
|
||||||
Student originalStudent = new Student("John", 20);
|
School school = new School("New School");
|
||||||
Student deepCopy = new Student(originalStudent.getName(), originalStudent.getRollno());
|
Student originalStudent = new Student("Alice", 10, school);
|
||||||
|
Student deepCopy = (Student) originalStudent.clone();
|
||||||
|
school.setSchoolName("New Baeldung School");
|
||||||
|
assertNotEquals(deepCopy.getSchool().getSchoolName(), originalStudent.getSchool().getSchoolName());
|
||||||
|
}
|
||||||
|
|
||||||
deepCopy.setName("Baeldung");
|
@Test
|
||||||
deepCopy.setRollno(10);
|
public void whenModifyingActualObject_thenCopyNotChange() {
|
||||||
|
School school = new School("Baeldung School");
|
||||||
assertEquals("John", originalStudent.getName());
|
Student originalStudent = new Student("Alice", 30, school);
|
||||||
assertEquals(20, originalStudent.getRollno());
|
Student deepCopy = new Student(originalStudent);
|
||||||
|
school.setSchoolName("New Baeldung School");
|
||||||
assertEquals("Baeldung", deepCopy.getName());
|
assertNotEquals( originalStudent.getSchool().getSchoolName(), deepCopy.getSchool().getSchoolName());
|
||||||
assertEquals(10, deepCopy.getRollno());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user