|
| 1 | +/* |
| 2 | + * Copyright 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 | +package org.springframework.data.repository.core.support; |
| 17 | + |
| 18 | +import static org.hamcrest.CoreMatchers.*; |
| 19 | +import static org.junit.Assert.*; |
| 20 | + |
| 21 | +import java.util.Arrays; |
| 22 | + |
| 23 | +import org.junit.Assume; |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | +import org.springframework.beans.factory.BeanFactoryUtils; |
| 27 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 28 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 29 | +import org.springframework.core.SpringVersion; |
| 30 | +import org.springframework.data.querydsl.User; |
| 31 | +import org.springframework.data.repository.Repository; |
| 32 | + |
| 33 | +/** |
| 34 | + * Integration test to make sure Spring Data repository factory beans are found without the factories already |
| 35 | + * instantiated. Only executed for Spring version newer than 3.2.2.RELEASE. |
| 36 | + * |
| 37 | + * @see SPR-10517 |
| 38 | + * @author Oliver Gierke |
| 39 | + */ |
| 40 | +public class RepositoryInterfaceAwareBeanPostProcessorIntegrationTests { |
| 41 | + |
| 42 | + static final String LAST_ERROR_VERSION = "3.2.2.RELEASE"; |
| 43 | + |
| 44 | + DefaultListableBeanFactory factory; |
| 45 | + |
| 46 | + @Before |
| 47 | + public void setUp() { |
| 48 | + |
| 49 | + factory = new DefaultListableBeanFactory(); |
| 50 | + |
| 51 | + // Register factory bean for repository |
| 52 | + BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DummyRepositoryFactoryBean.class); |
| 53 | + builder.addPropertyValue("repositoryInterface", UserRepository.class); |
| 54 | + factory.registerBeanDefinition("repository", builder.getBeanDefinition()); |
| 55 | + |
| 56 | + // Register predicting BeanPostProcessor |
| 57 | + RepositoryInterfaceAwareBeanPostProcessor processor = new RepositoryInterfaceAwareBeanPostProcessor(); |
| 58 | + processor.setBeanFactory(factory); |
| 59 | + factory.addBeanPostProcessor(processor); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void lookupBeforeInstantiation() { |
| 64 | + |
| 65 | + Assume.assumeTrue(isFixedSpringVersion()); |
| 66 | + |
| 67 | + String[] strings = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, RepositoryFactoryInformation.class, |
| 68 | + false, false); |
| 69 | + assertThat(Arrays.asList(strings), hasItem("&repository")); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void lookupAfterInstantiation() { |
| 74 | + |
| 75 | + Assume.assumeTrue(isFixedSpringVersion()); |
| 76 | + |
| 77 | + factory.getBean(UserRepository.class); |
| 78 | + |
| 79 | + String[] strings = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, RepositoryFactoryInformation.class, |
| 80 | + false, false); |
| 81 | + assertThat(Arrays.asList(strings), hasItem("&repository")); |
| 82 | + } |
| 83 | + |
| 84 | + private static boolean isFixedSpringVersion() { |
| 85 | + return SpringVersion.getVersion().compareTo(LAST_ERROR_VERSION) == 1; |
| 86 | + } |
| 87 | + |
| 88 | + interface UserRepository extends Repository<User, Long> { |
| 89 | + |
| 90 | + } |
| 91 | +} |
0 commit comments