@Test public void testWithNoFilters() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE); assertEquals(0, candidates.size()); }
@Test public void bogusPackageWithScan() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); provider.setResourceLoader(new DefaultResourceLoader( CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader()))); Set<BeanDefinition> candidates = provider.findCandidateComponents("bogus"); assertEquals(0, candidates.size()); }
@Test public void customAssignableTypeIncludeFilterWithScan() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.setResourceLoader(new DefaultResourceLoader( CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader()))); testCustomAssignableTypeIncludeFilter(provider, ScannedGenericBeanDefinition.class); }
@Test public void testWithNullEnvironment() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_PROFILE_PACKAGE); assertThat(containsBeanClass(candidates, ProfileAnnotatedComponent.class), is(false)); }
@Test public void customAnnotationTypeIncludeFilterWithScan() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.setResourceLoader(new DefaultResourceLoader( CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader()))); testCustomAnnotationTypeIncludeFilter(provider, ScannedGenericBeanDefinition.class); }
@Test public void excludeFilterWithScan() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); provider.setResourceLoader(new DefaultResourceLoader( CandidateComponentsTestClassLoader.disableIndex(getClass().getClassLoader()))); provider.addExcludeFilter(new RegexPatternTypeFilter(Pattern.compile(TEST_BASE_PACKAGE + ".*Named.*"))); testExclude(provider, ScannedGenericBeanDefinition.class); }
@Test public void bogusPackageWithIndex() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); Set<BeanDefinition> candidates = provider.findCandidateComponents("bogus"); assertEquals(0, candidates.size()); }
@Test public void antStylePackageWithIndex() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); testAntStyle(provider, AnnotatedGenericBeanDefinition.class); }
@Test public void customAnnotationTypeIncludeFilterWithIndex() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); testCustomAnnotationTypeIncludeFilter(provider, AnnotatedGenericBeanDefinition.class); }
@Test public void customSupportedIncludeAndExcludeFilterWithIndex() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); testCustomSupportedIncludeAndExcludeFilter(provider, AnnotatedGenericBeanDefinition.class); }
@Test public void defaultsWithIndex() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); testDefault(provider, AnnotatedGenericBeanDefinition.class); }
@Test public void customAssignableTypeIncludeFilterWithIndex() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); testCustomAssignableTypeIncludeFilter(provider, AnnotatedGenericBeanDefinition.class); }
@Test public void excludeFilterWithIndex() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); provider.addExcludeFilter(new RegexPatternTypeFilter(Pattern.compile(TEST_BASE_PACKAGE + ".*Named.*"))); testExclude(provider, AnnotatedGenericBeanDefinition.class); }
@Test public void testWithInterfaceType() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.addIncludeFilter(new AssignableTypeFilter(FooDao.class)); Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE); assertEquals(1, candidates.size()); assertTrue(containsBeanClass(candidates, StubFooDao.class)); }
@Test public void testWithInactiveProfile() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); ConfigurableEnvironment env = new StandardEnvironment(); env.setActiveProfiles("other"); provider.setEnvironment(env); Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_PROFILE_PACKAGE); assertThat(containsBeanClass(candidates, ProfileAnnotatedComponent.class), is(false)); }
@Test public void testWithClassType() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.addIncludeFilter(new AssignableTypeFilter(MessageBean.class)); Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE); assertEquals(1, candidates.size()); assertTrue(containsBeanClass(candidates, MessageBean.class)); }
@Test public void customNotSupportedIncludeFilterUseScan() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); provider.addIncludeFilter(new AssignableTypeFilter(FooDao.class)); Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE); assertTrue(containsBeanClass(candidates, StubFooDao.class)); assertEquals(1, candidates.size()); assertBeanDefinitionType(candidates, ScannedGenericBeanDefinition.class); }
@Test public void customSupportIncludeFilterWithNonIndexedTypeUseScan() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); // This annotation type is not directly annotated with Indexed so we can use // the index to find candidates provider.addIncludeFilter(new AnnotationTypeFilter(CustomStereotype.class)); Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE); assertTrue(containsBeanClass(candidates, DefaultNamedComponent.class)); assertEquals(1, candidates.size()); assertBeanDefinitionType(candidates, ScannedGenericBeanDefinition.class); }
@Test public void customFiltersFollowedByResetUseIndex() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.setResourceLoader(new DefaultResourceLoader(TEST_BASE_CLASSLOADER)); provider.addIncludeFilter(new AnnotationTypeFilter(Component.class)); provider.resetFilters(true); Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE); assertBeanDefinitionType(candidates, AnnotatedGenericBeanDefinition.class); }
@Test public void testWithMultipleMatchingFilters() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.addIncludeFilter(new AnnotationTypeFilter(Component.class)); provider.addIncludeFilter(new AssignableTypeFilter(FooServiceImpl.class)); Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE); assertEquals(7, candidates.size()); assertTrue(containsBeanClass(candidates, NamedComponent.class)); assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class)); assertTrue(containsBeanClass(candidates, FooServiceImpl.class)); assertTrue(containsBeanClass(candidates, BarComponent.class)); }