Search in sources :

Example 6 with LargeProject

use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method internalDeleteAllProjectsWithNullTeamLeader.

protected void internalDeleteAllProjectsWithNullTeamLeader(String className) {
    String name = "testDeleteAllProjectsWithNull";
    // setup
    SmallProject sp = new SmallProject();
    sp.setName(name);
    LargeProject lp = new LargeProject();
    lp.setName(name);
    EntityManager em = createEntityManager();
    try {
        beginTransaction(em);
        // make sure there are no pre-existing objects with this name
        em.createQuery("DELETE FROM " + className + " p WHERE p.name = '" + name + "'").executeUpdate();
        em.persist(sp);
        em.persist(lp);
        commitTransaction(em);
    } catch (RuntimeException ex) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        throw ex;
    } finally {
        closeEntityManager(em);
    }
    // test
    em = createEntityManager();
    beginTransaction(em);
    try {
        em.createQuery("DELETE FROM " + className + " p WHERE p.name = '" + name + "' AND p.teamLeader IS NULL").executeUpdate();
        commitTransaction(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        throw e;
    } finally {
        closeEntityManager(em);
    }
    // verify
    String error = null;
    em = createEntityManager();
    List result = em.createQuery("SELECT OBJECT(p) FROM Project p WHERE p.name = '" + name + "'").getResultList();
    if (result.isEmpty()) {
        if (!className.equals("Project")) {
            error = "Target Class " + className + ": no objects left";
        }
    } else {
        if (result.size() > 1) {
            error = "Target Class " + className + ": too many objects left: " + result.size();
        } else {
            Project p = (Project) result.get(0);
            if (p.getClass().getName().endsWith(className)) {
                error = "Target Class " + className + ": object of wrong type left: " + p.getClass().getName();
            }
        }
    }
    // clean up
    try {
        beginTransaction(em);
        // make sure there are no pre-existing objects with this name
        em.createQuery("DELETE FROM " + className + " p WHERE p.name = '" + name + "'").executeUpdate();
        commitTransaction(em);
    } catch (RuntimeException ex) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        throw ex;
    } finally {
        closeEntityManager(em);
    }
    if (error != null) {
        fail(error);
    }
}
Also used : SmallProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SmallProject) LargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SuperLargeProject) SmallProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SmallProject) Project(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) ArrayList(java.util.ArrayList) List(java.util.List) LargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SuperLargeProject)

Example 7 with LargeProject

use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method testWeaving.

/**
 * Test that all of the classes in the advanced model were weaved as expected.
 */
public void testWeaving() {
    // Only test if weaving was on, test runs without weaving must set this system property.
    if (JUnitTestCase.isWeavingEnabled("fieldaccess")) {
        internalTestWeaving(new Employee(), true, true);
        internalTestWeaving(new FormerEmployment(), true, false);
        internalTestWeaving(new Address(), true, false);
        internalTestWeaving(new PhoneNumber(), true, false);
        internalTestWeaving(new EmploymentPeriod(), true, false);
        // field-locking
        internalTestWeaving(new Buyer(), false, false);
        // field-locking
        internalTestWeaving(new GoldBuyer(), false, false);
        // field-locking
        internalTestWeaving(new PlatinumBuyer(), false, false);
        internalTestWeaving(new Department(), true, false);
        internalTestWeaving(new Golfer(), true, false);
        internalTestWeaving(new GolferPK(), true, false);
        internalTestWeaving(new SmallProject(), true, false);
        internalTestWeaving(new LargeProject(), true, false);
        internalTestWeaving(new Man(), true, false);
        internalTestWeaving(new Woman(), true, false);
        // serialized
        internalTestWeaving(new Vegetable(), false, false);
        internalTestWeaving(new VegetablePK(), false, false);
        internalTestWeaving(new WorldRank(), true, false);
        internalTestWeaving(new Equipment(), true, false);
        internalTestWeaving(new EquipmentCode(), true, false);
        internalTestWeaving(new PartnerLink(), true, false);
    }
    if (((OneToOneMapping) getDatabaseSession().getDescriptor(DatabaseField.class).getMappingForAttributeName("table")).usesIndirection()) {
        fail("LAZY mapping should have been reverted as class was not weaved.");
    }
}
Also used : EmploymentPeriod(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.EmploymentPeriod) SmallProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SmallProject) VegetablePK(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.VegetablePK) WorldRank(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.WorldRank) Address(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Address) EquipmentCode(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.EquipmentCode) FormerEmployment(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.FormerEmployment) GoldBuyer(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.GoldBuyer) GoldBuyer(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.GoldBuyer) Buyer(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Buyer) PlatinumBuyer(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PlatinumBuyer) PlatinumBuyer(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PlatinumBuyer) Department(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Department) Employee(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee) Equipment(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Equipment) GolferPK(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.GolferPK) PhoneNumber(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber) Golfer(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Golfer) Man(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Man) Woman(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Woman) PartnerLink(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PartnerLink) OneToOneMapping(org.eclipse.persistence.mappings.OneToOneMapping) Vegetable(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Vegetable) LargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SuperLargeProject)

Example 8 with LargeProject

use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject in project eclipselink by eclipse-ee4j.

the class EntityManagerJUnitTestSuite method internalTestUpdateAllProjects.

