From b85141738d8e281d179f7e99f33fc5305cbba1d2 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 11 Aug 2013 17:38:41 +0300 Subject: [PATCH] further persistence tests --- spring-exceptions/pom.xml | 6 ++++++ .../spring/Cause2DataContextWithJavaConfig.java | 3 +++ .../spring/Cause3DataContextWithJavaConfig.java | 3 +++ ...egrityViolationExceptionIntegrationTest.java | 12 ++++++++++++ ...egrityViolationExceptionIntegrationTest.java | 16 ++++++++++++---- ...egrityViolationExceptionIntegrationTest.java | 17 ++++++++++++++--- 6 files changed, 50 insertions(+), 7 deletions(-) diff --git a/spring-exceptions/pom.xml b/spring-exceptions/pom.xml index 7a52560615..002ee4c258 100644 --- a/spring-exceptions/pom.xml +++ b/spring-exceptions/pom.xml @@ -87,6 +87,12 @@ guava ${guava.version} + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + test + diff --git a/spring-exceptions/src/main/java/org/baeldung/ex/dataIntegrityviolationexception/spring/Cause2DataContextWithJavaConfig.java b/spring-exceptions/src/main/java/org/baeldung/ex/dataIntegrityviolationexception/spring/Cause2DataContextWithJavaConfig.java index e0e54e5abc..d38898d9cf 100644 --- a/spring-exceptions/src/main/java/org/baeldung/ex/dataIntegrityviolationexception/spring/Cause2DataContextWithJavaConfig.java +++ b/spring-exceptions/src/main/java/org/baeldung/ex/dataIntegrityviolationexception/spring/Cause2DataContextWithJavaConfig.java @@ -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() { diff --git a/spring-exceptions/src/main/java/org/baeldung/ex/dataIntegrityviolationexception/spring/Cause3DataContextWithJavaConfig.java b/spring-exceptions/src/main/java/org/baeldung/ex/dataIntegrityviolationexception/spring/Cause3DataContextWithJavaConfig.java index 3eb8df39be..232435377a 100644 --- a/spring-exceptions/src/main/java/org/baeldung/ex/dataIntegrityviolationexception/spring/Cause3DataContextWithJavaConfig.java +++ b/spring-exceptions/src/main/java/org/baeldung/ex/dataIntegrityviolationexception/spring/Cause3DataContextWithJavaConfig.java @@ -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() { diff --git a/spring-exceptions/src/test/java/org/baeldung/ex/dataIntegrityviolationexception/Cause1DataIntegrityViolationExceptionIntegrationTest.java b/spring-exceptions/src/test/java/org/baeldung/ex/dataIntegrityviolationexception/Cause1DataIntegrityViolationExceptionIntegrationTest.java index ea94d5612c..e6cf53b68c 100644 --- a/spring-exceptions/src/test/java/org/baeldung/ex/dataIntegrityviolationexception/Cause1DataIntegrityViolationExceptionIntegrationTest.java +++ b/spring-exceptions/src/test/java/org/baeldung/ex/dataIntegrityviolationexception/Cause1DataIntegrityViolationExceptionIntegrationTest.java @@ -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); + } + } diff --git a/spring-exceptions/src/test/java/org/baeldung/ex/dataIntegrityviolationexception/Cause2DataIntegrityViolationExceptionIntegrationTest.java b/spring-exceptions/src/test/java/org/baeldung/ex/dataIntegrityviolationexception/Cause2DataIntegrityViolationExceptionIntegrationTest.java index 2eec55736d..909e7f95c8 100644 --- a/spring-exceptions/src/test/java/org/baeldung/ex/dataIntegrityviolationexception/Cause2DataIntegrityViolationExceptionIntegrationTest.java +++ b/spring-exceptions/src/test/java/org/baeldung/ex/dataIntegrityviolationexception/Cause2DataIntegrityViolationExceptionIntegrationTest.java @@ -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()); + } } diff --git a/spring-exceptions/src/test/java/org/baeldung/ex/dataIntegrityviolationexception/Cause3DataIntegrityViolationExceptionIntegrationTest.java b/spring-exceptions/src/test/java/org/baeldung/ex/dataIntegrityviolationexception/Cause3DataIntegrityViolationExceptionIntegrationTest.java index 5f4cf59f3a..6605a1cd28 100644 --- a/spring-exceptions/src/test/java/org/baeldung/ex/dataIntegrityviolationexception/Cause3DataIntegrityViolationExceptionIntegrationTest.java +++ b/spring-exceptions/src/test/java/org/baeldung/ex/dataIntegrityviolationexception/Cause3DataIntegrityViolationExceptionIntegrationTest.java @@ -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))); } }