|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.context.transaction.reactive; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import io.r2dbc.spi.Connection; |
| 22 | +import io.r2dbc.spi.ConnectionFactory; |
| 23 | +import org.apache.commons.logging.Log; |
| 24 | +import org.apache.commons.logging.LogFactory; |
| 25 | +import org.jspecify.annotations.Nullable; |
| 26 | + |
| 27 | +import org.springframework.beans.BeansException; |
| 28 | +import org.springframework.beans.factory.BeanFactory; |
| 29 | +import org.springframework.beans.factory.BeanFactoryUtils; |
| 30 | +import org.springframework.beans.factory.ListableBeanFactory; |
| 31 | +import org.springframework.test.context.TestContext; |
| 32 | +import org.springframework.transaction.PlatformTransactionManager; |
| 33 | +import org.springframework.util.Assert; |
| 34 | + |
| 35 | +/** |
| 36 | + * Utility methods for working with transactions and data access related beans |
| 37 | + * within the <em>Spring TestContext Framework</em>. |
| 38 | + * |
| 39 | + * <p>Mainly for internal use within the framework. |
| 40 | + * |
| 41 | + * @author jonghoon park |
| 42 | + * @since 7.0 |
| 43 | + */ |
| 44 | +public abstract class TestContextReactiveTransactionUtils { |
| 45 | + |
| 46 | + /** |
| 47 | + * Default bean name for a {@link ConnectionFactory}: |
| 48 | + * {@code "connectionFactory"}. |
| 49 | + */ |
| 50 | + public static final String DEFAULT_CONNECTION_FACTORY_NAME = "connectionFactory"; |
| 51 | + |
| 52 | + |
| 53 | + private static final Log logger = LogFactory.getLog(TestContextReactiveTransactionUtils.class); |
| 54 | + |
| 55 | + /** |
| 56 | + * Retrieve the {@link ConnectionFactory} to use for the supplied {@linkplain TestContext |
| 57 | + * test context}. |
| 58 | + * <p>The following algorithm is used to retrieve the {@code ConnectionFactory} from |
| 59 | + * the {@link org.springframework.context.ApplicationContext ApplicationContext} |
| 60 | + * of the supplied test context: |
| 61 | + * <ol> |
| 62 | + * <li>Attempt to look up the single {@code ConnectionFactory} by type. |
| 63 | + * <li>Attempt to look up the <em>primary</em> {@code ConnectionFactory} by type. |
| 64 | + * <li>Attempt to look up the {@code ConnectionFactory} by type and the |
| 65 | + * {@linkplain #DEFAULT_CONNECTION_FACTORY_NAME default data source name}. |
| 66 | + * </ol> |
| 67 | + * @param testContext the test context for which the {@code ConnectionFactory} |
| 68 | + * should be retrieved; never {@code null} |
| 69 | + * @return the {@code DataSource} to use, or {@code null} if not found |
| 70 | + */ |
| 71 | + @Nullable |
| 72 | + public static ConnectionFactory retrieveConnectionFactory(TestContext testContext) { |
| 73 | + Assert.notNull(testContext, "TestContext must not be null"); |
| 74 | + BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory(); |
| 75 | + |
| 76 | + try { |
| 77 | + if (bf instanceof ListableBeanFactory lbf) { |
| 78 | + // Look up single bean by type |
| 79 | + Map<String, ConnectionFactory> ConnectionFactories = |
| 80 | + BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ConnectionFactory.class); |
| 81 | + if (ConnectionFactories.size() == 1) { |
| 82 | + return ConnectionFactories.values().iterator().next(); |
| 83 | + } |
| 84 | + |
| 85 | + try { |
| 86 | + // look up single bean by type, with support for 'primary' beans |
| 87 | + return bf.getBean(ConnectionFactory.class); |
| 88 | + } |
| 89 | + catch (BeansException ex) { |
| 90 | + logBeansException(testContext, ex, PlatformTransactionManager.class); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // look up by type and default name |
| 95 | + return bf.getBean(DEFAULT_CONNECTION_FACTORY_NAME, ConnectionFactory.class); |
| 96 | + } |
| 97 | + catch (BeansException ex) { |
| 98 | + logBeansException(testContext, ex, Connection.class); |
| 99 | + return null; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private static void logBeansException(TestContext testContext, BeansException ex, Class<?> beanType) { |
| 104 | + if (logger.isTraceEnabled()) { |
| 105 | + logger.trace("Caught exception while retrieving %s for test context %s" |
| 106 | + .formatted(beanType.getSimpleName(), testContext), ex); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments