further persistence tests

This commit is contained in:
eugenp 2013-08-11 17:38:41 +03:00
parent cc7775d6d8
commit b85141738d
6 changed files with 50 additions and 7 deletions

View File

@ -87,6 +87,12 @@
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
<scope>test</scope>
</dependency>
<!-- test scoped -->

View File

@ -1,10 +1,13 @@
package org.baeldung.ex.dataIntegrityviolationexception.spring;
import org.baeldung.spring.config.PersistenceConfig;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@ComponentScan("org.baeldung.ex.dataIntegrityviolationexception.cause2")
@Import(PersistenceConfig.class)
public class Cause2DataContextWithJavaConfig {
public Cause2DataContextWithJavaConfig() {

View File

@ -1,10 +1,13 @@
package org.baeldung.ex.dataIntegrityviolationexception.spring;
import org.baeldung.spring.config.PersistenceConfig;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@ComponentScan("org.baeldung.ex.dataIntegrityviolationexception.cause3")
@Import(PersistenceConfig.class)
public class Cause3DataContextWithJavaConfig {
public Cause3DataContextWithJavaConfig() {

View File

@ -36,4 +36,16 @@ public class Cause1DataIntegrityViolationExceptionIntegrationTest {
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);
}
}

View File

@ -1,8 +1,12 @@
package org.baeldung.ex.dataIntegrityviolationexception;
import org.baeldung.ex.dataIntegrityviolationexception.spring.Cause2DataContextWithJavaConfig;
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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@ -11,9 +15,13 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
@ContextConfiguration(classes = { Cause2DataContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
public class Cause2DataIntegrityViolationExceptionIntegrationTest {
@Test
public final void givenContextIsInitialized_thenNoException() {
//
}
@Autowired
private IFooService fooService;
// tests
@Test(expected = DataIntegrityViolationException.class)
public void whenInvalidEntityIsCreated_thenDataException() {
fooService.create(new Foo());
}
}

View File

@ -1,8 +1,14 @@
package org.baeldung.ex.dataIntegrityviolationexception;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import org.baeldung.ex.dataIntegrityviolationexception.spring.Cause3DataContextWithJavaConfig;
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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@ -11,9 +17,14 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
@ContextConfiguration(classes = { Cause3DataContextWithJavaConfig.class }, loader = AnnotationConfigContextLoader.class)
public class Cause3DataIntegrityViolationExceptionIntegrationTest {
@Test
public final void givenContextIsInitialized_thenNoException() {
//
@Autowired
private IFooService fooService;
// tests
@Test(expected = DataIntegrityViolationException.class)
public final void whenEntityWithLongNameIsCreated_thenDataException() {
fooService.create(new Foo(randomAlphabetic(2048)));
}
}