Search in sources :

Example 1 with InvalidDataAccessApiUsageException

use of cn.taketoday.dao.InvalidDataAccessApiUsageException in project today-infrastructure by TAKETODAY.

the class ChainedPersistenceExceptionTranslatorTests method exceptionTranslationWithTranslation.

@Test
public void exceptionTranslationWithTranslation() {
    MapPersistenceExceptionTranslator mpet1 = new MapPersistenceExceptionTranslator();
    RuntimeException in1 = new RuntimeException("in");
    InvalidDataAccessApiUsageException out1 = new InvalidDataAccessApiUsageException("out");
    InvalidDataAccessApiUsageException out2 = new InvalidDataAccessApiUsageException("out");
    mpet1.addTranslation(in1, out1);
    ChainedPersistenceExceptionTranslator chainedPet1 = new ChainedPersistenceExceptionTranslator();
    assertThat(DataAccessUtils.translateIfNecessary(in1, chainedPet1)).as("Should not translate yet").isSameAs(in1);
    chainedPet1.addDelegate(mpet1);
    assertThat(DataAccessUtils.translateIfNecessary(in1, chainedPet1)).as("Should now translate").isSameAs(out1);
    // Now add a new translator and verify it wins
    MapPersistenceExceptionTranslator mpet2 = new MapPersistenceExceptionTranslator();
    mpet2.addTranslation(in1, out2);
    chainedPet1.addDelegate(mpet2);
    assertThat(DataAccessUtils.translateIfNecessary(in1, chainedPet1)).as("Should still translate the same due to ordering").isSameAs(out1);
    ChainedPersistenceExceptionTranslator chainedPet2 = new ChainedPersistenceExceptionTranslator();
    chainedPet2.addDelegate(mpet2);
    chainedPet2.addDelegate(mpet1);
    assertThat(DataAccessUtils.translateIfNecessary(in1, chainedPet2)).as("Should translate differently due to ordering").isSameAs(out2);
    RuntimeException in2 = new RuntimeException("in2");
    OptimisticLockingFailureException out3 = new OptimisticLockingFailureException("out2");
    assertThat(chainedPet2.translateExceptionIfPossible(in2)).isNull();
    MapPersistenceExceptionTranslator mpet3 = new MapPersistenceExceptionTranslator();
    mpet3.addTranslation(in2, out3);
    chainedPet2.addDelegate(mpet3);
    assertThat(chainedPet2.translateExceptionIfPossible(in2)).isSameAs(out3);
}
Also used : OptimisticLockingFailureException(cn.taketoday.dao.OptimisticLockingFailureException) MapPersistenceExceptionTranslator(cn.taketoday.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator) InvalidDataAccessApiUsageException(cn.taketoday.dao.InvalidDataAccessApiUsageException) Test(org.junit.jupiter.api.Test)

Example 2 with InvalidDataAccessApiUsageException

use of cn.taketoday.dao.InvalidDataAccessApiUsageException in project today-infrastructure by TAKETODAY.

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(cn.taketoday.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) InvalidDataAccessApiUsageException(cn.taketoday.dao.InvalidDataAccessApiUsageException)

Example 3 with InvalidDataAccessApiUsageException

use of cn.taketoday.dao.InvalidDataAccessApiUsageException in project today-infrastructure by TAKETODAY.

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);
    assertThat(DataAccessUtils.translateIfNecessary(in, mpet)).isSameAs(out);
}
Also used : InvalidDataAccessApiUsageException(cn.taketoday.dao.InvalidDataAccessApiUsageException) Test(org.junit.jupiter.api.Test)

Example 4 with InvalidDataAccessApiUsageException

use of cn.taketoday.dao.InvalidDataAccessApiUsageException in project today-infrastructure by TAKETODAY.

the class EntityManagerFactoryUtilsTests method testTranslatesIllegalArgumentException.

@Test
public void testTranslatesIllegalArgumentException() {
    IllegalArgumentException iae = new IllegalArgumentException();
    DataAccessException dex = EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(iae);
    assertThat(dex.getCause()).isSameAs(iae);
    boolean condition = dex instanceof InvalidDataAccessApiUsageException;
    assertThat(condition).isTrue();
}
Also used : InvalidDataAccessApiUsageException(cn.taketoday.dao.InvalidDataAccessApiUsageException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) EmptyResultDataAccessException(cn.taketoday.dao.EmptyResultDataAccessException) IncorrectResultSizeDataAccessException(cn.taketoday.dao.IncorrectResultSizeDataAccessException) DataAccessException(cn.taketoday.dao.DataAccessException) Test(org.junit.jupiter.api.Test)

Example 5 with InvalidDataAccessApiUsageException

use of cn.taketoday.dao.InvalidDataAccessApiUsageException in project today-infrastructure by TAKETODAY.

the class RdbmsOperation method validateNamedParameters.

/**
 * Validate the named parameters passed to an execute method based on declared parameters.
 * Subclasses should invoke this method before every {@code executeQuery()} or
 * {@code update()} method.
 *
 * @param parameters parameter Map supplied (may be {@code null})
 * @throws InvalidDataAccessApiUsageException if the parameters are invalid
 */
protected void validateNamedParameters(@Nullable Map<String, ?> parameters) throws InvalidDataAccessApiUsageException {
    checkCompiled();
    Map<String, ?> paramsToUse = (parameters != null ? parameters : Collections.emptyMap());
    int declaredInParameters = 0;
    for (SqlParameter param : this.declaredParameters) {
        if (param.isInputValueProvided()) {
            if (!supportsLobParameters() && (param.getSqlType() == Types.BLOB || param.getSqlType() == Types.CLOB)) {
                throw new InvalidDataAccessApiUsageException("BLOB or CLOB parameters are not allowed for this kind of operation");
            }
            if (param.getName() != null && !paramsToUse.containsKey(param.getName())) {
                throw new InvalidDataAccessApiUsageException("The parameter named '" + param.getName() + "' was not among the parameters supplied: " + paramsToUse.keySet());
            }
            declaredInParameters++;
        }
    }
    validateParameterCount(paramsToUse.size(), declaredInParameters);
}
Also used : SqlParameter(cn.taketoday.jdbc.core.SqlParameter) InvalidDataAccessApiUsageException(cn.taketoday.dao.InvalidDataAccessApiUsageException)

Aggregations

InvalidDataAccessApiUsageException (cn.taketoday.dao.InvalidDataAccessApiUsageException)36 Test (org.junit.jupiter.api.Test)14 SQLException (java.sql.SQLException)12 ArrayList (java.util.ArrayList)10 DataAccessException (cn.taketoday.dao.DataAccessException)8 Connection (java.sql.Connection)8 ResultSet (java.sql.ResultSet)8 BadSqlGrammarException (cn.taketoday.jdbc.BadSqlGrammarException)6 SqlParameter (cn.taketoday.jdbc.core.SqlParameter)6 ResultSetMetaData (java.sql.ResultSetMetaData)6 PreparedStatement (java.sql.PreparedStatement)5 Statement (java.sql.Statement)5 Arrays (java.util.Arrays)5 Collections (java.util.Collections)5 List (java.util.List)5 Map (java.util.Map)5 DataSource (javax.sql.DataSource)5 DataIntegrityViolationException (cn.taketoday.dao.DataIntegrityViolationException)4 CannotGetJdbcConnectionException (cn.taketoday.jdbc.CannotGetJdbcConnectionException)4 SQLWarningException (cn.taketoday.jdbc.SQLWarningException)4