Ejemplo n.º 1
0
  public void testLazyCollectionRollback() throws PersistenceException, SQLException {

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

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

    // set up the data object
    _db.begin();
    TestLazyEmployee loadPerson = (TestLazyEmployee) _db.load(TestLazyEmployee.class, fullname);
    Collection projects = loadPerson.getProjects();
    TestLazyProject project = new TestLazyProject();

    project = new TestLazyProject();
    project.setId(1002);
    project.setName("Project Two");
    project.setOwner(loadPerson);
    projects.add(project);
    _db.create(project);

    _db.commit();

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

    // is the collection populated?
    assertTrue("The projects collection is not valid!.", projects != null && projects.size() > 0);

    // is the collection instanceof Lazy?
    assertTrue(
        "Collection has to be lazy! It is " + loadPerson.getProjects().getClass(),
        loadPerson.getProjects() instanceof org.exolab.castor.persist.Lazy);

    // OK, the collection of projects is there, let's test a rollback for bug #1046
    stream.println("Rolling back transaction");
    _db.rollback();

    // test it again since the rollback - is the collection instanceof Lazy?
    assertTrue(
        "Collection has to be lazy! It is " + loadPerson.getProjects().getClass(),
        loadPerson.getProjects() instanceof org.exolab.castor.persist.Lazy);
  }
Ejemplo n.º 2
0
  public void testComplex() throws PersistenceException, SQLException {

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

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

    // set up the data object
    _db.begin();
    TestLazyEmployee loadPerson = (TestLazyEmployee) _db.load(TestLazyEmployee.class, fullname);
    Collection projects = loadPerson.getProjects();

    TestLazyProject project = new TestLazyProject();
    project.setId(1001);
    project.setName("Project One");
    project.setOwner(loadPerson);
    projects.add(project);
    _db.create(project);

    project = new TestLazyProject();
    project.setId(1002);
    project.setName("Project Two");
    project.setOwner(loadPerson);
    projects.add(project);
    _db.create(project);

    project = new TestLazyProject();
    project.setId(1003);
    project.setName("Project Three");
    project.setOwner(loadPerson);
    projects.add(project);
    _db.create(project);

    _db.commit();

    // reload and test bug 823
    _db.begin();
    project = (TestLazyProject) _db.load(TestLazyProject.class, new Integer(1002));
    _db.remove(project);

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

    Iterator itor = projects.iterator();
    while (itor.hasNext()) {
      project = (TestLazyProject) itor.next();
      if (project.getId() == 1002) {
        itor.remove();
        break;
      }
    }

    itor = projects.iterator();
    while (itor.hasNext()) {
      project = (TestLazyProject) itor.next();
      if (project.getId() == 1002) {
        itor.remove();
        break;
      }
    }
    _db.commit();

    // reload and make sure the cache is consistent
    _db.begin();
    loadPerson = (TestLazyEmployee) _db.load(TestLazyEmployee.class, fullname);
    projects = loadPerson.getProjects();
    itor = projects.iterator();
    int id1 = 0;
    if (itor.hasNext()) {
      project = (TestLazyProject) itor.next();
      id1 = project.getId();
      if (project.getId() != 1001 && project.getId() != 1003) {
        stream.println("Error: found project1 with unexpected id " + project.getId());
        fail("Error: found project1 with unexpected id " + project.getId());
      }
    } else {
      stream.println("Error: expected project is not found");
      fail("Error: expected project is not found");
    }

    if (itor.hasNext()) {
      project = (TestLazyProject) itor.next();
      if (project.getId() == id1 || (project.getId() != 1001 && project.getId() != 1003)) {
        stream.println("Error: found project2 with unexpected id " + project.getId());
        fail("Error: found project2 with unexpected id " + project.getId());
      }
    } else {
      stream.println("Error: expected project is not found");
      fail("Error: expected project is not found");
    }

    if (itor.hasNext()) {
      project = (TestLazyProject) itor.next();
      stream.println("Error: unexpected project is found: " + project.getId());
      fail("Error: unexpected project is found: " + project.getId());
    }
    _db.commit();
  }