|
| 1 | +package org.springframework.data.redis.repository.configuration; |
| 2 | +/* |
| 3 | + * Copyright 2016 the original author or authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import static org.hamcrest.core.Is.*; |
| 19 | +import static org.hamcrest.core.IsEqual.*; |
| 20 | +import static org.hamcrest.core.IsNull.*; |
| 21 | +import static org.junit.Assert.*; |
| 22 | +import static org.mockito.Mockito.*; |
| 23 | + |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runner.RunWith; |
| 26 | +import org.junit.runners.Suite; |
| 27 | +import org.junit.runners.Suite.SuiteClasses; |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.context.ApplicationContext; |
| 30 | +import org.springframework.context.annotation.Bean; |
| 31 | +import org.springframework.data.annotation.Id; |
| 32 | +import org.springframework.data.redis.connection.RedisConnection; |
| 33 | +import org.springframework.data.redis.connection.RedisConnectionFactory; |
| 34 | +import org.springframework.data.redis.core.RedisHash; |
| 35 | +import org.springframework.data.redis.core.RedisKeyValueAdapter; |
| 36 | +import org.springframework.data.redis.core.RedisTemplate; |
| 37 | +import org.springframework.data.redis.core.convert.ReferenceResolver; |
| 38 | +import org.springframework.data.redis.repository.configuration.RedisRepositoryConfigurationUnitTests.ContextWithCustomReferenceResolver; |
| 39 | +import org.springframework.data.redis.repository.configuration.RedisRepositoryConfigurationUnitTests.ContextWithoutCustomization; |
| 40 | +import org.springframework.data.repository.Repository; |
| 41 | +import org.springframework.test.annotation.DirtiesContext; |
| 42 | +import org.springframework.test.context.ContextConfiguration; |
| 43 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 44 | +import org.springframework.test.util.ReflectionTestUtils; |
| 45 | + |
| 46 | +/** |
| 47 | + * @author Christoph Strobl |
| 48 | + */ |
| 49 | +@RunWith(Suite.class) |
| 50 | +@SuiteClasses({ ContextWithCustomReferenceResolver.class, ContextWithoutCustomization.class }) |
| 51 | +public class RedisRepositoryConfigurationUnitTests { |
| 52 | + |
| 53 | + static RedisTemplate<?, ?> createTemplateMock() { |
| 54 | + |
| 55 | + RedisTemplate<?, ?> template = mock(RedisTemplate.class); |
| 56 | + RedisConnectionFactory connectionFactory = mock(RedisConnectionFactory.class); |
| 57 | + RedisConnection connection = mock(RedisConnection.class); |
| 58 | + |
| 59 | + when(template.getConnectionFactory()).thenReturn(connectionFactory); |
| 60 | + when(connectionFactory.getConnection()).thenReturn(connection); |
| 61 | + |
| 62 | + return template; |
| 63 | + } |
| 64 | + |
| 65 | + @RunWith(SpringJUnit4ClassRunner.class) |
| 66 | + @DirtiesContext |
| 67 | + @ContextConfiguration(classes = { ContextWithCustomReferenceResolver.Config.class }) |
| 68 | + public static class ContextWithCustomReferenceResolver { |
| 69 | + |
| 70 | + @EnableRedisRepositories(considerNestedRepositories = true) |
| 71 | + static class Config { |
| 72 | + |
| 73 | + @Bean |
| 74 | + RedisTemplate<?, ?> redisTemplate() { |
| 75 | + return createTemplateMock(); |
| 76 | + } |
| 77 | + |
| 78 | + @Bean |
| 79 | + ReferenceResolver redisReferenceResolver() { |
| 80 | + return mock(ReferenceResolver.class); |
| 81 | + } |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + @Autowired ApplicationContext ctx; |
| 86 | + |
| 87 | + /** |
| 88 | + * @see DATAREDIS-425 |
| 89 | + */ |
| 90 | + @Test |
| 91 | + public void shouldPickUpReferenceResolver() { |
| 92 | + |
| 93 | + RedisKeyValueAdapter adapter = (RedisKeyValueAdapter) ctx.getBean("redisKeyValueAdapter"); |
| 94 | + |
| 95 | + Object referenceResolver = ReflectionTestUtils.getField(adapter.getConverter(), "referenceResolver"); |
| 96 | + |
| 97 | + assertThat(referenceResolver, is(equalTo(ctx.getBean("redisReferenceResolver")))); |
| 98 | + assertThat(mockingDetails(referenceResolver).isMock(), is(true)); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @RunWith(SpringJUnit4ClassRunner.class) |
| 103 | + @DirtiesContext |
| 104 | + @ContextConfiguration(classes = { ContextWithoutCustomization.Config.class }) |
| 105 | + public static class ContextWithoutCustomization { |
| 106 | + |
| 107 | + @EnableRedisRepositories(considerNestedRepositories = true) |
| 108 | + static class Config { |
| 109 | + |
| 110 | + @Bean |
| 111 | + RedisTemplate<?, ?> redisTemplate() { |
| 112 | + return createTemplateMock(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Autowired ApplicationContext ctx; |
| 117 | + |
| 118 | + /** |
| 119 | + * @see DATAREDIS-425 |
| 120 | + */ |
| 121 | + @Test |
| 122 | + public void shouldInitWithDefaults() { |
| 123 | + assertThat(ctx.getBean(SampleRepository.class), is(notNullValue())); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @see DATAREDIS-425 |
| 128 | + */ |
| 129 | + @Test |
| 130 | + public void shouldRegisterDefaultBeans() { |
| 131 | + |
| 132 | + assertThat(ctx.getBean(SampleRepository.class), is(notNullValue())); |
| 133 | + assertThat(ctx.getBean("redisKeyValueAdapter"), is(notNullValue())); |
| 134 | + assertThat(ctx.getBean("redisCustomConversions"), is(notNullValue())); |
| 135 | + assertThat(ctx.getBean("redisReferenceResolver"), is(notNullValue())); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @RedisHash |
| 140 | + static class Sample { |
| 141 | + @Id String id; |
| 142 | + } |
| 143 | + |
| 144 | + interface SampleRepository extends Repository<Sample, Long> {} |
| 145 | +} |
0 commit comments