|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 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.redis; |
| 18 | + |
| 19 | +import org.junit.After; |
| 20 | +import org.junit.Rule; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; |
| 24 | +import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; |
| 25 | +import org.springframework.boot.autoconfigure.data.alt.redis.CityRedisRepository; |
| 26 | +import org.springframework.boot.autoconfigure.data.empty.EmptyDataPackage; |
| 27 | +import org.springframework.boot.autoconfigure.data.redis.city.City; |
| 28 | +import org.springframework.boot.autoconfigure.data.redis.city.CityRepository; |
| 29 | +import org.springframework.boot.redis.RedisTestServer; |
| 30 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | +import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; |
| 33 | + |
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
| 35 | + |
| 36 | +/** |
| 37 | + * Tests for {@link RedisRepositoriesAutoConfiguration}. |
| 38 | + * |
| 39 | + * @author Eddú Meléndez |
| 40 | + */ |
| 41 | +public class RedisRepositoriesAutoConfigurationTests { |
| 42 | + |
| 43 | + @Rule |
| 44 | + public RedisTestServer redis = new RedisTestServer(); |
| 45 | + |
| 46 | + private AnnotationConfigApplicationContext context |
| 47 | + = new AnnotationConfigApplicationContext(); |
| 48 | + |
| 49 | + @After |
| 50 | + public void close() { |
| 51 | + this.context.close(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testDefaultRepositoryConfiguration() { |
| 56 | + this.context.register(TestConfiguration.class, |
| 57 | + RedisAutoConfiguration.class, RedisRepositoriesAutoConfiguration.class, |
| 58 | + PropertyPlaceholderAutoConfiguration.class); |
| 59 | + this.context.refresh(); |
| 60 | + assertThat(this.context.getBean(CityRepository.class)).isNotNull(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testNoRepositoryConfiguration() { |
| 65 | + this.context.register(EmptyConfiguration.class, |
| 66 | + RedisAutoConfiguration.class, |
| 67 | + RedisRepositoriesAutoConfiguration.class, |
| 68 | + PropertyPlaceholderAutoConfiguration.class); |
| 69 | + this.context.refresh(); |
| 70 | + assertThat(this.context.getBean("redisTemplate")).isNotNull(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() { |
| 75 | + this.context.register(CustomizedConfiguration.class, |
| 76 | + RedisAutoConfiguration.class, |
| 77 | + RedisRepositoriesAutoConfiguration.class, |
| 78 | + PropertyPlaceholderAutoConfiguration.class); |
| 79 | + this.context.refresh(); |
| 80 | + assertThat(this.context.getBean(CityRedisRepository.class)).isNotNull(); |
| 81 | + } |
| 82 | + |
| 83 | + @Configuration |
| 84 | + @TestAutoConfigurationPackage(City.class) |
| 85 | + protected static class TestConfiguration { |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + @Configuration |
| 90 | + @TestAutoConfigurationPackage(EmptyDataPackage.class) |
| 91 | + protected static class EmptyConfiguration { |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + @Configuration |
| 96 | + @TestAutoConfigurationPackage(RedisRepositoriesAutoConfigurationTests.class) |
| 97 | + @EnableRedisRepositories(basePackageClasses = CityRedisRepository.class) |
| 98 | + static class CustomizedConfiguration { |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments