Improve Mongo auto-configuration
Disable Mongo auto-configuation when @EnableMongoRepositories is used and adapt the test helper classes accordingly. Change the property prefix and dependency management version property from `...mongo` to `...mongodb` for consistency with Spring Data. Fixes gh-315
This commit is contained in:
committed by
Phillip Webb
parent
16df38e928
commit
cf4df1befb
@@ -36,6 +36,7 @@ import static org.junit.Assert.assertNotNull;
|
||||
* Tests for {@link JpaRepositoriesAutoConfiguration}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class JpaRepositoriesAutoConfigurationTests {
|
||||
|
||||
@@ -65,7 +66,7 @@ public class JpaRepositoriesAutoConfigurationTests {
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertNotNull(this.context
|
||||
.getBean(org.springframework.boot.autoconfigure.data.alt.CityRepository.class));
|
||||
.getBean(org.springframework.boot.autoconfigure.data.alt.CityJpaRepository.class));
|
||||
assertNotNull(this.context.getBean(PlatformTransactionManager.class));
|
||||
assertNotNull(this.context.getBean(EntityManagerFactory.class));
|
||||
}
|
||||
@@ -77,7 +78,7 @@ public class JpaRepositoriesAutoConfigurationTests {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableJpaRepositories(basePackageClasses = org.springframework.boot.autoconfigure.data.alt.CityRepository.class)
|
||||
@EnableJpaRepositories(basePackageClasses = org.springframework.boot.autoconfigure.data.alt.CityJpaRepository.class)
|
||||
@TestAutoConfigurationPackage(City.class)
|
||||
protected static class CustomConfiguration {
|
||||
|
||||
|
||||
@@ -19,19 +19,26 @@ package org.springframework.boot.autoconfigure.data;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
|
||||
import org.springframework.boot.autoconfigure.data.alt.CityMongoDbRepository;
|
||||
import org.springframework.boot.autoconfigure.data.mongo.City;
|
||||
import org.springframework.boot.autoconfigure.data.mongo.CityRepository;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
import com.mongodb.Mongo;
|
||||
import com.mongodb.MongoClient;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link MongoRepositoriesAutoConfiguration}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class MongoRepositoriesAutoConfigurationTests {
|
||||
|
||||
@@ -45,7 +52,9 @@ public class MongoRepositoriesAutoConfigurationTests {
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertNotNull(this.context.getBean(CityRepository.class));
|
||||
assertNotNull(this.context.getBean(Mongo.class));
|
||||
|
||||
Mongo mongo = this.context.getBean(Mongo.class);
|
||||
assertThat(mongo, is(instanceOf(MongoClient.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -55,7 +64,19 @@ public class MongoRepositoriesAutoConfigurationTests {
|
||||
MongoRepositoriesAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertNotNull(this.context.getBean(Mongo.class));
|
||||
|
||||
Mongo mongo = this.context.getBean(Mongo.class);
|
||||
assertThat(mongo, is(instanceOf(MongoClient.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(CustomizedConfiguration.class,
|
||||
MongoRepositoriesAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertNotNull(this.context.getBean(CityMongoDbRepository.class));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@@ -70,4 +91,10 @@ public class MongoRepositoriesAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@TestAutoConfigurationPackage(MongoRepositoriesAutoConfigurationTests.class)
|
||||
@EnableMongoRepositories(basePackageClasses = CityMongoDbRepository.class)
|
||||
protected static class CustomizedConfiguration {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,6 @@ package org.springframework.boot.autoconfigure.data.alt;
|
||||
import org.springframework.boot.autoconfigure.data.jpa.City;
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
public interface CityRepository extends Repository<City, Long> {
|
||||
public interface CityJpaRepository extends Repository<City, Long> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2014-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.data.alt;
|
||||
|
||||
import org.springframework.boot.autoconfigure.data.mongo.City;
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
public interface CityMongoDbRepository extends Repository<City, Long> {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user