Update FooServiceSortingTests.java
This commit is contained in:
parent
82ccad7146
commit
554df1148d
|
@ -1,11 +1,7 @@
|
|||
package org.baeldung.persistence.service;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.EntityTransaction;
|
||||
|
@ -15,82 +11,79 @@ import javax.persistence.TypedQuery;
|
|||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.baeldung.persistence.model.Bar;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.junit.After;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import com.cc.jpa.example.Foo;
|
||||
import com.cc.jpa.example.Bar;
|
||||
|
||||
public class FooServiceSortingTests {
|
||||
private EntityManager entityManager;
|
||||
private static EntityManager entityManager;
|
||||
private static EntityManagerFactory emf;
|
||||
private static EntityTransaction entityTransaction;
|
||||
private static CriteriaBuilder criteriaBuilder;
|
||||
|
||||
@BeforeClass
|
||||
public static void before() {
|
||||
//
|
||||
}
|
||||
|
||||
@After
|
||||
public final void after() {
|
||||
//
|
||||
emf = Persistence.createEntityManagerFactory("punit");
|
||||
entityManager = emf.createEntityManager();
|
||||
entityTransaction = entityManager.getTransaction();
|
||||
entityTransaction.begin();
|
||||
criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() {
|
||||
|
||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||
final EntityManager entityManager = emf.createEntityManager();
|
||||
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||
entityTransaction.begin();
|
||||
final String jql = "Select f from Foo as f order by f.id";
|
||||
final Query sortQuery = entityManager.createQuery(jql);
|
||||
final List<Foo> fooList = sortQuery.getResultList();
|
||||
assertEquals(1, fooList.get(0).getId());
|
||||
assertEquals(100, fooList.get(fooList.toArray().length - 1).getId());
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSortingByOneAttributeSetOrder_thenSortedPrintResult() {
|
||||
|
||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||
final EntityManager entityManager = emf.createEntityManager();
|
||||
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||
entityTransaction.begin();
|
||||
final String jql = "Select f from Foo as f order by f.id desc";
|
||||
final Query sortQuery = entityManager.createQuery(jql);
|
||||
final List<Foo> fooList = sortQuery.getResultList();
|
||||
assertEquals(100, fooList.get(0).getId());
|
||||
assertEquals(1, fooList.get(fooList.toArray().length - 1).getId());
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSortingByTwoAttributes_thenPrintSortedResult() {
|
||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||
final EntityManager entityManager = emf.createEntityManager();
|
||||
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||
entityTransaction.begin();
|
||||
|
||||
final String jql = "Select f from Foo as f order by f.name asc, f.id desc";
|
||||
final Query sortQuery = entityManager.createQuery(jql);
|
||||
final List<Foo> fooList = sortQuery.getResultList();
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSortingFooByBar_thenBarsSorted() {
|
||||
|
||||
final String jql = "Select f from Foo as f order by f.name, f.bar.id";
|
||||
final Query barJoinQuery = entityManager.createQuery(jql);
|
||||
final List<Foo> fooList = barJoinQuery.getResultList();
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name:" + foo.getName() + "-------BarId:" + foo.getBar().getId());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSortinfBar_thenPrintBarsSortedWithFoos() {
|
||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||
final EntityManager entityManager = emf.createEntityManager();
|
||||
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||
entityTransaction.begin();
|
||||
final String jql = "Select b from Bar as b order by b.id";
|
||||
|
||||
final String jql = "Select b from Bar as b order by b.id";
|
||||
final Query barQuery = entityManager.createQuery(jql);
|
||||
final List<Bar> barList = barQuery.getResultList();
|
||||
for (final Bar bar : barList) {
|
||||
|
@ -99,15 +92,12 @@ public class FooServiceSortingTests {
|
|||
System.out.println("FooName:" + foo.getName());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSortingByStringNullLast_thenLastNull() {
|
||||
|
||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||
final EntityManager entityManager = emf.createEntityManager();
|
||||
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||
entityTransaction.begin();
|
||||
final String jql = "Select f from Foo as f order by f.name desc NULLS LAST";
|
||||
final Query sortQuery = entityManager.createQuery(jql);
|
||||
final List<Foo> fooList = sortQuery.getResultList();
|
||||
|
@ -117,43 +107,34 @@ public class FooServiceSortingTests {
|
|||
}
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public final void whenSortingByStringNullFirst_thenFirstNull() {
|
||||
// final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||
// final EntityManager entityManager = emf.createEntityManager();
|
||||
// final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||
// entityTransaction.begin();
|
||||
// final String jql = "Select f from Foo as f order by f.name desc NULLS FIRST";
|
||||
// final Query sortQuery = entityManager.createQuery(jql);
|
||||
// final List<Foo> fooList = sortQuery.getResultList();
|
||||
// assertNull(fooList.get(0).getName());
|
||||
// for (final Foo foo : fooList) {
|
||||
// System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getTest_Null());
|
||||
// }
|
||||
// }
|
||||
|
||||
@Test
|
||||
public final void whenSortingByIntNull_thenException() {
|
||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||
final EntityManager entityManager = emf.createEntityManager();
|
||||
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||
entityTransaction.begin();
|
||||
final String jql = "Select f from Foo as f order by f.test_Null desc NULLS FIRST";
|
||||
public final void whenSortingByStringNullFirst_thenFirstNull() {
|
||||
|
||||
final Foo nullNameFoo = new Foo();
|
||||
nullNameFoo.setName(null);
|
||||
|
||||
final Bar bar = new Bar();
|
||||
final List<Foo> fooList1 = Lists.newArrayList();
|
||||
bar.setName("Bar_Me");
|
||||
nullNameFoo.setBar(bar);
|
||||
fooList1.add(nullNameFoo);
|
||||
bar.setFooList(fooList1);
|
||||
entityManager.persist(bar);
|
||||
entityManager.persist(nullNameFoo);
|
||||
entityTransaction.commit();
|
||||
final String jql = "Select f from Foo as f order by f.name desc NULLS FIRST";
|
||||
final Query sortQuery = entityManager.createQuery(jql);
|
||||
boolean thrown = false;
|
||||
try {
|
||||
final List<Foo> fooList = sortQuery.getResultList();
|
||||
} catch (final javax.persistence.PersistenceException e) {
|
||||
thrown = true;
|
||||
final List<Foo> fooList = sortQuery.getResultList();
|
||||
assertNull(fooList.get(0).getName());
|
||||
for (final Foo foo : fooList) {
|
||||
System.out.println("Name:" + foo.getName());
|
||||
}
|
||||
assertTrue(thrown);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenSortingFooWithCriteria_thenPrintSortedFoos() {
|
||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||
final EntityManager entityManager = emf.createEntityManager();
|
||||
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
|
||||
criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
|
||||
final Root<Foo> from = criteriaQuery.from(Foo.class);
|
||||
final CriteriaQuery<Foo> select = criteriaQuery.select(from);
|
||||
|
@ -168,9 +149,8 @@ public class FooServiceSortingTests {
|
|||
|
||||
@Test
|
||||
public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos() {
|
||||
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||
final EntityManager entityManager = emf.createEntityManager();
|
||||
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
|
||||
criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
|
||||
final Root<Foo> from = criteriaQuery.from(Foo.class);
|
||||
final CriteriaQuery<Foo> select = criteriaQuery.select(from);
|
||||
|
|
Loading…
Reference in New Issue