protected void internalTestUpdateAllProjects(Class<?> cls) {
    if (getDatabaseSession().getPlatform().isSymfoware()) {
        getDatabaseSession().logMessage("Test testUpdateAll*Projects skipped for this platform, " + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
        return;
    }
    String className = Helper.getShortClassName(cls);
    String name = "testUpdateAllProjects";
    String newName = "testUpdateAllProjectsNEW";
    HashMap map = null;
    boolean ok = false;
    try {
        // setup
        // populate Projects - necessary only if no SmallProject and/or LargeProject objects already exist.
        createProjectsWithName(name, null);
        // save the original names of projects: will set them back in cleanup
        // to restore the original state.
        EntityManager em = createEntityManager();
        List projects = em.createQuery("SELECT OBJECT(p) FROM Project p").getResultList();
        map = new HashMap(projects.size());
        for (int i = 0; i < projects.size(); i++) {
            Project p = (Project) projects.get(i);
            map.put(p.getId(), p.getName());
        }
        // test
        beginTransaction(em);
        try {
            em.createQuery("UPDATE " + className + " p set p.name = '" + newName + "'").executeUpdate();
            commitTransaction(em);
        } catch (RuntimeException ex) {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
            throw ex;
        } finally {
            closeEntityManager(em);
        }
        // verify
        em = createEntityManager();
        String errorMsg = "";
        projects = em.createQuery("SELECT OBJECT(p) FROM Project p").getResultList();
        for (int i = 0; i < projects.size(); i++) {
            Project p = (Project) projects.get(i);
            String readName = p.getName();
            if (cls.isInstance(p)) {
                if (!newName.equals(readName)) {
                    errorMsg = errorMsg + "haven't updated name: " + p + "; ";
                }
            } else {
                if (newName.equals(readName)) {
                    errorMsg = errorMsg + "have updated name: " + p + "; ";
                }
            }
        }
        closeEntityManager(em);
        if (errorMsg.length() > 0) {
            fail(errorMsg);
        } else {
            ok = true;
        }
    } finally {
        // clean-up
        try {
            if (map != null) {
                EntityManager em = createEntityManager();
                beginTransaction(em);
                List projects = em.createQuery("SELECT OBJECT(p) FROM Project p").getResultList();
                try {
                    for (int i = 0; i < projects.size(); i++) {
                        Project p = (Project) projects.get(i);
                        String oldName = (String) map.get(((Project) projects.get(i)).getId());
                        p.setName(oldName);
                    }
                    commitTransaction(em);
                } catch (RuntimeException ex) {
                    if (isTransactionActive(em)) {
                        rollbackTransaction(em);
                    }
                    throw ex;
                } finally {
                    closeEntityManager(em);
                }
            }
            // delete projects that createProjectsWithName has created in setup
            deleteProjectsWithName(name);
        } catch (RuntimeException ex) {
            // eat clean-up exception in case the test failed
            if (ok) {
                throw ex;
            }
        }
    }
}
Also used : LargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SuperLargeProject) SmallProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SmallProject) Project(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List)

Example 9 with LargeProject

use of org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject in project eclipselink by eclipse-ee4j.

the class AdvancedJPAJunitTest method testTransientConstructorSetFields.

/**
 * test for bug 292385 - Transient fields not initialized for Entities
 */
public void testTransientConstructorSetFields() {
    EntityManager em = createEntityManager("fieldaccess");
    Project project = new Project();
    LargeProject lproject = new LargeProject();
    SmallProject sproject = new SmallProject();
    project.setName("TCSetFieldsProject");
    lproject.setName("TCSetFieldsLProject");
    sproject.setName("TCSetFieldsSProject");
    beginTransaction(em);
    project = em.merge(project);
    lproject = em.merge(lproject);
    sproject = em.merge(sproject);
    assertTrue("Project's default constructor wasn't used to create merged entity", project.getFieldOnlySetThroughConstructor() == 1);
    assertTrue("LargeProject's default constructor wasn't used to create merged entity", lproject.getFieldOnlySetThroughConstructor() == 2);
    assertTrue("Project's default constructor wasn't used to create merged SmallProject", sproject.getFieldOnlySetThroughConstructor() == 1);
    try {
        em.flush();
        this.commitTransaction(em);
        closeEntityManager(em);
    } catch (RuntimeException e) {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
        // Re-throw exception to ensure stacktrace appears in test result.
        throw e;
    }
    em = createEntityManager("fieldaccess");
    project = em.find(Project.class, project.getId());
    lproject = em.find(LargeProject.class, lproject.getId());
    sproject = em.find(SmallProject.class, sproject.getId());
    closeEntityManager(em);
    assertTrue("Project's default constructor wasn't used to create managed entity", project.getFieldOnlySetThroughConstructor() == 1);
    assertTrue("LargeProject's default constructor wasn't used to create managed entity", lproject.getFieldOnlySetThroughConstructor() == 2);
    assertTrue("Project's default constructor wasn't used to create managed SmallProject", sproject.getFieldOnlySetThroughConstructor() == 1);
}
Also used : SmallProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SmallProject) LargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject) Project(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project) SmallProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SmallProject) EntityManager(jakarta.persistence.EntityManager) LargeProject(org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject)

Aggregations

LargeProject (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.LargeProject)9 SmallProject (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SmallProject)8 EntityManager (jakarta.persistence.EntityManager)7 SuperLargeProject (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.SuperLargeProject)7 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)6 Project (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Project)5 Employee (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 HashMap (java.util.HashMap)2 Address (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Address)2 Department (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Department)2 EmploymentPeriod (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.EmploymentPeriod)2 Equipment (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Equipment)2 FormerEmployment (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.FormerEmployment)2 PhoneNumber (org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.PhoneNumber)2 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Set (java.util.Set)1