Skip to content

Commit e8436d5

Browse files
committed
DATAJPA-650, DATAJPA-655 - Moved attribute converters into distinct packages.
Keeping both the ThreeTen and JSR-310 JPA 2.1 attribute converters in a single package will make the use of packages-to-scan with Spring's EntityManagerFactoryBean rather difficult as all converters will end up being picked up by the persistence provider where both of them rely on optional project settings (Java 8 or ThreeTenBp on the classpath). By moving them into separate packages, the classes can be used to point to packages without interfering with each other.
1 parent 2cf114b commit e8436d5

File tree

7 files changed

+94
-32
lines changed

7 files changed

+94
-32
lines changed

src/main/java/org/springframework/data/jpa/domain/support/Jsr310JpaConverters.java renamed to src/main/java/org/springframework/data/jpa/convert/threeten/Jsr310JpaConverters.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.jpa.domain.support;
16+
package org.springframework.data.jpa.convert.threeten;
1717

1818
import java.time.Instant;
1919
import java.time.LocalDate;

src/main/java/org/springframework/data/jpa/domain/support/ThreeTenBackPortJpaConverters.java renamed to src/main/java/org/springframework/data/jpa/convert/threetenbp/ThreeTenBackPortJpaConverters.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.jpa.domain.support;
16+
package org.springframework.data.jpa.convert.threetenbp;
1717

1818
import java.util.Date;
1919

src/test/java/org/springframework/data/jpa/domain/support/DateTimeSample.java renamed to src/test/java/org/springframework/data/jpa/convert/threeten/DateTimeSample.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.jpa.domain.support;
16+
package org.springframework.data.jpa.convert.threeten;
1717

1818
import java.time.Instant;
1919
import java.time.LocalDate;
@@ -35,9 +35,4 @@ public class DateTimeSample {
3535
LocalDate localDate;
3636
LocalTime localTime;
3737
LocalDateTime localDateTime;
38-
39-
org.threeten.bp.Instant bpInstant;
40-
org.threeten.bp.LocalDate bpLocalDate;
41-
org.threeten.bp.LocalTime bpLocalTime;
42-
org.threeten.bp.LocalDateTime bpLocalDateTime;
4338
}

src/test/java/org/springframework/data/jpa/domain/support/Jsr310JpaConvertersIntegrationTests.java renamed to src/test/java/org/springframework/data/jpa/convert/threeten/Jsr310JpaConvertersIntegrationTests.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.jpa.domain.support;
16+
package org.springframework.data.jpa.convert.threeten;
1717

1818
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
@@ -29,14 +29,29 @@
2929
import javax.persistence.PersistenceContext;
3030

3131
import org.junit.Test;
32+
import org.springframework.context.annotation.Configuration;
33+
import org.springframework.data.jpa.domain.support.AbstractAttributeConverterIntegrationTests;
34+
import org.springframework.test.context.ContextConfiguration;
35+
import org.springframework.transaction.annotation.Transactional;
3236

