示例#1
0
  public void testMasterUpdate() throws PersistenceException {
    Complex fullname = new Complex("First", "Person");

    // 1. load master object, add a new dependent object and commit
    _db.begin();
    TestLazyPerson loadPerson = (TestLazyEmployee) _db.load(TestLazyEmployee.class, fullname);
    TestLazyAddress address = new TestLazyAddress();
    address.setId(201);
    address.setStreet("Halfpipe 1");
    address.setCity("Skater Heaven");
    address.setPerson(loadPerson);
    loadPerson.getAddress().add(address);
    _db.commit();

    // 2. reuse master and commit. if we get a DuplicateIdentityException,
    //    the dependent object (id=201) was created twice.
    _db.begin();
    _db.update(loadPerson);
    try {
      _db.commit();
    } catch (TransactionAbortedException e) {
      if (e.getException() instanceof DuplicateIdentityException) {
        stream.println("Error: The dependent object Address was just duplicated");
        fail("The dependent object Address was just duplicated");
      } else {
        throw e;
      }
    }
    // No DuplicateIdentityException means, that just one instance of Address (id=201)
    // was created in the database. That's what we want to have.
  }
示例#2
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");
    }
  }
示例#3
0
  public void testCollection() throws PersistenceException, SQLException {

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

    Complex fullname = new Complex("First", "Person");

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

    Collection addresses = loadPerson.getAddress();
    addresses.clear();
    _db.commit();

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

    // check if clear() work
    if (!addresses.isEmpty()) {
      stream.println("Error: Collection.clear() is not working!");
      fail("Error: Collection.clear() is not working!");
    }

    // modify the collection to test java.util.Collection.addAll()
    // for lazy loading (bug 801)
    Collection c = new ArrayList();

    TestLazyAddress address = new TestLazyAddress();
    address.setId(101);
    address.setStreet("Mattrew Street");
    address.setCity("Rome City");
    address.setState("RM");
    address.setZip("10000");
    address.setPerson(loadPerson);
    c.add(address);

    address = new TestLazyAddress();
    address.setId(102);
    address.setStreet("Luke Street");
    address.setCity("Rome City");
    address.setState("RM");
    address.setZip("10000");
    address.setPerson(loadPerson);
    c.add(address);

    address = new TestLazyAddress();
    address.setId(103);
    address.setStreet("John Street");
    address.setCity("Rome City");
    address.setState("RM");
    address.setZip("10000");
    address.setPerson(loadPerson);
    c.add(address);

    addresses.addAll(c);
    _db.commit();

    // check if add all work
    _db.begin();
    loadPerson = (TestLazyEmployee) _db.load(TestLazyEmployee.class, fullname);
    addresses = loadPerson.getAddress();
    Iterator itor = addresses.iterator();

    boolean hasAddr1, hasAddr2, hasAddr3;
    hasAddr1 = hasAddr2 = hasAddr3 = false;
    while (itor.hasNext()) {
      address = (TestLazyAddress) itor.next();
      if (address.getId() == 101) hasAddr1 = true;
      else if (address.getId() == 102) hasAddr2 = true;
      else if (address.getId() == 103) hasAddr3 = true;
      else {
        stream.println("Error: Address with unexpected id is found! " + address);
        fail("Erorr: Address with unexpected id is found! " + address);
      }
    }
    if (!hasAddr1 || !hasAddr2 || !hasAddr3) {
      stream.println("Error: Collection.addAll( Collection ) fail");
      fail("Error: Collection.addAll( Collection ) fail");
    }
    _db.commit();
  }
