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;
class Student {
private String name;
private int age;
private School school;
public Student(String name, int age, School school) {
this.name = name;
this.rollno = rollno;
this.school = school;
}
public Student(Student st) {
this(st.getName(), st.getAge(), st.getSchool());
} // standard getters and setters
private String name;
private int age;
private School school;
public Student(String name, int age, School school) {
this.name = name;
this.rollno = rollno;
this.school = school;
}
public Student(Student st) {
this(st.getName(), st.getAge(), st.getSchool());
} // 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

@ -4,31 +4,38 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
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
void testShallowCopy() {
Student originalStudent = new Student("John", 20);
Student shallowCopy = originalStudent;
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());
}
shallowCopy.setName("Baeldung");
shallowCopy.setRollno(10);
assertEquals("Baeldung", originalStudent.getName());
assertEquals(10, originalStudent.getRollno());
}
@Test
void testDeepCopy() {
Student originalStudent = new Student("John", 20);
Student deepCopy = new Student(originalStudent.getName(), originalStudent.getRollno());
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());
}
}