Skip to content

Commit

Permalink
HHH-12129 - Fix expected exceptions on various Query methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Dec 5, 2017
1 parent 3521e11 commit 97a7d46
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 12 deletions.
Expand Up @@ -705,18 +705,6 @@ public <T> QueryParameter<T> getParameter(String name, Class<T> type) {

@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 ) {
Expand All @@ -738,6 +726,7 @@ public QueryParameter<?> getParameter(int position) {
@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 ) ) {
Expand Down Expand Up @@ -909,6 +898,8 @@ public int getMaxResults() {
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setMaxResults(int maxResult) {
getProducer().checkOpen();

if ( maxResult < 0 ) {
throw new IllegalArgumentException( "max-results cannot be negative" );
}
Expand Down
@@ -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) {
}
}
}

0 comments on commit 97a7d46

Please sign in to comment.