コード例 #1
0
 public String toString() {
   StringBuffer sb = new StringBuffer();
   sb.append("<");
   sb.append(_id);
   sb.append(": ");
   sb.append(_name);
   sb.append(" of ");
   if (_owner != null) {
     sb.append(_owner.getLastName());
     sb.append(", ");
     sb.append(_owner.getFirstName());
   } else {
     sb.append("--nobody--");
   }
   sb.append(">");
   return sb.toString();
 }
コード例 #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();
  }