11package org .baeldung .persistence .service ;
22
3- import static org .junit .Assert .*;
3+ import static org .junit .Assert .assertNull ;
4+
45import java .util .List ;
6+
57import javax .persistence .EntityManager ;
68import javax .persistence .EntityManagerFactory ;
79import javax .persistence .EntityTransaction ;
1113import javax .persistence .criteria .CriteriaBuilder ;
1214import javax .persistence .criteria .CriteriaQuery ;
1315import javax .persistence .criteria .Root ;
14- import com .google .common .collect .Lists ;
16+
17+ import org .baeldung .persistence .model .Bar ;
18+ import org .baeldung .persistence .model .Foo ;
1519import org .junit .BeforeClass ;
1620import org .junit .Test ;
17- import com . cc . jpa . example . Foo ;
18- import com .cc . jpa . example . Bar ;
21+
22+ import com .google . common . collect . Lists ;
1923
2024public class FooServiceSortingTests {
2125 private static EntityManager entityManager ;
@@ -25,7 +29,6 @@ public class FooServiceSortingTests {
2529
2630 @ BeforeClass
2731 public static void before () {
28-
2932 emf = Persistence .createEntityManagerFactory ("punit" );
3033 entityManager = emf .createEntityManager ();
3134 entityTransaction = entityManager .getTransaction ();
@@ -35,43 +38,36 @@ public static void before() {
3538
3639 @ Test
3740 public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult () {
38-
3941 final String jql = "Select f from Foo as f order by f.id" ;
4042 final Query sortQuery = entityManager .createQuery (jql );
4143 final List <Foo > fooList = sortQuery .getResultList ();
4244 for (final Foo foo : fooList ) {
4345 System .out .println ("Name:" + foo .getName () + "-------Id:" + foo .getId ());
4446 }
45-
4647 }
4748
4849 @ Test
4950 public final void whenSortingByOneAttributeSetOrder_thenSortedPrintResult () {
50-
5151 final String jql = "Select f from Foo as f order by f.id desc" ;
5252 final Query sortQuery = entityManager .createQuery (jql );
5353 final List <Foo > fooList = sortQuery .getResultList ();
5454 for (final Foo foo : fooList ) {
5555 System .out .println ("Name:" + foo .getName () + "-------Id:" + foo .getId ());
5656 }
57-
5857 }
5958
6059 @ Test
6160 public final void whenSortingByTwoAttributes_thenPrintSortedResult () {
62-
6361 final String jql = "Select f from Foo as f order by f.name asc, f.id desc" ;
6462 final Query sortQuery = entityManager .createQuery (jql );
6563 final List <Foo > fooList = sortQuery .getResultList ();
6664 for (final Foo foo : fooList ) {
6765 System .out .println ("Name:" + foo .getName () + "-------Id:" + foo .getId ());
6866 }
69-
7067 }
7168
7269 @ Test
7370 public final void whenSortingFooByBar_thenBarsSorted () {
74-
7571 final String jql = "Select f from Foo as f order by f.name, f.bar.id" ;
7672 final Query barJoinQuery = entityManager .createQuery (jql );
7773 final List <Foo > fooList = barJoinQuery .getResultList ();
@@ -82,7 +78,6 @@ public final void whenSortingFooByBar_thenBarsSorted() {
8278
8379 @ Test
8480 public final void whenSortinfBar_thenPrintBarsSortedWithFoos () {
85-
8681 final String jql = "Select b from Bar as b order by b.id" ;
8782 final Query barQuery = entityManager .createQuery (jql );
8883 final List <Bar > barList = barQuery .getResultList ();
@@ -92,12 +87,10 @@ public final void whenSortinfBar_thenPrintBarsSortedWithFoos() {
9287 System .out .println ("FooName:" + foo .getName ());
9388 }
9489 }
95-
9690 }
9791
9892 @ Test
9993 public final void whenSortingByStringNullLast_thenLastNull () {
100-
10194 final String jql = "Select f from Foo as f order by f.name desc NULLS LAST" ;
10295 final Query sortQuery = entityManager .createQuery (jql );
10396 final List <Foo > fooList = sortQuery .getResultList ();
@@ -109,7 +102,6 @@ public final void whenSortingByStringNullLast_thenLastNull() {
109102
110103 @ Test
111104 public final void whenSortingByStringNullFirst_thenFirstNull () {
112-
113105 final Foo nullNameFoo = new Foo ();
114106 nullNameFoo .setName (null );
115107
@@ -133,7 +125,6 @@ public final void whenSortingByStringNullFirst_thenFirstNull() {
133125
134126 @ Test
135127 public final void whenSortingFooWithCriteria_thenPrintSortedFoos () {
136-
137128 criteriaBuilder = entityManager .getCriteriaBuilder ();
138129 final CriteriaQuery <Foo > criteriaQuery = criteriaBuilder .createQuery (Foo .class );
139130 final Root <Foo > from = criteriaQuery .from (Foo .class );
@@ -144,12 +135,10 @@ public final void whenSortingFooWithCriteria_thenPrintSortedFoos() {
144135 for (final Foo foo : fooList ) {
145136 System .out .println ("Name:" + foo .getName () + "--------Id:" + foo .getId ());
146137 }
147-
148138 }
149139
150140 @ Test
151141 public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos () {
152-
153142 criteriaBuilder = entityManager .getCriteriaBuilder ();
154143 final CriteriaQuery <Foo > criteriaQuery = criteriaBuilder .createQuery (Foo .class );
155144 final Root <Foo > from = criteriaQuery .from (Foo .class );
0 commit comments