|
16 | 16 |
|
17 | 17 | package org.springframework.test.context.jdbc;
|
18 | 18 |
|
| 19 | +import java.lang.reflect.AnnotatedElement; |
19 | 20 | import java.lang.reflect.Method;
|
20 | 21 | import java.util.List;
|
21 | 22 | import java.util.Set;
|
| 23 | +import java.util.stream.Collectors; |
22 | 24 | import javax.sql.DataSource;
|
23 | 25 |
|
24 | 26 | import org.apache.commons.logging.Log;
|
25 | 27 | import org.apache.commons.logging.LogFactory;
|
26 | 28 |
|
| 29 | +import org.jetbrains.annotations.NotNull; |
27 | 30 | import org.springframework.context.ApplicationContext;
|
28 | 31 | import org.springframework.core.annotation.AnnotatedElementUtils;
|
29 | 32 | import org.springframework.core.io.ByteArrayResource;
|
@@ -126,19 +129,35 @@ public void afterTestMethod(TestContext testContext) throws Exception {
|
126 | 129 | * {@link TestContext} and {@link ExecutionPhase}.
|
127 | 130 | */
|
128 | 131 | private void executeSqlScripts(TestContext testContext, ExecutionPhase executionPhase) throws Exception {
|
129 |
| - boolean classLevel = false; |
130 |
| - |
131 |
| - Set<Sql> sqlAnnotations = AnnotatedElementUtils.getMergedRepeatableAnnotations( |
132 |
| - testContext.getTestMethod(), Sql.class, SqlGroup.class); |
133 |
| - if (sqlAnnotations.isEmpty()) { |
134 |
| - sqlAnnotations = AnnotatedElementUtils.getMergedRepeatableAnnotations( |
135 |
| - testContext.getTestClass(), Sql.class, SqlGroup.class); |
136 |
| - if (!sqlAnnotations.isEmpty()) { |
137 |
| - classLevel = true; |
138 |
| - } |
| 132 | + Set<Sql> methodLevelSqls = getScriptsFromElement(testContext.getTestMethod()); |
| 133 | + List<Sql> methodLevelOverrides = methodLevelSqls.stream() |
| 134 | + .filter(s -> s.executionPhase() == executionPhase) |
| 135 | + .filter(s -> s.mergeMode() == Sql.MergeMode.OVERRIDE) |
| 136 | + .collect(Collectors.toList()); |
| 137 | + if (methodLevelOverrides.isEmpty()) { |
| 138 | + executeScripts(getScriptsFromElement(testContext.getTestClass()), testContext, executionPhase, true); |
| 139 | + executeScripts(methodLevelSqls, testContext, executionPhase, false); |
| 140 | + } else { |
| 141 | + executeScripts(methodLevelOverrides, testContext, executionPhase, false); |
139 | 142 | }
|
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Get SQL scripts configured via {@link Sql @Sql} for the supplied |
| 147 | + * {@link AnnotatedElement}. |
| 148 | + */ |
| 149 | + private Set<Sql> getScriptsFromElement(AnnotatedElement annotatedElement) throws Exception { |
| 150 | + return AnnotatedElementUtils.getMergedRepeatableAnnotations(annotatedElement, Sql.class, SqlGroup.class); |
| 151 | + } |
140 | 152 |
|
141 |
| - for (Sql sql : sqlAnnotations) { |
| 153 | + /** |
| 154 | + * Execute given {@link Sql @Sql} scripts. |
| 155 | + * {@link AnnotatedElement}. |
| 156 | + */ |
| 157 | + private void executeScripts(Iterable<Sql> scripts, TestContext testContext, ExecutionPhase executionPhase, |
| 158 | + boolean classLevel) |
| 159 | + throws Exception { |
| 160 | + for (Sql sql : scripts) { |
142 | 161 | executeSqlScripts(sql, executionPhase, testContext, classLevel);
|
143 | 162 | }
|
144 | 163 | }
|
@@ -166,14 +185,7 @@ private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestConte
|
166 | 185 | mergedSqlConfig, executionPhase, testContext));
|
167 | 186 | }
|
168 | 187 |
|
169 |
| - final ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); |
170 |
| - populator.setSqlScriptEncoding(mergedSqlConfig.getEncoding()); |
171 |
| - populator.setSeparator(mergedSqlConfig.getSeparator()); |
172 |
| - populator.setCommentPrefix(mergedSqlConfig.getCommentPrefix()); |
173 |
| - populator.setBlockCommentStartDelimiter(mergedSqlConfig.getBlockCommentStartDelimiter()); |
174 |
| - populator.setBlockCommentEndDelimiter(mergedSqlConfig.getBlockCommentEndDelimiter()); |
175 |
| - populator.setContinueOnError(mergedSqlConfig.getErrorMode() == ErrorMode.CONTINUE_ON_ERROR); |
176 |
| - populator.setIgnoreFailedDrops(mergedSqlConfig.getErrorMode() == ErrorMode.IGNORE_FAILED_DROPS); |
| 188 | + final ResourceDatabasePopulator populator = configurePopulator(mergedSqlConfig); |
177 | 189 |
|
178 | 190 | String[] scripts = getScripts(sql, testContext, classLevel);
|
179 | 191 | scripts = TestContextResourceUtils.convertToClasspathResourcePaths(testContext.getTestClass(), scripts);
|
@@ -232,6 +244,19 @@ private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestConte
|
232 | 244 | }
|
233 | 245 | }
|
234 | 246 |
|
| 247 | + @NotNull |
| 248 | + private ResourceDatabasePopulator configurePopulator(MergedSqlConfig mergedSqlConfig) { |
| 249 | + final ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); |
| 250 | + populator.setSqlScriptEncoding(mergedSqlConfig.getEncoding()); |
| 251 | + populator.setSeparator(mergedSqlConfig.getSeparator()); |
| 252 | + populator.setCommentPrefix(mergedSqlConfig.getCommentPrefix()); |
| 253 | + populator.setBlockCommentStartDelimiter(mergedSqlConfig.getBlockCommentStartDelimiter()); |
| 254 | + populator.setBlockCommentEndDelimiter(mergedSqlConfig.getBlockCommentEndDelimiter()); |
| 255 | + populator.setContinueOnError(mergedSqlConfig.getErrorMode() == ErrorMode.CONTINUE_ON_ERROR); |
| 256 | + populator.setIgnoreFailedDrops(mergedSqlConfig.getErrorMode() == ErrorMode.IGNORE_FAILED_DROPS); |
| 257 | + return populator; |
| 258 | + } |
| 259 | + |
235 | 260 | @Nullable
|
236 | 261 | private DataSource getDataSourceFromTransactionManager(PlatformTransactionManager transactionManager) {
|
237 | 262 | try {
|
|
0 commit comments