Program: 66782
Project: 66782
Buggy source:
public void testPESSIMISTIC_ExtendedScope(){
  ServerSession session=JUnitTestCase.getServerSession();
  if (isOnServer() || !isSelectForUpateSupported()) {
    return;
  }
  ServerSession ss=((EntityManagerFactoryImpl)getEntityManagerFactory()).getServerSession();
  boolean shouldSpawnThread=!isSelectForUpateNoWaitSupported();
  long timeToWait=1000;
  String errorMsg="String_Node_Str";
  LockModeType lockMode=LockModeType.PESSIMISTIC_WRITE;
  Employee emp=new Employee();
  emp.setFirstName("String_Node_Str");
  emp.setLastName("String_Node_Str");
  emp.addResponsibility("String_Node_Str");
  emp.addResponsibility("String_Node_Str");
  SmallProject smallProject=new SmallProject();
  smallProject.setName("String_Node_Str");
  emp.addProject(smallProject);
  LargeProject largeProject=new LargeProject();
  largeProject.setName("String_Node_Str");
  largeProject.setBudget(5000);
  emp.addProject(largeProject);
  EntityManager em=createEntityManager();
  try {
    beginTransaction(em);
    em.persist(emp);
    commitTransaction(em);
  }
  finally {
    if (isTransactionActive(em)) {
      rollbackTransaction(em);
    }
    closeEntityManager(em);
  }
  int id=emp.getId();
  int smallProjId=smallProject.getId();
  clearCache();
  Map<String,Object> properties=new HashMap();
  properties.put(QueryHints.PESSIMISTIC_LOCK_SCOPE,PessimisticLockScope.EXTENDED);
  String forUpdateClause=session.getPlatform().getSelectForUpdateString();
  if (isSelectForUpateNoWaitSupported()) {
    properties.put(QueryHints.PESSIMISTIC_LOCK_TIMEOUT,0);
    forUpdateClause=session.getPlatform().getSelectForUpdateNoWaitString();
  }
  String lockingClauseAfterWhereClause="String_Node_Str";
  String lockingClauseBeforeWhereClause="String_Node_Str";
  if (session.getPlatform().shouldPrintLockingClauseAfterWhereClause()) {
    lockingClauseAfterWhereClause=forUpdateClause;
  }
 else {
    lockingClauseBeforeWhereClause=forUpdateClause;
  }
  boolean[] isObjectCached={false,true};
  String[] testModeArray1={"String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str"};
  String[] testModeArray2={"String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str"};
  for (int i=0; i < testModeArray1.length; i++) {
    String testMode1=testModeArray1[i];
    for (int k=0; k < isObjectCached.length; k++) {
      boolean isObjCached=isObjectCached[k];
      for (int j=0; j < testModeArray2.length; j++) {
        String testMode2=testModeArray2[j];
        boolean isExceptionExpected=!testMode2.equals("String_Node_Str");
        EntityManager em1=createEntityManager();
        if (isObjCached) {
          ss.log(SessionLog.FINEST,SessionLog.QUERY,"String_Node_Str",(Object[])null,null,false);
          em1.find(Employee.class,id);
        }
        Employee emp1;
        try {
          beginTransaction(em1);
          ss.log(SessionLog.FINEST,SessionLog.QUERY,"String_Node_Str" + testMode1,(Object[])null,null,false);
          if (testMode1.equals("String_Node_Str")) {
            Query query1=em1.createQuery("String_Node_Str" + id).setLockMode(lockMode).setHint(QueryHints.PESSIMISTIC_LOCK_SCOPE,PessimisticLockScope.EXTENDED);
            if (isSelectForUpateNoWaitSupported()) {
              query1.setHint(QueryHints.PESSIMISTIC_LOCK_TIMEOUT,0);
            }
            emp1=(Employee)query1.getSingleResult();
          }
 else           if (testMode1.equals("String_Node_Str")) {
            emp1=em1.find(Employee.class,id,lockMode,properties);
          }
 else {
            emp1=em1.find(Employee.class,id);
            if (testMode1.equals("String_Node_Str")) {
              em1.lock(emp1,lockMode,properties);
            }
 else             if (testMode1.equals("String_Node_Str")) {
              em1.refresh(emp1,lockMode,properties);
            }
 else {
              fail("String_Node_Str" + testMode1);
            }
          }
          TransactionKiller transactionKiller=null;
          EntityManager em2=createEntityManager();
          Employee emp2;
          try {
            beginTransaction(em2);
            ss.log(SessionLog.FINEST,SessionLog.QUERY,"String_Node_Str" + testMode2,(Object[])null,null,false);
            if (shouldSpawnThread) {
              transactionKiller=new TransactionKiller(em1,timeToWait);
              transactionKiller.start();
            }
            if (testMode2.equals("String_Node_Str")) {
              Query query2=em2.createQuery("String_Node_Str" + id).setLockMode(lockMode).setHint(QueryHints.PESSIMISTIC_LOCK_SCOPE,PessimisticLockScope.EXTENDED);
              if (isSelectForUpateNoWaitSupported()) {
                query2.setHint(QueryHints.PESSIMISTIC_LOCK_TIMEOUT,0);
              }
              emp2=(Employee)query2.getSingleResult();
            }
 else             if (testMode2.equals("String_Node_Str")) {
              emp2=em2.find(Employee.class,id,lockMode,properties);
            }
 else             if (testMode2.equals("String_Node_Str")) {
              em2.createNativeQuery("String_Node_Str" + lockingClauseBeforeWhereClause + "String_Node_Str"+ id+ lockingClauseAfterWhereClause).getSingleResult();
            }
 else             if (testMode2.equals("String_Node_Str")) {
              em2.createNativeQuery("String_Node_Str" + lockingClauseBeforeWhereClause + "String_Node_Str"+ id+ lockingClauseAfterWhereClause).getSingleResult();
            }
 else             if (testMode2.equals("String_Node_Str")) {
              em2.createNativeQuery("String_Node_Str" + lockingClauseBeforeWhereClause + "String_Node_Str"+ id+ lockingClauseAfterWhereClause).getResultList();
            }
 else             if (testMode2.equals("String_Node_Str")) {
              em2.createNativeQuery("String_Node_Str" + lockingClauseBeforeWhereClause + "String_Node_Str"+ id+ lockingClauseAfterWhereClause).getResultList();
            }
 else             if (testMode2.equals("String_Node_Str")) {
              em2.createNativeQuery("String_Node_Str" + lockingClauseBeforeWhereClause + "String_Node_Str"+ smallProjId+ lockingClauseAfterWhereClause).getSingleResult();
            }
 else             if (testMode2.equals("String_Node_Str")) {
              em2.createNativeQuery("String_Node_Str" + lockingClauseBeforeWhereClause + "String_Node_Str"+ id+ lockingClauseAfterWhereClause).getResultList();
            }
 else {
              emp2=em2.find(Employee.class,id);
              if (testMode2.equals("String_Node_Str")) {
                em2.lock(emp2,lockMode,properties);
              }
 else               if (testMode2.equals("String_Node_Str")) {
                em2.refresh(emp2,lockMode,properties);
              }
 else {
                fail("String_Node_Str" + testMode2);
              }
            }
            boolean hasKilledTransaction=false;
            if (transactionKiller != null) {
              transactionKiller.shouldKillTransaction=false;
              try {
                transactionKiller.join();
              }
 catch (              InterruptedException intEx) {
              }
              hasKilledTransaction=transactionKiller.hasKilledTransaction;
            }
            if (isExceptionExpected && !hasKilledTransaction) {
              String localErrorMsg=testMode1 + (isObjCached ? "String_Node_Str" : "String_Node_Str") + testMode2+ "String_Node_Str";
              ss.log(SessionLog.FINEST,SessionLog.QUERY,localErrorMsg,(Object[])null,null,false);
              errorMsg+='\n' + localErrorMsg;
            }
          }
 catch (          Exception ex) {
            if (transactionKiller != null) {
              transactionKiller.shouldKillTransaction=false;
              try {
                transactionKiller.join();
              }
 catch (              InterruptedException intEx) {
              }
            }
            if (!isExceptionExpected) {
              String localErrorMsg=testMode1 + (isObjCached ? "String_Node_Str" : "String_Node_Str") + testMode2+ "String_Node_Str"+ ex.getMessage();
              ss.log(SessionLog.FINEST,SessionLog.QUERY,localErrorMsg,(Object[])null,null,false);
              errorMsg+='\n' + localErrorMsg;
            }
          }
 finally {
            if (isTransactionActive(em2)) {
              rollbackTransaction(em2);
            }
            closeEntityManager(em2);
          }
        }
  finally {
          if (isTransactionActive(em1)) {
            rollbackTransaction(em1);
          }
          closeEntityManager(em1);
        }
        clearCache();
      }
    }
  }
  em=createEntityManager();
  emp=em.find(Employee.class,id);
  try {
    beginTransaction(em);
    Iterator<Project> it=emp.getProjects().iterator();
    while (it.hasNext()) {
      Project project=it.next();
      it.remove();
      em.remove(project);
    }
    em.remove(emp);
    commitTransaction(em);
  }
  finally {
    if (isTransactionActive(em)) {
      rollbackTransaction(em);
    }
    closeEntityManager(em);
  }
  if (errorMsg.length() > 0) {
    fail(errorMsg);
  }
}

