Search in sources :

Example 11 with SmallProject

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

the class EntityManagerJUnitTestSuite method testPersistenceProperties.

// The test removed because we moved back to binding literals
// on platforms other than DB2 and Derby
/*    public void testDontBindLiteral() {
        EntityManager em = createEntityManager();

        Query controlQuery = em.createQuery("SELECT OBJECT(p) FROM SmallProject p WHERE p.name = CONCAT(:param1, :param2)");
        controlQuery.setParameter("param1", "A").setParameter("param2", "B");
        List controlResults = controlQuery.getResultList();
        int nControlParams = ((ExpressionQueryMechanism)((EJBQueryImpl)controlQuery).getDatabaseQuery().getQueryMechanism()).getCall().getParameters().size();
        if(nControlParams != 2) {
            fail("controlQuery has wrong number of parameters = "+nControlParams+"; 2 is expected");
        }

        Query query = em.createQuery("SELECT OBJECT(p) FROM SmallProject p WHERE p.name = CONCAT('A', 'B')");
        List results = query.getResultList();
        int nParams = ((ExpressionQueryMechanism)((EJBQueryImpl)query).getDatabaseQuery().getQueryMechanism()).getCall().getParameters().size();
        if(nParams > 0) {
            fail("Query processed literals as parameters");
        }

        closeEntityManager(em);
    }*/
