testing fixes

This commit is contained in:
eugenp 2014-05-06 00:39:49 +03:00
parent 15f753ce55
commit a4f71ef281
2 changed files with 239 additions and 186 deletions

View File

@ -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();
}
}

View File

@ -1,222 +1,185 @@
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
public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() {
@Test final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() { final EntityManager entityManager = emf.createEntityManager();
final EntityTransaction entityTransaction = entityManager.getTransaction();
entityTransaction.begin();
final String jql = "Select f from Foo as f order by f.id";
final Query sortQuery = entityManager.createQuery(jql);
final List<Foo> fooList = sortQuery.getResultList();
assertEquals(1, fooList.get(0).getId());
assertEquals(100, fooList.get(fooList.toArray().length - 1).getId());
for (final Foo foo : fooList) {
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
}
}
EntityManagerFactory emf = Persistence @Test
.createEntityManagerFactory("punit"); public final void whenSortingByOneAttributeSetOrder_thenSortedPrintResult() {
EntityManager entityManager = emf.createEntityManager();
EntityTransaction entityTransaction = entityManager
.getTransaction();
entityTransaction.begin();
String jql = "Select f from Foo as f order by f.id";
Query sortQuery = entityManager.createQuery
(jql);
List<Foo> fooList = sortQuery.getResultList();
assertEquals(1,fooList.get(0).getId());
assertEquals(100,fooList.get(fooList.toArray().length-1).getId());
for(Foo foo:fooList){
System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId());
}
} final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
final EntityManager entityManager = emf.createEntityManager();
final EntityTransaction entityTransaction = entityManager.getTransaction();
entityTransaction.begin();
final String jql = "Select f from Foo as f order by f.id desc";
final Query sortQuery = entityManager.createQuery(jql);
final List<Foo> fooList = sortQuery.getResultList();
assertEquals(100, fooList.get(0).getId());
assertEquals(1, fooList.get(fooList.toArray().length - 1).getId());
for (final Foo foo : fooList) {
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
}
}
@Test @Test
public final void whenSortingByOneAttributeSetOrder_thenSortedPrintResult() { public final void whenSortingByTwoAttributes_thenPrintSortedResult() {
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
final EntityManager entityManager = emf.createEntityManager();
final EntityTransaction entityTransaction = entityManager.getTransaction();
entityTransaction.begin();
final String jql = "Select f from Foo as f order by f.name asc, f.id desc";
final Query sortQuery = entityManager.createQuery(jql);
final List<Foo> fooList = sortQuery.getResultList();
for (final Foo foo : fooList) {
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
}
}
EntityManagerFactory emf = Persistence @Test
.createEntityManagerFactory("punit"); public final void whenSortinfBar_thenPrintBarsSortedWithFoos() {
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.id desc"; final String jql = "Select b from Bar as b order by b.id";
Query sortQuery = entityManager.createQuery
(jql);
List<Foo> fooList = sortQuery.getResultList();
assertEquals(100,fooList.get(0).getId());
assertEquals(1,fooList.get(fooList.toArray().length-1).getId());
for(Foo foo:fooList){
System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId());
}
} final Query barQuery = entityManager.createQuery(jql);
final List<Bar> barList = barQuery.getResultList();
for (final Bar bar : barList) {
System.out.println("Bar Id:" + bar.getId());
for (final Foo foo : bar.getFooList()) {
System.out.println("FooName:" + foo.getName());
}
}
}
@Test @Test
public final void whenSortingByTwoAttributes_thenPrintSortedResult() { 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 entityTransaction.begin();
.getTransaction(); final String jql = "Select f from Foo as f order by f.name desc NULLS LAST";
entityTransaction.begin(); final Query sortQuery = entityManager.createQuery(jql);
String jql = "Select f from Foo as f order by f.name asc, f.id desc"; final List<Foo> fooList = sortQuery.getResultList();
Query sortQuery = entityManager.createQuery assertNull(fooList.get(fooList.toArray().length - 1).getName());
(jql); for (final Foo foo : fooList) {
List<Foo> fooList = sortQuery.getResultList(); System.out.println("Name:" + foo.getName());
for(Foo foo:fooList){ }
System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId()); }
}
} // @Test
// public final void whenSortingByStringNullFirst_thenFirstNull() {
@Test // final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
public final void whenSortinfBar_thenPrintBarsSortedWithFoos(){ // final EntityManager entityManager = emf.createEntityManager();
EntityManagerFactory emf = Persistence // final EntityTransaction entityTransaction = entityManager.getTransaction();
.createEntityManagerFactory("punit"); // entityTransaction.begin();
EntityManager entityManager = emf.createEntityManager(); // final String jql = "Select f from Foo as f order by f.name desc NULLS FIRST";
EntityTransaction entityTransaction = entityManager // final Query sortQuery = entityManager.createQuery(jql);
.getTransaction(); // final List<Foo> fooList = sortQuery.getResultList();
entityTransaction.begin(); // assertNull(fooList.get(0).getName());
String jql = "Select b from Bar as b order by b.id"; // for (final Foo foo : fooList) {
// System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getTest_Null());
Query barQuery = entityManager.createQuery(jql); // }
List<Bar>barList = barQuery.getResultList(); // }
for(Bar bar:barList){
System.out.println("Bar Id:"+bar.getId());
for(Foo foo:bar.getFooList()){
System.out.println("FooName:"+foo.getName());
}
}
}
@Test
public final void whenSortingByStringNullLast_thenLastNull() {
EntityManagerFactory emf = Persistence @Test
.createEntityManagerFactory("punit"); public final void whenSortingByIntNull_thenException() {
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 LAST"; 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;
List<Foo> fooList = sortQuery.getResultList(); try {
assertNull(fooList.get(fooList.toArray().length-1).getName()); final List<Foo> fooList = sortQuery.getResultList();
for(Foo foo:fooList){ } catch (final javax.persistence.PersistenceException e) {
System.out.println("Name:"+foo.getName()); thrown = true;
} }
} assertTrue(thrown);
@Test }
public final void whenSortingByStringNullFirst_thenFirstNull() {
EntityManagerFactory emf = Persistence @Test
.createEntityManagerFactory("punit"); public final void whenSortingFooWithCriteria_thenPrintSortedFoos() {
EntityManager entityManager = emf.createEntityManager(); final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
EntityTransaction entityTransaction = entityManager final EntityManager entityManager = emf.createEntityManager();
.getTransaction(); final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
entityTransaction.begin(); final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
String jql = "Select f from Foo as f order by f.name desc NULLS FIRST"; final Root<Foo> from = criteriaQuery.from(Foo.class);
Query sortQuery = entityManager.createQuery final CriteriaQuery<Foo> select = criteriaQuery.select(from);
(jql); criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name")));
List<Foo> fooList = sortQuery.getResultList(); final TypedQuery<Foo> typedQuery = entityManager.createQuery(select);
assertNull(fooList.get(0).getName()); final List<Foo> fooList = typedQuery.getResultList();
for(Foo foo:fooList){ for (final Foo foo : fooList) {
System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getTest_Null()); System.out.println("Name:" + foo.getName() + "--------Id:" + foo.getId());
} }
}
@Test }
public final void whenSortingByIntNull_thenException() {
@Test
public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos() {
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
final EntityManager entityManager = emf.createEntityManager();
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
final CriteriaQuery<Foo> criteriaQuery = criteriaBuilder.createQuery(Foo.class);
final Root<Foo> from = criteriaQuery.from(Foo.class);
final CriteriaQuery<Foo> select = criteriaQuery.select(from);
criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name")), criteriaBuilder.desc(from.get("id")));
final TypedQuery<Foo> typedQuery = entityManager.createQuery(select);
final List<Foo> fooList = typedQuery.getResultList();
for (final Foo foo : fooList) {
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
}
}
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("punit");
EntityManager entityManager = emf.createEntityManager();
EntityTransaction entityTransaction = entityManager
.getTransaction();
entityTransaction.begin();
String jql = "Select f from Foo as f order by f.test_Null desc NULLS FIRST";
Query sortQuery = entityManager.createQuery
(jql);
boolean thrown = false;
try {
List<Foo> fooList = sortQuery.getResultList();
} catch (javax.persistence.PersistenceException e) {
thrown = true;
}
assertTrue(thrown);
}
@Test
public final void whenSortingFooWithCriteria_thenPrintSortedFoos(){
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("punit");
EntityManager entityManager = emf.createEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Foo> criteriaQuery = criteriaBuilder
.createQuery(Foo.class);
Root<Foo> from = criteriaQuery.from(Foo.class);
CriteriaQuery<Foo> select = criteriaQuery.select(from);
criteriaQuery.orderBy(criteriaBuilder.asc(from.get("name")));
TypedQuery<Foo> typedQuery = entityManager.createQuery(select);
List<Foo>fooList = typedQuery.getResultList();
for(Foo foo:fooList){
System.out.println("Name:"+foo.getName()+"--------Id:"+foo.getId());
}
}
@Test
public final void whenSortingFooWithCriteriaAndMultipleAttributes_thenPrintSortedFoos(){
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("punit");
EntityManager entityManager = emf.createEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Foo> criteriaQuery = criteriaBuilder
.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")));
TypedQuery<Foo> typedQuery = entityManager.createQuery(select);
List<Foo>fooList = typedQuery.getResultList();
for(Foo foo:fooList){
System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getId());
}
}
} }