private TypeExcludeFilter createDelegatingTypeExcludeFilter() { final Set<TypeExcludeFilter> filters = new LinkedHashSet<TypeExcludeFilter>( this.filterClasses.size()); for (Class<? extends TypeExcludeFilter> filterClass : this.filterClasses) { filters.add(createTypeExcludeFilter(filterClass)); } return new TypeExcludeFilter() { @Override public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { for (TypeExcludeFilter filter : filters) { if (filter.match(metadataReader, metadataReaderFactory)) { return true; } } return false; } }; }
@Test public void getContextCustomizerShouldAddExcludeFilters() throws Exception { ContextCustomizer customizer = this.factory .createContextCustomizer(WithExcludeFilters.class, null); customizer.customizeContext(this.context, this.mergedContextConfiguration); this.context.refresh(); TypeExcludeFilter filter = this.context.getBean(TypeExcludeFilter.class); MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); MetadataReader metadataReader = metadataReaderFactory .getMetadataReader(NoAnnotation.class.getName()); assertThat(filter.match(metadataReader, metadataReaderFactory)).isFalse(); metadataReader = metadataReaderFactory .getMetadataReader(SimpleExclude.class.getName()); assertThat(filter.match(metadataReader, metadataReaderFactory)).isTrue(); metadataReader = metadataReaderFactory .getMetadataReader(TestClassAwareExclude.class.getName()); assertThat(filter.match(metadataReader, metadataReaderFactory)).isTrue(); }
private TypeExcludeFilter createTypeExcludeFilter(Class<?> type) { try { Constructor<?> constructor = getTypeExcludeFilterConstructor(type); ReflectionUtils.makeAccessible(constructor); if (constructor.getParameterTypes().length == 1) { return (TypeExcludeFilter) constructor.newInstance(this.testClass); } return (TypeExcludeFilter) constructor.newInstance(); } catch (Exception ex) { throw new IllegalStateException("Unable to create filter for " + type, ex); } }
@Override public ContextCustomizer createContextCustomizer(Class<?> testClass, List<ContextConfigurationAttributes> configurationAttributes) { TypeExcludeFilters annotation = AnnotatedElementUtils .findMergedAnnotation(testClass, TypeExcludeFilters.class); if (annotation != null) { Set<Class<? extends TypeExcludeFilter>> filterClasses = new LinkedHashSet<Class<? extends TypeExcludeFilter>>( Arrays.asList(annotation.value())); return new TypeExcludeFiltersContextCustomizer(testClass, filterClasses); } return null; }
TypeExcludeFiltersContextCustomizer(Class<?> testClass, Set<Class<? extends TypeExcludeFilter>> filterClasses) { this.testClass = testClass; this.filterClasses = filterClasses; }