use of org.eclipse.persistence.testing.models.jpa.advanced.Employee in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaSimpleTestSuite method testOneEqualsOne.
/**
* Tests 1=1 returns correct result.
*/
public void testOneEqualsOne() throws Exception {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
CriteriaBuilder qb = em.getCriteriaBuilder();
// "SELECT e FROM Employee e"
Query query = em.createQuery(qb.createQuery(Employee.class));
List emps = query.getResultList();
assertNotNull(emps);
int numRead = emps.size();
// "SELECT e FROM Employee e WHERE 1=1");
CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
cq.where(qb.equal(qb.literal(1), 1));
emps = em.createQuery(cq).getResultList();
assertNotNull(emps);
assertEquals(numRead, emps.size());
ExpressionBuilder builder = new ExpressionBuilder();
query = ((JpaEntityManager) em.getDelegate()).createQuery(builder.value(1).equal(builder.value(1)), Employee.class);
emps = query.getResultList();
assertNotNull(emps);
assertEquals(numRead, emps.size());
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Employee in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaSimpleTestSuiteBase method testTupleStringTypeValidation.
/**
* bug 366179 : test Tuple.get(string, Type) validation
*/
public void testTupleStringTypeValidation() {
EntityManager em = createEntityManager();
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = qb.createTupleQuery();
Root<Employee> emp = wrapper.from(criteria, Employee.class);
criteria.multiselect(wrapper.get(emp, Employee_lastName).alias("lastName"), wrapper.get(emp, Employee_firstName).alias("firstName"));
// make sure firstName !=null for type checking
criteria.where(qb.isNotNull(wrapper.get(emp, Employee_firstName)));
TypedQuery<Tuple> query = em.createQuery(criteria);
List<Tuple> list = query.getResultList();
Tuple row = list.get(0);
// verify it doesn't throw an exception in a valid case first:
row.get("lastName", String.class);
try {
Object result = row.get("firstName", java.sql.Date.class);
fail("IllegalArgumentException expected using an invalid value. Result returned:" + result);
} catch (Exception iae) {
assertEquals(iae.getClass(), IllegalArgumentException.class);
}
closeEntityManager(em);
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Employee in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaSimpleTestSuiteBase method simpleReverseParameterTest.
public void simpleReverseParameterTest() {
EntityManager em = createEntityManager();
Employee emp = (Employee) getServerSession().readAllObjects(Employee.class).elementAt(0);
clearCache();
String parameterName = "firstName";
ExpressionBuilder builder = new ExpressionBuilder();
Expression whereClause = builder.get("firstName").equal(builder.getParameter(parameterName));
ReadAllQuery raq = new ReadAllQuery();
raq.setReferenceClass(Employee.class);
raq.setSelectionCriteria(whereClause);
raq.addArgument(parameterName);
Vector<String> parameters = new Vector<>();
parameters.add(emp.getFirstName());
Vector expectedResult = (Vector) getServerSession().executeQuery(raq, parameters);
clearCache();
// "SELECT OBJECT(emp) FROM Employee emp WHERE ?1 = emp.firstName "
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Employee> cq = qb.createQuery(Employee.class);
cq.where(qb.equal(qb.parameter(String.class, "1"), wrapper.get(wrapper.from(cq, Employee.class), Employee_firstName)));
beginTransaction(em);
try {
List<Employee> result = em.createQuery(cq).setParameter("1", parameters.get(0)).getResultList();
assertTrue("Simple Reverse Parameter test failed", comparer.compareObjects(result, expectedResult));
} finally {
rollbackTransaction(em);
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Employee in project eclipselink by eclipse-ee4j.
the class JUnitCriteriaSimpleTestSuiteBase method testTupleIndexValidation.
/**
* bug 366104 : Tuple.get(int) validation
*/
public void testTupleIndexValidation() {
EntityManager em = createEntityManager();
CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = qb.createTupleQuery();
Root<Employee> emp = wrapper.from(criteria, Employee.class);
criteria.multiselect(wrapper.get(emp, Employee_lastName), wrapper.get(emp, Employee_firstName));
TypedQuery<Tuple> query = em.createQuery(criteria);
List<Tuple> list = query.getResultList();
Tuple row = list.get(0);
try {
Object result = row.get(-1);
fail("IllegalArgumentException not thrown when using index of -1 on a Tuple. returned: " + result);
} catch (Exception iae) {
assertEquals(iae.getClass(), IllegalArgumentException.class);
}
try {
row.get(2);
fail("IllegalArgumentException expected. Array:" + Arrays.toString(row.toArray()));
} catch (Exception iae) {
assertEquals(iae.getClass(), IllegalArgumentException.class);
}
}
use of org.eclipse.persistence.testing.models.jpa.advanced.Employee 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);
}
}
Aggregations