Non transient exception (#593)

* non transient data access exception examples

* change to derby db

* change to in memory derby db

* delete failed test

* fix invalidresource test

* fix cleanupfailure test

* more exception examples
This commit is contained in:
lor6
2016-08-10 22:05:35 +03:00
committed by Grzegorz Piwowarek
parent 8a104053d6
commit 2817953d0b
6 changed files with 161 additions and 43 deletions
@@ -0,0 +1,31 @@
package org.baeldung.ex.nontransientdataaccessexception;
import javax.sql.DataSource;
import org.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
import org.baeldung.ex.nontransientexception.cause.Cause5NonTransientConfig;
import org.baeldung.persistence.model.Foo;
import org.baeldung.persistence.service.IFooService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { Cause5NonTransientConfig.class }, loader = AnnotationConfigContextLoader.class)
public class CannotGetJdbcConnectionExceptionTest {
@Autowired
private DataSource restDataSource;
@Test(expected = CannotGetJdbcConnectionException.class)
public void whenJdbcUrlIncorrect_thenCannotGetJdbcConnectionException() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
jdbcTemplate.execute("select * from foo");
}
}
@@ -1,43 +0,0 @@
package org.baeldung.ex.nontransientdataaccessexception;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
import org.baeldung.persistence.model.Foo;
import org.baeldung.persistence.service.IFooService;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.CleanupFailureDataAccessException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { Cause1NonTransientConfig.class }, loader = AnnotationConfigContextLoader.class)
public class CleanupFailureExceptionTest {
private static final Logger LOG = Logger.getLogger(CleanupFailureExceptionTest.class.getName());
@Autowired
private SessionFactory sessionFactory;
@Autowired
private IFooService fooService;
@Test
public void whenCleanupAfterSaving_thenCleanupException() {
try {
final Foo fooEntity = new Foo("foo");
fooService.create(fooEntity);
} finally {
try {
sessionFactory.close();
} catch (final CleanupFailureDataAccessException exc) {
LOG.log(Level.SEVERE, exc.getMessage());
}
}
}
}
@@ -1,5 +1,7 @@
package org.baeldung.ex.nontransientdataaccessexception;
import javax.sql.DataSource;
import org.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
import org.baeldung.persistence.model.Foo;
import org.baeldung.persistence.service.IFooService;
@@ -7,6 +9,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@@ -18,9 +22,25 @@ public class DataIntegrityExceptionTest {
@Autowired
private IFooService fooService;
@Autowired
private DataSource restDataSource;
@Test(expected = DataIntegrityViolationException.class)
public void whenSavingNullValue_thenDataIntegrityException() {
final Foo fooEntity = new Foo();
fooService.create(fooEntity);
}
@Test(expected = DuplicateKeyException.class)
public void whenSavingDuplicateKeyValues_thenDuplicateKeyException() {
final JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
try {
jdbcTemplate.execute("insert into foo(id,name) values (1,'a')");
jdbcTemplate.execute("insert into foo(id,name) values (1,'b')");
} finally {
jdbcTemplate.execute("delete from foo where id=1");
}
}
}
@@ -3,10 +3,13 @@ package org.baeldung.ex.nontransientdataaccessexception;
import javax.sql.DataSource;
import org.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
import org.baeldung.persistence.model.Foo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.IncorrectResultSetColumnCountException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -25,4 +28,26 @@ public class DataRetrievalExceptionTest {
jdbcTemplate.queryForObject("select * from foo where id=3", Integer.class);
}
@Test(expected = IncorrectResultSetColumnCountException.class)
public void whenRetrievingMultipleColumns_thenIncorrectResultSetColumnCountException() {
final JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
try {
jdbcTemplate.execute("insert into foo(id,name) values (1,'a')");
jdbcTemplate.queryForList("select id,name from foo where id=1", Foo.class);
} finally {
jdbcTemplate.execute("delete from foo where id=1");
}
}
@Test(expected = IncorrectResultSizeDataAccessException.class)
public void whenRetrievingMultipleValues_thenIncorrectResultSizeException() {
final JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
jdbcTemplate.execute("insert into foo(name) values ('a')");
jdbcTemplate.execute("insert into foo(name) values ('a')");
jdbcTemplate.queryForObject("select id from foo where name='a'", Integer.class);
}
}