3337
/**
3438
* Integration tests for {@link Jsr310JpaConverters}.
3539
*
3640
* @author Oliver Gierke
3741
*/
42+
@ContextConfiguration
43+
@Transactional
3844
public class Jsr310JpaConvertersIntegrationTests extends AbstractAttributeConverterIntegrationTests {
3945

46+
@Configuration
47+
static class Config extends InfrastructureConfig {
48+
49+
@Override
50+
protected String getPackageName() {
51+
return getClass().getPackage().getName();
52+
}
53+
}
54+
4055
@PersistenceContext EntityManager em;
4156

4257
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2014-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.convert.threetenbp;
17+
18+
import javax.persistence.Entity;
19+
import javax.persistence.GeneratedValue;
20+
import javax.persistence.Id;
21+
22+
import org.threeten.bp.Instant;
23+
import org.threeten.bp.LocalDate;
24+
import org.threeten.bp.LocalDateTime;
25+
import org.threeten.bp.LocalTime;
26+
27+
/**
28+
* @author Oliver Gierke
29+
*/
30+
@Entity
31+
public class DateTimeSample {
32+
33+
@Id @GeneratedValue Long id;
34+
35+
Instant instant;
36+
LocalDate localDate;
37+
LocalTime localTime;
38+
LocalDateTime localDateTime;
39+
}
+24-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.jpa.domain.support;
16+
package org.springframework.data.jpa.convert.threetenbp;
1717

1818
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
@@ -24,6 +24,10 @@
2424
import javax.persistence.PersistenceContext;
2525

2626
import org.junit.Test;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.data.jpa.domain.support.AbstractAttributeConverterIntegrationTests;
29+
import org.springframework.test.context.ContextConfiguration;
30+
import org.springframework.transaction.annotation.Transactional;
2731
import org.threeten.bp.Instant;
2832
import org.threeten.bp.LocalDate;
2933
import org.threeten.bp.LocalDateTime;
@@ -35,8 +39,19 @@
3539
* @author Oliver Gierke
3640
* @since 1.8
3741
*/
42+
@ContextConfiguration
43+
@Transactional
3844
public class ThreeTenBackPortJpaConvertersIntegrationTests extends AbstractAttributeConverterIntegrationTests {
3945

46+
@Configuration
47+
static class Config extends InfrastructureConfig {
48+
49+
@Override
50+
protected String getPackageName() {
51+
return getClass().getPackage().getName();
52+
}
53+
}
54+
4055
@PersistenceContext EntityManager em;
4156

4257
/**
@@ -49,20 +64,20 @@ public void usesThreeTenBackPortJpaConverters() {
4964

5065
DateTimeSample sample = new DateTimeSample();
5166

52-
sample.bpInstant = Instant.now();
53-
sample.bpLocalDate = LocalDate.now();
54-
sample.bpLocalTime = LocalTime.now();
55-
sample.bpLocalDateTime = LocalDateTime.now();
67+
sample.instant = Instant.now();
68+
sample.localDate = LocalDate.now();
69+
sample.localTime = LocalTime.now();
70+
sample.localDateTime = LocalDateTime.now();
5671

5772
em.persist(sample);
5873
em.clear();
5974

6075
DateTimeSample result = em.find(DateTimeSample.class, sample.id);
6176

6277
assertThat(result, is(notNullValue()));
63-
assertThat(result.bpInstant, is(sample.bpInstant));
64-
assertThat(result.bpLocalDate, is(sample.bpLocalDate));
65-
assertThat(result.bpLocalTime, is(sample.bpLocalTime));
66-
assertThat(result.bpLocalDateTime, is(sample.bpLocalDateTime));
78+
assertThat(result.instant, is(sample.instant));
79+
assertThat(result.localDate, is(sample.localDate));
80+
assertThat(result.localTime, is(sample.localTime));
81+
assertThat(result.localDateTime, is(sample.localDateTime));
6782
}
6883
}

src/test/java/org/springframework/data/jpa/domain/support/AbstractAttributeConverterIntegrationTests.java

+9-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import org.junit.runner.RunWith;
2222
import org.springframework.context.annotation.Bean;
23-
import org.springframework.context.annotation.Configuration;
2423
import org.springframework.data.jpa.domain.sample.User;
2524
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
2625
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
@@ -29,45 +28,44 @@
2928
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
3029
import org.springframework.orm.jpa.vendor.Database;
3130
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
32-
import org.springframework.test.context.ContextConfiguration;
3331
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3432
import org.springframework.transaction.PlatformTransactionManager;
35-
import org.springframework.transaction.annotation.Transactional;
3633

3734
/**
3835
* Base class for integration tests for JPA 2.1 {@link AttributeConverter} integration.
3936
*
4037
* @author Oliver Gierke
4138
*/
4239
@RunWith(SpringJUnit4ClassRunner.class)
43-
@ContextConfiguration
44-
@Transactional
4540
public abstract class AbstractAttributeConverterIntegrationTests {
4641

47-
@Configuration
48-
static class Config {
42+
protected abstract static class InfrastructureConfig {
4943

5044
@Bean
51-
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
45+
LocalContainerEntityManagerFactoryBean entityManagerFactory() {
5246

5347
AbstractJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
5448
vendorAdapter.setDatabase(Database.HSQL);
5549
vendorAdapter.setGenerateDdl(true);
5650

5751
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
5852
factory.setDataSource(dataSource());
59-
factory.setPackagesToScan(getClass().getPackage().getName(), User.class.getPackage().getName());
53+
factory.setPackagesToScan(getPackageName(), User.class.getPackage().getName());
6054
factory.setJpaVendorAdapter(vendorAdapter);
6155

6256
return factory;
6357
}
6458

65-
public @Bean PlatformTransactionManager transactionManager() {
59+
@Bean
60+
PlatformTransactionManager transactionManager() {
6661
return new JpaTransactionManager();
6762
}
6863

69-
public @Bean DataSource dataSource() {
64+
@Bean
65+
DataSource dataSource() {
7066
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL).build();
7167
}
68+
69+
protected abstract String getPackageName();
7270
}
7371
}

0 commit comments

Comments
 (0)