Program: 39234
Project: 39234
Buggy source:
@Test public final void shouldCopyEntityWithoutInfinityCycleWith4Entities(){
  SampleSimpleDatabaseObject simpleDatabaseObject1=new SampleSimpleDatabaseObject();
  simpleDatabaseObject1.setId(1L);
  simpleDatabaseObject1.setName(SIMPLE_1);
  SampleSimpleDatabaseObject simpleDatabaseObject2=new SampleSimpleDatabaseObject();
  simpleDatabaseObject2.setId(2L);
  simpleDatabaseObject2.setName(SIMPLE_2);
  SampleSimpleDatabaseObject simpleDatabaseObject3=new SampleSimpleDatabaseObject();
  simpleDatabaseObject3.setId(3L);
  simpleDatabaseObject3.setName(SIMPLE_3);
  SampleSimpleDatabaseObject simpleDatabaseObject4=new SampleSimpleDatabaseObject();
  simpleDatabaseObject4.setId(4L);
  simpleDatabaseObject4.setName(SIMPLE_4);
  simpleDatabaseObject1.setBelongsToSimple(simpleDatabaseObject2);
  simpleDatabaseObject2.setBelongsToSimple(simpleDatabaseObject3);
  simpleDatabaseObject3.setBelongsToSimple(simpleDatabaseObject4);
  simpleDatabaseObject4.setBelongsToSimple(simpleDatabaseObject1);
  stubSessionGet(simpleDatabaseObject1);
  stubSessionGet(simpleDatabaseObject2);
  stubSessionGet(simpleDatabaseObject3);
  stubSessionGet(simpleDatabaseObject4);
  List<Entity> entities=null;
  try {
    entities=dataDefinition.copy(new Long[]{simpleDatabaseObject1.getId()});
  }
 catch (  StackOverflowError e) {
    Assert.fail();
  }
  final ArgumentCaptor<SampleSimpleDatabaseObject> argCaptor=ArgumentCaptor.forClass(SampleSimpleDatabaseObject.class);
  verify(session,times(1)).save(Mockito.any(SampleSimpleDatabaseObject.class));
  verify(session,times(1)).save(argCaptor.capture());
  final SampleSimpleDatabaseObject savedObject=argCaptor.getValue();
  assertEquals(SIMPLE_1,savedObject.getName());
  assertFalse(simpleDatabaseObject1.getId().equals(savedObject.getId()));
  assertEquals(SIMPLE_2,savedObject.getBelongsToSimple().getName());
  assertEquals(simpleDatabaseObject2.getId(),savedObject.getBelongsToSimple().getId());
  assertEquals(SIMPLE_3,savedObject.getBelongsToSimple().getBelongsToSimple().getName());
  assertEquals(simpleDatabaseObject3.getId(),savedObject.getBelongsToSimple().getBelongsToSimple().getId());
  assertEquals(SIMPLE_4,savedObject.getBelongsToSimple().getBelongsToSimple().getBelongsToSimple().getName());
  assertEquals(simpleDatabaseObject4.getId(),savedObject.getBelongsToSimple().getBelongsToSimple().getBelongsToSimple().getId());
  assertEquals(1,entities.size());
  assertTrue(entities.get(0).isValid());
  final Entity entityCopy=entities.get(0);
  Assert.assertEquals(SIMPLE_1,entityCopy.getField(NAME));
  Assert.assertEquals(SIMPLE_2,entityCopy.getBelongsToField(BELONGS_TO_SIMPLE).getField(NAME));
  Assert.assertEquals(SIMPLE_3,entityCopy.getBelongsToField(BELONGS_TO_SIMPLE).getBelongsToField(BELONGS_TO_SIMPLE).getField(NAME));
  Assert.assertEquals(SIMPLE_4,entityCopy.getBelongsToField(BELONGS_TO_SIMPLE).getBelongsToField(BELONGS_TO_SIMPLE).getBelongsToField(BELONGS_TO_SIMPLE).getField(NAME));
  Assert.assertEquals(Long.valueOf(2L),entityCopy.getBelongsToField(BELONGS_TO_SIMPLE).getId());
  assertFalse(simpleDatabaseObject1.getId().equals(entityCopy.getId()));
}

