Example #1
0
  public void testIterWithDelete() throws PersistenceException, SQLException {

    stream.println("Running testIterWithDelete...");

    // Tests iterating over a lazy-loaded Collection that has
    // had data deleted
    Complex fullname = new Complex("First", "Person");

    // First add a record, then commit
    _db.begin();
    TestLazyEmployee loadPerson = (TestLazyEmployee) _db.load(TestLazyEmployee.class, fullname);
    Collection addresses = loadPerson.getAddress();
    TestLazyAddress la = new TestLazyAddress();
    la.setId(999);
    la.setStreet("Rogue Street");
    la.setCity("Rogue City");
    la.setState("RS");
    la.setZip("10666");
    la.setPerson(loadPerson);
    addresses.add(la);
    _db.commit();

    // New transaction
    _db.begin();

    ArrayList masterData = new ArrayList();

    // test java.util.Collection.clear() for lazy loading (bug 801)
    loadPerson = (TestLazyEmployee) _db.load(TestLazyEmployee.class, fullname);

    addresses = loadPerson.getAddress();
    // Store the list in the database at the start of the transaction,
    // for comparison purposes.  Select a victim for deletion.
    Iterator it = addresses.iterator();
    // victim is last element to test bug 1022
    int victim = addresses.size() - 1;
    int recNo = 0;
    TestLazyAddress victimAddr = null;
    while (it.hasNext()) {
      TestLazyAddress addr = (TestLazyAddress) it.next();
      if (recNo++ == victim) {
        victimAddr = addr;
      } else {
        masterData.add(addr);
      }
    }

    // The expected number of items to iterate over
    int collSize = addresses.size();

    _db.rollback();

    // Now start over, and add something to the collection.  Then try
    // iterating and clearing the collection
    _db.begin();
    loadPerson = (TestLazyEmployee) _db.load(TestLazyEmployee.class, fullname);
    addresses = loadPerson.getAddress();
    addresses.remove(victimAddr);

    Iterator it2 = addresses.iterator();

    while (it2.hasNext()) {

      TestLazyAddress addr = (TestLazyAddress) it2.next();
      if (addr.equals(victimAddr)) {
        stream.println("Error: Deleted record should not show up in iteration");
        fail("Error: Deleted record should not show up in iteration");
      } else {

        if (!masterData.remove(addr)) {
          stream.println("Error: unrecognized element from list in testIterWithDelete");
          fail("Error: unrecognized element from list in testIterWithDelete");
        }
      }
    }

    _db.rollback();

    if (!masterData.isEmpty()) {
      stream.println("Error: iteration/deletion failed in testIterWithDelete");
      fail("Error: iteration/deletion failed in testIterWithDelete");
    }
  }
