Program: 66271
Project: 66271
Buggy source:
public void testExpertBeerConsumerRecordsCRUD(){
  String errorMsg="String_Node_Str";
  int nRecords;
  int nRecordsExpected=2;
  clearCache();
  EntityManager em=createEntityManager();
  try {
    beginTransaction(em);
    ExpertBeerConsumer consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    nRecords=consumer.getRecords().size();
    commitTransaction(em);
    closeEntityManager(em);
    if (nRecords != nRecordsExpected) {
      errorMsg+="String_Node_Str";
    }
    em=createEntityManager();
    beginTransaction(em);
    consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    Record recordToRemove=consumer.getRecords().iterator().next();
    consumer.getRecords().remove(recordToRemove);
    commitTransaction(em);
    closeEntityManager(em);
    nRecordsExpected--;
    em=createEntityManager();
    consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    closeEntityManager(em);
    nRecords=consumer.getRecords().size();
    if (nRecords != nRecordsExpected) {
      errorMsg+="String_Node_Str";
    }
    clearCache();
    em=createEntityManager();
    consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    closeEntityManager(em);
    nRecords=consumer.getRecords().size();
    if (nRecords != nRecordsExpected) {
      errorMsg+="String_Node_Str";
    }
    Record record1=new Record();
    record1.setDescription("String_Node_Str");
    record1.setDate(Helper.dateFromYearMonthDate(2009,1,1));
    record1.setLocation(new Location("String_Node_Str","String_Node_Str"));
    Venue venue1=new Venue();
    venue1.setAttendance(10);
    venue1.setName("String_Node_Str");
    record1.setVenue(venue1);
    em=createEntityManager();
    beginTransaction(em);
    consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    consumer.getRecords().add(record1);
    commitTransaction(em);
    closeEntityManager(em);
    nRecordsExpected++;
    em=createEntityManager();
    consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    closeEntityManager(em);
    nRecords=consumer.getRecords().size();
    if (nRecords != nRecordsExpected) {
      errorMsg+="String_Node_Str";
    }
    clearCache();
    em=createEntityManager();
    consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    closeEntityManager(em);
    nRecords=consumer.getRecords().size();
    if (nRecords != nRecordsExpected) {
      errorMsg+="String_Node_Str";
    }
    String newDescription="String_Node_Str";
    String newName="String_Node_Str";
    em=createEntityManager();
    beginTransaction(em);
    consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    int i=0;
    Iterator<Record> it=consumer.getRecords().iterator();
    while (it.hasNext()) {
      Record record=it.next();
      String index=Integer.toString(i++);
      record.setDescription(newDescription + index);
      record.getVenue().setName(newName + index);
    }
    commitTransaction(em);
    em=createEntityManager();
    consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    closeEntityManager(em);
    nRecords=consumer.getRecords().size();
    if (nRecords != nRecordsExpected) {
      errorMsg+="String_Node_Str";
    }
    HashSet<String> usedDescriptions=new HashSet(nRecords);
    HashSet<String> usedNames=new HashSet(nRecords);
    it=consumer.getRecords().iterator();
    while (it.hasNext()) {
      Record record=it.next();
      String description=record.getDescription();
      if (!description.startsWith(newDescription)) {
        errorMsg+="String_Node_Str";
      }
      usedDescriptions.add(description);
      String name=record.getVenue().getName();
      if (!name.startsWith(newName)) {
        errorMsg+="String_Node_Str";
      }
      usedNames.add(name);
    }
    if (usedDescriptions.size() != nRecords) {
      errorMsg+="String_Node_Str";
    }
    if (usedNames.size() != nRecords) {
      errorMsg+="String_Node_Str";
    }
    clearCache();
    em=createEntityManager();
    consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    closeEntityManager(em);
    nRecords=consumer.getRecords().size();
    if (nRecords != nRecordsExpected) {
      errorMsg+="String_Node_Str";
    }
    usedDescriptions.clear();
    usedNames.clear();
    it=consumer.getRecords().iterator();
    while (it.hasNext()) {
      Record record=it.next();
      String description=record.getDescription();
      if (!description.startsWith(newDescription)) {
        errorMsg+="String_Node_Str";
      }
      usedDescriptions.add(description);
      String name=record.getVenue().getName();
      if (!name.startsWith(newName)) {
        errorMsg+="String_Node_Str";
      }
      usedNames.add(name);
    }
    if (usedDescriptions.size() != nRecords) {
      errorMsg+="String_Node_Str";
    }
    if (usedNames.size() != nRecords) {
      errorMsg+="String_Node_Str";
    }
    if (errorMsg.length() > 0) {
      fail(errorMsg);
    }
  }
  finally {
    if (em != null) {
      if (isTransactionActive(em)) {
        rollbackTransaction(em);
      }
      if (em.isOpen()) {
        closeEntityManager(em);
      }
    }
  }
}

