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