Search in sources :

Example 6 with InvalidDataAccessApiUsageException

use of org.springframework.dao.InvalidDataAccessApiUsageException in project spring-framework by spring-projects.

the class JdbcTemplateTests method testStaticResultSetClosed.

@Test
public void testStaticResultSetClosed() throws Exception {
    ResultSet resultSet2 = mock(ResultSet.class);
    reset(this.preparedStatement);
    given(this.preparedStatement.executeQuery()).willReturn(resultSet2);
    given(this.connection.createStatement()).willReturn(this.statement);
    try {
        this.template.query("my query", new ResultSetExtractor<Object>() {

            @Override
            public Object extractData(ResultSet rs) {
                throw new InvalidDataAccessApiUsageException("");
            }
        });
        fail("Should have thrown InvalidDataAccessApiUsageException");
    } catch (InvalidDataAccessApiUsageException idaauex) {
    // ok
    }
    try {
        this.template.query(new PreparedStatementCreator() {

            @Override
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                return con.prepareStatement("my query");
            }
        }, new ResultSetExtractor<Object>() {

            @Override
            public Object extractData(ResultSet rs2) {
                throw new InvalidDataAccessApiUsageException("");
            }
        });
        fail("Should have thrown InvalidDataAccessApiUsageException");
    } catch (InvalidDataAccessApiUsageException idaauex) {
    // ok
    }
    verify(this.resultSet).close();
    verify(resultSet2).close();
    verify(this.preparedStatement).close();
    verify(this.connection, atLeastOnce()).close();
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 7 with InvalidDataAccessApiUsageException

use of org.springframework.dao.InvalidDataAccessApiUsageException in project spring-framework by spring-projects.

the class EntityManagerFactoryUtilsTests method testTranslatesIllegalStateException.

@Test
public void testTranslatesIllegalStateException() {
    IllegalStateException ise = new IllegalStateException();
    DataAccessException dex = EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ise);
    assertSame(ise, dex.getCause());
    assertTrue(dex instanceof InvalidDataAccessApiUsageException);
}
Also used : InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) DataAccessException(org.springframework.dao.DataAccessException) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Test(org.junit.Test)

Example 8 with InvalidDataAccessApiUsageException

use of org.springframework.dao.InvalidDataAccessApiUsageException in project spring-framework by spring-projects.

the class DataAccessUtilsTests method exceptionTranslationWithTranslation.

@Test
public void exceptionTranslationWithTranslation() {
    MapPersistenceExceptionTranslator mpet = new MapPersistenceExceptionTranslator();
    RuntimeException in = new RuntimeException("in");
    InvalidDataAccessApiUsageException out = new InvalidDataAccessApiUsageException("out");
    mpet.addTranslation(in, out);
    assertSame(out, DataAccessUtils.translateIfNecessary(in, mpet));
}
Also used : InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) Test(org.junit.Test)

Example 9 with InvalidDataAccessApiUsageException

use of org.springframework.dao.InvalidDataAccessApiUsageException in project spring-framework by spring-projects.

the class EntityManagerFactoryUtilsTests method testTranslatesIllegalArgumentException.

@Test
public void testTranslatesIllegalArgumentException() {
    IllegalArgumentException iae = new IllegalArgumentException();
    DataAccessException dex = EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(iae);
    assertSame(iae, dex.getCause());
    assertTrue(dex instanceof InvalidDataAccessApiUsageException);
}
Also used : InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) DataAccessException(org.springframework.dao.DataAccessException) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Test(org.junit.Test)

Example 10 with InvalidDataAccessApiUsageException

use of org.springframework.dao.InvalidDataAccessApiUsageException in project spring-framework by spring-projects.

the class PersistenceExceptionTranslationAdvisorTests method createProxy.

protected RepositoryInterface createProxy(RepositoryInterfaceImpl target) {
    MapPersistenceExceptionTranslator mpet = new MapPersistenceExceptionTranslator();
    mpet.addTranslation(persistenceException1, new InvalidDataAccessApiUsageException("", persistenceException1));
    ProxyFactory pf = new ProxyFactory(target);
    pf.addInterface(RepositoryInterface.class);
    addPersistenceExceptionTranslation(pf, mpet);
    return (RepositoryInterface) pf.getProxy();
}
Also used : MapPersistenceExceptionTranslator(org.springframework.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator) ProxyFactory(org.springframework.aop.framework.ProxyFactory) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException)

Aggregations

InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)18 Test (org.junit.Test)6 SQLException (java.sql.SQLException)5 SqlParameter (org.springframework.jdbc.core.SqlParameter)4 Connection (java.sql.Connection)3 ResultSet (java.sql.ResultSet)3 DataAccessException (org.springframework.dao.DataAccessException)3 PreparedStatement (java.sql.PreparedStatement)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)2 IncorrectResultSizeDataAccessException (org.springframework.dao.IncorrectResultSizeDataAccessException)2 MapPersistenceExceptionTranslator (org.springframework.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator)2 PropertyDescriptor (java.beans.PropertyDescriptor)1 Method (java.lang.reflect.Method)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 Statement (java.sql.Statement)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1