Skip to content

Commit 376d6fb

Browse files
author
Dave Syer
committed
Add test for mixed Mongo/Jpa repositories
1 parent b75578d commit 376d6fb

File tree

5 files changed

+183
-1
lines changed

5 files changed

+183
-1
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoRepositoriesAutoConfiguration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
package org.springframework.boot.autoconfigure.data.mongo;
1818

1919
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2021
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2122
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2223
import org.springframework.context.annotation.Configuration;
2324
import org.springframework.context.annotation.Import;
25+
import org.springframework.data.mongodb.core.MongoOperations;
2426
import org.springframework.data.mongodb.repository.MongoRepository;
2527
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
2628
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean;
@@ -50,6 +52,7 @@
5052
*/
5153
@Configuration
5254
@ConditionalOnClass({ Mongo.class, MongoRepository.class })
55+
@ConditionalOnBean(MongoOperations.class)
5356
@ConditionalOnMissingBean(MongoRepositoryFactoryBean.class)
5457
@Import(MongoRepositoriesAutoConfigureRegistrar.class)
5558
public class MongoRepositoriesAutoConfiguration {

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoRepositoriesAutoConfigureRegistrar.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ protected RepositoryConfigurationExtension getRepositoryConfigurationExtension()
5050

5151
@EnableMongoRepositories
5252
private static class EnableMongoRepositoriesConfiguration {
53-
5453
}
5554

5655
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2012-2014 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+
17+
package org.springframework.boot.autoconfigure.data.mongo;
18+
19+
import org.junit.After;
20+
import org.junit.Test;
21+
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
22+
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
23+
import org.springframework.boot.autoconfigure.data.jpa.city.City;
24+
import org.springframework.boot.autoconfigure.data.jpa.city.CityRepository;
25+
import org.springframework.boot.autoconfigure.data.mongo.country.Country;
26+
import org.springframework.boot.autoconfigure.data.mongo.country.CountryRepository;
27+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
28+
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
29+
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfigurationTests;
30+
import org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration;
31+
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
32+
import org.springframework.boot.orm.jpa.EntityScan;
33+
import org.springframework.boot.test.EnvironmentTestUtils;
34+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
35+
import org.springframework.context.annotation.Configuration;
36+
import org.springframework.context.annotation.Import;
37+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
38+
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
39+
40+
import static org.junit.Assert.assertNotNull;
41+
42+
/**
43+
* Tests for {@link MongoRepositoriesAutoConfiguration}.
44+
*
45+
* @author Dave Syer
46+
* @author Oliver Gierke
47+
*/
48+
public class MixedMongoRepositoriesAutoConfigurationTests {
49+
50+
private AnnotationConfigApplicationContext context;
51+
52+
@After
53+
public void close() {
54+
this.context.close();
55+
}
56+
57+
@Test
58+
public void testDefaultRepositoryConfiguration() throws Exception {
59+
this.context = new AnnotationConfigApplicationContext();
60+
EnvironmentTestUtils.addEnvironment(this.context,
61+
"spring.datasource.initialize:false");
62+
this.context.register(TestConfiguration.class, BaseConfiguration.class);
63+
this.context.refresh();
64+
assertNotNull(this.context.getBean(CountryRepository.class));
65+
}
66+
67+
@Test
68+
public void testMixedRepositoryConfiguration() throws Exception {
69+
this.context = new AnnotationConfigApplicationContext();
70+
EnvironmentTestUtils.addEnvironment(this.context,
71+
"spring.datasource.initialize:false");
72+
this.context.register(MixedConfiguration.class, BaseConfiguration.class);
73+
this.context.refresh();
74+
assertNotNull(this.context.getBean(CountryRepository.class));
75+
assertNotNull(this.context.getBean(CityRepository.class));
76+
}
77+
78+
@Configuration
79+
@Import({ MongoAutoConfiguration.class, MongoDataAutoConfiguration.class,
80+
MongoRepositoriesAutoConfiguration.class, DataSourceAutoConfiguration.class,
81+
HibernateJpaAutoConfiguration.class,
82+
PropertyPlaceholderAutoConfiguration.class })
83+
protected static class BaseConfiguration {
84+
85+
}
86+
87+
@Configuration
88+
@TestAutoConfigurationPackage(MongoAutoConfigurationTests.class)
89+
// Not this package or its parent
90+
@EnableMongoRepositories(basePackageClasses = Country.class)
91+
protected static class TestConfiguration {
92+
93+
}
94+
95+
@Configuration
96+
@TestAutoConfigurationPackage(MongoAutoConfigurationTests.class)
97+
@EnableMongoRepositories(basePackageClasses = Country.class)
98+
@EntityScan(basePackageClasses = City.class)
99+
@EnableJpaRepositories(basePackageClasses = CityRepository.class)
100+
protected static class MixedConfiguration {
101+
102+
}
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2012-2013 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+
17+
package org.springframework.boot.autoconfigure.data.mongo.country;
18+
19+
import java.io.Serializable;
20+
21+
import javax.persistence.Column;
22+
import javax.persistence.Entity;
23+
import javax.persistence.GeneratedValue;
24+
import javax.persistence.Id;
25+
26+
@Entity
27+
public class Country implements Serializable {
28+
29+
private static final long serialVersionUID = 1L;
30+
31+
@Id
32+
@GeneratedValue
33+
private Long id;
34+
35+
@Column(nullable = false)
36+
private String name;
37+
38+
protected Country() {
39+
}
40+
41+
public Country(String name) {
42+
super();
43+
this.name = name;
44+
}
45+
46+
public String getName() {
47+
return this.name;
48+
}
49+
50+
@Override
51+
public String toString() {
52+
return getName();
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2012-2013 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+
17+
package org.springframework.boot.autoconfigure.data.mongo.country;
18+
19+
import org.springframework.data.repository.Repository;
20+
21+
public interface CountryRepository extends Repository<Country, Long> {
22+
23+
}

0 commit comments

Comments
 (0)