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,180 +1,149 @@
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 javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.OrderBy;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Root;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.annotations.common.util.StringHelper;
import org.baeldung.persistence.model.Bar;
import org.baeldung.persistence.model.Foo;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.cc.jpa.example.Foo;
import com.cc.jpa.example.Bar;
public class FooServiceSortingTests {
private EntityManager entityManager;
@BeforeClass
public static void before() {
//
}
@After
public final void after() {
//
}
@Test
public final void whenSortingByOneAttributeDefaultOrder_thenPrintSortedResult() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("punit");
EntityManager entityManager = emf.createEntityManager();
EntityTransaction entityTransaction = entityManager
.getTransaction();
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
final EntityManager entityManager = emf.createEntityManager();
final 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();
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(Foo foo:fooList){
for (final Foo foo : fooList) {
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
}
}
@Test
public final void whenSortingByOneAttributeSetOrder_thenSortedPrintResult() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("punit");
EntityManager entityManager = emf.createEntityManager();
EntityTransaction entityTransaction = entityManager
.getTransaction();
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
final EntityManager entityManager = emf.createEntityManager();
final EntityTransaction entityTransaction = entityManager.getTransaction();
entityTransaction.begin();
String jql = "Select f from Foo as f order by f.id desc";
Query sortQuery = entityManager.createQuery
(jql);
List<Foo> fooList = sortQuery.getResultList();
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(Foo foo:fooList){
for (final Foo foo : fooList) {
System.out.println("Name:" + foo.getName() + "-------Id:" + foo.getId());
}
}
@Test
public final void whenSortingByTwoAttributes_thenPrintSortedResult() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("punit");
EntityManager entityManager = emf.createEntityManager();
EntityTransaction entityTransaction = entityManager
.getTransaction();
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
final EntityManager entityManager = emf.createEntityManager();
final EntityTransaction entityTransaction = entityManager.getTransaction();
entityTransaction.begin();
String jql = "Select f from Foo as f order by f.name asc, f.id desc";
Query sortQuery = entityManager.createQuery
(jql);
List<Foo> fooList = sortQuery.getResultList();
for(Foo foo:fooList){
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());
}
}
@Test
public final void whenSortinfBar_thenPrintBarsSortedWithFoos() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("punit");
EntityManager entityManager = emf.createEntityManager();
EntityTransaction entityTransaction = entityManager
.getTransaction();
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
final EntityManager entityManager = emf.createEntityManager();
final EntityTransaction entityTransaction = entityManager.getTransaction();
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);
List<Bar>barList = barQuery.getResultList();
for(Bar bar:barList){
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(Foo foo:bar.getFooList()){
for (final Foo foo : bar.getFooList()) {
System.out.println("FooName:" + foo.getName());
}
}
}
@Test
public final void whenSortingByStringNullLast_thenLastNull() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("punit");
EntityManager entityManager = emf.createEntityManager();
EntityTransaction entityTransaction = entityManager
.getTransaction();
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
final EntityManager entityManager = emf.createEntityManager();
final EntityTransaction entityTransaction = entityManager.getTransaction();
entityTransaction.begin();
String jql = "Select f from Foo as f order by f.name desc NULLS LAST";
Query sortQuery = entityManager.createQuery
(jql);
List<Foo> fooList = sortQuery.getResultList();
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(Foo foo:fooList){
for (final Foo foo : fooList) {
System.out.println("Name:" + foo.getName());
}
}
@Test
public final void whenSortingByStringNullFirst_thenFirstNull() {
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.name desc NULLS FIRST";
Query sortQuery = entityManager.createQuery
(jql);
List<Foo> fooList = sortQuery.getResultList();
assertNull(fooList.get(0).getName());
for(Foo foo:fooList){
System.out.println("Name:"+foo.getName()+"-------Id:"+foo.getTest_Null());
}
}
// @Test
// public final void whenSortingByStringNullFirst_thenFirstNull() {
// 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 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() + "-------Id:" + foo.getTest_Null());
// }
// }
@Test
public final void whenSortingByIntNull_thenException() {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("punit");
EntityManager entityManager = emf.createEntityManager();
EntityTransaction entityTransaction = entityManager
.getTransaction();
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("punit");
final EntityManager entityManager = emf.createEntityManager();
final 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);
final String jql = "Select f from Foo as f order by f.test_Null desc NULLS FIRST";
final Query sortQuery = entityManager.createQuery(jql);
boolean thrown = false;
try {
List<Foo> fooList = sortQuery.getResultList();
} catch (javax.persistence.PersistenceException e) {
final List<Foo> fooList = sortQuery.getResultList();
} catch (final javax.persistence.PersistenceException e) {
thrown = true;
}
assertTrue(thrown);
@ -182,19 +151,16 @@ public class FooServiceSortingTests {
@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);
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")));
TypedQuery<Foo> typedQuery = entityManager.createQuery(select);
List<Foo>fooList = typedQuery.getResultList();
for(Foo foo:fooList){
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());
}
@ -202,19 +168,16 @@ public class FooServiceSortingTests {
@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);
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")));
TypedQuery<Foo> typedQuery = entityManager.createQuery(select);
List<Foo>fooList = typedQuery.getResultList();
for(Foo foo:fooList){
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());
}
}