|
| 1 | +/* |
| 2 | + * Copyright 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 | +package org.springframework.data.redis.repository.configuration; |
| 17 | + |
| 18 | +import static org.junit.Assert.*; |
| 19 | + |
| 20 | +import java.util.Collection; |
| 21 | + |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | +import org.springframework.core.env.Environment; |
| 25 | +import org.springframework.core.env.StandardEnvironment; |
| 26 | +import org.springframework.core.io.ResourceLoader; |
| 27 | +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; |
| 28 | +import org.springframework.core.type.StandardAnnotationMetadata; |
| 29 | +import org.springframework.data.annotation.Id; |
| 30 | +import org.springframework.data.keyvalue.repository.KeyValueRepository; |
| 31 | +import org.springframework.data.redis.core.RedisHash; |
| 32 | +import org.springframework.data.repository.Repository; |
| 33 | +import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource; |
| 34 | +import org.springframework.data.repository.config.RepositoryConfiguration; |
| 35 | +import org.springframework.data.repository.config.RepositoryConfigurationSource; |
| 36 | + |
| 37 | +/** |
| 38 | + * @author Christoph Strobl |
| 39 | + */ |
| 40 | +public class RedisRepositoryConfigurationExtensionUnitTests { |
| 41 | + |
| 42 | + StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(Config.class, true); |
| 43 | + ResourceLoader loader = new PathMatchingResourcePatternResolver(); |
| 44 | + Environment environment = new StandardEnvironment(); |
| 45 | + RepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(metadata, |
| 46 | + EnableRedisRepositories.class, loader, environment); |
| 47 | + |
| 48 | + RedisRepositoryConfigurationExtension extension; |
| 49 | + |
| 50 | + @Before |
| 51 | + public void setUp() { |
| 52 | + extension = new RedisRepositoryConfigurationExtension(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @see DATAREDIS-425 |
| 57 | + */ |
| 58 | + @Test |
| 59 | + public void isStrictMatchIfDomainTypeIsAnnotatedWithDocument() { |
| 60 | + assertHasRepo(SampleRepository.class, extension.getRepositoryConfigurations(configurationSource, loader, true)); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @see DATAREDIS-425 |
| 65 | + */ |
| 66 | + @Test |
| 67 | + public void isStrictMatchIfRepositoryExtendsStoreSpecificBase() { |
| 68 | + assertHasRepo(StoreRepository.class, extension.getRepositoryConfigurations(configurationSource, loader, true)); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @see DATAREDIS-425 |
| 73 | + */ |
| 74 | + @Test |
| 75 | + public void isNotStrictMatchIfDomainTypeIsNotAnnotatedWithDocument() { |
| 76 | + |
| 77 | + assertDoesNotHaveRepo(UnannotatedRepository.class, |
| 78 | + extension.getRepositoryConfigurations(configurationSource, loader, true)); |
| 79 | + } |
| 80 | + |
| 81 | + private static void assertDoesNotHaveRepo(Class<?> repositoryInterface, |
| 82 | + Collection<RepositoryConfiguration<RepositoryConfigurationSource>> configs) { |
| 83 | + |
| 84 | + try { |
| 85 | + |
| 86 | + assertHasRepo(repositoryInterface, configs); |
| 87 | + fail("Expected not to find config for repository interface ".concat(repositoryInterface.getName())); |
| 88 | + } catch (AssertionError error) { |
| 89 | + // repo not there. we're fine. |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static void assertHasRepo(Class<?> repositoryInterface, |
| 94 | + Collection<RepositoryConfiguration<RepositoryConfigurationSource>> configs) { |
| 95 | + |
| 96 | + for (RepositoryConfiguration<?> config : configs) { |
| 97 | + if (config.getRepositoryInterface().equals(repositoryInterface.getName())) { |
| 98 | + return; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + fail("Expected to find config for repository interface ".concat(repositoryInterface.getName()).concat(" but got ") |
| 103 | + .concat(configs.toString())); |
| 104 | + } |
| 105 | + |
| 106 | + @EnableRedisRepositories(considerNestedRepositories = true) |
| 107 | + static class Config { |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + @RedisHash |
| 112 | + static class Sample { |
| 113 | + @Id String id; |
| 114 | + } |
| 115 | + |
| 116 | + interface SampleRepository extends Repository<Sample, Long> {} |
| 117 | + |
| 118 | + interface UnannotatedRepository extends Repository<Object, Long> {} |
| 119 | + |
| 120 | + interface StoreRepository extends KeyValueRepository<Object, Long> {} |
| 121 | +} |
0 commit comments