All JPA Sorting Tests

This commit is contained in:
egmp777 2014-04-30 12:58:51 -05:00
parent d244b373b8
commit 845cad104e
1 changed files with 181 additions and 59 deletions

View File

@ -7,24 +7,33 @@ import java.util.List;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction; import javax.persistence.EntityTransaction;
import javax.persistence.OrderBy;
import javax.persistence.Persistence; import javax.persistence.Persistence;
import javax.persistence.Query; import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Root;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
import org.hibernate.annotations.common.util.StringHelper;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import com.cc.jpa.example.Foo; import com.cc.jpa.example.Foo;
import com.cc.jpa.example.Bar;
public class FooServiceSortingTests { public class FooServiceSortingTests {
private EntityManager entityManager; private EntityManager entityManager;
@BeforeClass @BeforeClass
public static void before(){ public static void before(){
} }
@After @After
@ -33,10 +42,59 @@ private EntityManager entityManager;
} }
@Test @Test
public final void whenSortingByOneAttributeDefault_thenSortedResult() { public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("punit");
EntityManager entityManager = emf.createEntityManager();
EntityTransaction entityTransaction = entityManager
.getTransaction();
entityTransaction.begin();
String jql = "Select f from Foo as f order by f.id";
Query sortQuery = entityManager.createQuery Query sortQuery = entityManager.createQuery
("Select f from Foo as f order by f.id"); (jql);
List<Foo> fooList = sortQuery.getResultList();
assertEquals(1,fooList.get(0).getId());
assertEquals(100,fooList.get(fooList.toArray().length-1).getId());
for(Foo foo:fooList){
System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId());
}
}
@Test
public final void whenSortingByOneAttributeSetOrder_thenSortedPrintResult() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("punit");
EntityManager entityManager = emf.createEntityManager();
EntityTransaction entityTransaction = entityManager
.getTransaction();
entityTransaction.begin();
String jql = "Select f from Foo as f order by f.id desc";
Query sortQuery = entityManager.createQuery
(jql);
List<Foo> fooList = sortQuery.getResultList();
assertEquals(100,fooList.get(0).getId());
assertEquals(1,fooList.get(fooList.toArray().length-1).getId());
for(Foo foo:fooList){
System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId());
}
}
@Test
public final void whenSortingByTwoAttributes_thenPrintSortedResult() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("punit");
EntityManager entityManager = emf.createEntityManager();
EntityTransaction entityTransaction = entityManager
.getTransaction();
entityTransaction.begin();
String jql = "Select f from Foo as f order by f.name asc, f.id desc";
Query sortQuery = entityManager.createQuery
(jql);
List<Foo> fooList = sortQuery.getResultList(); List<Foo> fooList = sortQuery.getResultList();
for(Foo foo:fooList){ for(Foo foo:fooList){
System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId());
@ -45,32 +103,92 @@ private EntityManager entityManager;
} }
@Test @Test
public final void whenSortingByOneAttribute_thenSortedResult() { public final void whenSortinfBar_thenPrintBarsSortedWithFoos(){
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("punit");
EntityManager entityManager = emf.createEntityManager();
EntityTransaction entityTransaction = entityManager
.getTransaction();
entityTransaction.begin();
String jql = "Select b from Bar as b order by b.id";
Query sortQuery = entityManager.createQuery Query barQuery = entityManager.createQuery(jql);
("Select f from Foo as f order by f.id desc"); List<Bar>barList = barQuery.getResultList();
List<Foo> fooList = sortQuery.getResultList(); for(Bar bar:barList){
for(Foo foo:fooList){ System.out.println("Bar Id:"+bar.getId());
System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); for(Foo foo:bar.getFooList()){
System.out.println("FooName:"+foo.getName());
}
} }
} }
@Test @Test
public final void whenSortingByTwoAttributes_thenSortedResult() { public final void whenSortingByStringNullLast_thenLastNull() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("punit");
EntityManager entityManager = emf.createEntityManager();
EntityTransaction entityTransaction = entityManager
.getTransaction();
entityTransaction.begin();
String jql = "Select f from Foo as f order by f.name desc NULLS LAST";
Query sortQuery = entityManager.createQuery Query sortQuery = entityManager.createQuery
("Select f from Foo as f order by f.name asc, f.id desc"); (jql);
List<Foo> fooList = sortQuery.getResultList(); List<Foo> fooList = sortQuery.getResultList();
assertNull(fooList.get(fooList.toArray().length-1).getName());
for(Foo foo:fooList){ for(Foo foo:fooList){
System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); System.out.println("Name:"+foo.getName());
} }
}
@Test
public final void whenSortingByStringNullFirst_thenFirstNull() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("punit");
EntityManager entityManager = emf.createEntityManager();
EntityTransaction entityTransaction = entityManager
.getTransaction();
entityTransaction.begin();
String jql = "Select f from Foo as f order by f.name desc NULLS FIRST";
Query sortQuery = entityManager.createQuery
(jql);
List<Foo> fooList = sortQuery.getResultList();
assertNull(fooList.get(0).getName());
for(Foo foo:fooList){
System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getTest_Null());
}
}
@Test
public final void whenSortingByIntNull_thenException() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("punit");
EntityManager entityManager = emf.createEntityManager();
EntityTransaction entityTransaction = entityManager
.getTransaction();
entityTransaction.begin();
String jql = "Select f from Foo as f order by f.test_Null desc NULLS FIRST";
Query sortQuery = entityManager.createQuery
(jql);
boolean thrown = false;
try {
List<Foo> fooList = sortQuery.getResultList();
} catch (javax.persistence.PersistenceException e) {
thrown = true;
}
assertTrue(thrown);
} }
@Test @Test
public final void whenSortingFooWithCriteria_thenSortedFoos(){ public final void whenSortingFooWithCriteria_thenPrintSortedFoos(){
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("punit");
EntityManager entityManager = emf.createEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Foo> criteriaQuery = criteriaBuilder
.createQuery(Foo.class);
Root<Foo> from = criteriaQuery.from(Foo.class); Root<Foo> from = criteriaQuery.from(Foo.class);
CriteriaQuery<Foo> select = criteriaQuery.select(from); CriteriaQuery<Foo> select = criteriaQuery.select(from);
criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name"))); criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name")));
@ -83,8 +201,14 @@ private EntityManager entityManager;
} }
@Test @Test
public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenSortedFoos(){ public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos(){
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("punit");
EntityManager entityManager = emf.createEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Foo> criteriaQuery = criteriaBuilder
.createQuery(Foo.class);
Root<Foo> from = criteriaQuery.from(Foo.class); Root<Foo> from = criteriaQuery.from(Foo.class);
CriteriaQuery<Foo> select = criteriaQuery.select(from); CriteriaQuery<Foo> select = criteriaQuery.select(from);
criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name")), criteriaBuilder.desc(from.get("id"))); criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name")), criteriaBuilder.desc(from.get("id")));
@ -95,6 +219,4 @@ private EntityManager entityManager;
} }
} }
} }