11/*
2- * Copyright 2002-2016 the original author or authors.
2+ * Copyright 2002-2018 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1717package org .springframework .test .context .transaction ;
1818
1919import java .util .Map ;
20-
2120import javax .sql .DataSource ;
2221
2322import org .apache .commons .logging .Log ;
4039/**
4140 * Utility methods for working with transactions and data access related beans
4241 * within the <em>Spring TestContext Framework</em>.
42+ *
4343 * <p>Mainly for internal use within the framework.
4444 *
4545 * @author Sam Brannen
4848 */
4949public abstract class TestContextTransactionUtils {
5050
51- private static final Log logger = LogFactory .getLog (TestContextTransactionUtils .class );
52-
5351 /**
5452 * Default bean name for a {@link DataSource}: {@code "dataSource"}.
5553 */
@@ -62,9 +60,8 @@ public abstract class TestContextTransactionUtils {
6260 public static final String DEFAULT_TRANSACTION_MANAGER_NAME = "transactionManager" ;
6361
6462
65- private TestContextTransactionUtils () {
66- /* prevent instantiation */
67- }
63+ private static final Log logger = LogFactory .getLog (TestContextTransactionUtils .class );
64+
6865
6966 /**
7067 * Retrieve the {@link DataSource} to use for the supplied {@linkplain TestContext
@@ -82,8 +79,8 @@ private TestContextTransactionUtils() {
8279 * {@linkplain #DEFAULT_DATA_SOURCE_NAME default data source name}.
8380 * @param testContext the test context for which the {@code DataSource}
8481 * should be retrieved; never {@code null}
85- * @param name the name of the {@code DataSource} to retrieve; may be {@code null}
86- * or <em>empty</em>
82+ * @param name the name of the {@code DataSource} to retrieve
83+ * (may be {@code null} or <em>empty</em>)
8784 * @return the {@code DataSource} to use, or {@code null} if not found
8885 * @throws BeansException if an error occurs while retrieving an explicitly
8986 * named {@code DataSource}
@@ -94,24 +91,24 @@ public static DataSource retrieveDataSource(TestContext testContext, @Nullable S
9491 BeanFactory bf = testContext .getApplicationContext ().getAutowireCapableBeanFactory ();
9592
9693 try {
97- // look up by type and explicit name
94+ // Look up by type and explicit name
9895 if (StringUtils .hasText (name )) {
9996 return bf .getBean (name , DataSource .class );
10097 }
10198 }
10299 catch (BeansException ex ) {
103- logger .error (
104- String . format ( "Failed to retrieve DataSource named '%s' for test context %s" , name , testContext ), ex );
100+ logger .error (String . format ( "Failed to retrieve DataSource named '%s' for test context %s" ,
101+ name , testContext ), ex );
105102 throw ex ;
106103 }
107104
108105 try {
109106 if (bf instanceof ListableBeanFactory ) {
110107 ListableBeanFactory lbf = (ListableBeanFactory ) bf ;
111108
112- // look up single bean by type
113- Map <String , DataSource > dataSources = BeanFactoryUtils . beansOfTypeIncludingAncestors ( lbf ,
114- DataSource .class );
109+ // Look up single bean by type
110+ Map <String , DataSource > dataSources =
111+ BeanFactoryUtils . beansOfTypeIncludingAncestors ( lbf , DataSource .class );
115112 if (dataSources .size () == 1 ) {
116113 return dataSources .values ().iterator ().next ();
117114 }
@@ -153,8 +150,8 @@ public static DataSource retrieveDataSource(TestContext testContext, @Nullable S
153150 * name}.
154151 * @param testContext the test context for which the transaction manager
155152 * should be retrieved; never {@code null}
156- * @param name the name of the transaction manager to retrieve; may be
157- * {@code null} or <em>empty</em>
153+ * @param name the name of the transaction manager to retrieve
154+ * (may be {@code null} or <em>empty</em>)
158155 * @return the transaction manager to use, or {@code null} if not found
159156 * @throws BeansException if an error occurs while retrieving an explicitly
160157 * named transaction manager
@@ -167,39 +164,39 @@ public static PlatformTransactionManager retrieveTransactionManager(TestContext
167164 BeanFactory bf = testContext .getApplicationContext ().getAutowireCapableBeanFactory ();
168165
169166 try {
170- // look up by type and explicit name
167+ // Look up by type and explicit name
171168 if (StringUtils .hasText (name )) {
172169 return bf .getBean (name , PlatformTransactionManager .class );
173170 }
174171 }
175172 catch (BeansException ex ) {
176- logger .error (String .format ("Failed to retrieve transaction manager named '%s' for test context %s" , name ,
177- testContext ), ex );
173+ logger .error (String .format ("Failed to retrieve transaction manager named '%s' for test context %s" ,
174+ name , testContext ), ex );
178175 throw ex ;
179176 }
180177
181178 try {
182179 if (bf instanceof ListableBeanFactory ) {
183180 ListableBeanFactory lbf = (ListableBeanFactory ) bf ;
184181
185- // look up single bean by type
186- Map <String , PlatformTransactionManager > txMgrs = BeanFactoryUtils . beansOfTypeIncludingAncestors ( lbf ,
187- PlatformTransactionManager .class );
182+ // Look up single bean by type
183+ Map <String , PlatformTransactionManager > txMgrs =
184+ BeanFactoryUtils . beansOfTypeIncludingAncestors ( lbf , PlatformTransactionManager .class );
188185 if (txMgrs .size () == 1 ) {
189186 return txMgrs .values ().iterator ().next ();
190187 }
191188
192189 try {
193- // look up single bean by type, with support for 'primary' beans
190+ // Look up single bean by type, with support for 'primary' beans
194191 return bf .getBean (PlatformTransactionManager .class );
195192 }
196193 catch (BeansException ex ) {
197194 logBeansException (testContext , ex , PlatformTransactionManager .class );
198195 }
199196
200- // look up single TransactionManagementConfigurer
201- Map <String , TransactionManagementConfigurer > configurers = BeanFactoryUtils . beansOfTypeIncludingAncestors (
202- lbf , TransactionManagementConfigurer .class );
197+ // Look up single TransactionManagementConfigurer
198+ Map <String , TransactionManagementConfigurer > configurers =
199+ BeanFactoryUtils . beansOfTypeIncludingAncestors ( lbf , TransactionManagementConfigurer .class );
203200 Assert .state (configurers .size () <= 1 ,
204201 "Only one TransactionManagementConfigurer may exist in the ApplicationContext" );
205202 if (configurers .size () == 1 ) {
@@ -227,13 +224,13 @@ private static void logBeansException(TestContext testContext, BeansException ex
227224 * Create a delegating {@link TransactionAttribute} for the supplied target
228225 * {@link TransactionAttribute} and {@link TestContext}, using the names of
229226 * the test class and test method to build the name of the transaction.
230- *
231- * @param testContext the {@code TestContext} upon which to base the name; never {@code null}
232- * @param targetAttribute the {@code TransactionAttribute} to delegate to; never {@code null}
227+ * @param testContext the {@code TestContext} upon which to base the name
228+ * @param targetAttribute the {@code TransactionAttribute} to delegate to
233229 * @return the delegating {@code TransactionAttribute}
234230 */
235- public static TransactionAttribute createDelegatingTransactionAttribute (TestContext testContext ,
236- TransactionAttribute targetAttribute ) {
231+ public static TransactionAttribute createDelegatingTransactionAttribute (
232+ TestContext testContext , TransactionAttribute targetAttribute ) {
233+
237234 Assert .notNull (testContext , "TestContext must not be null" );
238235 Assert .notNull (targetAttribute , "Target TransactionAttribute must not be null" );
239236 return new TestContextTransactionAttribute (targetAttribute , testContext );
0 commit comments