HHH-13300 Test behavior when query.getSingleResult() throws an exception

(cherry picked from commit 53f70ab213)
This commit is contained in:
Yoann Rodière 2019-03-05 10:18:18 +01:00 committed by gbadner
parent 7559ecf196
commit 8d4c47bffc
3 changed files with 134 additions and 67 deletions

View File

@ -7,6 +7,7 @@
package org.hibernate.test.exceptionhandling;
import java.sql.SQLException;
import javax.persistence.NoResultException;
import javax.persistence.OptimisticLockException;
import javax.persistence.PersistenceException;
import javax.persistence.RollbackException;
@ -54,6 +55,16 @@ interface ExceptionExpectations {
assertThat( e.getCause(), instanceOf( QuerySyntaxException.class ) );
}
@Override
public void onGetSingleResultWithMultipleResults(RuntimeException e) {
assertThat( e, instanceOf( javax.persistence.NonUniqueResultException.class ) );
}
@Override
public void onGetSingleResultWithNoResults(RuntimeException e) {
assertThat( e, instanceOf( NoResultException.class ) );
}
@Override
public void onStaleObjectMergeAndUpdateFlush(RuntimeException e) {
assertThat( e, instanceOf( OptimisticLockException.class ) );
@ -115,6 +126,16 @@ interface ExceptionExpectations {
assertThat( e, instanceOf( QuerySyntaxException.class ) );
}
@Override
public void onGetSingleResultWithMultipleResults(RuntimeException e) {
assertThat( e, instanceOf( org.hibernate.NonUniqueResultException.class ) );
}
@Override
public void onGetSingleResultWithNoResults(RuntimeException e) {
assertThat( e, instanceOf( NoResultException.class ) );
}
@Override
public void onStaleObjectMergeAndUpdateFlush(RuntimeException e) {
assertThat( e, instanceOf( StaleObjectStateException.class ) );
@ -174,6 +195,16 @@ interface ExceptionExpectations {
assertThat( e.getCause(), instanceOf( QuerySyntaxException.class ) );
}
@Override
public void onGetSingleResultWithMultipleResults(RuntimeException e) {
assertThat( e, instanceOf( javax.persistence.NonUniqueResultException.class ) );
}
@Override
public void onGetSingleResultWithNoResults(RuntimeException e) {
assertThat( e, instanceOf( NoResultException.class ) );
}
@Override
public void onStaleObjectMergeAndUpdateFlush(RuntimeException e) {
assertThat( e, instanceOf( OptimisticLockException.class ) );
@ -215,6 +246,10 @@ interface ExceptionExpectations {
void onInvalidQueryExecuted(RuntimeException e);
void onGetSingleResultWithMultipleResults(RuntimeException e);
void onGetSingleResultWithNoResults(RuntimeException e);
void onStaleObjectMergeAndUpdateFlush(RuntimeException e);
void onIdentifierGeneratorFailure(RuntimeException e);

View File

@ -0,0 +1,99 @@
/*
* 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.exceptionhandling;
import static org.junit.Assert.fail;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.transaction.TransactionUtil2;
import org.junit.Before;
import org.junit.Test;
@RequiresDialect(H2Dialect.class)
public class QueryExceptionHandlingTest extends BaseExceptionHandlingTest {
public QueryExceptionHandlingTest(
BootstrapMethod bootstrapMethod,
ExceptionHandlingSetting exceptionHandlingSetting,
ExceptionExpectations exceptionExpectations) {
super( bootstrapMethod, exceptionHandlingSetting, exceptionExpectations );
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
A.class
};
}
@Before
public void initData() {
TransactionUtil2.inTransaction( sessionFactory(), s -> {
s.createQuery( "delete from A" ).executeUpdate();
A a1 = new A();
a1.id = 1;
s.persist( a1 );
A a2 = new A();
a2.id = 2;
s.persist( a2 );
} );
}
@Test
@TestForIssue(jiraKey = "HHH-12666")
public void testInvalidQuery() {
try {
TransactionUtil2.inSession( sessionFactory(), s -> {
s.createQuery( "from A where blahblahblah" ).list();
} );
fail( "should have thrown an exception" );
}
catch (RuntimeException expected) {
exceptionExpectations.onInvalidQueryExecuted( expected );
}
}
@Test
@TestForIssue(jiraKey = "HHH-13300")
public void testGetSingleResultWithMultipleResults() {
try {
TransactionUtil2.inSession( sessionFactory(), s -> {
s.createQuery( "from A where id in (1, 2)" ).getSingleResult();
} );
fail( "should have thrown an exception" );
}
catch (RuntimeException expected) {
exceptionExpectations.onGetSingleResultWithMultipleResults( expected );
}
}
@Test
@TestForIssue(jiraKey = "HHH-13300")
public void testGetSingleResultWithNoResults() {
try {
TransactionUtil2.inSession( sessionFactory(), s -> {
s.createQuery( "from A where id = 3" ).getSingleResult();
} );
fail( "should have thrown an exception" );
}
catch (RuntimeException expected) {
exceptionExpectations.onGetSingleResultWithNoResults( expected );
}
}
@Entity(name = "A")
public static class A {
@Id
private long id;
}
}

View File

@ -1,67 +0,0 @@
/*
* 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.exceptionhandling;
import static org.junit.Assert.fail;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
@TestForIssue(jiraKey = "HHH-12666")
@RequiresDialect(H2Dialect.class)
public class QuerySyntaxExceptionHandlingTest extends BaseExceptionHandlingTest {
public QuerySyntaxExceptionHandlingTest(
BootstrapMethod bootstrapMethod,
ExceptionHandlingSetting exceptionHandlingSetting,
ExceptionExpectations exceptionExpectations) {
super( bootstrapMethod, exceptionHandlingSetting, exceptionExpectations );
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
A.class
};
}
@Test
public void testInvalidQuery() {
Session s = openSession();
Transaction tx = s.beginTransaction();
A a = new A();
a.id = 1;
s.persist( a );
s.flush();
s.clear();
try {
s.createQuery( "from A where blahblahblah" ).list();
fail( "should have thrown an exception" );
}
catch (RuntimeException expected) {
exceptionExpectations.onInvalidQueryExecuted( expected );
}
finally {
tx.rollback();
s.close();
}
}
@Entity(name = "A")
public static class A {
@Id
private long id;
}
}