use of cn.taketoday.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator 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);
}
use of cn.taketoday.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator 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();
}
use of cn.taketoday.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator in project today-framework 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);
}
use of cn.taketoday.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator in project today-framework 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();
}
Aggregations