Program: 66242
Project: 66242
Buggy source:
@Test public void testSimpleCascadeDetached() throws SQLException {
  final JPAEnvironment env=getEnvironment();
  final EntityManager em=env.getEntityManager();
  try {
    CascadingNode parent=new CascadingNode(21,null);
    env.beginTransaction(em);
    em.persist(parent);
    env.commitTransactionAndClear(em);
    CascadingNode child=new CascadingNode(22,parent);
    child.setParent(null);
    env.beginTransaction(em);
    boolean persistFailed=false;
    boolean immediateException=false;
    try {
      em.persist(parent);
      verify(em.contains(parent),"String_Node_Str");
      verify(em.contains(child),"String_Node_Str");
    }
 catch (    IllegalArgumentException e) {
      persistFailed=true;
      immediateException=true;
    }
    if (!immediateException) {
      try {
        env.commitTransactionAndClear(em);
      }
 catch (      RuntimeException e) {
        if (!checkForPersistenceException(e)) {
          throw e;
        }
        persistFailed=true;
      }
    }
 else {
      env.rollbackTransactionAndClear(em);
    }
    verify(persistFailed,"String_Node_Str");
    verifyExistenceOnDatabase(parent.getId());
    verifyAbsenceFromDatabase(child.getId());
    CascadingNode existing=new CascadingNode(23,null);
    parent=new CascadingNode(existing.getId(),null);
    child=new CascadingNode(24,parent);
    child.setParent(null);
    env.beginTransaction(em);
    em.persist(existing);
    persistFailed=false;
    immediateException=false;
    try {
      em.persist(parent);
      verify(em.contains(parent),"String_Node_Str");
      verify(em.contains(child),"String_Node_Str");
    }
 catch (    IllegalArgumentException e) {
      persistFailed=true;
      immediateException=true;
    }
    verify(em.contains(existing),"String_Node_Str");
    if (!immediateException) {
      try {
        env.commitTransactionAndClear(em);
      }
 catch (      RuntimeException e) {
        if (!checkForPersistenceException(e)) {
          throw e;
        }
        persistFailed=true;
      }
    }
 else {
      env.rollbackTransactionAndClear(em);
    }
    verify(persistFailed,"String_Node_Str");
    verifyAbsenceFromDatabase(existing.getId());
    verifyAbsenceFromDatabase(child.getId());
    existing=new CascadingNode(25,null);
    parent=new CascadingNode(existing.getId(),null);
    child=new CascadingNode(26,parent);
    child.setParent(null);
    env.beginTransaction(em);
    em.persist(existing);
    env.commitTransactionAndClear(em);
    env.beginTransaction(em);
    existing=em.find(CascadingNode.class,new Integer(existing.getId()));
    persistFailed=false;
    immediateException=false;
    try {
      em.persist(parent);
      verify(em.contains(parent),"String_Node_Str");
      verify(em.contains(child),"String_Node_Str");
    }
 catch (    IllegalArgumentException e) {
      persistFailed=true;
      immediateException=true;
    }
    verify(em.contains(existing),"String_Node_Str");
    if (!immediateException) {
      try {
        env.commitTransactionAndClear(em);
      }
 catch (      RuntimeException e) {
        if (!checkForPersistenceException(e)) {
          throw e;
        }
        persistFailed=true;
      }
    }
 else {
      env.rollbackTransactionAndClear(em);
    }
    verify(persistFailed,"String_Node_Str");
    verifyExistenceOnDatabase(existing.getId());
    verifyAbsenceFromDatabase(child.getId());
    existing=new CascadingNode(27,null);
    parent=new CascadingNode(existing.getId(),null);
    child=new CascadingNode(28,parent);
    child.setParent(null);
    env.beginTransaction(em);
    em.persist(existing);
    env.commitTransactionAndClear(em);
    env.beginTransaction(em);
    existing=em.find(CascadingNode.class,new Integer(existing.getId()));
    em.remove(existing);
    persistFailed=false;
    immediateException=false;
    try {
      em.persist(parent);
      verify(em.contains(parent),"String_Node_Str");
      verify(em.contains(child),"String_Node_Str");
    }
 catch (    IllegalArgumentException e) {
      persistFailed=true;
      immediateException=true;
    }
    if (!immediateException) {
      try {
        env.commitTransactionAndClear(em);
      }
 catch (      RuntimeException e) {
        if (!checkForPersistenceException(e)) {
          throw e;
        }
        persistFailed=true;
      }
    }
 else {
      env.rollbackTransactionAndClear(em);
    }
    verify(persistFailed,"String_Node_Str");
    verifyExistenceOnDatabase(existing.getId());
    verifyAbsenceFromDatabase(child.getId());
  }
  finally {
    closeEntityManager(em);
  }
}

