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
|
||||
}
|
||||
|
||||
@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 {
|
||||
|
||||
@Test
|
||||
void testShallowCopy() {
|
||||
Student originalStudent = new Student("John", 20);
|
||||
Student shallowCopy = originalStudent;
|
||||
public void whenPerformShallowCopy_thenObjectsNotSame() {
|
||||
School school = new School("Baeldung School");
|
||||
Student originalStudent = new Student("John", 20, school);
|
||||
Student shallowCopy = new Student(originalStudent.getName(), originalStudent.getAge(), originalStudent.getSchool());
|
||||
assertNotSame(shallowCopy, originalStudent);
|
||||
}
|
||||
|
||||
shallowCopy.setName("Baeldung");
|
||||
shallowCopy.setRollno(10);
|
||||
|
||||
assertEquals("Baeldung", originalStudent.getName());
|
||||
assertEquals(10, originalStudent.getRollno());
|
||||
@Test public void whenModifyingActualObject_thenCopyAlsoChange() {
|
||||
School school = new School("Baeldung School");
|
||||
Student originalStudent = new Student("John", 20, school);
|
||||
Student shallowCopy = new Student(originalStudent.getName(), originalStudent.getAge(), originalStudent.getSchool());
|
||||
school.setSchoolName("New Baeldung School");
|
||||
assertEquals(shallowCopy.getSchool().getSchoolName(), originalStudent.getSchool().getSchoolName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeepCopy() {
|
||||
Student originalStudent = new Student("John", 20);
|
||||
Student deepCopy = new Student(originalStudent.getName(), originalStudent.getRollno());
|
||||
public void whenModifyingActualObject_thenCloneCopyNotChange() {
|
||||
School school = new School("New School");
|
||||
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");
|
||||
deepCopy.setRollno(10);
|
||||
|
||||
assertEquals("John", originalStudent.getName());
|
||||
assertEquals(20, originalStudent.getRollno());
|
||||
|
||||
assertEquals("Baeldung", deepCopy.getName());
|
||||
assertEquals(10, deepCopy.getRollno());
|
||||
@Test
|
||||
public void whenModifyingActualObject_thenCopyNotChange() {
|
||||
School school = new School("Baeldung School");
|
||||
Student originalStudent = new Student("Alice", 30, school);
|
||||
Student deepCopy = new Student(originalStudent);
|
||||
school.setSchoolName("New Baeldung School");
|
||||
assertNotEquals( originalStudent.getSchool().getSchoolName(), deepCopy.getSchool().getSchoolName());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user