Fix some tests

This commit is contained in:
Andrea Boriero 2016-04-22 16:17:06 +01:00 committed by Steve Ebersole
parent e3ab793fe8
commit a0da5fe677
28 changed files with 275 additions and 130 deletions

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.test.annotations;
import javax.persistence.PersistenceException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
@ -221,7 +222,7 @@ public class EntityTest extends BaseNonConfigCoreFunctionalTestCase {
tx.commit();
fail( "unique constraints not respected" );
}
catch (HibernateException e) {
catch (PersistenceException e) {
//success
if ( tx != null ) {
tx.rollback();
@ -273,8 +274,13 @@ public class EntityTest extends BaseNonConfigCoreFunctionalTestCase {
tx.commit();
fail( "Optimistic locking should work" );
}
catch (StaleStateException expected) {
// expected exception
catch (PersistenceException expected) {
if ( expected.getCause() instanceof StaleStateException ) {
//expected
}
else {
fail( "StaleStateException expected but is " + expected.getCause() );
}
}
finally {
if ( tx != null ) {

View File

@ -8,6 +8,7 @@ package org.hibernate.test.annotations.beanvalidation;
import java.math.BigDecimal;
import java.util.Map;
import javax.persistence.PersistenceException;
import javax.validation.ConstraintViolationException;
import org.hibernate.Session;
@ -102,15 +103,19 @@ public class DDLWithoutCallbackTest extends BaseNonConfigCoreFunctionalTestCase
s.flush();
fail( "expecting SQL constraint violation" );
}
catch ( ConstraintViolationException e ) {
fail( "invalid object should not be validated" );
}
catch ( org.hibernate.exception.ConstraintViolationException e ) {
if ( getDialect().supportsColumnCheck() ) {
// expected
catch (PersistenceException pe) {
final Throwable cause = pe.getCause();
if ( cause instanceof ConstraintViolationException ) {
fail( "invalid object should not be validated" );
}
else {
fail( "Unexpected SQL constraint violation [" + e.getConstraintName() + "] : " + e.getSQLException() );
else if ( cause instanceof org.hibernate.exception.ConstraintViolationException ) {
if ( getDialect().supportsColumnCheck() ) {
// expected
}
else {
org.hibernate.exception.ConstraintViolationException cve = (org.hibernate.exception.ConstraintViolationException) cause;
fail( "Unexpected SQL constraint violation [" + cve.getConstraintName() + "] : " + cve.getSQLException() );
}
}
}
tx.rollback();

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.test.annotations.immutable;
import javax.persistence.PersistenceException;
import java.util.ArrayList;
import java.util.List;
@ -102,9 +103,10 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase {
tx.commit();
fail();
}
catch (HibernateException e) {
assertTrue(e.getMessage().contains("changed an immutable collection instance"));
log.debug("success");
catch ( PersistenceException ex ) {
// expected
assertTrue(ex.getMessage().contains("changed an immutable collection instance"));
log.debug("success");
}
s.close();
@ -119,7 +121,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase {
try {
tx.commit();
fail();
} catch (HibernateException e) {
} catch (PersistenceException e) {
assertTrue(e.getMessage().contains("changed an immutable collection instance"));
log.debug("success");
}

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.test.annotations.join;
import javax.persistence.PersistenceException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
@ -18,11 +19,13 @@ import org.hibernate.Transaction;
import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl;
import org.hibernate.criterion.Restrictions;
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.mapping.Join;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@ -162,9 +165,14 @@ public class JoinTest extends BaseNonConfigCoreFunctionalTestCase {
tx.commit();
fail( "unique constraints violation on secondary table" );
}
catch (HibernateException e) {
//success
tx.rollback();
catch (PersistenceException e) {
try {
assertTyping( ConstraintViolationException.class, e.getCause() );
//success
}
finally {
tx.rollback();
}
}
finally {
s.close();

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.test.annotations.onetomany;
import javax.persistence.PersistenceException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
@ -19,6 +20,7 @@ import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Table;
@ -33,6 +35,7 @@ import org.hibernate.test.annotations.TicketComparator;
import org.junit.Assert;
import org.junit.Test;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@ -161,9 +164,15 @@ public class OneToManyTest extends BaseNonConfigCoreFunctionalTestCase {
tx.commit();
fail( "A one to many should not allow several trainer per Tiger" );
}
catch ( HibernateException ce ) {
tx.rollback();
//success
catch (PersistenceException ce) {
try {
assertTyping( ConstraintViolationException.class, ce.getCause() );
//success
}
finally {
tx.rollback();
}
}
s.close();
}

View File

@ -6,6 +6,8 @@
*/
package org.hibernate.test.annotations.onetoone;
import javax.persistence.PersistenceException;
import org.junit.Test;
import org.hibernate.Session;
@ -14,6 +16,7 @@ import org.hibernate.criterion.Restrictions;
import org.hibernate.id.IdentifierGenerationException;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@ -37,7 +40,8 @@ public class OptionalOneToOneMappedByTest extends BaseCoreFunctionalTestCase {
s.flush();
fail( "should have failed with IdentifierGenerationException" );
}
catch (IdentifierGenerationException ex) {
catch (PersistenceException ex) {
assertTyping(IdentifierGenerationException.class, ex.getCause());
// expected
}
finally {

View File

@ -6,6 +6,8 @@
*/
package org.hibernate.test.annotations.onetoone;
import javax.persistence.PersistenceException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
@ -15,6 +17,7 @@ import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@ -38,7 +41,8 @@ public class OptionalOneToOnePKJCTest extends BaseCoreFunctionalTestCase {
s.flush();
fail( "should have thrown IdentifierGenerationException.");
}
catch ( IdentifierGenerationException ex ) {
catch (PersistenceException ex) {
assertTyping(IdentifierGenerationException.class, ex.getCause());
// expected
}
finally {
@ -61,7 +65,8 @@ public class OptionalOneToOnePKJCTest extends BaseCoreFunctionalTestCase {
s.flush();
fail( "should have thrown IdentifierGenerationException.");
}
catch ( IdentifierGenerationException ex ) {
catch (PersistenceException ex) {
assertTyping(IdentifierGenerationException.class, ex.getCause());
// expected
}
finally {

View File

@ -6,20 +6,25 @@
*/
package org.hibernate.test.annotations.onetoone.hhh9798;
import javax.persistence.PersistenceException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.id.IdentifierGenerationException;
import org.junit.Test;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.fail;
@TestForIssue(jiraKey = "HHH-9798")
public class OneToOneJoinTableTest extends BaseCoreFunctionalTestCase {
@Test(expected = org.hibernate.exception.ConstraintViolationException.class)
@Test
public void storeNonUniqueRelationship() throws Throwable {
Session session = null;
try {
@ -38,6 +43,9 @@ public class OneToOneJoinTableTest extends BaseCoreFunctionalTestCase {
tx.commit();
fail();
}catch (PersistenceException e){
assertTyping( ConstraintViolationException.class, e.getCause());
// expected
}
finally {
if ( session != null ) {

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.test.annotations.override;
import javax.persistence.PersistenceException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@ -45,7 +46,7 @@ public class AssociationOverrideTest extends BaseNonConfigCoreFunctionalTestCase
s.flush();
fail( "Should be non nullable" );
}
catch (HibernateException e) {
catch (PersistenceException e) {
//success
}
finally {

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.test.annotations.tableperclass;
import javax.persistence.PersistenceException;
import java.util.List;
import org.junit.Test;
@ -16,6 +17,7 @@ import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
@ -74,13 +76,16 @@ public class TablePerClassTest extends BaseCoreFunctionalTestCase {
s.persist( product2 );
try {
s.flush();
fail("Database Exception not handled");
fail( "Database Exception not handled" );
}
catch( JDBCException e ) {
catch (PersistenceException e) {
assertTyping( JDBCException.class, e.getCause() );
//success
}
tx.rollback();
s.close();
finally {
tx.rollback();
s.close();
}
}
@Override

View File

@ -6,12 +6,15 @@
*/
package org.hibernate.test.annotations.uniqueconstraint;
import javax.persistence.PersistenceException;
import org.hibernate.JDBCException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.fail;
/**
@ -52,12 +55,16 @@ public class UniqueConstraintTest extends BaseCoreFunctionalTestCase {
s.persist(house2);
try {
s.flush();
fail("Database constraint non-existant");
} catch(JDBCException e) {
fail( "Database constraint non-existant" );
}
catch (PersistenceException e) {
assertTyping( JDBCException.class, e.getCause() );
//success
}
tx.rollback();
s.close();
finally {
tx.rollback();
s.close();
}
}
}

View File

@ -6,6 +6,9 @@
*/
package org.hibernate.test.any;
import javax.persistence.PersistenceException;
import org.hibernate.JDBCException;
import org.hibernate.Session;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
@ -15,6 +18,8 @@ import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
/**
* @author Steve Ebersole
*/
@ -57,13 +62,25 @@ public class AnyTypeTest extends BaseCoreFunctionalTestCase {
session.close();
}
@Test( expected = QuerySyntaxException.class )
@Test
public void testJoinFetchOfAnAnyTypeAttribute() {
// Query translator should dis-allow join fetching of an <any/> mapping. Let's make sure it does...
Session session = openSession();
session.beginTransaction();
session.createQuery( "select p from Person p join fetch p.data" ).list();
session.getTransaction().commit();
session.close();
try {
session.beginTransaction();
session.createQuery( "select p from Person p join fetch p.data" ).list();
session.getTransaction().commit();
}
catch (IllegalArgumentException e) {
//expected
assertTyping( QuerySyntaxException.class, e.getCause() );
session.getTransaction().rollback();
}
catch (QuerySyntaxException qe) {
//expected
}
finally {
session.close();
}
}
}

View File

@ -5,6 +5,9 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.cut;
import javax.persistence.PersistenceException;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import java.math.BigDecimal;
@ -222,7 +225,7 @@ public class CompositeUserTypeTest extends BaseCoreFunctionalTestCase {
* Tests the {@code <} operator on composite types. As long as we don't support it, we need to throw an exception
* rather than create a random query.
*/
@Test( expected = QuerySyntaxException.class )
@Test
@TestForIssue( jiraKey = "HHH-5946" )
@RequiresDialectFeature( value = DialectChecks.DoesNotSupportRowValueConstructorSyntax.class )
public void testLessThanOperator() {
@ -232,6 +235,10 @@ public class CompositeUserTypeTest extends BaseCoreFunctionalTestCase {
q.setParameter( "amount", new MonetoryAmount( BigDecimal.ZERO, Currency.getInstance( "EUR" ) ) );
q.list();
}
catch (IllegalArgumentException e) {
assertTyping( QuerySyntaxException.class, e.getCause() );
//expected
}
finally {
s.close();
}
@ -241,7 +248,7 @@ public class CompositeUserTypeTest extends BaseCoreFunctionalTestCase {
* Tests the {@code <=} operator on composite types. As long as we don't support it, we need to throw an exception
* rather than create a random query.
*/
@Test( expected = QuerySyntaxException.class )
@Test
@TestForIssue( jiraKey = "HHH-5946" )
@RequiresDialectFeature( value = DialectChecks.DoesNotSupportRowValueConstructorSyntax.class )
public void testLessOrEqualOperator() {
@ -251,6 +258,10 @@ public class CompositeUserTypeTest extends BaseCoreFunctionalTestCase {
q.setParameter( "amount", new MonetoryAmount( BigDecimal.ZERO, Currency.getInstance( "USD" ) ) );
q.list();
}
catch (IllegalArgumentException e) {
assertTyping( QuerySyntaxException.class, e.getCause() );
//expected
}
finally {
s.close();
}
@ -260,7 +271,7 @@ public class CompositeUserTypeTest extends BaseCoreFunctionalTestCase {
* Tests the {@code >} operator on composite types. As long as we don't support it, we need to throw an exception
* rather than create a random query.
*/
@Test( expected = QuerySyntaxException.class )
@Test
@TestForIssue( jiraKey = "HHH-5946" )
@RequiresDialectFeature( value = DialectChecks.DoesNotSupportRowValueConstructorSyntax.class )
public void testGreaterThanOperator() {
@ -270,6 +281,10 @@ public class CompositeUserTypeTest extends BaseCoreFunctionalTestCase {
q.setParameter( "amount", new MonetoryAmount( BigDecimal.ZERO, Currency.getInstance( "EUR" ) ) );
q.list();
}
catch (IllegalArgumentException e) {
assertTyping( QuerySyntaxException.class, e.getCause() );
//expected
}
finally {
s.close();
}
@ -279,7 +294,7 @@ public class CompositeUserTypeTest extends BaseCoreFunctionalTestCase {
* Tests the {@code >=} operator on composite types. As long as we don't support it, we need to throw an exception
* rather than create a random query.
*/
@Test( expected = QuerySyntaxException.class )
@Test
@TestForIssue( jiraKey = "HHH-5946" )
@RequiresDialectFeature( value = DialectChecks.DoesNotSupportRowValueConstructorSyntax.class )
public void testGreaterOrEqualOperator() {
@ -289,6 +304,10 @@ public class CompositeUserTypeTest extends BaseCoreFunctionalTestCase {
q.setParameter( "amount", new MonetoryAmount( BigDecimal.ZERO, Currency.getInstance( "USD" ) ) );
q.list();
}
catch (IllegalArgumentException e) {
assertTyping( QuerySyntaxException.class, e.getCause() );
//expected
}
finally {
s.close();
}

View File

@ -5,6 +5,7 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.immutable;
import javax.persistence.PersistenceException;
import java.util.Iterator;
import org.junit.Test;
@ -859,7 +860,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase {
t.commit();
fail( "should have failed because reassociated object has a dirty collection");
}
catch ( HibernateException ex ) {
catch ( PersistenceException ex ) {
// expected
}
finally {
@ -1082,7 +1083,7 @@ public class ImmutableTest extends BaseCoreFunctionalTestCase {
t.commit();
fail( "should have failed because an immutable collection was changed");
}
catch ( HibernateException ex ) {
catch ( PersistenceException ex ) {
// expected
t.rollback();
}

View File

@ -5,6 +5,7 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.immutable.entitywithmutablecollection;
import javax.persistence.PersistenceException;
import java.util.Iterator;
import org.junit.Test;
@ -21,6 +22,7 @@ import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
@ -860,7 +862,8 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona
s.merge( pOrig );
assertFalse( isContractVersioned );
}
catch (StaleObjectStateException ex) {
catch (PersistenceException ex) {
assertTyping(StaleObjectStateException.class, ex.getCause());
assertTrue( isContractVersioned);
}
finally {
@ -918,11 +921,14 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona
t.commit();
assertFalse( isContractVersioned );
}
catch (StaleStateException ex) {
catch (PersistenceException ex) {
t.rollback();
assertTrue( isContractVersioned );
if ( ! sessionFactory().getSessionFactoryOptions().isJdbcBatchVersionedData() ) {
assertTrue( StaleObjectStateException.class.isInstance( ex ) );
if ( !sessionFactory().getSessionFactoryOptions().isJdbcBatchVersionedData() ) {
assertTyping( StaleObjectStateException.class, ex.getCause() );
}
else {
assertTyping( StaleStateException.class, ex.getCause() );
}
}
s.close();

View File

@ -5,6 +5,8 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.immutable.entitywithmutablecollection;
import javax.persistence.PersistenceException;
import org.junit.Test;
import org.hibernate.QueryException;
@ -20,6 +22,7 @@ import org.hibernate.criterion.Restrictions;
import org.hibernate.internal.SessionFactoryImpl;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
@ -1087,7 +1090,8 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional
s.merge( cOrig );
assertFalse( isContractVersioned );
}
catch (StaleObjectStateException ex) {
catch (PersistenceException ex) {
assertTyping(StaleObjectStateException.class, ex.getCause());
assertTrue( isContractVersioned);
}
finally {
@ -1145,11 +1149,14 @@ public abstract class AbstractEntityWithOneToManyTest extends BaseCoreFunctional
t.commit();
assertFalse( isContractVersioned );
}
catch (StaleStateException ex) {
catch (PersistenceException ex) {
t.rollback();
assertTrue( isContractVersioned );
if ( ! sessionFactory().getSessionFactoryOptions().isJdbcBatchVersionedData() ) {
assertTrue( StaleObjectStateException.class.isInstance( ex ) );
if ( !sessionFactory().getSessionFactoryOptions().isJdbcBatchVersionedData() ) {
assertTyping( StaleObjectStateException.class, ex.getCause() );
}
else {
assertTyping( StaleStateException.class, ex.getCause() );
}
}
s.close();

View File

@ -5,6 +5,7 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.interceptor;
import javax.persistence.PersistenceException;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
@ -23,6 +24,7 @@ import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.type.Type;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@ -139,11 +141,12 @@ public class InterceptorTest extends BaseCoreFunctionalTestCase {
t.commit();
fail( "Transaction should have timed out" );
}
catch (TransactionException e) {
catch (PersistenceException e){
assertTyping(TransactionException.class, e.getCause());
assertTrue(
"Transaction failed for the wrong reason. Expecting transaction timeout, but found [" +
e.getMessage() + "]" ,
e.getMessage().contains( "transaction timeout expired" )
e.getCause().getMessage() + "]" ,
e.getCause().getMessage().contains( "transaction timeout expired" )
);
}
}

View File

@ -10,11 +10,10 @@ import org.jboss.logging.Logger;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.TransactionException;
import org.hibernate.TransientObjectException;
import org.hibernate.test.jpa.AbstractJPATest;
import static org.junit.Assert.assertTrue;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.fail;
/**
@ -53,8 +52,9 @@ public class CascadeTest extends AbstractJPATest {
s.getTransaction().commit();
fail( "expecting TransientObjectException on flush" );
}
catch (TransientObjectException toe) {
log.trace( "handled expected exception", toe );
catch (IllegalStateException e) {
assertTyping( TransientObjectException.class, e.getCause() );
log.trace( "handled expected exception", e );
s.getTransaction().rollback();
}
finally {

View File

@ -6,6 +6,8 @@
*/
package org.hibernate.test.jpa.naturalid;
import javax.persistence.PersistenceException;
import org.junit.Test;
import org.hibernate.HibernateException;
@ -82,7 +84,7 @@ public class ImmutableNaturalIdTest extends AbstractJPATest {
s.flush();
fail();
}
catch ( HibernateException he ) {
catch ( PersistenceException p ) {
}
u.setUserName( "steve" );
s.delete( u );

View File

@ -83,6 +83,7 @@ import org.junit.Test;
import org.jboss.logging.Logger;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
@ -1769,8 +1770,10 @@ public class FooBarTest extends LegacyTestCase {
try {
q.setParameterList("nameList", (Collection)null);
fail("Should throw an queryexception when passing a null!");
} catch (QueryException qe) {
}
catch (IllegalArgumentException qe) {
//should happen
assertTyping( QueryException.class, qe.getCause() );
}
q = s.createQuery("select bar, b from Bar bar inner join bar.baz baz inner join baz.cascadingBars b where bar.name like 'Bar%'");

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.test.manytomanyassociationclass.surrogateid.generated;
import javax.persistence.PersistenceException;
import java.util.HashSet;
import org.hibernate.Session;
@ -15,6 +16,7 @@ import org.hibernate.test.manytomanyassociationclass.AbstractManyToManyAssociati
import org.hibernate.test.manytomanyassociationclass.Membership;
import org.junit.Test;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.fail;
/**
@ -50,9 +52,10 @@ public class ManyToManyAssociationClassGeneratedIdTest extends AbstractManyToMan
s.getTransaction().commit();
fail( "should have failed because inserts are beforeQuery deletes");
}
catch( ConstraintViolationException ex ) {
// expected
catch (PersistenceException e) {
s.getTransaction().rollback();
// expected
assertTyping( ConstraintViolationException.class, e.getCause() );
}
finally {
s.close();
@ -78,9 +81,10 @@ public class ManyToManyAssociationClassGeneratedIdTest extends AbstractManyToMan
s.getTransaction().commit();
fail( "should have failed because inserts are beforeQuery deletes");
}
catch( ConstraintViolationException ex ) {
// expected
catch (PersistenceException e) {
s.getTransaction().rollback();
// expected
assertTyping( ConstraintViolationException.class, e.getCause() );
}
finally {
s.close();
@ -106,9 +110,10 @@ public class ManyToManyAssociationClassGeneratedIdTest extends AbstractManyToMan
s.getTransaction().commit();
fail( "should have failed because inserts are beforeQuery deletes");
}
catch( ConstraintViolationException ex ) {
// expected
catch (PersistenceException e) {
s.getTransaction().rollback();
// expected
assertTyping( ConstraintViolationException.class, e.getCause() );
}
finally {
s.close();

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.test.naturalid.immutable;
import javax.persistence.PersistenceException;
import java.lang.reflect.Field;
import org.junit.Test;
@ -57,7 +58,7 @@ public class ImmutableEntityNaturalIdTest extends BaseCoreFunctionalTestCase {
s.flush();
fail( "should have failed because immutable natural ID was altered");
}
catch (HibernateException he) {
catch (PersistenceException e) {
// expected
}
finally {

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.test.ops;
import javax.persistence.PersistenceException;
import java.util.ArrayList;
import java.util.Collection;
@ -16,6 +17,7 @@ import org.hibernate.exception.ConstraintViolationException;
import org.junit.Test;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@ -129,8 +131,9 @@ public class CreateTest extends AbstractOperationTestCase {
tx.commit();
fail( "Expecting constraint failure" );
}
catch (ConstraintViolationException te) {
catch (PersistenceException e){
//verify that an exception is thrown!
assertTyping(ConstraintViolationException.class, e.getCause());
}
tx.rollback();
s.close();
@ -145,8 +148,9 @@ public class CreateTest extends AbstractOperationTestCase {
tx.commit();
assertFalse(true);
}
catch (ConstraintViolationException te) {
catch (PersistenceException e){
//verify that an exception is thrown!
assertTyping(ConstraintViolationException.class, e.getCause());
}
tx.rollback();
s.close();
@ -168,8 +172,9 @@ public class CreateTest extends AbstractOperationTestCase {
s.persist(dupe);
assertFalse(true);
}
catch (PersistentObjectException poe) {
catch (PersistenceException e){
//verify that an exception is thrown!
assertTyping(PersistentObjectException.class, e.getCause());
}
tx.rollback();
s.close();
@ -183,8 +188,9 @@ public class CreateTest extends AbstractOperationTestCase {
s.persist(nondupe);
assertFalse(true);
}
catch (PersistentObjectException poe) {
catch (PersistenceException e){
//verify that an exception is thrown!
assertTyping(PersistentObjectException.class, e.getCause());
}
tx.rollback();
s.close();

View File

@ -6,11 +6,13 @@
*/
package org.hibernate.test.ops;
import javax.persistence.PersistenceException;
import java.util.List;
import org.junit.Test;
import org.hibernate.Hibernate;
import org.hibernate.PersistentObjectException;
import org.hibernate.Session;
import org.hibernate.StaleObjectStateException;
import org.hibernate.Transaction;
@ -18,6 +20,7 @@ import org.hibernate.cfg.Configuration;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@ -671,8 +674,9 @@ public class MergeMultipleEntityCopiesAllowedTest extends BaseCoreFunctionalTest
category1_1 = (Category) s.merge( category1_1 );
fail( "should have failed because one representation is an older version." );
}
catch( StaleObjectStateException ex ) {
catch (PersistenceException e){
// expected
assertTyping( StaleObjectStateException.class, e.getCause());
}
finally {
tx.rollback();
@ -722,8 +726,9 @@ public class MergeMultipleEntityCopiesAllowedTest extends BaseCoreFunctionalTest
category1_1 = (Category) s.merge( category1_1 );
fail( "should have failed because one representation is an older version." );
}
catch( StaleObjectStateException ex ) {
catch (PersistenceException e){
// expected
assertTyping( StaleObjectStateException.class, e.getCause());
}
finally {
tx.rollback();

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.test.ops;
import javax.persistence.PersistenceException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@ -19,6 +20,7 @@ import org.hibernate.StaleObjectStateException;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
@ -55,8 +57,9 @@ public class MergeTest extends AbstractOperationTestCase {
s.getTransaction().commit();
fail( "was expecting staleness error" );
}
catch ( StaleObjectStateException expected ) {
// expected outcome...
catch (PersistenceException e){
// expected
assertTyping( StaleObjectStateException.class, e.getCause());
}
finally {
s.getTransaction().rollback();

View File

@ -7,6 +7,8 @@
package org.hibernate.test.optlock;
import javax.persistence.PersistenceException;
import org.hibernate.JDBCException;
import org.hibernate.Session;
import org.hibernate.StaleObjectStateException;
@ -18,6 +20,7 @@ import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.fail;
/**
@ -85,26 +88,12 @@ public class OptimisticLockTest extends BaseCoreFunctionalTestCase {
mainSession.flush();
fail( "expecting opt lock failure" );
}
catch ( StaleObjectStateException expected ) {
// expected result...
}
catch( StaleStateException expected ) {
// expected result (if using versioned batching)...
}
catch( JDBCException e ) {
// SQLServer will report this condition via a SQLException
// when using its SNAPSHOT transaction isolation...
if ( ! ( getDialect() instanceof SQLServerDialect && e.getErrorCode() == 3960 ) ) {
throw e;
}
else {
// it seems to "lose track" of the transaction as well...
mainSession.getTransaction().rollback();
mainSession.beginTransaction();
}
catch (PersistenceException e){
// expected
checkException( mainSession, e );
}
mainSession.clear();
mainSession.getTransaction().commit();
mainSession.getTransaction().rollback();
mainSession.close();
mainSession = openSession();
@ -153,23 +142,12 @@ public class OptimisticLockTest extends BaseCoreFunctionalTestCase {
catch ( StaleObjectStateException e ) {
// expected
}
catch( StaleStateException expected ) {
// expected result (if using versioned batching)...
}
catch( JDBCException e ) {
// SQLServer will report this condition via a SQLException
// when using its SNAPSHOT transaction isolation...
if ( ! ( getDialect() instanceof SQLServerDialect && e.getErrorCode() == 3960 ) ) {
throw e;
}
else {
// it seems to "lose track" of the transaction as well...
mainSession.getTransaction().rollback();
mainSession.beginTransaction();
}
catch (PersistenceException e){
// expected
checkException( mainSession, e );
}
mainSession.clear();
mainSession.getTransaction().commit();
mainSession.getTransaction().rollback();
mainSession.close();
mainSession = openSession();
@ -180,5 +158,25 @@ public class OptimisticLockTest extends BaseCoreFunctionalTestCase {
mainSession.close();
}
private void checkException(Session mainSession, PersistenceException e) {
final Throwable cause = e.getCause();
if ( cause instanceof JDBCException ) {
// SQLServer will report this condition via a SQLException
// when using its SNAPSHOT transaction isolation...
if ( !(getDialect() instanceof SQLServerDialect && ((JDBCException) cause).getErrorCode() == 3960) ) {
throw e;
}
else {
// it seems to "lose track" of the transaction as well...
mainSession.getTransaction().rollback();
mainSession.beginTransaction();
}
}
else if ( !(cause instanceof StaleObjectStateException) && !(cause instanceof StaleStateException) ) {
fail( "expectd StaleObjectStateException or StaleStateException exception but is" + cause );
}
}
}

View File

@ -1403,7 +1403,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest {
s.isReadOnly( dp );
fail( "should have failed because session was closed" );
}
catch ( SessionException ex) {
catch ( IllegalStateException ex) {
// expected
assertFalse( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() );
}
@ -1531,7 +1531,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest {
s.setReadOnly( dp, true );
fail( "should have failed because session was closed" );
}
catch ( SessionException ex) {
catch ( IllegalStateException ex) {
// expected
assertFalse( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() );
}
@ -1600,7 +1600,7 @@ public class ReadOnlyProxyTest extends AbstractReadOnlyTest {
( ( HibernateProxy ) dp ).getHibernateLazyInitializer().setSession( ( SessionImplementor ) s );
fail( "should have failed because session was closed" );
}
catch ( SessionException ex) {
catch ( IllegalStateException ex) {
// expected
assertFalse( ( ( HibernateProxy ) dp ).getHibernateLazyInitializer().isReadOnlySettingAvailable() );
}

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.test.sql.autodiscovery;
import javax.persistence.PersistenceException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -24,6 +25,7 @@ import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Assert;
import org.junit.Test;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
import static org.junit.Assert.assertEquals;
/**
@ -47,7 +49,7 @@ public class AutoDiscoveryTest extends BaseCoreFunctionalTestCase {
return new Class[] { Group.class, User.class, Membership.class };
}
@Test( expected = NonUniqueDiscoveredSqlAliasException.class )
@Test
public void testAutoDiscoveryWithDuplicateColumnLabels() {
Session session = openSession();
session.beginTransaction();
@ -57,24 +59,31 @@ public class AutoDiscoveryTest extends BaseCoreFunctionalTestCase {
session.close();
session = openSession();
session.beginTransaction();
List results = session.createSQLQuery( "select u.name, u2.name from t_user u, t_user u2 where u.name='steve'" ).list();
// this should result in a result set like:
// [0] steve, steve
// [1] steve, stliu
// although the rows could be reversed
assertEquals( 2, results.size() );
final Object[] row1 = (Object[]) results.get( 0 );
final Object[] row2 = (Object[]) results.get( 1 );
assertEquals( "steve", row1[0] );
assertEquals( "steve", row2[0] );
if ( "steve".equals( row1[1] ) ) {
assertEquals( "stliu", row2[1] );
try {
session.beginTransaction();
List results = session.createSQLQuery(
"select u.name, u2.name from t_user u, t_user u2 where u.name='steve'" ).list();
// this should result in a result set like:
// [0] steve, steve
// [1] steve, stliu
// although the rows could be reversed
assertEquals( 2, results.size() );
final Object[] row1 = (Object[]) results.get( 0 );
final Object[] row2 = (Object[]) results.get( 1 );
assertEquals( "steve", row1[0] );
assertEquals( "steve", row2[0] );
if ( "steve".equals( row1[1] ) ) {
assertEquals( "stliu", row2[1] );
}
else {
assertEquals( "stliu", row1[1] );
}
session.getTransaction().commit();
}
else {
assertEquals( "stliu", row1[1] );
catch (PersistenceException e) {
//expected
assertTyping( NonUniqueDiscoveredSqlAliasException.class, e.getCause() );
}
session.getTransaction().commit();
session.close();
session = openSession();