Fixed version (ground truth):
@Test public void testSimpleCascadeDetached() throws SQLException {
  final JPAEnvironment env=getEnvironment();
  final EntityManager em=env.getEntityManager();
  try {
    CascadingNode parent=new CascadingNode(21,null);
    env.beginTransaction(em);
    em.persist(parent);
    env.commitTransactionAndClear(em);
    CascadingNode child=new CascadingNode(22,parent);
    child.setParent(null);
    env.beginTransaction(em);
    boolean persistFailed=false;
    boolean immediateException=false;
    try {
      em.persist(parent);
      verify(em.contains(parent),"String_Node_Str");
      verify(em.contains(child),"String_Node_Str");
    }
 catch (    IllegalArgumentException e) {
      persistFailed=true;
      immediateException=true;
    }
    if (!immediateException) {
      try {
        env.commitTransactionAndClear(em);
      }
 catch (      RuntimeException e) {
        if (!checkForPersistenceException(e)) {
          throw e;
        }
        persistFailed=true;
      }
    }
 else {
      env.rollbackTransactionAndClear(em);
    }
    verify(persistFailed,"String_Node_Str");
    CascadingNode existing=new CascadingNode(23,null);
    parent=new CascadingNode(existing.getId(),null);
    child=new CascadingNode(24,parent);
    child.setParent(null);
    env.beginTransaction(em);
    em.persist(existing);
    persistFailed=false;
    immediateException=false;
    try {
      em.persist(parent);
      verify(em.contains(parent),"String_Node_Str");
      verify(em.contains(child),"String_Node_Str");
    }
 catch (    IllegalArgumentException e) {
      persistFailed=true;
      immediateException=true;
    }
    verify(em.contains(existing),"String_Node_Str");
    if (!immediateException) {
      try {
        env.commitTransactionAndClear(em);
      }
 catch (      RuntimeException e) {
        if (!checkForPersistenceException(e)) {
          throw e;
        }
        persistFailed=true;
      }
    }
 else {
      env.rollbackTransactionAndClear(em);
    }
    verify(persistFailed,"String_Node_Str");
    existing=new CascadingNode(25,null);
    parent=new CascadingNode(existing.getId(),null);
    child=new CascadingNode(26,parent);
    child.setParent(null);
    env.beginTransaction(em);
    em.persist(existing);
    env.commitTransactionAndClear(em);
    env.beginTransaction(em);
    existing=em.find(CascadingNode.class,new Integer(existing.getId()));
    persistFailed=false;
    immediateException=false;
    try {
      em.persist(parent);
      verify(em.contains(parent),"String_Node_Str");
      verify(em.contains(child),"String_Node_Str");
    }
 catch (    IllegalArgumentException e) {
      persistFailed=true;
      immediateException=true;
    }
    verify(em.contains(existing),"String_Node_Str");
    if (!immediateException) {
      try {
        env.commitTransactionAndClear(em);
      }
 catch (      RuntimeException e) {
        if (!checkForPersistenceException(e)) {
          throw e;
        }
        persistFailed=true;
      }
    }
 else {
      env.rollbackTransactionAndClear(em);
    }
    verify(persistFailed,"String_Node_Str");
    existing=new CascadingNode(27,null);
    parent=new CascadingNode(existing.getId(),null);
    child=new CascadingNode(28,parent);
    child.setParent(null);
    env.beginTransaction(em);
    em.persist(existing);
    env.commitTransactionAndClear(em);
    env.beginTransaction(em);
    existing=em.find(CascadingNode.class,new Integer(existing.getId()));
    em.remove(existing);
    persistFailed=false;
    immediateException=false;
    try {
      em.persist(parent);
      verify(em.contains(parent),"String_Node_Str");
      verify(em.contains(child),"String_Node_Str");
    }
 catch (    IllegalArgumentException e) {
      persistFailed=true;
      immediateException=true;
    }
    if (!immediateException) {
      try {
        env.commitTransactionAndClear(em);
      }
 catch (      RuntimeException e) {
        if (!checkForPersistenceException(e)) {
          throw e;
        }
        persistFailed=true;
      }
    }
 else {
      env.rollbackTransactionAndClear(em);
    }
    verify(persistFailed,"String_Node_Str");
  }
  finally {
    closeEntityManager(em);
  }
}