BAEL-20262: Migrate spring-exceptions module to the com.baeldung package
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package com.baeldung;
|
||||
|
||||
import com.baeldung.spring.config.PersistenceConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class SpringContextLiveTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.ex.beancreationexception;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@Suite.SuiteClasses({ // @formatter:off
|
||||
Cause1BeanCreationExceptionManualTest.class
|
||||
,Cause2BeanCreationExceptionManualTest.class
|
||||
,Cause3BeanCreationExceptionManualTest.class
|
||||
,Cause4BeanCreationExceptionManualTest.class
|
||||
,Cause5BeanCreationExceptionManualTest.class
|
||||
,Cause6BeanCreationExceptionManualTest.class
|
||||
,Cause7BeanCreationExceptionManualTest.class
|
||||
,Cause8BeanCreationExceptionManualTest.class
|
||||
,Cause9BeanCreationExceptionManualTest.class
|
||||
}) // @formatter:on
|
||||
public class BeanCreationExceptionTestSuite {
|
||||
//
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.ex.beancreationexception;
|
||||
|
||||
import com.baeldung.ex.beancreationexception.spring.Cause1ContextWithJavaConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { Cause1ContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause1BeanCreationExceptionManualTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.ex.beancreationexception;
|
||||
|
||||
import com.baeldung.ex.beancreationexception.spring.Cause2ContextWithJavaConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { Cause2ContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause2BeanCreationExceptionManualTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.ex.beancreationexception;
|
||||
|
||||
import com.baeldung.ex.beancreationexception.spring.Cause3ContextWithJavaConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { Cause3ContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause3BeanCreationExceptionManualTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.ex.beancreationexception;
|
||||
|
||||
import com.baeldung.ex.beancreationexception.spring.Cause4ContextWithJavaConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { Cause4ContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause4BeanCreationExceptionManualTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.ex.beancreationexception;
|
||||
|
||||
import com.baeldung.ex.beancreationexception.spring.Cause5ContextWithJavaConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { Cause5ContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause5BeanCreationExceptionManualTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.ex.beancreationexception;
|
||||
|
||||
import com.baeldung.ex.beancreationexception.spring.Cause6ContextWithJavaConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { Cause6ContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause6BeanCreationExceptionManualTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.ex.beancreationexception;
|
||||
|
||||
import com.baeldung.ex.beancreationexception.spring.Cause7ContextWithJavaConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { Cause7ContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause7BeanCreationExceptionManualTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.ex.beancreationexception;
|
||||
|
||||
import com.baeldung.ex.beancreationexception.spring.Cause8ContextWithJavaConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { Cause8ContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause8BeanCreationExceptionManualTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.ex.beancreationexception;
|
||||
|
||||
import com.baeldung.ex.beancreationexception.spring.Cause9ContextWithJavaConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { Cause9ContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause9BeanCreationExceptionManualTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.ex.beandefinitionstoreexception;
|
||||
|
||||
import com.baeldung.ex.beandefinitionstoreexception.spring.Cause1ContextWithJavaConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { Cause1ContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause1BeanDefinitionStoreExceptionManualTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.ex.beandefinitionstoreexception;
|
||||
|
||||
import com.baeldung.ex.beandefinitionstoreexception.spring.Cause2ContextWithJavaConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { Cause2ContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause2BeanDefinitionStoreExceptionManualTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.ex.beandefinitionstoreexception;
|
||||
|
||||
import com.baeldung.ex.beandefinitionstoreexception.spring.Cause3ContextWithJavaConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { Cause3ContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause3BeanDefinitionStoreExceptionManualTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.ex.dataintegrityviolationexception;
|
||||
|
||||
import com.baeldung.ex.dataintegrityviolationexception.spring.Cause1DataContextWithJavaConfig;
|
||||
import com.baeldung.persistence.model.Child;
|
||||
import com.baeldung.persistence.model.Parent;
|
||||
import com.baeldung.persistence.service.IChildService;
|
||||
import com.baeldung.persistence.service.IParentService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
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 = { Cause1DataContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause1DataIntegrityViolationExceptionManualTest {
|
||||
|
||||
@Autowired
|
||||
private IParentService service;
|
||||
|
||||
@Autowired
|
||||
private IChildService childService;
|
||||
|
||||
// tests
|
||||
|
||||
@Test(expected = DataIntegrityViolationException.class)
|
||||
public void whenChildIsDeletedWhileParentStillHasForeignKeyToIt_thenDataException() {
|
||||
final Child childEntity = new Child();
|
||||
childService.create(childEntity);
|
||||
|
||||
final Parent parentEntity = new Parent(childEntity);
|
||||
service.create(parentEntity);
|
||||
|
||||
childService.delete(childEntity);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenChildIsDeletedAfterTheParent_thenNoExceptions() {
|
||||
final Child childEntity = new Child();
|
||||
childService.create(childEntity);
|
||||
|
||||
final Parent parentEntity = new Parent(childEntity);
|
||||
service.create(parentEntity);
|
||||
|
||||
service.delete(parentEntity);
|
||||
childService.delete(childEntity);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.ex.dataintegrityviolationexception;
|
||||
|
||||
import com.baeldung.ex.dataintegrityviolationexception.spring.Cause2DataContextWithJavaConfig;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import com.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.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { Cause2DataContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause2DataIntegrityViolationExceptionManualTest {
|
||||
|
||||
@Autowired
|
||||
private IFooService fooService;
|
||||
|
||||
// tests
|
||||
|
||||
@Test(expected = DataIntegrityViolationException.class)
|
||||
public void whenInvalidEntityIsCreated_thenDataException() {
|
||||
fooService.create(new Foo());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.ex.dataintegrityviolationexception;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
|
||||
import com.baeldung.ex.dataintegrityviolationexception.spring.Cause3DataContextWithJavaConfig;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import com.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.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { Cause3DataContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause3DataIntegrityViolationExceptionManualTest {
|
||||
|
||||
@Autowired
|
||||
private IFooService fooService;
|
||||
|
||||
// tests
|
||||
|
||||
@Test(expected = DataIntegrityViolationException.class)
|
||||
public final void whenEntityWithLongNameIsCreated_thenDataException() {
|
||||
fooService.create(new Foo(randomAlphabetic(2048)));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.ex.mappingexception;
|
||||
|
||||
import com.baeldung.ex.mappingexception.cause1.persistence.model.Foo;
|
||||
import com.baeldung.ex.mappingexception.spring.Cause1PersistenceConfig;
|
||||
import org.hibernate.MappingException;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { Cause1PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause1MappingExceptionManualTest {
|
||||
|
||||
@Autowired
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
// tests
|
||||
|
||||
@Test(expected = MappingException.class)
|
||||
@Transactional
|
||||
public final void givenEntityIsPersisted_thenException() {
|
||||
sessionFactory.getCurrentSession().saveOrUpdate(new Foo());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.ex.mappingexception;
|
||||
|
||||
import com.baeldung.ex.mappingexception.cause2.persistence.model.Foo;
|
||||
import com.baeldung.ex.mappingexception.spring.Cause2PersistenceConfig;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { Cause2PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause2MappingExceptionManualTest {
|
||||
|
||||
@Autowired
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
// tests
|
||||
|
||||
// @Test(expected = MappingException.class)
|
||||
@Test
|
||||
@Transactional
|
||||
public final void givenEntityIsPersisted_thenException() {
|
||||
sessionFactory.getCurrentSession().saveOrUpdate(new Foo());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.ex.mappingexception;
|
||||
|
||||
import com.baeldung.ex.mappingexception.cause3.persistence.model.Foo;
|
||||
import com.baeldung.ex.mappingexception.spring.Cause3PersistenceConfig;
|
||||
import org.hibernate.MappingException;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { Cause3PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause3MappingExceptionManualTest {
|
||||
|
||||
@Autowired
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
// tests
|
||||
|
||||
@Test(expected = MappingException.class)
|
||||
@Transactional
|
||||
public final void givenEntityIsPersisted_thenException() {
|
||||
sessionFactory.getCurrentSession().saveOrUpdate(new Foo());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.ex.mappingexception;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.baeldung.ex.mappingexception.cause4.persistence.model.Foo;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
import org.junit.Test;
|
||||
|
||||
public class Cause4MappingExceptionManualTest {
|
||||
|
||||
// tests
|
||||
|
||||
@Test
|
||||
public final void givenEntityIsPersisted_thenException() throws IOException {
|
||||
final SessionFactory sessionFactory = configureSessionFactory();
|
||||
|
||||
final Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
session.saveOrUpdate(new Foo());
|
||||
session.getTransaction().commit();
|
||||
}
|
||||
|
||||
private final SessionFactory configureSessionFactory() throws IOException {
|
||||
final Configuration configuration = new Configuration();
|
||||
final InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("hibernate-mysql.properties");
|
||||
final Properties hibernateProperties = new Properties();
|
||||
hibernateProperties.load(inputStream);
|
||||
configuration.setProperties(hibernateProperties);
|
||||
|
||||
configuration.addAnnotatedClass(Foo.class);
|
||||
|
||||
final ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
|
||||
final SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||
return sessionFactory;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.ex.nontransientdataaccessexception;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.baeldung.ex.nontransientexception.cause.Cause5NonTransientConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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 CannotGetJdbcConnectionExceptionManualTest {
|
||||
|
||||
@Autowired
|
||||
private DataSource restDataSource;
|
||||
|
||||
@Test(expected = CannotGetJdbcConnectionException.class)
|
||||
public void whenJdbcUrlIncorrect_thenCannotGetJdbcConnectionException() {
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
|
||||
jdbcTemplate.execute("select * from foo");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.ex.nontransientdataaccessexception;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
import com.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.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;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { Cause1NonTransientConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class DataIntegrityExceptionManualTest {
|
||||
|
||||
@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");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.ex.nontransientdataaccessexception;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
|
||||
import com.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;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { Cause1NonTransientConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class DataRetrievalExceptionManualTest {
|
||||
|
||||
@Autowired
|
||||
private DataSource restDataSource;
|
||||
|
||||
@Test(expected = DataRetrievalFailureException.class)
|
||||
public void whenRetrievingNonExistentValue_thenDataRetrievalException() {
|
||||
final JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.ex.nontransientdataaccessexception;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.baeldung.ex.nontransientexception.cause.Cause4NonTransientConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException;
|
||||
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
|
||||
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 = { Cause4NonTransientConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class DataSourceLookupExceptionManualTest {
|
||||
|
||||
@Test(expected = DataSourceLookupFailureException.class)
|
||||
public void whenLookupNonExistentDataSource_thenDataSourceLookupFailureException() {
|
||||
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
|
||||
dsLookup.setResourceRef(true);
|
||||
final DataSource dataSource = dsLookup.getDataSource("java:comp/env/jdbc/example_db");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.ex.nontransientdataaccessexception;
|
||||
|
||||
import com.baeldung.ex.nontransientexception.cause.Cause1NonTransientConfig;
|
||||
import com.baeldung.persistence.service.IFooService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.InvalidDataAccessResourceUsageException;
|
||||
import org.springframework.jdbc.BadSqlGrammarException;
|
||||
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;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { Cause1NonTransientConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class InvalidResourceUsageExceptionManualTest {
|
||||
|
||||
@Autowired
|
||||
private IFooService fooService;
|
||||
|
||||
@Autowired
|
||||
private DataSource restDataSource;
|
||||
|
||||
@Test(expected = InvalidDataAccessResourceUsageException.class)
|
||||
public void whenRetrievingDataUserNoSelectRights_thenInvalidResourceUsageException() {
|
||||
final JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
|
||||
jdbcTemplate.execute("revoke select from tutorialuser");
|
||||
|
||||
try {
|
||||
fooService.findAll();
|
||||
} finally {
|
||||
jdbcTemplate.execute("grant select to tutorialuser");
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = BadSqlGrammarException.class)
|
||||
public void whenIncorrectSql_thenBadSqlGrammarException() {
|
||||
final JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
|
||||
|
||||
jdbcTemplate.queryForObject("select * fro foo where id=3", Integer.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.ex.nosuchbeandefinitionexception;
|
||||
|
||||
import com.baeldung.ex.nosuchbeandefinitionexception.spring.Cause1ContextWithJavaConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { Cause1ContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause1NoSuchBeanDefinitionExceptionManualTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.ex.nosuchbeandefinitionexception;
|
||||
|
||||
import com.baeldung.ex.nosuchbeandefinitionexception.spring.Cause2ContextWithJavaConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { Cause2ContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause2NoSuchBeanDefinitionExceptionManualTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.ex.nosuchbeandefinitionexception;
|
||||
|
||||
import com.baeldung.ex.nosuchbeandefinitionexception.spring.Cause3ContextWithJavaConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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 = { Cause3ContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class Cause3NoSuchBeanDefinitionExceptionManualTest {
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.properties.core;
|
||||
|
||||
import com.baeldung.properties.spring.PropertiesWithJavaConfig;
|
||||
import com.baeldung.properties.spring.PropertiesWithJavaConfigOther;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.env.Environment;
|
||||
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 = { PropertiesWithJavaConfig.class, PropertiesWithJavaConfigOther.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class PropertiesWithJavaManualTest {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
@Value("${key.something}")
|
||||
private String injectedProperty;
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
System.out.println("in test via @Value: " + injectedProperty);
|
||||
System.out.println("in test Environment: " + env.getProperty("key.something"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.properties.core;
|
||||
|
||||
import com.baeldung.properties.spring.PropertiesWithXmlConfigOne;
|
||||
import com.baeldung.properties.spring.PropertiesWithXmlConfigTwo;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.env.Environment;
|
||||
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 = { PropertiesWithXmlConfigOne.class, PropertiesWithXmlConfigTwo.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class PropertiesWithMultipleXmlsManualTest {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
@Value("${key.something}")
|
||||
private String injectedProperty;
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
System.out.println("in test via @Value: " + injectedProperty);
|
||||
System.out.println("in test Environment: " + env.getProperty("key.something"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.properties.core;
|
||||
|
||||
import com.baeldung.properties.spring.PropertiesWithXmlConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.env.Environment;
|
||||
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 = { PropertiesWithXmlConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class PropertiesWithXmlManualTest {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
@Value("${key.something}")
|
||||
private String injectedProperty;
|
||||
|
||||
@Test
|
||||
public final void givenContextIsInitialized_thenNoException() {
|
||||
System.out.println("in test via @Value: " + injectedProperty);
|
||||
System.out.println("in test Environment: " + env.getProperty("key.something"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user