Fixed version (ground truth):
public void testPESSIMISTIC_ExtendedScope(){
  ServerSession session=JUnitTestCase.getServerSession();
  if (isOnServer() || !isSelectForUpateSupported() || !isPessimisticWriteLockSupported()) {
    return;
  }
  ServerSession ss=((EntityManagerFactoryImpl)getEntityManagerFactory()).getServerSession();
  boolean shouldSpawnThread=!isSelectForUpateNoWaitSupported();
  long timeToWait=1000;
  String errorMsg="String_Node_Str";
  LockModeType lockMode=LockModeType.PESSIMISTIC_WRITE;
  Employee emp=new Employee();
  emp.setFirstName("String_Node_Str");
  emp.setLastName("String_Node_Str");
  emp.addResponsibility("String_Node_Str");
  emp.addResponsibility("String_Node_Str");
  SmallProject smallProject=new SmallProject();
  smallProject.setName("String_Node_Str");
  emp.addProject(smallProject);
  LargeProject largeProject=new LargeProject();
  largeProject.setName("String_Node_Str");
  largeProject.setBudget(5000);
  emp.addProject(largeProject);
  EntityManager em=createEntityManager();
  try {
    beginTransaction(em);
    em.persist(emp);
    commitTransaction(em);
  }
  finally {
    if (isTransactionActive(em)) {
      rollbackTransaction(em);
    }
    closeEntityManager(em);
  }
  int id=emp.getId();
  int smallProjId=smallProject.getId();
  clearCache();
  Map<String,Object> properties=new HashMap();
  properties.put(QueryHints.PESSIMISTIC_LOCK_SCOPE,PessimisticLockScope.EXTENDED);
  String forUpdateClause=session.getPlatform().getSelectForUpdateString();
  if (isSelectForUpateNoWaitSupported()) {
    properties.put(QueryHints.PESSIMISTIC_LOCK_TIMEOUT,0);
    forUpdateClause=session.getPlatform().getSelectForUpdateNoWaitString();
  }
  String lockingClauseAfterWhereClause="String_Node_Str";
  String lockingClauseBeforeWhereClause="String_Node_Str";
  if (session.getPlatform().shouldPrintLockingClauseAfterWhereClause()) {
    lockingClauseAfterWhereClause=forUpdateClause;
  }
 else {
    lockingClauseBeforeWhereClause=forUpdateClause;
  }
  boolean[] isObjectCached={false,true};
  String[] testModeArray1={"String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str"};
  String[] testModeArray2={"String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str","String_Node_Str"};
  for (int i=0; i < testModeArray1.length; i++) {
    String testMode1=testModeArray1[i];
    for (int k=0; k < isObjectCached.length; k++) {
      boolean isObjCached=isObjectCached[k];
      for (int j=0; j < testModeArray2.length; j++) {
        String testMode2=testModeArray2[j];
        boolean isExceptionExpected=!testMode2.equals("String_Node_Str");
        EntityManager em1=createEntityManager();
        if (isObjCached) {
          ss.log(SessionLog.FINEST,SessionLog.QUERY,"String_Node_Str",(Object[])null,null,false);
          em1.find(Employee.class,id);
        }
        Employee emp1;
        try {
          beginTransaction(em1);
          ss.log(SessionLog.FINEST,SessionLog.QUERY,"String_Node_Str" + testMode1,(Object[])null,null,false);
          if (testMode1.equals("String_Node_Str")) {
            Query query1=em1.createQuery("String_Node_Str" + id).setLockMode(lockMode).setHint(QueryHints.PESSIMISTIC_LOCK_SCOPE,PessimisticLockScope.EXTENDED);
            if (isSelectForUpateNoWaitSupported()) {
              query1.setHint(QueryHints.PESSIMISTIC_LOCK_TIMEOUT,0);
            }
            emp1=(Employee)query1.getSingleResult();
          }
 else           if (testMode1.equals("String_Node_Str")) {
            emp1=em1.find(Employee.class,id,lockMode,properties);
          }
 else {
            emp1=em1.find(Employee.class,id);
            if (testMode1.equals("String_Node_Str")) {
              em1.lock(emp1,lockMode,properties);
            }
 else             if (testMode1.equals("String_Node_Str")) {
              em1.refresh(emp1,lockMode,properties);
            }
 else {
              fail("String_Node_Str" + testMode1);
            }
          }
          TransactionKiller transactionKiller=null;
          EntityManager em2=createEntityManager();
          Employee emp2;
          try {
            beginTransaction(em2);
            ss.log(SessionLog.FINEST,SessionLog.QUERY,"String_Node_Str" + testMode2,(Object[])null,null,false);
            if (shouldSpawnThread) {
              transactionKiller=new TransactionKiller(em1,timeToWait);
              transactionKiller.start();
            }
            if (testMode2.equals("String_Node_Str")) {
              Query query2=em2.createQuery("String_Node_Str" + id).setLockMode(lockMode).setHint(QueryHints.PESSIMISTIC_LOCK_SCOPE,PessimisticLockScope.EXTENDED);
              if (isSelectForUpateNoWaitSupported()) {
                query2.setHint(QueryHints.PESSIMISTIC_LOCK_TIMEOUT,0);
              }
              emp2=(Employee)query2.getSingleResult();
            }
 else             if (testMode2.equals("String_Node_Str")) {
              emp2=em2.find(Employee.class,id,lockMode,properties);
            }
 else             if (testMode2.equals("String_Node_Str")) {
              em2.createNativeQuery("String_Node_Str" + lockingClauseBeforeWhereClause + "String_Node_Str"+ id+ lockingClauseAfterWhereClause).getSingleResult();
            }
 else             if (testMode2.equals("String_Node_Str")) {
              em2.createNativeQuery("String_Node_Str" + lockingClauseBeforeWhereClause + "String_Node_Str"+ id+ lockingClauseAfterWhereClause).getSingleResult();
            }
 else             if (testMode2.equals("String_Node_Str")) {
              em2.createNativeQuery("String_Node_Str" + lockingClauseBeforeWhereClause + "String_Node_Str"+ id+ lockingClauseAfterWhereClause).getResultList();
            }
 else             if (testMode2.equals("String_Node_Str")) {
              em2.createNativeQuery("String_Node_Str" + lockingClauseBeforeWhereClause + "String_Node_Str"+ id+ lockingClauseAfterWhereClause).getResultList();
            }
 else             if (testMode2.equals("String_Node_Str")) {
              em2.createNativeQuery("String_Node_Str" + lockingClauseBeforeWhereClause + "String_Node_Str"+ smallProjId+ lockingClauseAfterWhereClause).getSingleResult();
            }
 else             if (testMode2.equals("String_Node_Str")) {
              em2.createNativeQuery("String_Node_Str" + lockingClauseBeforeWhereClause + "String_Node_Str"+ id+ lockingClauseAfterWhereClause).getResultList();
            }
 else {
              emp2=em2.find(Employee.class,id);
              if (testMode2.equals("String_Node_Str")) {
                em2.lock(emp2,lockMode,properties);
              }
 else               if (testMode2.equals("String_Node_Str")) {
                em2.refresh(emp2,lockMode,properties);
              }
 else {
                fail("String_Node_Str" + testMode2);
              }
            }
            boolean hasKilledTransaction=false;
            if (transactionKiller != null) {
              transactionKiller.shouldKillTransaction=false;
              try {
                transactionKiller.join();
              }
 catch (              InterruptedException intEx) {
              }
              hasKilledTransaction=transactionKiller.hasKilledTransaction;
            }
            if (isExceptionExpected && !hasKilledTransaction) {
              String localErrorMsg=testMode1 + (isObjCached ? "String_Node_Str" : "String_Node_Str") + testMode2+ "String_Node_Str";
              ss.log(SessionLog.FINEST,SessionLog.QUERY,localErrorMsg,(Object[])null,null,false);
              errorMsg+='\n' + localErrorMsg;
            }
          }
 catch (          Exception ex) {
            if (transactionKiller != null) {
              transactionKiller.shouldKillTransaction=false;
              try {
                transactionKiller.join();
              }
 catch (              InterruptedException intEx) {
              }
            }
            if (!isExceptionExpected) {
              String localErrorMsg=testMode1 + (isObjCached ? "String_Node_Str" : "String_Node_Str") + testMode2+ "String_Node_Str"+ ex.getMessage();
              ss.log(SessionLog.FINEST,SessionLog.QUERY,localErrorMsg,(Object[])null,null,false);
              errorMsg+='\n' + localErrorMsg;
            }
          }
 finally {
            if (isTransactionActive(em2)) {
              rollbackTransaction(em2);
            }
            closeEntityManager(em2);
          }
        }
  finally {
          if (isTransactionActive(em1)) {
            rollbackTransaction(em1);
          }
          closeEntityManager(em1);
        }
        clearCache();
      }
    }
  }
  em=createEntityManager();
  emp=em.find(Employee.class,id);
  try {
    beginTransaction(em);
    Iterator<Project> it=emp.getProjects().iterator();
    while (it.hasNext()) {
      Project project=it.next();
      it.remove();
      em.remove(project);
    }
    em.remove(emp);
    commitTransaction(em);
  }
  finally {
    if (isTransactionActive(em)) {
      rollbackTransaction(em);
    }
    closeEntityManager(em);
  }
  if (errorMsg.length() > 0) {
    fail(errorMsg);
  }
}