Fixed version (ground truth):
@Test public void shouldCopyEntityWithoutInfinityCycleWith4Entities(){
  SampleSimpleDatabaseObject simpleDatabaseObject1=new SampleSimpleDatabaseObject();
  simpleDatabaseObject1.setId(1L);
  simpleDatabaseObject1.setName(SIMPLE_1);
  SampleSimpleDatabaseObject simpleDatabaseObject2=new SampleSimpleDatabaseObject();
  simpleDatabaseObject2.setId(2L);
  simpleDatabaseObject2.setName(SIMPLE_2);
  SampleSimpleDatabaseObject simpleDatabaseObject3=new SampleSimpleDatabaseObject();
  simpleDatabaseObject3.setId(3L);
  simpleDatabaseObject3.setName(SIMPLE_3);
  SampleSimpleDatabaseObject simpleDatabaseObject4=new SampleSimpleDatabaseObject();
  simpleDatabaseObject4.setId(4L);
  simpleDatabaseObject4.setName(SIMPLE_4);
  simpleDatabaseObject1.setBelongsToSimple(simpleDatabaseObject2);
  simpleDatabaseObject2.setBelongsToSimple(simpleDatabaseObject3);
  simpleDatabaseObject3.setBelongsToSimple(simpleDatabaseObject4);
  simpleDatabaseObject4.setBelongsToSimple(simpleDatabaseObject1);
  stubSessionGet(simpleDatabaseObject1);
  stubSessionGet(simpleDatabaseObject2);
  stubSessionGet(simpleDatabaseObject3);
  stubSessionGet(simpleDatabaseObject4);
  List<Entity> entities=null;
  try {
    entities=dataDefinition.copy(new Long[]{simpleDatabaseObject1.getId()});
  }
 catch (  StackOverflowError e) {
    Assert.fail();
  }
  final ArgumentCaptor<SampleSimpleDatabaseObject> argCaptor=ArgumentCaptor.forClass(SampleSimpleDatabaseObject.class);
  verify(session,times(1)).save(Mockito.any(SampleSimpleDatabaseObject.class));
  verify(session,times(1)).save(argCaptor.capture());
  final SampleSimpleDatabaseObject savedObject=argCaptor.getValue();
  assertEquals(SIMPLE_1,savedObject.getName());
  assertFalse(simpleDatabaseObject1.getId().equals(savedObject.getId()));
  assertEquals(SIMPLE_2,savedObject.getBelongsToSimple().getName());
  assertEquals(simpleDatabaseObject2.getId(),savedObject.getBelongsToSimple().getId());
  assertEquals(SIMPLE_3,savedObject.getBelongsToSimple().getBelongsToSimple().getName());
  assertEquals(simpleDatabaseObject3.getId(),savedObject.getBelongsToSimple().getBelongsToSimple().getId());
  assertEquals(SIMPLE_4,savedObject.getBelongsToSimple().getBelongsToSimple().getBelongsToSimple().getName());
  assertEquals(simpleDatabaseObject4.getId(),savedObject.getBelongsToSimple().getBelongsToSimple().getBelongsToSimple().getId());
  assertEquals(1,entities.size());
  assertTrue(entities.get(0).isValid());
  final Entity entityCopy=entities.get(0);
  Assert.assertEquals(SIMPLE_1,entityCopy.getField(NAME));
  Assert.assertEquals(SIMPLE_2,entityCopy.getBelongsToField(BELONGS_TO_SIMPLE).getField(NAME));
  Assert.assertEquals(SIMPLE_3,entityCopy.getBelongsToField(BELONGS_TO_SIMPLE).getBelongsToField(BELONGS_TO_SIMPLE).getField(NAME));
  Assert.assertEquals(SIMPLE_4,entityCopy.getBelongsToField(BELONGS_TO_SIMPLE).getBelongsToField(BELONGS_TO_SIMPLE).getBelongsToField(BELONGS_TO_SIMPLE).getField(NAME));
  Assert.assertEquals(Long.valueOf(2L),entityCopy.getBelongsToField(BELONGS_TO_SIMPLE).getId());
  assertFalse(simpleDatabaseObject1.getId().equals(entityCopy.getId()));
}