|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2020 the original author or authors. |
| 2 | + * Copyright 2012-2021 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.
|
|
26 | 26 |
|
27 | 27 | import com.rabbitmq.client.Address;
|
28 | 28 | import com.rabbitmq.client.Connection;
|
| 29 | +import com.rabbitmq.client.JDKSaslConfig; |
29 | 30 | import com.rabbitmq.client.SslContextFactory;
|
30 | 31 | import com.rabbitmq.client.TrustEverythingTrustManager;
|
31 | 32 | import com.rabbitmq.client.impl.CredentialsProvider;
|
32 | 33 | import com.rabbitmq.client.impl.CredentialsRefreshService;
|
33 | 34 | import com.rabbitmq.client.impl.DefaultCredentialsProvider;
|
34 | 35 | import org.aopalliance.aop.Advice;
|
35 | 36 | import org.junit.jupiter.api.Test;
|
| 37 | +import org.mockito.InOrder; |
36 | 38 |
|
37 | 39 | import org.springframework.amqp.core.AcknowledgeMode;
|
38 | 40 | import org.springframework.amqp.core.AmqpAdmin;
|
|
60 | 62 | import org.springframework.context.annotation.Bean;
|
61 | 63 | import org.springframework.context.annotation.Configuration;
|
62 | 64 | import org.springframework.context.annotation.Primary;
|
| 65 | +import org.springframework.core.Ordered; |
| 66 | +import org.springframework.core.annotation.Order; |
63 | 67 | import org.springframework.retry.RetryPolicy;
|
64 | 68 | import org.springframework.retry.backoff.BackOffPolicy;
|
65 | 69 | import org.springframework.retry.backoff.ExponentialBackOffPolicy;
|
|
75 | 79 | import static org.mockito.ArgumentMatchers.eq;
|
76 | 80 | import static org.mockito.ArgumentMatchers.isNull;
|
77 | 81 | import static org.mockito.BDDMockito.given;
|
| 82 | +import static org.mockito.Mockito.inOrder; |
78 | 83 | import static org.mockito.Mockito.mock;
|
79 | 84 | import static org.mockito.Mockito.verify;
|
80 | 85 |
|
@@ -822,6 +827,29 @@ void whenMultipleCredentialsRefreshServiceAreAvailableThenConnectionFactoryHasNo
|
822 | 827 | .isNull());
|
823 | 828 | }
|
824 | 829 |
|
| 830 | + @Test |
| 831 | + void whenAConnectionFactoryCustomizerIsDefinedThenItCustomizesTheConnectionFactory() { |
| 832 | + this.contextRunner.withUserConfiguration(SaslConfigCustomizerConfiguration.class) |
| 833 | + .run((context) -> assertThat(getTargetConnectionFactory(context).getSaslConfig()) |
| 834 | + .isInstanceOf(JDKSaslConfig.class)); |
| 835 | + } |
| 836 | + |
| 837 | + @Test |
| 838 | + void whenMultipleConnectionFactoryCustomizersAreDefinedThenTheyAreCalledInOrder() { |
| 839 | + this.contextRunner.withUserConfiguration(MultipleConnectionFactoryCustomizersConfiguration.class) |
| 840 | + .run((context) -> { |
| 841 | + ConnectionFactoryCustomizer firstCustomizer = context.getBean("firstCustomizer", |
| 842 | + ConnectionFactoryCustomizer.class); |
| 843 | + ConnectionFactoryCustomizer secondCustomizer = context.getBean("secondCustomizer", |
| 844 | + ConnectionFactoryCustomizer.class); |
| 845 | + InOrder inOrder = inOrder(firstCustomizer, secondCustomizer); |
| 846 | + com.rabbitmq.client.ConnectionFactory targetConnectionFactory = getTargetConnectionFactory(context); |
| 847 | + inOrder.verify(firstCustomizer).customize(targetConnectionFactory); |
| 848 | + inOrder.verify(secondCustomizer).customize(targetConnectionFactory); |
| 849 | + inOrder.verifyNoMoreInteractions(); |
| 850 | + }); |
| 851 | + } |
| 852 | + |
825 | 853 | private TrustManager getTrustManager(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory) {
|
826 | 854 | SslContextFactory sslContextFactory = (SslContextFactory) ReflectionTestUtils.getField(rabbitConnectionFactory,
|
827 | 855 | "sslContextFactory");
|
@@ -1071,4 +1099,31 @@ CredentialsRefreshService credentialsRefreshService2() {
|
1071 | 1099 |
|
1072 | 1100 | }
|
1073 | 1101 |
|
| 1102 | + @Configuration(proxyBeanMethods = false) |
| 1103 | + static class SaslConfigCustomizerConfiguration { |
| 1104 | + |
| 1105 | + @Bean |
| 1106 | + ConnectionFactoryCustomizer connectionFactoryCustomizer() { |
| 1107 | + return (connectionFactory) -> connectionFactory.setSaslConfig(new JDKSaslConfig(connectionFactory)); |
| 1108 | + } |
| 1109 | + |
| 1110 | + } |
| 1111 | + |
| 1112 | + @Configuration(proxyBeanMethods = false) |
| 1113 | + static class MultipleConnectionFactoryCustomizersConfiguration { |
| 1114 | + |
| 1115 | + @Bean |
| 1116 | + @Order(Ordered.LOWEST_PRECEDENCE) |
| 1117 | + ConnectionFactoryCustomizer secondCustomizer() { |
| 1118 | + return mock(ConnectionFactoryCustomizer.class); |
| 1119 | + } |
| 1120 | + |
| 1121 | + @Bean |
| 1122 | + @Order(0) |
| 1123 | + ConnectionFactoryCustomizer firstCustomizer() { |
| 1124 | + return mock(ConnectionFactoryCustomizer.class); |
| 1125 | + } |
| 1126 | + |
| 1127 | + } |
| 1128 | + |
1074 | 1129 | }
|
0 commit comments