示例#4
0
  public void testIterWithAdd() throws PersistenceException, SQLException {

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

    // Tests iterating over a lazy-loaded Collection that has
    // had data added

    ArrayList masterData = new ArrayList();
    Complex fullname = new Complex("First", "Person");

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

    Collection addresses = loadPerson.getAddress();
    // Store the list in the database at the start of the transaction,
    // for comparison purposes
    Iterator it = addresses.iterator();
    while (it.hasNext()) {
      masterData.add(it.next());
    }

    _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();
    int collSize = addresses.size();
    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);

    // bruce
    stream.println("masterData size: " + masterData.size());
    stream.println(" addresses size: " + addresses.size());

    if (addresses.size() != masterData.size() + 1)
      fail("Lazy collection size is different from what is expected");

    boolean matchNewElement = false;
    int matchCount = 0;

    it = addresses.iterator();

    /*
     * The problem with the following block is that the second loop is
     * entered but never exited.
     *
     */

    bigloop:
    while (it.hasNext()) {
      TestLazyAddress addr = (TestLazyAddress) it.next();

      Iterator it2 = masterData.iterator();
      while (it2.hasNext()) {
        TestLazyAddress addr2 = (TestLazyAddress) it2.next();

        stream.println(" addr: " + addr);
        stream.println("addr2: " + addr2);

        if (addr2.equals(addr)) {
          stream.println("matched");
          matchCount++;
          continue bigloop;
        } else if (addr == la) {
          stream.println("matched lazy");
          matchNewElement = true;
          matchCount++;
          continue bigloop;
        } else {
          stream.println("no match");
        }
      }
      stream.println("newly added:" + la + "@" + System.identityHashCode(la));
      stream.println("matchNewElement " + matchNewElement);
      stream.println("matchCount " + matchCount);

      fail(
          "found unexpected address in the new lazy collection:"
              + addr
              + "@"
              + System.identityHashCode(la));
    }

    if (!matchNewElement) fail("Newly added element is missing");

    if (matchCount != (masterData.size() + 1))
      fail(
          "Lazy collection contains unexpected number of elements. expected: "
              + (masterData.size() + 1)
              + " found: "
              + matchCount);

    addresses.clear();
    if (!addresses.isEmpty()) {
      stream.println("Error: clear failed in testIterWithAdd");
      fail("Error: clear failed in testIterWithAdd");
    }

    _db.rollback();
  }
示例#5
0
  public void createDataObjects() throws PersistenceException, SQLException {

    _db.begin();
    // create person 1
    TestLazyEmployee person = new TestLazyEmployee();
    person.setFirstName("First");
    person.setLastName("Person");
    person.setBirthday(new Date(1922, 2, 2));
    person.setStartDate(new Date(2000, 2, 2));

    TestLazyAddress address1 = new TestLazyAddress();
    address1.setId(1);
    address1.setStreet("#1 Address Street");
    address1.setCity("First City");
    address1.setState("AB");
    address1.setZip("10000");
    address1.setPerson(person);

    TestLazyAddress address2 = new TestLazyAddress();
    address2.setId(2);
    address2.setStreet("2nd Ave");
    address2.setCity("Second City");
    address2.setState("BC");
    address2.setZip("22222");
    address2.setPerson(person);

    TestLazyAddress address3 = new TestLazyAddress();
    address3.setId(3);
    address3.setStreet("3rd Court");
    address3.setCity("Third Ave");
    address3.setState("AB");
    address3.setZip("30003");
    address3.setPerson(person);

    ArrayList addresslist = new ArrayList();
    addresslist.add(address1);
    addresslist.add(address2);
    addresslist.add(address3);

    person.setAddress(addresslist);

    TestLazyPayRoll pr1 = new TestLazyPayRoll();
    pr1.setId(1);
    pr1.setHoliday(15);
    pr1.setHourlyRate(25);
    pr1.setEmployee(person);
    person.setPayRoll(pr1);

    TestLazyContractCategory cc = new TestLazyContractCategory();
    cc.setId(101);
    cc.setName("Full-time slave");
    _db.create(cc);

    TestLazyContractCategory cc2 = new TestLazyContractCategory();
    cc2.setId(102);
    cc2.setName("Full-time employee");
    _db.create(cc2);
    ArrayList category = new ArrayList();
    category.add(cc);
    category.add(cc2);

    TestLazyContract con = new TestLazyContract();
    con.setPolicyNo(1001);
    con.setComment(
        "80 hours a week, no pay hoilday, no sick leave, arrive office at 7:30am everyday");
    con.setContractNo(78);
    con.setEmployee(person);
    con.setCategory(category);
    person.setContract(con);
    _db.create(person);
    _db.commit();
  }