Example #2
0
  public void testGeneral() throws PersistenceException, SQLException {

    stream.println("Running testGeneral...");

    _db.begin();
    Complex fullname = new Complex("First", "Person");

    TestLazyEmployee loadPerson = (TestLazyEmployee) _db.load(TestLazyEmployee.class, fullname);

    if (loadPerson.getBirthday().equals(new Date(1922, 2, 2))
        && loadPerson.getFirstName().equals("First")
        && loadPerson.getLastName().equals("Person")) {
      stream.println("OK: Employee is valid");

      Collection address = loadPerson.getAddress();
      Iterator itor = address.iterator();
      TestLazyAddress[] addresses = {null, null, null};
      TestLazyAddress addr;
      while (itor.hasNext()) {
        addr = (TestLazyAddress) itor.next();
        if (addr.getId() < 1 || addr.getId() > 3) {
          _db.rollback();
          stream.println("Error: Address id is incorrect");
          fail("address id is incorrect");
        }
        addresses[addr.getId() - 1] = addr;
      }

      if (addresses[0] == null
          || !addresses[0].getStreet().equals("#1 Address Street")
          || !addresses[0].getCity().equals("First City")
          || !addresses[0].getState().equals("AB")
          || !addresses[0].getZip().equals("10000")
          || addresses[0].getPerson() != loadPerson) {
        stream.println("Error: Address 1 is incorrect: " + addresses[0]);
        _db.rollback();
        fail("address 1 is incorrect");
      }
      stream.println("OK: Address 1 are valid");

      if (addresses[1] == null
          || !addresses[1].getStreet().equals("2nd Ave")
          || !addresses[1].getCity().equals("Second City")
          || !addresses[1].getState().equals("BC")
          || !addresses[1].getZip().equals("22222")
          || addresses[1].getPerson() != loadPerson) {
        stream.println("Error: Address 2 is incorrect");
        _db.rollback();
        fail("address 2 is incorrect");
      }
      stream.println("OK: Address 2 are valid");

      TestLazyPayRoll payroll = loadPerson.getPayRoll();
      if (payroll == null
          || payroll.getId() != 1
          || payroll.getHoliday() != 15
          || payroll.getEmployee() != loadPerson
          || payroll.getHourlyRate() != 25) {
        stream.println("Error: PayRoll loaded incorrect");
        _db.rollback();
        fail("payroll is incorrect");
      }
      stream.println("OK: PayRoll is valid");

      TestLazyContract cont = loadPerson.getContract();
      if (cont == null
          || cont.getPolicyNo() != 1001
          || cont.getEmployee() != loadPerson
          || cont.getContractNo() != 78) {
        stream.println("Error: Contract is not what expected!");
        stream.println(
            "employe==null? "
                + cont.getEmployee()
                + "/"
                + cont.getEmployee().getFirstName()
                + "/"
                + cont.getEmployee().getLastName());
        stream.println(
            "loadPerson? "
                + loadPerson
                + "/"
                + loadPerson.getFirstName()
                + "/"
                + loadPerson.getLastName());
        _db.rollback();
        fail("contract is incorrect");
      }
      stream.println("OK: Contract is valid");

      Collection catelist = cont.getCategory();
      itor = catelist.iterator();
      TestLazyContractCategory cate;
      while (itor.hasNext()) {
        cate = (TestLazyContractCategory) itor.next();
        if (cate.getId() == 101 && cate.getName().equals("Full-time slave")) {
        } else if (cate.getId() == 102 && cate.getName().equals("Full-time employee")) {
        } else {
          stream.println("Error: Category is incorrect");
          _db.rollback();
          fail("category is incorrect");
        }
      }
      stream.println("OK: Categories are valid");

      // now modified the object and store it
      address.remove(addresses[0]);
      addresses[1].setStreet("New Second Street");

    } else {
      _db.rollback();
      stream.println("Error: FirstName, LastName or Birthday is incorrect!");
      fail("FirstName, LastName or Birthday is incorrect!");
    }
    _db.commit();

    // test and see if changes made succeed
    _db.begin();
    loadPerson = (TestLazyEmployee) _db.load(TestLazyEmployee.class, fullname);

    if (loadPerson.getBirthday().equals(new Date(1922, 2, 2))
        && loadPerson.getFirstName().equals("First")
        && loadPerson.getLastName().equals("Person")) {
      stream.println("OK: Employee is valid");

      Collection address = loadPerson.getAddress();
      Iterator itor = address.iterator();
      TestLazyAddress[] addresses = {null, null, null};
      TestLazyAddress addr;
      while (itor.hasNext()) {
        addr = (TestLazyAddress) itor.next();
        if (addr.getId() < 1 || addr.getId() > 3) {
          _db.rollback();
          stream.println("Error: Address id is incorrect");
          fail("address id is incorrect");
        }
        addresses[addr.getId() - 1] = addr;
      }

      if (addresses[0] != null) {
        stream.println("Error: Address 1 is not deleted: " + addresses[0]);
        fail("address 1 is not deleted");
      }
      stream.println("OK: Address 1 is deleted");

      if (addresses[1] == null
          || !addresses[1].getStreet().equals("New Second Street")
          || !addresses[1].getCity().equals("Second City")
          || !addresses[1].getState().equals("BC")
          || !addresses[1].getZip().equals("22222")
          || addresses[1].getPerson() != loadPerson) {
        stream.println("Error: Address 2 is incorrect");
        _db.rollback();
        fail("address 2 is incorrect");
      }
      stream.println("OK: Address 2 are valid: " + addresses[1]);

      TestLazyPayRoll payroll = loadPerson.getPayRoll();
      if (payroll == null
          || payroll.getId() != 1
          || payroll.getHoliday() != 15
          || payroll.getEmployee() != loadPerson
          || payroll.getHourlyRate() != 25) {
        stream.println("Error: PayRoll loaded incorrect");
        _db.rollback();
        fail("payroll is incorrect");
      }
      stream.println("OK: PayRoll is valid");

      TestLazyContract cont = loadPerson.getContract();
      if (cont == null
          || cont.getPolicyNo() != 1001
          || cont.getEmployee() != loadPerson
          || cont.getContractNo() != 78) {
        stream.println("Error: Contract is not what expected!");
        stream.println(
            "employe==null? "
                + cont.getEmployee()
                + "/"
                + cont.getEmployee().getFirstName()
                + "/"
                + cont.getEmployee().getLastName());
        stream.println(
            "loadPerson? "
                + loadPerson
                + "/"
                + loadPerson.getFirstName()
                + "/"
                + loadPerson.getLastName());
        _db.rollback();
        fail("contract is incorrect");
      }
      stream.println("OK: Contract is valid");

      Collection catelist = cont.getCategory();
      itor = catelist.iterator();
      TestLazyContractCategory cate;
      while (itor.hasNext()) {
        cate = (TestLazyContractCategory) itor.next();
        if (cate.getId() == 101 && cate.getName().equals("Full-time slave")) {
        } else if (cate.getId() == 102 && cate.getName().equals("Full-time employee")) {
        } else {
          stream.println("Error: Category is incorrect");
          _db.rollback();
          fail("category is incorrect");
        }
      }
      stream.println("OK: Categories are valid");

    } else {
      _db.rollback();
      stream.println("Error: FirstName, LastName or Birthday is incorrect!");
      fail("FirstName, LastName or Birthday is incorrect!");
    }
    _db.commit();
  }