public void testPersistenceProperties() {
    // Different properties are used on the server.
    if (isOnServer()) {
        return;
    }
    EntityManager em = createEntityManager();
    ServerSession ss = ((org.eclipse.persistence.internal.jpa.EntityManagerImpl) em).getServerSession();
    // these properties were set in persistence unit
    // and overridden in CMP3TestModel.setup - the values should be overridden.
    boolean isReadShared = (ss.getReadConnectionPool() instanceof ReadConnectionPool);
    if (isReadShared != Boolean.parseBoolean((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.JDBC_READ_CONNECTIONS_SHARED))) {
        fail("isReadShared is wrong");
    }
    int writeMin = ss.getDefaultConnectionPool().getMinNumberOfConnections();
    if (writeMin != Integer.parseInt((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.JDBC_WRITE_CONNECTIONS_MIN))) {
        fail("writeMin is wrong");
    }
    int writeInitial = ss.getDefaultConnectionPool().getInitialNumberOfConnections();
    if (writeInitial != Integer.parseInt((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.JDBC_WRITE_CONNECTIONS_INITIAL))) {
        fail("writeInitial is wrong");
    }
    int writeMax = ss.getDefaultConnectionPool().getMaxNumberOfConnections();
    if (writeMax != Integer.parseInt((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.JDBC_WRITE_CONNECTIONS_MAX))) {
        fail("writeMax is wrong");
    }
    int readMin = ss.getReadConnectionPool().getMinNumberOfConnections();
    if (readMin != Integer.parseInt((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.JDBC_READ_CONNECTIONS_MIN))) {
        fail("readMin is wrong");
    }
    int readMax = ss.getReadConnectionPool().getMaxNumberOfConnections();
    if (readMax != Integer.parseInt((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.JDBC_READ_CONNECTIONS_MAX))) {
        fail("readMax is wrong");
    }
    int batchSize = ss.getPlatform().getMaxBatchWritingSize();
    if (batchSize != Integer.parseInt((String) JUnitTestCaseHelper.propertiesMap.get(PersistenceUnitProperties.BATCH_WRITING_SIZE))) {
        fail("batchSize is wrong");
    }
    // these properties were set in persistence unit - the values should be the same as in persistence.xml
    /*
            <property name="eclipselink.session-name" value="default-session"/>
            <property name="eclipselink.cache.size.default" value="500"/>
            <property name="eclipselink.cache.size.Employee" value="550"/>
            <property name="eclipselink.cache.size.org.eclipse.persistence.testing.models.jpa.advanced.Address" value="555"/>
            <property name="eclipselink.cache.type.default" value="Full"/>
            <property name="eclipselink.cache.type.Employee" value="Weak"/>
            <property name="eclipselink.cache.type.org.eclipse.persistence.testing.models.jpa.advanced.Address" value="HardWeak"/>
            <property name="eclipselink.session.customizer" value="org.eclipse.persistence.testing.models.jpa.advanced.Customizer"/>
            <property name="eclipselink.descriptor.customizer.Employee" value="org.eclipse.persistence.testing.models.jpa.advanced.Customizer"/>
            <property name="eclipselink.descriptor.customizer.org.eclipse.persistence.testing.models.jpa.advanced.Address" value="org.eclipse.persistence.testing.models.jpa.advanced.Customizer"/>
            <property name="eclipselink.descriptor.customizer.Project" value="org.eclipse.persistence.testing.models.jpa.advanced.Customizer"/>
        */
    String sessionName = ss.getName();
    if (!sessionName.equals("default-session")) {
        fail("sessionName is wrong: " + sessionName);
    }
    int defaultCacheSize = ss.getDescriptor(Project.class).getIdentityMapSize();
    if (defaultCacheSize != 500) {
        fail("defaultCacheSize is wrong: " + defaultCacheSize);
    }
    int employeeCacheSize = ss.getDescriptor(Employee.class).getIdentityMapSize();
    if (employeeCacheSize != 550) {
        fail("employeeCacheSize is wrong: " + employeeCacheSize);
    }
    int addressCacheSize = ss.getDescriptor(Address.class).getIdentityMapSize();
    if (addressCacheSize != 555) {
        fail("addressCacheSize is wrong: " + addressCacheSize);
    }
    // Department cache size specified in @Cache annotation - that should override default property
    int departmentCacheSize = ss.getDescriptor(Department.class).getIdentityMapSize();
    if (departmentCacheSize != 777) {
        fail("departmentCacheSize is wrong: " + departmentCacheSize);
    }
    Class<?> defaultCacheType = ss.getDescriptor(Project.class).getIdentityMapClass();
    if (!Helper.getShortClassName(defaultCacheType).equals("FullIdentityMap")) {
        fail("defaultCacheType is wrong: " + Helper.getShortClassName(defaultCacheType));
    }
    Class<?> employeeCacheType = ss.getDescriptor(Employee.class).getIdentityMapClass();
    if (!Helper.getShortClassName(employeeCacheType).equals("WeakIdentityMap")) {
        fail("employeeCacheType is wrong: " + Helper.getShortClassName(employeeCacheType));
    }
    Class<?> addressCacheType = ss.getDescriptor(Address.class).getIdentityMapClass();
    if (!Helper.getShortClassName(addressCacheType).equals("HardCacheWeakIdentityMap")) {
        fail("addressCacheType is wrong: " + Helper.getShortClassName(addressCacheType));
    }
    // Department cache type specified in @Cache annotation - that should override default property
    Class<?> departmentCacheType = ss.getDescriptor(Department.class).getIdentityMapClass();
    if (!Helper.getShortClassName(departmentCacheType).equals("SoftCacheWeakIdentityMap")) {
        fail("departmentCacheType is wrong: " + Helper.getShortClassName(departmentCacheType));
    }
    int numSessionCalls = Customizer.getNumberOfCallsForSession(ss.getName());
    if (numSessionCalls == 0) {
        fail("session customizer hasn't been called");
    }
    int numProjectCalls = Customizer.getNumberOfCallsForClass(Project.class.getName());
    if (numProjectCalls == 0) {
        fail("Project customizer hasn't been called");
    }
    int numEmployeeCalls = Customizer.getNumberOfCallsForClass(Employee.class.getName());
    if (numEmployeeCalls == 0) {
        fail("Employee customizer hasn't been called");
    }
    int numAddressCalls = Customizer.getNumberOfCallsForClass(Address.class.getName());
    if (numAddressCalls == 0) {
        fail("Address customizer hasn't been called");
    }
    IdValidation employeeIdValidation = ss.getDescriptor(Employee.class).getIdValidation();
    if (employeeIdValidation != IdValidation.ZERO) {
        fail("employeeIdValidation is wrong, IdValidation.ZERO assigned through PrimaryKey annotation was expected");
    }
    IdValidation addressIdValidation = ss.getDescriptor(Address.class).getIdValidation();
    if (addressIdValidation != IdValidation.NEGATIVE) {
        fail("addressIdValidation is wrong, IdValidation.NEGATIVE set as a default value in persistence.xml was expected");
    }
    closeEntityManager(em);
}
Also used : ServerSession(org.eclipse.persistence.sessions.server.ServerSession) IdValidation(org.eclipse.persistence.annotations.IdValidation) ReadConnectionPool(org.eclipse.persistence.sessions.server.ReadConnectionPool) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) EntityManagerImpl(org.eclipse.persistence.internal.jpa.EntityManagerImpl) Project(org.eclipse.persistence.testing.models.jpa.advanced.Project) HugeProject(org.eclipse.persistence.testing.models.jpa.advanced.HugeProject) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) SpecialHugeProject(org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee)

Example 12 with SmallProject

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

the class JUnitCriteriaSimpleTestSuiteBase method smallProjectNOTMemberOfProjectsTest.

