HHH-10729 HHH-9021 HHH-10230 - Add test for issue

(cherry picked from commit e0963d56b9)
This commit is contained in:
Andrea Boriero 2016-06-19 12:00:15 +01:00 committed by Gail Badner
parent 99aa8dd34a
commit 903a3befb3
2 changed files with 343 additions and 0 deletions

View File

@ -0,0 +1,214 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.jpa.test.criteria.nulliteral;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Tuple;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.io.Serializable;
import java.util.List;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.testing.TestForIssue;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/**
* @author Andrea Boriero
*/
public class CriteriaLiteralInSelectExpressionTest extends BaseEntityManagerFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {MyEntity.class};
}
@Before
public void init() {
final EntityManager entityManager = getOrCreateEntityManager();
try {
entityManager.getTransaction().begin();
entityManager.persist( new MyEntity( "Fab", "A" ) );
entityManager.getTransaction().commit();
}
catch (Exception e) {
if ( entityManager.getTransaction().isActive() ) {
entityManager.getTransaction().rollback();
}
throw e;
}
finally {
entityManager.close();
}
}
@Test
@TestForIssue(jiraKey = "HHH-10729")
public void testBooleanLiteral() throws Exception {
final EntityManager entityManager = getOrCreateEntityManager();
try {
entityManager.getTransaction().begin();
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
final CriteriaQuery<MyEntityDTO> query = criteriaBuilder.createQuery( MyEntityDTO.class );
final Root<MyEntity> entity = query.from( MyEntity.class );
query.multiselect( criteriaBuilder.literal( false ), entity.get( "name" ) );
final List<MyEntityDTO> dtos = entityManager.createQuery( query ).getResultList();
assertThat( dtos.size(), is( 1 ) );
entityManager.getTransaction().commit();
}
catch (Exception e) {
if ( entityManager.getTransaction().isActive() ) {
entityManager.getTransaction().rollback();
}
throw e;
}
finally {
entityManager.close();
}
}
@Test
@TestForIssue(jiraKey = "HHH-10861")
public void testNullLiteral() throws Exception {
final EntityManager entityManager = getOrCreateEntityManager();
try {
entityManager.getTransaction().begin();
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
final CriteriaQuery<MyEntityDTO> query = criteriaBuilder.createQuery( MyEntityDTO.class );
final Root<MyEntity> entity = query.from( MyEntity.class );
query.multiselect( criteriaBuilder.literal( false ), criteriaBuilder.nullLiteral( String.class ) );
final List<MyEntityDTO> dtos = entityManager.createQuery( query ).getResultList();
assertThat( dtos.size(), is( 1 ) );
entityManager.getTransaction().commit();
}
catch (Exception e) {
if ( entityManager.getTransaction().isActive() ) {
entityManager.getTransaction().rollback();
}
throw e;
}
finally {
entityManager.close();
}
}
@Test
@TestForIssue(jiraKey = "HHH-10729")
public void testStringLiteral() throws Exception {
final EntityManager entityManager = getOrCreateEntityManager();
try {
entityManager.getTransaction().begin();
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
final CriteriaQuery<MyEntityDTO> query = criteriaBuilder.createQuery( MyEntityDTO.class );
final Root<MyEntity> entity = query.from( MyEntity.class );
query.multiselect( criteriaBuilder.literal( "Leo" ), entity.get( "surname" ) );
final List<MyEntityDTO> dtos = entityManager.createQuery( query ).getResultList();
assertThat( dtos.size(), is( 1 ) );
entityManager.getTransaction().commit();
}
catch (Exception e) {
if ( entityManager.getTransaction().isActive() ) {
entityManager.getTransaction().rollback();
}
throw e;
}
finally {
entityManager.close();
}
}
@Test
@TestForIssue(jiraKey = "HHH-9021")
public void testStringLiteral2() {
final EntityManager entityManager = getOrCreateEntityManager();
try {
final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
final CriteriaQuery<Tuple> criteriaQuery = builder.createQuery( Tuple.class );
criteriaQuery.from( MyEntity.class );
criteriaQuery.multiselect( builder.equal( builder.literal( 1 ), builder.literal( 2 ) ) );
final TypedQuery<Tuple> typedQuery = entityManager.createQuery( criteriaQuery );
final List<Tuple> results = typedQuery.getResultList();
assertThat( results.size(), is( 1 ) );
}
finally {
entityManager.close();
}
}
@Entity(name = "MyEntity")
@Table(name = "MY_ENTITY")
public static class MyEntity implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue
private Integer id;
private String name;
private String surname;
public MyEntity() {
}
public MyEntity(String name, String surname) {
this.name = name;
this.surname = surname;
}
public MyEntity(String name, boolean surname) {
this.name = name;
}
}
public static class MyEntityDTO {
private String name;
private String surname;
private boolean active;
public MyEntityDTO() {
}
public MyEntityDTO(String name, String surname) {
this.name = name;
this.surname = surname;
}
public MyEntityDTO(boolean active, String name) {
this.name = name;
this.active = active;
}
}
}

View File

@ -0,0 +1,129 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.jpa.test.query;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.List;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.testing.TestForIssue;
/**
* @author Andrea Boriero
*/
public class QueryWithLiteralsInSelectExpressionTest extends BaseEntityManagerFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {MyEntity.class};
}
@Before
public void init() {
final EntityManager entityManager = getOrCreateEntityManager();
try {
entityManager.getTransaction().begin();
entityManager.persist( new MyEntity( "Fab", "A" ) );
entityManager.getTransaction().commit();
}
catch (Exception e) {
if ( entityManager.getTransaction().isActive() ) {
entityManager.getTransaction().rollback();
}
throw e;
}
finally {
entityManager.close();
}
}
@Test
@TestForIssue(jiraKey = "HHH-10230")
public void testSelectLiterals() {
final EntityManager entityManager = getOrCreateEntityManager();
try {
final List<Object[]> elements = entityManager.createQuery(
"SELECT true, false, e.name FROM MyEntity e",
Object[].class
).getResultList();
Assert.assertEquals( 1, elements.size() );
}
finally {
entityManager.close();
}
}
@Test
@TestForIssue(jiraKey = "HHH-10230")
public void testSelectNullLiterals() {
final EntityManager entityManager = getOrCreateEntityManager();
try {
final List<Object[]> elements = entityManager.createQuery(
"SELECT cast(null as boolean), false, e.name FROM MyEntity e",
Object[].class
).getResultList();
Assert.assertEquals( 1, elements.size() );
}
finally {
entityManager.close();
}
}
@Entity(name = "MyEntity")
@Table(name = "MY_ENTITY")
public static class MyEntity implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue
private Integer id;
private String name;
private String surname;
public MyEntity() {
}
public MyEntity(String name, String surname) {
this.name = name;
this.surname = surname;
}
public MyEntity(String name, boolean surname) {
this.name = name;
}
}
public static class MyEntityDTO {
private String name;
private String surname;
private boolean active;
public MyEntityDTO() {
}
public MyEntityDTO(String name, String surname) {
this.name = name;
this.surname = surname;
}
public MyEntityDTO(String name, boolean active) {
this.name = name;
this.active = active;
}
}
}