Skip to content

Commit 65bf9a0

Browse files
author
eugenp
committed
persistence sorting work
1 parent b4c1ace commit 65bf9a0

2 files changed

Lines changed: 79 additions & 56 deletions

File tree

‎spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java‎

Lines changed: 19 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
11
package org.baeldung.persistence.service;
22

3-
import static org.junit.Assert.assertNull;
4-
53
import java.util.List;
64

75
import javax.persistence.EntityManager;
8-
import javax.persistence.EntityManagerFactory;
9-
import javax.persistence.EntityTransaction;
10-
import javax.persistence.Persistence;
6+
import javax.persistence.PersistenceContext;
117
import javax.persistence.Query;
128
import javax.persistence.TypedQuery;
139
import javax.persistence.criteria.CriteriaBuilder;
1410
import javax.persistence.criteria.CriteriaQuery;
1511
import javax.persistence.criteria.Root;
1612

13+
import org.baeldung.config.PersistenceJPAConfig;
1714
import org.baeldung.persistence.model.Bar;
1815
import org.baeldung.persistence.model.Foo;
19-
import org.junit.BeforeClass;
2016
import org.junit.Test;
17+
import org.junit.runner.RunWith;
18+
import org.springframework.beans.factory.annotation.Autowired;
19+
import org.springframework.test.context.ContextConfiguration;
20+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
21+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
22+
23+
@RunWith(SpringJUnit4ClassRunner.class)
24+
@ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class)
25+
@SuppressWarnings("unchecked")
26+
public class FooServiceSortingTests {
2127

22-
import com.google.common.collect.Lists;
28+
@PersistenceContext
29+
private EntityManager entityManager;
2330

24-
public class FooServiceSortingTests {
25-
private static EntityManager entityManager;
26-
private static EntityManagerFactory emf;
27-
private static EntityTransaction entityTransaction;
28-
private static CriteriaBuilder criteriaBuilder;
31+
@Autowired
32+
private FooService service;
2933

30-
@BeforeClass
31-
public static void before() {
32-
emf = Persistence.createEntityManagerFactory("punit");
33-
entityManager = emf.createEntityManager();
34-
entityTransaction = entityManager.getTransaction();
35-
entityTransaction.begin();
36-
criteriaBuilder = entityManager.getCriteriaBuilder();
37-
}
34+
// tests
3835

3936
@Test
4037
public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() {
@@ -89,43 +86,9 @@ public final void whenSortinfBar_thenPrintBarsSortedWithFoos() {
8986
}
9087
}
9188

92-
@Test
93-
public final void whenSortingByStringNullLast_thenLastNull() {
94-
final String jql = "Select f from Foo as f order by f.name desc NULLS LAST";
95-
final Query sortQuery = entityManager.createQuery(jql);
96-
final List<Foo> fooList = sortQuery.getResultList();
97-
assertNull(fooList.get(fooList.toArray().length - 1).getName());
98-
for (final Foo foo : fooList) {
99-
System.out.println("Name:" + foo.getName());
100-
}
101-
}
102-
103-
@Test
104-
public final void whenSortingByStringNullFirst_thenFirstNull() {
105-
final Foo nullNameFoo = new Foo();
106-
nullNameFoo.setName(null);
107-
108-
final Bar bar = new Bar();
109-
final List<Foo> fooList1 = Lists.newArrayList();
110-
bar.setName("Bar_Me");
111-
nullNameFoo.setBar(bar);
112-
fooList1.add(nullNameFoo);
113-
bar.setFooList(fooList1);
114-
entityManager.persist(bar);
115-
entityManager.persist(nullNameFoo);
116-
entityTransaction.commit();
117-
final String jql = "Select f from Foo as f order by f.name desc NULLS FIRST";
118-
final Query sortQuery = entityManager.createQuery(jql);
119-
final List<Foo> fooList = sortQuery.getResultList();
120-
assertNull(fooList.get(0).getName());
121-
for (final Foo foo : fooList) {
122-
System.out.println("Name:" + foo.getName());
123-
}
124-
}
125-
12689
@Test
12790
public final void whenSortingFooWithCriteria_thenPrintSortedFoos() {
128-
criteriaBuilder = entityManager.getCriteriaBuilder();
91+
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
12992
final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
13093
final Root<Foo> from = criteriaQuery.from(Foo.class);
13194
final CriteriaQuery<Foo> select = criteriaQuery.select(from);
@@ -139,7 +102,7 @@ public final void whenSortingFooWithCriteria_thenPrintSortedFoos() {
139102

140103
@Test
141104
public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos() {
142-
criteriaBuilder = entityManager.getCriteriaBuilder();
105+
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
143106
final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
144107
final Root<Foo> from = criteriaQuery.from(Foo.class);
145108
final CriteriaQuery<Foo> select = criteriaQuery.select(from);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.baeldung.persistence.service;
2+
3+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
4+
import static org.junit.Assert.assertNull;
5+
6+
import java.util.List;
7+
8+
import javax.persistence.EntityManager;
9+
import javax.persistence.PersistenceContext;
10+
import javax.persistence.Query;
11+
12+
import org.baeldung.config.PersistenceJPAConfig;
13+
import org.baeldung.persistence.model.Foo;
14+
import org.junit.Test;
15+
import org.junit.runner.RunWith;
16+
import org.springframework.beans.factory.annotation.Autowired;
17+
import org.springframework.test.context.ContextConfiguration;
18+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
19+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
20+
21+
@RunWith(SpringJUnit4ClassRunner.class)
22+
@ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class)
23+
public class FooServiceSortingTestsWithNulls {
24+
25+
@PersistenceContext
26+
private EntityManager entityManager;
27+
28+
@Autowired
29+
private FooService service;
30+
31+
// tests
32+
33+
@Test
34+
public final void whenSortingByStringNullLast_thenLastNull() {
35+
service.create(new Foo());
36+
service.create(new Foo(randomAlphabetic(6)));
37+
38+
final String jql = "Select f from Foo as f order by f.name desc NULLS LAST";
39+
final Query sortQuery = entityManager.createQuery(jql);
40+
final List<Foo> fooList = sortQuery.getResultList();
41+
assertNull(fooList.get(fooList.toArray().length - 1).getName());
42+
for (final Foo foo : fooList) {
43+
System.out.println("Name:" + foo.getName());
44+
}
45+
}
46+
47+
@Test
48+
public final void whenSortingByStringNullFirst_thenFirstNull() {
49+
service.create(new Foo());
50+
51+
final String jql = "Select f from Foo as f order by f.name desc NULLS FIRST";
52+
final Query sortQuery = entityManager.createQuery(jql);
53+
final List<Foo> fooList = sortQuery.getResultList();
54+
assertNull(fooList.get(0).getName());
55+
for (final Foo foo : fooList) {
56+
System.out.println("Name:" + foo.getName());
57+
}
58+
}
59+
60+
}

0 commit comments

Comments
 (0)