HHH-12129 - Fix expected exceptions on various Query methods

This commit is contained in:
Steve Ebersole 2017-12-05 16:51:58 -06:00
parent bb19c30679
commit 6dc94ff7f7
2 changed files with 101 additions and 12 deletions

View File

@ -705,18 +705,6 @@ public abstract class AbstractProducedQuery<R> implements QueryImplementor<R> {
@Override
public QueryParameter<?> getParameter(int position) {
// It is important to understand that there are 2 completely distinct conceptualization of
// "positional parameters" in play here:
// 1) The legacy Hibernate concept is akin to JDBC PreparedStatement parameters. Very limited and
// deprecated since 5.x. These are numbered starting from 0 and kept in the
// ParameterMetadata positional-parameter array keyed by this zero-based position
// 2) JPA's definition is really just a named parameter, but expected to explicitly be
// sequential integers starting from 1 (ONE); they can repeat.
//
// It is considered illegal to mix positional-parameter with named parameters of any kind. So therefore.
// if ParameterMetadata reports that it has any positional-parameters it is talking about the
// legacy Hibernate concept.
// lookup jpa-based positional parameters first by name.
getProducer().checkOpen( false );
try {
if ( getParameterMetadata().getPositionalParameterCount() == 0 ) {
@ -738,6 +726,7 @@ public abstract class AbstractProducedQuery<R> implements QueryImplementor<R> {
@Override
@SuppressWarnings("unchecked")
public <T> QueryParameter<T> getParameter(int position, Class<T> type) {
getProducer().checkOpen( false );
try {
final QueryParameter parameter = getParameterMetadata().getQueryParameter( position );
if ( !parameter.getParameterType().isAssignableFrom( type ) ) {
@ -909,6 +898,8 @@ public abstract class AbstractProducedQuery<R> implements QueryImplementor<R> {
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setMaxResults(int maxResult) {
getProducer().checkOpen();
if ( maxResult < 0 ) {
throw new IllegalArgumentException( "max-results cannot be negative" );
}

View File

@ -0,0 +1,98 @@
/*
* 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.test.jpa.compliance.tck2_2;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.hibernate.test.jpa.AbstractJPATest;
import org.junit.Test;
import org.hamcrest.CoreMatchers;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
/**
* @author Steve Ebersole
*/
public class ClosedManagerTests extends AbstractJPATest {
@Test
public void testQuerySetMaxResults() {
final Session session = sessionFactory().openSession();
final Query qry = session.createQuery( "select i from Item i" );
session.close();
assertThat( session.isOpen(), CoreMatchers.is ( false ) );
try {
qry.setMaxResults( 1 );
fail( "Expecting call to fail" );
}
catch (IllegalStateException expected) {
}
}
@Test
public void testQuerySetFirstResult() {
final Session session = sessionFactory().openSession();
final Query qry = session.createQuery( "select i from Item i" );
session.close();
assertThat( session.isOpen(), CoreMatchers.is ( false ) );
try {
qry.setFirstResult( 1 );
fail( "Expecting call to fail" );
}
catch (IllegalStateException expected) {
}
}
@Test
public void testQuerySetParameter() {
final Session session = sessionFactory().openSession();
final Query qry = session.createQuery( "select i from Item i where i.id = ?1" );
session.close();
assertThat( session.isOpen(), CoreMatchers.is ( false ) );
try {
qry.setParameter( 1, 1 );
fail( "Expecting call to fail" );
}
catch (IllegalStateException expected) {
}
}
@Test
public void testQueryGetParameter() {
final Session session = sessionFactory().openSession();
final Query qry = session.createQuery( "select i from Item i where i.id = ?1" );
qry.setParameter( 1, 1 );
session.close();
assertThat( session.isOpen(), CoreMatchers.is ( false ) );
try {
qry.getParameter( 1 );
fail( "Expecting call to fail" );
}
catch (IllegalStateException expected) {
}
try {
qry.getParameter( 1, Integer.class );
fail( "Expecting call to fail" );
}
catch (IllegalStateException expected) {
}
}
}