|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2019 the original author or authors. |
| 2 | + * Copyright 2012-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.domain;
|
18 | 18 |
|
| 19 | +import java.util.Collections; |
19 | 20 | import java.util.Set;
|
20 | 21 |
|
21 | 22 | import javax.persistence.Embeddable;
|
22 | 23 | import javax.persistence.Entity;
|
23 | 24 |
|
24 | 25 | import org.junit.jupiter.api.Test;
|
| 26 | +import org.mockito.ArgumentCaptor; |
25 | 27 |
|
26 | 28 | import org.springframework.boot.autoconfigure.domain.scan.a.EmbeddableA;
|
27 | 29 | import org.springframework.boot.autoconfigure.domain.scan.a.EntityA;
|
28 | 30 | import org.springframework.boot.autoconfigure.domain.scan.b.EmbeddableB;
|
29 | 31 | import org.springframework.boot.autoconfigure.domain.scan.b.EntityB;
|
30 | 32 | import org.springframework.boot.autoconfigure.domain.scan.c.EmbeddableC;
|
31 | 33 | import org.springframework.boot.autoconfigure.domain.scan.c.EntityC;
|
| 34 | +import org.springframework.context.ApplicationContext; |
32 | 35 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
| 36 | +import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; |
33 | 37 | import org.springframework.context.annotation.Configuration;
|
| 38 | +import org.springframework.core.type.filter.AnnotationTypeFilter; |
34 | 39 |
|
35 | 40 | import static org.assertj.core.api.Assertions.assertThat;
|
36 | 41 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
| 42 | +import static org.mockito.BDDMockito.given; |
| 43 | +import static org.mockito.Mockito.mock; |
| 44 | +import static org.mockito.Mockito.verify; |
| 45 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
37 | 46 |
|
38 | 47 | /**
|
39 | 48 | * Tests for {@link EntityScanner}.
|
@@ -79,6 +88,41 @@ void scanShouldFilterOnAnnotation() throws Exception {
|
79 | 88 | context.close();
|
80 | 89 | }
|
81 | 90 |
|
| 91 | + @Test |
| 92 | + void scanShouldUseCustomCandidateComponentProvider() throws ClassNotFoundException { |
| 93 | + ClassPathScanningCandidateComponentProvider candidateComponentProvider = mock( |
| 94 | + ClassPathScanningCandidateComponentProvider.class); |
| 95 | + given(candidateComponentProvider.findCandidateComponents("org.springframework.boot.autoconfigure.domain.scan")) |
| 96 | + .willReturn(Collections.emptySet()); |
| 97 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScanConfig.class); |
| 98 | + TestEntityScanner scanner = new TestEntityScanner(context, candidateComponentProvider); |
| 99 | + scanner.scan(Entity.class); |
| 100 | + ArgumentCaptor<AnnotationTypeFilter> annotationTypeFilter = ArgumentCaptor.forClass(AnnotationTypeFilter.class); |
| 101 | + verify(candidateComponentProvider).addIncludeFilter(annotationTypeFilter.capture()); |
| 102 | + verify(candidateComponentProvider) |
| 103 | + .findCandidateComponents("org.springframework.boot.autoconfigure.domain.scan"); |
| 104 | + verifyNoMoreInteractions(candidateComponentProvider); |
| 105 | + assertThat(annotationTypeFilter.getValue().getAnnotationType()).isEqualTo(Entity.class); |
| 106 | + } |
| 107 | + |
| 108 | + private static class TestEntityScanner extends EntityScanner { |
| 109 | + |
| 110 | + private final ClassPathScanningCandidateComponentProvider candidateComponentProvider; |
| 111 | + |
| 112 | + TestEntityScanner(ApplicationContext context, |
| 113 | + ClassPathScanningCandidateComponentProvider candidateComponentProvider) { |
| 114 | + super(context); |
| 115 | + this.candidateComponentProvider = candidateComponentProvider; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + protected ClassPathScanningCandidateComponentProvider createClassPathScanningCandidateComponentProvider( |
| 120 | + ApplicationContext context) { |
| 121 | + return this.candidateComponentProvider; |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
82 | 126 | @Configuration(proxyBeanMethods = false)
|
83 | 127 | @EntityScan("org.springframework.boot.autoconfigure.domain.scan")
|
84 | 128 | static class ScanConfig {
|
|
0 commit comments