HHH-8444 - Fix criteriaapi failures from JPA 2.1 TCK

This commit is contained in:
Steve Ebersole 2013-08-15 19:01:07 -05:00
parent 05144811e4
commit 6162a4b394
2 changed files with 83 additions and 2 deletions

View File

@ -499,7 +499,18 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
@Override
public <X> X get(int i, Class<X> type) {
return (X) get( i );
final Object result = get( i );
if ( result != null && ! type.isInstance( result ) ) {
throw new IllegalArgumentException(
String.format(
"Requested tuple value [index=%s, realType=%s] cannot be assigned to requested type [%s]",
i,
result.getClass().getName(),
type.getName()
)
);
}
return ( X ) result;
}
@Override
@ -661,7 +672,18 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
}
public <X> X get(int i, Class<X> type) {
return ( X ) get( i );
final Object result = get( i );
if ( result != null && ! type.isInstance( result ) ) {
throw new IllegalArgumentException(
String.format(
"Requested tuple value [index=%s, realType=%s] cannot be assigned to requested type [%s]",
i,
result.getClass().getName(),
type.getName()
)
);
}
return ( X ) result;
}
public Object[] toArray() {

View File

@ -37,13 +37,16 @@ import org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest;
import org.hibernate.jpa.test.metamodel.Customer;
import org.hibernate.jpa.test.metamodel.Customer_;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Emmanuel Bernard
*/
public class TupleCriteriaTest extends AbstractMetamodelSpecificTest {
@Test
public void testArray() {
EntityManager em = entityManagerFactory().createEntityManager();
@ -120,4 +123,60 @@ public class TupleCriteriaTest extends AbstractMetamodelSpecificTest {
em.close();
}
@Test
public void testInvalidTupleIndexAccess() {
EntityManager em = entityManagerFactory().createEntityManager();
em.getTransaction().begin();
Customer c1 = new Customer();
c1.setId( "c1" );
c1.setAge( 18 );
c1.setName( "Bob" );
em.persist( c1 );
em.getTransaction().commit();
em.close();
// the actual assertion block
em = entityManagerFactory().createEntityManager();
em.getTransaction().begin();
final CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
Root<Customer> customerRoot = criteria.from( Customer.class );
criteria.multiselect( customerRoot.get( Customer_.name ), customerRoot.get( Customer_.age ) );
List<Tuple> results = em.createQuery( criteria ).getResultList();
assertEquals( 1, results.size() );
Tuple tuple = results.get( 0 );
try {
tuple.get( 99 );
fail( "99 is invalid index" );
}
catch (IllegalArgumentException expected) {
}
try {
tuple.get( 99, String.class );
fail( "99 is invalid index" );
}
catch (IllegalArgumentException expected) {
}
tuple.get( 0, String.class );
tuple.get( 1, Integer.class );
try {
tuple.get( 0, java.util.Date.class );
fail( "Date is invalid type" );
}
catch (IllegalArgumentException expected) {
}
em.getTransaction().commit();
em.close();
em = entityManagerFactory().createEntityManager();
em.getTransaction().begin();
em.createQuery( "delete Customer" ).executeUpdate();
em.getTransaction().commit();
em.close();
}
}