public void smallProjectNOTMemberOfProjectsTest() {
    EntityManager em = createEntityManager();
    // query for those employees with Project named "Enterprise" (which should be a SmallProject)
    ReadObjectQuery smallProjectQuery = new ReadObjectQuery();
    smallProjectQuery.setReferenceClass(SmallProject.class);
    smallProjectQuery.setSelectionCriteria(new ExpressionBuilder().get("name").equal("Enterprise"));
    SmallProject smallProject = (SmallProject) getServerSession().executeQuery(smallProjectQuery);
    ReadAllQuery query = new ReadAllQuery();
    query.addArgument("smallProject");
    Expression selectionCriteria = new ExpressionBuilder().noneOf("projects", new ExpressionBuilder().equal(new ExpressionBuilder().getParameter("smallProject")));
    query.setSelectionCriteria(selectionCriteria);
    query.setReferenceClass(Employee.class);
    Vector<SmallProject> arguments = new Vector<>();
    arguments.add(smallProject);
    Vector expectedResult = (Vector) getServerSession().executeQuery(query, arguments);
    // "SELECT OBJECT(employee) FROM Employee employee WHERE ?1 NOT MEMBER OF employee.projects"
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
    cq.where(qb.isNotMember(qb.parameter(Project.class, "1"), wrapper.<Employee, Collection<Project>>get(wrapper.from(cq, Employee.class), Employee_projects)));
    beginTransaction(em);
    try {
        List<Employee> result = em.createQuery(cq).setParameter("1", smallProject).getResultList();
        assertTrue("Simple small Project NOT Member Of Projects test failed", comparer.compareObjects(result, expectedResult));
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Expression(org.eclipse.persistence.expressions.Expression) Collection(java.util.Collection) Vector(java.util.Vector)

Example 13 with SmallProject

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

the class JUnitCriteriaSimpleTestSuiteBase method smallProjectMemberOfProjectsTest.

public void smallProjectMemberOfProjectsTest() {
    EntityManager em = createEntityManager();
    ReadAllQuery query = new ReadAllQuery();
    Expression selectionCriteria = new ExpressionBuilder().anyOf("projects").equal(new ExpressionBuilder(SmallProject.class));
    query.setSelectionCriteria(selectionCriteria);
    query.setReferenceClass(Employee.class);
    // gf 1395 changed jpql to not use distinct on joins
    query.dontUseDistinct();
    Vector expectedResult = (Vector) getServerSession().executeQuery(query);
    clearCache();
    // "SELECT OBJECT(employee) FROM Employee employee, SmallProject sp WHERE sp MEMBER OF employee.projects";
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
    cq.where(qb.isMember(wrapper.from(cq, SmallProject.class), wrapper.<Employee, Collection<SmallProject>>get(wrapper.from(cq, Employee.class), Employee_projects)));
    beginTransaction(em);
    try {
        List<Employee> result = em.createQuery(cq).getResultList();
        assertTrue("Simple small Project Member Of Projects test failed", comparer.compareObjects(result, expectedResult));
    } finally {
        rollbackTransaction(em);
        closeEntityManager(em);
    }
}
Also used : CriteriaBuilder(jakarta.persistence.criteria.CriteriaBuilder) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Expression(org.eclipse.persistence.expressions.Expression) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) Collection(java.util.Collection) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) Vector(java.util.Vector)

Example 14 with SmallProject

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

the class EntityManagerJUnitTestSuite method testIsLoadedAttribute.

public void testIsLoadedAttribute() {
    EntityManagerFactory emf = getEntityManagerFactory();
    EntityManager em = createEntityManager();
    beginTransaction(em);
    try {
        Employee emp = new Employee();
        emp.setFirstName("Abe");
        emp.setLastName("Jones");
        Address addr = new Address();
        addr.setCity("Palo Alto");
        emp.setAddress(addr);
        PhoneNumber pn = new PhoneNumber();
        pn.setNumber("1234456");
        pn.setType("Home");
        emp.addPhoneNumber(pn);
        pn.setOwner(emp);
        SmallProject project = new SmallProject();
        project.setName("Utility Testing");
        project.addTeamMember(emp);
        emp.addProject(project);
        em.persist(emp);
        em.flush();
        em.clear();
        clearCache();
        emp = em.find(Employee.class, emp.getId());
        PersistenceUnitUtil util = emf.getPersistenceUnitUtil();
        if (emp instanceof PersistenceWeaved) {
            assertFalse("PersistenceUnitUtil says address is loaded when it is not", util.isLoaded(emp, "address"));
        } else {
            assertTrue("Non-weaved PersistenceUnitUtil says address is not loaded", util.isLoaded(emp, "address"));
        }
        assertFalse("PersistenceUnitUtil says projects is loaded when it is not", util.isLoaded(emp, "projects"));
        emp.getPhoneNumbers().size();
        assertTrue("PersistenceUnitUtil says phoneNumbers is not loaded when it is", util.isLoaded(emp, "phoneNumbers"));
        assertTrue("PersistenceUnitUtil says firstName is not loaded when it is", util.isLoaded(emp, "firstName"));
    } finally {
        rollbackTransaction(em);
    }
}
Also used : SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) EntityManager(jakarta.persistence.EntityManager) JpaEntityManager(org.eclipse.persistence.jpa.JpaEntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) PersistenceWeaved(org.eclipse.persistence.internal.weaving.PersistenceWeaved) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) JpaEntityManagerFactory(org.eclipse.persistence.jpa.JpaEntityManagerFactory) PersistenceUnitUtil(jakarta.persistence.PersistenceUnitUtil) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber)

