Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
4cf9e045
Commit
4cf9e045
authored
Oct 09, 2014
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Provide auto-configuration of persistence exception translation
Closes gh-1435
parent
fa95a6f6
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
146 additions
and
0 deletions
+146
-0
PersistenceExceptionTranslationAutoConfiguration.java
...dao/PersistenceExceptionTranslationAutoConfiguration.java
+40
-0
spring.factories
...utoconfigure/src/main/resources/META-INF/spring.factories
+1
-0
PersistenceExceptionTranslationAutoConfigurationTests.java
...ersistenceExceptionTranslationAutoConfigurationTests.java
+105
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/dao/PersistenceExceptionTranslationAutoConfiguration.java
0 → 100644
View file @
4cf9e045
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
autoconfigure
.
dao
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
;
/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring's persistence exception
* translation
*
* @author Andy Wilkinson
*/
@ConditionalOnClass
(
PersistenceExceptionTranslationPostProcessor
.
class
)
public
class
PersistenceExceptionTranslationAutoConfiguration
{
@Bean
@ConditionalOnMissingBean
(
PersistenceExceptionTranslationPostProcessor
.
class
)
public
PersistenceExceptionTranslationPostProcessor
persistenceExceptionTranslationPostProcessor
()
{
return
new
PersistenceExceptionTranslationPostProcessor
();
}
}
spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories
View file @
4cf9e045
...
@@ -10,6 +10,7 @@ org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration,\
...
@@ -10,6 +10,7 @@ org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/dao/PersistenceExceptionTranslationAutoConfigurationTests.java
0 → 100644
View file @
4cf9e045
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
autoconfigure
.
dao
;
import
java.util.Map
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration
;
import
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.dao.InvalidDataAccessApiUsageException
;
import
org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
;
import
org.springframework.stereotype.Repository
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
/**
*
* Tests for {@link PersistenceExceptionTranslationAutoConfiguration}
*
* @author Andy Wilkinson
*/
public
class
PersistenceExceptionTranslationAutoConfigurationTests
{
private
AnnotationConfigApplicationContext
context
;
@After
public
void
close
()
{
if
(
this
.
context
!=
null
)
{
this
.
context
.
close
();
}
}
@Test
public
void
exceptionTranslationPostProcessorBeanIsCreated
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
(
PersistenceExceptionTranslationAutoConfiguration
.
class
);
Map
<
String
,
PersistenceExceptionTranslationPostProcessor
>
beans
=
this
.
context
.
getBeansOfType
(
PersistenceExceptionTranslationPostProcessor
.
class
);
assertThat
(
beans
.
size
(),
is
(
equalTo
(
1
)));
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
persistOfNullThrowsIllegalArgumentExceptionWithoutExceptionTranslation
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
(
EmbeddedDataSourceConfiguration
.
class
,
HibernateJpaAutoConfiguration
.
class
,
TestConfiguration
.
class
);
this
.
context
.
getBean
(
TestRepository
.
class
).
doSomething
();
}
@Test
(
expected
=
InvalidDataAccessApiUsageException
.
class
)
public
void
persistOfNullThrowsInvalidDataAccessApiUsageExceptionWithExceptionTranslation
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
(
EmbeddedDataSourceConfiguration
.
class
,
HibernateJpaAutoConfiguration
.
class
,
TestConfiguration
.
class
,
PersistenceExceptionTranslationAutoConfiguration
.
class
);
this
.
context
.
getBean
(
TestRepository
.
class
).
doSomething
();
}
@Configuration
static
class
TestConfiguration
{
@Bean
public
TestRepository
testRepository
(
EntityManagerFactory
entityManagerFactory
)
{
return
new
TestRepository
(
entityManagerFactory
.
createEntityManager
());
}
}
@Repository
private
static
class
TestRepository
{
private
final
EntityManager
entityManger
;
public
TestRepository
(
EntityManager
entityManager
)
{
this
.
entityManger
=
entityManager
;
}
public
void
doSomething
()
{
this
.
entityManger
.
persist
(
null
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment