testing fixes
This commit is contained in:
parent
15f753ce55
commit
a4f71ef281
|
@ -0,0 +1,90 @@
|
||||||
|
package org.baeldung.persistence.model;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.GenerationType;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Bar implements Serializable {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private List<Foo> foos;
|
||||||
|
|
||||||
|
public Bar() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Bar(final String name) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(final long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(final String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Foo> getFooList() {
|
||||||
|
return foos;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(final Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null)
|
||||||
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
final Bar other = (Bar) obj;
|
||||||
|
if (name == null) {
|
||||||
|
if (other.name != null)
|
||||||
|
return false;
|
||||||
|
} else if (!name.equals(other.name))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
final StringBuilder builder = new StringBuilder();
|
||||||
|
builder.append("Foo [name=").append(name).append("]");
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,180 +1,149 @@
|
||||||
package org.baeldung.persistence.service;
|
package org.baeldung.persistence.service;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
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;
|
||||||
import javax.persistence.OrderBy;
|
|
||||||
import javax.persistence.Persistence;
|
import javax.persistence.Persistence;
|
||||||
import javax.persistence.Query;
|
import javax.persistence.Query;
|
||||||
import javax.persistence.TypedQuery;
|
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.Order;
|
|
||||||
import javax.persistence.criteria.Root;
|
import javax.persistence.criteria.Root;
|
||||||
|
|
||||||
import org.hibernate.Session;
|
import org.baeldung.persistence.model.Bar;
|
||||||
import org.hibernate.SessionFactory;
|
import org.baeldung.persistence.model.Foo;
|
||||||
import org.hibernate.annotations.common.util.StringHelper;
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
|
||||||
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 EntityManager entityManager;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void before() {
|
public static void before() {
|
||||||
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public final void after() {
|
public final void after() {
|
||||||
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() {
|
public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() {
|
||||||
|
|
||||||
EntityManagerFactory emf = Persistence
|
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||||
.createEntityManagerFactory("punit");
|
final EntityManager entityManager = emf.createEntityManager();
|
||||||
EntityManager entityManager = emf.createEntityManager();
|
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||||
EntityTransaction entityTransaction = entityManager
|
|
||||||
.getTransaction();
|
|
||||||
entityTransaction.begin();
|
entityTransaction.begin();
|
||||||
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";
|
||||||
Query sortQuery = entityManager.createQuery
|
final Query sortQuery = entityManager.createQuery(jql);
|
||||||
(jql);
|
final List<Foo> fooList = sortQuery.getResultList();
|
||||||
List<Foo> fooList = sortQuery.getResultList();
|
|
||||||
assertEquals(1, fooList.get(0).getId());
|
assertEquals(1, fooList.get(0).getId());
|
||||||
assertEquals(100, fooList.get(fooList.toArray().length - 1).getId());
|
assertEquals(100, fooList.get(fooList.toArray().length - 1).getId());
|
||||||
for(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() {
|
||||||
|
|
||||||
EntityManagerFactory emf = Persistence
|
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||||
.createEntityManagerFactory("punit");
|
final EntityManager entityManager = emf.createEntityManager();
|
||||||
EntityManager entityManager = emf.createEntityManager();
|
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||||
EntityTransaction entityTransaction = entityManager
|
|
||||||
.getTransaction();
|
|
||||||
entityTransaction.begin();
|
entityTransaction.begin();
|
||||||
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";
|
||||||
Query sortQuery = entityManager.createQuery
|
final Query sortQuery = entityManager.createQuery(jql);
|
||||||
(jql);
|
final List<Foo> fooList = sortQuery.getResultList();
|
||||||
List<Foo> fooList = sortQuery.getResultList();
|
|
||||||
assertEquals(100, fooList.get(0).getId());
|
assertEquals(100, fooList.get(0).getId());
|
||||||
assertEquals(1, fooList.get(fooList.toArray().length - 1).getId());
|
assertEquals(1, fooList.get(fooList.toArray().length - 1).getId());
|
||||||
for(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");
|
||||||
EntityManagerFactory emf = Persistence
|
final EntityManager entityManager = emf.createEntityManager();
|
||||||
.createEntityManagerFactory("punit");
|
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||||
EntityManager entityManager = emf.createEntityManager();
|
|
||||||
EntityTransaction entityTransaction = entityManager
|
|
||||||
.getTransaction();
|
|
||||||
entityTransaction.begin();
|
entityTransaction.begin();
|
||||||
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";
|
||||||
Query sortQuery = entityManager.createQuery
|
final Query sortQuery = entityManager.createQuery(jql);
|
||||||
(jql);
|
final List<Foo> fooList = sortQuery.getResultList();
|
||||||
List<Foo> fooList = sortQuery.getResultList();
|
for (final Foo foo : fooList) {
|
||||||
for(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 whenSortinfBar_thenPrintBarsSortedWithFoos() {
|
public final void whenSortinfBar_thenPrintBarsSortedWithFoos() {
|
||||||
EntityManagerFactory emf = Persistence
|
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||||
.createEntityManagerFactory("punit");
|
final EntityManager entityManager = emf.createEntityManager();
|
||||||
EntityManager entityManager = emf.createEntityManager();
|
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||||
EntityTransaction entityTransaction = entityManager
|
|
||||||
.getTransaction();
|
|
||||||
entityTransaction.begin();
|
entityTransaction.begin();
|
||||||
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";
|
||||||
|
|
||||||
Query barQuery = entityManager.createQuery(jql);
|
final Query barQuery = entityManager.createQuery(jql);
|
||||||
List<Bar>barList = barQuery.getResultList();
|
final List<Bar> barList = barQuery.getResultList();
|
||||||
for(Bar bar:barList){
|
for (final Bar bar : barList) {
|
||||||
System.out.println("Bar Id:" + bar.getId());
|
System.out.println("Bar Id:" + bar.getId());
|
||||||
for(Foo foo:bar.getFooList()){
|
for (final Foo foo : bar.getFooList()) {
|
||||||
System.out.println("FooName:" + foo.getName());
|
System.out.println("FooName:" + foo.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSortingByStringNullLast_thenLastNull() {
|
public final void whenSortingByStringNullLast_thenLastNull() {
|
||||||
|
|
||||||
EntityManagerFactory emf = Persistence
|
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||||
.createEntityManagerFactory("punit");
|
final EntityManager entityManager = emf.createEntityManager();
|
||||||
EntityManager entityManager = emf.createEntityManager();
|
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||||
EntityTransaction entityTransaction = entityManager
|
|
||||||
.getTransaction();
|
|
||||||
entityTransaction.begin();
|
entityTransaction.begin();
|
||||||
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";
|
||||||
Query sortQuery = entityManager.createQuery
|
final Query sortQuery = entityManager.createQuery(jql);
|
||||||
(jql);
|
final List<Foo> fooList = sortQuery.getResultList();
|
||||||
List<Foo> fooList = sortQuery.getResultList();
|
|
||||||
assertNull(fooList.get(fooList.toArray().length - 1).getName());
|
assertNull(fooList.get(fooList.toArray().length - 1).getName());
|
||||||
for(Foo foo:fooList){
|
for (final Foo foo : fooList) {
|
||||||
System.out.println("Name:" + foo.getName());
|
System.out.println("Name:" + foo.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@Test
|
|
||||||
public final void whenSortingByStringNullFirst_thenFirstNull() {
|
|
||||||
|
|
||||||
EntityManagerFactory emf = Persistence
|
// @Test
|
||||||
.createEntityManagerFactory("punit");
|
// public final void whenSortingByStringNullFirst_thenFirstNull() {
|
||||||
EntityManager entityManager = emf.createEntityManager();
|
// final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||||
EntityTransaction entityTransaction = entityManager
|
// final EntityManager entityManager = emf.createEntityManager();
|
||||||
.getTransaction();
|
// final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||||
entityTransaction.begin();
|
// entityTransaction.begin();
|
||||||
String jql = "Select f from Foo as f order by f.name desc NULLS FIRST";
|
// final String jql = "Select f from Foo as f order by f.name desc NULLS FIRST";
|
||||||
Query sortQuery = entityManager.createQuery
|
// final Query sortQuery = entityManager.createQuery(jql);
|
||||||
(jql);
|
// final List<Foo> fooList = sortQuery.getResultList();
|
||||||
List<Foo> fooList = sortQuery.getResultList();
|
// assertNull(fooList.get(0).getName());
|
||||||
assertNull(fooList.get(0).getName());
|
// for (final Foo foo : fooList) {
|
||||||
for(Foo foo:fooList){
|
// System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getTest_Null());
|
||||||
System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getTest_Null());
|
// }
|
||||||
}
|
// }
|
||||||
}
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSortingByIntNull_thenException() {
|
public final void whenSortingByIntNull_thenException() {
|
||||||
|
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||||
EntityManagerFactory emf = Persistence
|
final EntityManager entityManager = emf.createEntityManager();
|
||||||
.createEntityManagerFactory("punit");
|
final EntityTransaction entityTransaction = entityManager.getTransaction();
|
||||||
EntityManager entityManager = emf.createEntityManager();
|
|
||||||
EntityTransaction entityTransaction = entityManager
|
|
||||||
.getTransaction();
|
|
||||||
entityTransaction.begin();
|
entityTransaction.begin();
|
||||||
String jql = "Select f from Foo as f order by f.test_Null desc NULLS FIRST";
|
final String jql = "Select f from Foo as f order by f.test_Null desc NULLS FIRST";
|
||||||
Query sortQuery = entityManager.createQuery
|
final Query sortQuery = entityManager.createQuery(jql);
|
||||||
(jql);
|
|
||||||
boolean thrown = false;
|
boolean thrown = false;
|
||||||
try {
|
try {
|
||||||
List<Foo> fooList = sortQuery.getResultList();
|
final List<Foo> fooList = sortQuery.getResultList();
|
||||||
} catch (javax.persistence.PersistenceException e) {
|
} catch (final javax.persistence.PersistenceException e) {
|
||||||
thrown = true;
|
thrown = true;
|
||||||
}
|
}
|
||||||
assertTrue(thrown);
|
assertTrue(thrown);
|
||||||
|
@ -182,19 +151,16 @@ public class FooServiceSortingTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSortingFooWithCriteria_thenPrintSortedFoos() {
|
public final void whenSortingFooWithCriteria_thenPrintSortedFoos() {
|
||||||
|
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||||
EntityManagerFactory emf = Persistence
|
final EntityManager entityManager = emf.createEntityManager();
|
||||||
.createEntityManagerFactory("punit");
|
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||||
EntityManager entityManager = emf.createEntityManager();
|
final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
|
||||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
final Root<Foo> from = criteriaQuery.from(Foo.class);
|
||||||
CriteriaQuery<Foo> criteriaQuery = criteriaBuilder
|
final CriteriaQuery<Foo> select = criteriaQuery.select(from);
|
||||||
.createQuery(Foo.class);
|
|
||||||
Root<Foo> from = criteriaQuery.from(Foo.class);
|
|
||||||
CriteriaQuery<Foo> select = criteriaQuery.select(from);
|
|
||||||
criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name")));
|
criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name")));
|
||||||
TypedQuery<Foo> typedQuery = entityManager.createQuery(select);
|
final TypedQuery<Foo> typedQuery = entityManager.createQuery(select);
|
||||||
List<Foo>fooList = typedQuery.getResultList();
|
final List<Foo> fooList = typedQuery.getResultList();
|
||||||
for(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());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -202,19 +168,16 @@ public class FooServiceSortingTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos() {
|
public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos() {
|
||||||
|
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
|
||||||
EntityManagerFactory emf = Persistence
|
final EntityManager entityManager = emf.createEntityManager();
|
||||||
.createEntityManagerFactory("punit");
|
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||||
EntityManager entityManager = emf.createEntityManager();
|
final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
|
||||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
final Root<Foo> from = criteriaQuery.from(Foo.class);
|
||||||
CriteriaQuery<Foo> criteriaQuery = criteriaBuilder
|
final CriteriaQuery<Foo> select = criteriaQuery.select(from);
|
||||||
.createQuery(Foo.class);
|
|
||||||
Root<Foo> from = criteriaQuery.from(Foo.class);
|
|
||||||
CriteriaQuery<Foo> select = criteriaQuery.select(from);
|
|
||||||
criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name")), criteriaBuilder.desc(from.get("id")));
|
criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name")), criteriaBuilder.desc(from.get("id")));
|
||||||
TypedQuery<Foo> typedQuery = entityManager.createQuery(select);
|
final TypedQuery<Foo> typedQuery = entityManager.createQuery(select);
|
||||||
List<Foo>fooList = typedQuery.getResultList();
|
final List<Foo> fooList = typedQuery.getResultList();
|
||||||
for(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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue