Modified code for Test

This commit is contained in:
Neetika KHANDELWAL 2023-09-22 23:06:36 +05:30
parent 601d051f32
commit fc07da9eda
3 changed files with 74 additions and 34 deletions

View File

@ -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());
}
}

View File

@ -1,16 +1,27 @@
package com.baeldung.shallowdeepcopy; package com.baeldung.shallowdeepcopy;
class Student { class Student {
private String name; private String name;
private int age; private int age;
private School school; private School school;
public Student(String name, int age, School school) { public Student(String name, int age, School school) {
this.name = name; this.name = name;
this.rollno = rollno; this.rollno = rollno;
this.school = school; this.school = school;
} }
public Student(Student st) { public Student(Student st) {
this(st.getName(), st.getAge(), st.getSchool()); this(st.getName(), st.getAge(), st.getSchool());
} // 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;
}

View File

@ -5,30 +5,37 @@ import org.junit.jupiter.api.Test;
public class ShallowDeepCopyExampleTest { public class ShallowDeepCopyExampleTest {
@Test
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);
}
@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 @Test
void testShallowCopy() { public void whenModifyingActualObject_thenCloneCopyNotChange() {
Student originalStudent = new Student("John", 20); School school = new School("New School");
Student shallowCopy = originalStudent; Student originalStudent = new Student("Alice", 10, school);
Student deepCopy = (Student) originalStudent.clone();
shallowCopy.setName("Baeldung"); school.setSchoolName("New Baeldung School");
shallowCopy.setRollno(10); assertNotEquals(deepCopy.getSchool().getSchoolName(), originalStudent.getSchool().getSchoolName());
}
assertEquals("Baeldung", originalStudent.getName());
assertEquals(10, originalStudent.getRollno());
}
@Test @Test
void testDeepCopy() { public void whenModifyingActualObject_thenCopyNotChange() {
Student originalStudent = new Student("John", 20); School school = new School("Baeldung School");
Student deepCopy = new Student(originalStudent.getName(), originalStudent.getRollno()); Student originalStudent = new Student("Alice", 30, school);
Student deepCopy = new Student(originalStudent);
deepCopy.setName("Baeldung"); school.setSchoolName("New Baeldung School");
deepCopy.setRollno(10); assertNotEquals( originalStudent.getSchool().getSchoolName(), deepCopy.getSchool().getSchoolName());
assertEquals("John", originalStudent.getName());
assertEquals(20, originalStudent.getRollno());
assertEquals("Baeldung", deepCopy.getName());
assertEquals(10, deepCopy.getRollno());
} }
} }