forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
eugenp
committed
Jun 3, 2014
1 parent
b4c1ace
commit 65bf9a0
Showing
2 changed files
with
79 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...g-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTestsWithNulls.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.baeldung.persistence.service; | ||
|
||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import java.util.List; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceContext; | ||
import javax.persistence.Query; | ||
|
||
import org.baeldung.config.PersistenceJPAConfig; | ||
import org.baeldung.persistence.model.Foo; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
import org.springframework.test.context.support.AnnotationConfigContextLoader; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class) | ||
public class FooServiceSortingTestsWithNulls { | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
@Autowired | ||
private FooService service; | ||
|
||
// tests | ||
|
||
@Test | ||
public final void whenSortingByStringNullLast_thenLastNull() { | ||
service.create(new Foo()); | ||
service.create(new Foo(randomAlphabetic(6))); | ||
|
||
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(); | ||
assertNull(fooList.get(fooList.toArray().length - 1).getName()); | ||
for (final Foo foo : fooList) { | ||
System.out.println("Name:" + foo.getName()); | ||
} | ||
} | ||
|
||
@Test | ||
public final void whenSortingByStringNullFirst_thenFirstNull() { | ||
service.create(new Foo()); | ||
|
||
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()); | ||
} | ||
} | ||
|
||
} |