Program: 63370
Project: 63370
Buggy source:
/** 
 * This test checks a special case that can occur with lazily loaded relationships: <ul> <li>Read Project proj1 and remove it.</li> <li>Read Employee emp1 with relationship to proj1 (lazy loading).</li> <li>Assign the set of emp1's projects to a new employee emp2 (forces implicit loading on flush).</li> <li>Flush -> IllegalStateException expected because of relation emp2 -> proj1 (removed).</li> </ul>
 */
@Test @Bugzilla(bugid=317760) public void testRelationshipToRemovedLazy(){
  JPAEnvironment env=getEnvironment();
  EntityManager em=env.getEntityManager();
  try {
    Department dep=new Department(201,"String_Node_Str");
    Employee emp1=new Employee(202,"String_Node_Str","String_Node_Str",dep);
    Project proj=new Project("String_Node_Str");
    Set<Project> emp1Projects=new HashSet<Project>();
    emp1Projects.add(proj);
    emp1.setProjects(emp1Projects);
    Set<Employee> projEmployees=new HashSet<Employee>();
    projEmployees.add(emp1);
    proj.setEmployees(projEmployees);
    env.beginTransaction(em);
    em.persist(proj);
    em.persist(dep);
    em.persist(emp1);
    env.commitTransactionAndClear(em);
    env.beginTransaction(em);
    dep=em.find(Department.class,new Integer(dep.getId()));
    proj=em.find(Project.class,proj.getId());
    em.remove(proj);
    emp1=em.find(Employee.class,new Integer(emp1.getId()));
    Employee emp2=new Employee(203,"String_Node_Str","String_Node_Str",dep);
    proj.addEmployee(emp2);
    emp2.setProjects(emp1.getProjects());
    em.persist(emp2);
    boolean flushFailed=false;
    try {
      em.flush();
    }
 catch (    IllegalStateException e) {
      flushFailed=true;
      verify(env.isTransactionMarkedForRollback(em),"String_Node_Str");
      env.rollbackTransactionAndClear(em);
    }
    try {
      if (env.isTransactionActive(em)) {
        env.commitTransactionAndClear(em);
      }
    }
 catch (    RuntimeException e) {
      if (!checkForIllegalStateException(e)) {
        throw e;
      }
      flushFailed=true;
    }
    verify(!env.isTransactionActive(em),"String_Node_Str");
    verify(flushFailed,"String_Node_Str");
    em.clear();
    dep=new Department(204,"String_Node_Str");
    emp1=new Employee(205,"String_Node_Str","String_Node_Str",dep);
    proj=new Project("String_Node_Str");
    emp1Projects=new HashSet<Project>();
    emp1Projects.add(proj);
    emp1.setProjects(emp1Projects);
    projEmployees=new HashSet<Employee>();
    projEmployees.add(emp1);
    proj.setEmployees(projEmployees);
    env.beginTransaction(em);
    em.persist(proj);
    em.persist(dep);
    em.persist(emp1);
    env.commitTransactionAndClear(em);
    env.beginTransaction(em);
    dep=em.find(Department.class,new Integer(dep.getId()));
    proj=em.find(Project.class,proj.getId());
    em.remove(proj);
    emp1=em.find(Employee.class,new Integer(emp1.getId()));
    emp2=new Employee(206,"String_Node_Str","String_Node_Str",dep);
    emp2.setProjects(emp1.getProjects());
    em.persist(emp2);
    flushFailed=false;
    try {
      env.commitTransactionAndClear(em);
    }
 catch (    RuntimeException e) {
      if (!checkForIllegalStateException(e)) {
        throw e;
      }
      flushFailed=true;
    }
    verify(!env.isTransactionActive(em),"String_Node_Str");
    verify(flushFailed,"String_Node_Str");
    em.clear();
  }
  finally {
    closeEntityManager(em);
  }
}

Fixed version (ground truth):
/** 
 * This test checks a special case that can occur with lazily loaded relationships: <ul> <li>Read Project proj1 and remove it.</li> <li>Read Employee emp1 with relationship to proj1 (lazy loading).</li> <li>Assign the set of emp1's projects to a new employee emp2 (forces implicit loading on flush).</li> <li>Flush -> IllegalStateException expected because of relation emp2 -> proj1 (removed).</li> </ul>
 */
@Test @Bugzilla(bugid=311760) public void testRelationshipToRemovedLazy(){
  JPAEnvironment env=getEnvironment();
  EntityManager em=env.getEntityManager();
  try {
    Department dep=new Department(201,"String_Node_Str");
    Employee emp1=new Employee(202,"String_Node_Str","String_Node_Str",dep);
    Project proj=new Project("String_Node_Str");
    Set<Project> emp1Projects=new HashSet<Project>();
    emp1Projects.add(proj);
    emp1.setProjects(emp1Projects);
    Set<Employee> projEmployees=new HashSet<Employee>();
    projEmployees.add(emp1);
    proj.setEmployees(projEmployees);
    env.beginTransaction(em);
    em.persist(proj);
    em.persist(dep);
    em.persist(emp1);
    env.commitTransactionAndClear(em);
    env.beginTransaction(em);
    dep=em.find(Department.class,new Integer(dep.getId()));
    proj=em.find(Project.class,proj.getId());
    em.remove(proj);
    emp1=em.find(Employee.class,new Integer(emp1.getId()));
    Employee emp2=new Employee(203,"String_Node_Str","String_Node_Str",dep);
    proj.addEmployee(emp2);
    emp2.setProjects(emp1.getProjects());
    em.persist(emp2);
    boolean flushFailed=false;
    try {
      em.flush();
    }
 catch (    IllegalStateException e) {
      flushFailed=true;
      verify(env.isTransactionMarkedForRollback(em),"String_Node_Str");
      env.rollbackTransactionAndClear(em);
    }
    try {
      if (env.isTransactionActive(em)) {
        env.commitTransactionAndClear(em);
      }
    }
 catch (    RuntimeException e) {
      if (!checkForIllegalStateException(e)) {
        throw e;
      }
      flushFailed=true;
    }
    verify(!env.isTransactionActive(em),"String_Node_Str");
    verify(flushFailed,"String_Node_Str");
    em.clear();
    dep=new Department(204,"String_Node_Str");
    emp1=new Employee(205,"String_Node_Str","String_Node_Str",dep);
    proj=new Project("String_Node_Str");
    emp1Projects=new HashSet<Project>();
    emp1Projects.add(proj);
    emp1.setProjects(emp1Projects);
    projEmployees=new HashSet<Employee>();
    projEmployees.add(emp1);
    proj.setEmployees(projEmployees);
    env.beginTransaction(em);
    em.persist(proj);
    em.persist(dep);
    em.persist(emp1);
    env.commitTransactionAndClear(em);
    env.beginTransaction(em);
    dep=em.find(Department.class,new Integer(dep.getId()));
    proj=em.find(Project.class,proj.getId());
    em.remove(proj);
    emp1=em.find(Employee.class,new Integer(emp1.getId()));
    emp2=new Employee(206,"String_Node_Str","String_Node_Str",dep);
    emp2.setProjects(emp1.getProjects());
    em.persist(emp2);
    flushFailed=false;
    try {
      env.commitTransactionAndClear(em);
    }
 catch (    RuntimeException e) {
      if (!checkForIllegalStateException(e)) {
        throw e;
      }
      flushFailed=true;
    }
    verify(!env.isTransactionActive(em),"String_Node_Str");
    verify(flushFailed,"String_Node_Str");
    em.clear();
  }
  finally {
    closeEntityManager(em);
  }
}