Fixed version (ground truth):
public void testExpertBeerConsumerRecordsCRUD(){
  String errorMsg="String_Node_Str";
  int nRecords;
  int nRecordsExpected=2;
  clearCache();
  EntityManager em=createEntityManager();
  try {
    beginTransaction(em);
    ExpertBeerConsumer consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    nRecords=consumer.getRecords().size();
    commitTransaction(em);
    closeEntityManager(em);
    if (nRecords != nRecordsExpected) {
      errorMsg+="String_Node_Str";
    }
    em=createEntityManager();
    beginTransaction(em);
    consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    Record recordToRemove=consumer.getRecords().iterator().next();
    consumer.getRecords().remove(recordToRemove);
    commitTransaction(em);
    closeEntityManager(em);
    nRecordsExpected--;
    em=createEntityManager();
    consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    closeEntityManager(em);
    nRecords=consumer.getRecords().size();
    if (nRecords != nRecordsExpected) {
      errorMsg+="String_Node_Str";
    }
    clearCache();
    em=createEntityManager();
    consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    closeEntityManager(em);
    nRecords=consumer.getRecords().size();
    if (nRecords != nRecordsExpected) {
      errorMsg+="String_Node_Str";
    }
    Record record1=new Record();
    record1.setDescription("String_Node_Str");
    record1.setDate(Helper.dateFromYearMonthDate(2009,1,1));
    record1.setLocation(new Location("String_Node_Str","String_Node_Str"));
    Venue venue1=new Venue();
    venue1.setAttendance(10);
    venue1.setName("String_Node_Str");
    record1.setVenue(venue1);
    em=createEntityManager();
    beginTransaction(em);
    consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    consumer.getRecords().add(record1);
    commitTransaction(em);
    closeEntityManager(em);
    nRecordsExpected++;
    em=createEntityManager();
    consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    closeEntityManager(em);
    nRecords=consumer.getRecords().size();
    if (nRecords != nRecordsExpected) {
      errorMsg+="String_Node_Str";
    }
    clearCache();
    em=createEntityManager();
    consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    closeEntityManager(em);
    nRecords=consumer.getRecords().size();
    if (nRecords != nRecordsExpected) {
      errorMsg+="String_Node_Str";
    }
    String newDescription="String_Node_Str";
    String newName="String_Node_Str";
    em=createEntityManager();
    beginTransaction(em);
    consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    int i=0;
    Iterator<Record> it=consumer.getRecords().iterator();
    while (it.hasNext()) {
      Record record=it.next();
      String index=Integer.toString(i++);
      record.setDescription(newDescription + index);
      record.getVenue().setName(newName + index);
    }
    commitTransaction(em);
    em=createEntityManager();
    consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    closeEntityManager(em);
    nRecords=consumer.getRecords().size();
    if (nRecords != nRecordsExpected) {
      errorMsg+="String_Node_Str";
    }
    HashSet<String> usedDescriptions=new HashSet(nRecords);
    HashSet<String> usedNames=new HashSet(nRecords);
    it=consumer.getRecords().iterator();
    while (it.hasNext()) {
      Record record=it.next();
      String description=record.getDescription();
      if (!description.startsWith(newDescription)) {
        errorMsg+="String_Node_Str";
      }
      usedDescriptions.add(description);
      String name=record.getVenue().getName();
      if (!name.startsWith(newName)) {
        errorMsg+="String_Node_Str";
      }
      usedNames.add(name);
    }
    if (usedDescriptions.size() != nRecords) {
      errorMsg+="String_Node_Str";
    }
    if (usedNames.size() != nRecords) {
      errorMsg+="String_Node_Str";
    }
    clearCache();
    em=createEntityManager();
    consumer=em.find(ExpertBeerConsumer.class,m_expertBeerConsumerId);
    closeEntityManager(em);
    nRecords=consumer.getRecords().size();
    if (nRecords != nRecordsExpected) {
      errorMsg+="String_Node_Str";
    }
    usedDescriptions.clear();
    usedNames.clear();
    it=consumer.getRecords().iterator();
    while (it.hasNext()) {
      Record record=it.next();
      String description=record.getDescription();
      if (!description.startsWith(newDescription)) {
        errorMsg+="String_Node_Str";
      }
      usedDescriptions.add(description);
      String name=record.getVenue().getName();
      if (!name.startsWith(newName)) {
        errorMsg+="String_Node_Str";
      }
      usedNames.add(name);
    }
    if (usedDescriptions.size() != nRecords) {
      errorMsg+="String_Node_Str";
    }
    if (usedNames.size() != nRecords) {
      errorMsg+="String_Node_Str";
    }
    if (errorMsg.length() > 0) {
      fail(errorMsg);
    }
  }
  finally {
    if (em != null) {
      if (isTransactionActive(em)) {
        rollbackTransaction(em);
      }
      if (isOnServer()) {
        closeEntityManager(em);
      }
 else       if (em.isOpen()) {
        closeEntityManager(em);
      }
    }
  }
}