11package org .baeldung .persistence .service ;
22
3- import static org .junit .Assert .assertNull ;
4-
53import java .util .List ;
64
75import javax .persistence .EntityManager ;
8- import javax .persistence .EntityManagerFactory ;
9- import javax .persistence .EntityTransaction ;
10- import javax .persistence .Persistence ;
6+ import javax .persistence .PersistenceContext ;
117import javax .persistence .Query ;
128import javax .persistence .TypedQuery ;
139import javax .persistence .criteria .CriteriaBuilder ;
1410import javax .persistence .criteria .CriteriaQuery ;
1511import javax .persistence .criteria .Root ;
1612
13+ import org .baeldung .config .PersistenceJPAConfig ;
1714import org .baeldung .persistence .model .Bar ;
1815import org .baeldung .persistence .model .Foo ;
19- import org .junit .BeforeClass ;
2016import 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 );
0 commit comments