Skip to content

Commit

Permalink
Search implemented interfaces for @ServiceConnection fields
Browse files Browse the repository at this point in the history
Fixes gh-37671
  • Loading branch information
scottfrederick committed Oct 9, 2023
1 parent 4271e6d commit 86216fb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
Expand Up @@ -41,6 +41,7 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
*/
class ServiceConnectionContextCustomizerFactory implements ContextCustomizerFactory {

Expand All @@ -61,6 +62,9 @@ private void findSources(Class<?> clazz, List<ContainerConnectionSource<?>> sour
if (TestContextAnnotationUtils.searchEnclosingClass(clazz)) {
findSources(clazz.getEnclosingClass(), sources);
}
for (Class<?> implementedInterface : clazz.getInterfaces()) {
findSources(implementedInterface, sources);
}
}

@SuppressWarnings("unchecked")
Expand Down
Expand Up @@ -71,15 +71,39 @@ void createContextCustomizerWhenEnclosingClassHasServiceConnectionsReturnsCustom
}

@Test
void createContextCustomizerWhenClassHasNonStaticServiceConnectionFailsWithHepfulException() {
void createContextCustomizerWhenInterfaceHasServiceConnectionsReturnsCustomizer() {
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
.createContextCustomizer(ServiceConnectionsInterface.class, null);
assertThat(customizer).isNotNull();
assertThat(customizer.getSources()).hasSize(2);
}

@Test
void createContextCustomizerWhenSuperclassHasServiceConnectionsReturnsCustomizer() {
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
.createContextCustomizer(ServiceConnectionsSubclass.class, null);
assertThat(customizer).isNotNull();
assertThat(customizer.getSources()).hasSize(2);
}

@Test
void createContextCustomizerWhenImplementedInterfaceHasServiceConnectionsReturnsCustomizer() {
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
.createContextCustomizer(ServiceConnectionsImpl.class, null);
assertThat(customizer).isNotNull();
assertThat(customizer.getSources()).hasSize(2);
}

@Test
void createContextCustomizerWhenClassHasNonStaticServiceConnectionFailsWithHelpfulException() {
assertThatIllegalStateException()
.isThrownBy(() -> this.factory.createContextCustomizer(NonStaticServiceConnection.class, null))
.withMessage("@ServiceConnection field 'service' must be static");

}

@Test
void createContextCustomizerWhenClassHasAnnotationOnNonConnectionFieldFailsWithHepfulException() {
void createContextCustomizerWhenClassHasAnnotationOnNonConnectionFieldFailsWithHelpfulException() {
assertThatIllegalStateException()
.isThrownBy(() -> this.factory.createContextCustomizer(ServiceConnectionOnWrongFieldType.class, null))
.withMessage("Field 'service2' in " + ServiceConnectionOnWrongFieldType.class.getName()
Expand Down Expand Up @@ -141,6 +165,27 @@ class NestedClass {

}

interface ServiceConnectionsInterface {

@ServiceConnection
Container<?> service1 = new MockContainer();

@ServiceConnection
Container<?> service2 = new MockContainer();

default void dummy() {
}

}

static class ServiceConnectionsSubclass extends ServiceConnections {

}

static class ServiceConnectionsImpl implements ServiceConnectionsInterface {

}

static class NonStaticServiceConnection {

@ServiceConnection
Expand Down

0 comments on commit 86216fb

Please sign in to comment.