public class StudentTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void testShallowClone() throws Exception {
String name = "Robert C. Martin";
int age = 40;
Student student = new Student(name, age);
Student studentCopy = (Student) student.clone();
studentCopy.studentName = "Kent Benk";
studentCopy.studentAge = 55;
assertEquals(student.studentName, "Robert C. Martin");
assertEquals(student.studentAge, 40);
}
@Test
public void testDeepClone() throws CloneNotSupportedException {
String manName = "Robert C. Martin";
int manAge = 40;
String teacherName = "T1";
int teacherAge = 50;
Teacher teacher = new Teacher(teacherName, teacherAge);
Man man = new Man(manName, manAge, teacher);
Man replication = (Man)man.clone();
replication.teacher.teacherName = "T2";
replication.teacher.teacherAge = 60;
assertEquals(man.teacher.teacherName, "T1"); //original teacher's properties don't change
assertEquals(man.teacher.teacherAge, 50);
}
@Test
public void testSerialDeepClone() throws IOException, ClassNotFoundException {
String manName = "Robert C. Martin";
int manAge = 40;
int teacherAge = 50;
String teacherName = "T1";
Teacher teacher = new Teacher(teacherName, teacherAge);
Man man = new Man(manName, manAge, teacher);
Man replication = (Man) man.serialClone();
replication.teacher.teacherName = "T2";
replication.teacher.teacherAge = 60;
assertEquals(man.teacher.teacherName, "T1"); //original teacher's properties don't change
assertEquals(man.teacher.teacherAge, 50);
}
|