Example 15 with SmallProject

use of org.eclipse.persistence.testing.models.jpa.advanced.SmallProject 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 (isWeavingEnabled()) {
        internalTestWeaving(new Employee(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), true);
        internalTestWeaving(new FormerEmployment(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Address(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new PhoneNumber(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new EmploymentPeriod(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        // field-locking
        internalTestWeaving(new Buyer(), false, isWeavingForFetchGroupsEnabled(), false);
        // field-locking
        internalTestWeaving(new GoldBuyer(), false, isWeavingForFetchGroupsEnabled(), false);
        // field-locking
        internalTestWeaving(new PlatinumBuyer(), false, isWeavingForFetchGroupsEnabled(), false);
        // eager 1-m
        internalTestWeaving(new Department(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Golfer(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new GolferPK(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new SmallProject(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new LargeProject(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Man(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Woman(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        // serialized
        internalTestWeaving(new Vegetable(), false, isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new VegetablePK(), false, isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new WorldRank(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new Equipment(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new EquipmentCode(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
        internalTestWeaving(new PartnerLink(), isWeavingForChangeTrackingEnabled(), isWeavingForFetchGroupsEnabled(), false);
    }
}
Also used : EmploymentPeriod(org.eclipse.persistence.testing.models.jpa.advanced.EmploymentPeriod) SmallProject(org.eclipse.persistence.testing.models.jpa.advanced.SmallProject) VegetablePK(org.eclipse.persistence.testing.models.jpa.advanced.VegetablePK) WorldRank(org.eclipse.persistence.testing.models.jpa.advanced.WorldRank) Address(org.eclipse.persistence.testing.models.jpa.advanced.Address) EquipmentCode(org.eclipse.persistence.testing.models.jpa.advanced.EquipmentCode) FormerEmployment(org.eclipse.persistence.testing.models.jpa.advanced.FormerEmployment) GoldBuyer(org.eclipse.persistence.testing.models.jpa.advanced.GoldBuyer) PlatinumBuyer(org.eclipse.persistence.testing.models.jpa.advanced.PlatinumBuyer) Buyer(org.eclipse.persistence.testing.models.jpa.advanced.Buyer) GoldBuyer(org.eclipse.persistence.testing.models.jpa.advanced.GoldBuyer) PlatinumBuyer(org.eclipse.persistence.testing.models.jpa.advanced.PlatinumBuyer) Department(org.eclipse.persistence.testing.models.jpa.advanced.Department) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) Equipment(org.eclipse.persistence.testing.models.jpa.advanced.Equipment) GolferPK(org.eclipse.persistence.testing.models.jpa.advanced.GolferPK) PhoneNumber(org.eclipse.persistence.testing.models.jpa.advanced.PhoneNumber) Golfer(org.eclipse.persistence.testing.models.jpa.advanced.Golfer) Man(org.eclipse.persistence.testing.models.jpa.advanced.Man) Woman(org.eclipse.persistence.testing.models.jpa.advanced.Woman) PartnerLink(org.eclipse.persistence.testing.models.jpa.advanced.PartnerLink) Vegetable(org.eclipse.persistence.testing.models.jpa.advanced.Vegetable) SuperLargeProject(org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject) LargeProject(org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)

Aggregations

SmallProject (org.eclipse.persistence.testing.models.jpa.advanced.SmallProject)30 EntityManager (jakarta.persistence.EntityManager)27 LargeProject (org.eclipse.persistence.testing.models.jpa.advanced.LargeProject)21 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)19 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)17 List (java.util.List)13 ReadAllQuery (org.eclipse.persistence.queries.ReadAllQuery)11 Project (org.eclipse.persistence.testing.models.jpa.advanced.Project)11 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)9 SuperLargeProject (org.eclipse.persistence.testing.models.jpa.advanced.SuperLargeProject)9 CriteriaBuilder (jakarta.persistence.criteria.CriteriaBuilder)8 HugeProject (org.eclipse.persistence.testing.models.jpa.advanced.HugeProject)8 SpecialHugeProject (org.eclipse.persistence.testing.models.jpa.advanced.SpecialHugeProject)8 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)7 Query (jakarta.persistence.Query)6 ArrayList (java.util.ArrayList)6 ReportQuery (org.eclipse.persistence.queries.ReportQuery)6 Vector (java.util.Vector)5 Expression (org.eclipse.persistence.expressions.Expression)5 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)5