Skip to content

Commit

Permalink
Polish contribution
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jul 21, 2019
1 parent d77b715 commit ab88762
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,7 +32,7 @@
* database during integration tests.
*
* <p>Method-level declarations override class-level declarations by default.
* This behaviour can be adjusted via {@link MergeMode}
* This behavior can be adjusted by setting the {@link #mergeMode}.
*
* <p>Script execution is performed by the {@link SqlScriptsTestExecutionListener},
* which is enabled by default.
Expand All @@ -54,6 +54,7 @@
* <em>composed annotations</em> with attribute overrides.
*
* @author Sam Brannen
* @author Dmitry Semukhin
* @since 4.1
* @see SqlConfig
* @see SqlGroup
Expand Down Expand Up @@ -138,6 +139,16 @@
*/
ExecutionPhase executionPhase() default ExecutionPhase.BEFORE_TEST_METHOD;

/**
* Indicates whether this {@code @Sql} annotation should be merged with
* class-level {@code @Sql} annotations or override them.
* <p>The merge mode is ignored if declared in a class-level {@code @Sql}
* annotation.
* <p>Defaults to {@link MergeMode#OVERRIDE OVERRIDE} for backwards compatibility.
* @since 5.2
*/
MergeMode mergeMode() default MergeMode.OVERRIDE;

/**
* Local configuration for the SQL scripts and statements declared within
* this {@code @Sql} annotation.
Expand All @@ -147,13 +158,6 @@
*/
SqlConfig config() default @SqlConfig;

/**
* Indicates whether this annotation should be merged with upper-level annotations
* or override them.
* <p>Defaults to {@link MergeMode#OVERRIDE}.
*/
MergeMode mergeMode() default MergeMode.OVERRIDE;


/**
* Enumeration of <em>phases</em> that dictate when SQL scripts are executed.
Expand All @@ -174,22 +178,23 @@ enum ExecutionPhase {
}

/**
* Enumeration of <em>modes</em> that dictate whether or not
* declared SQL {@link #scripts} and {@link #statements} are merged
* with the upper-level annotations.
* Enumeration of <em>modes</em> that dictate whether method-level {@code @Sql}
* declarations are merged with class-level {@code @Sql} declarations.
* @since 5.2
*/
enum MergeMode {

/**
* Indicates that locally declared SQL {@link #scripts} and {@link #statements}
* should override the upper-level (e.g. Class-level) annotations.
* Indicates that method-level {@code @Sql} declarations should override
* class-level {@code @Sql} declarations.
*/
OVERRIDE,

/**
* Indicates that locally declared SQL {@link #scripts} and {@link #statements}
* should be merged the upper-level (e.g. Class-level) annotations.
* Indicates that method-level {@code @Sql} declarations should be merged
* with class-level {@code @Sql} declarations.
*/
MERGE
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,13 +26,13 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.jetbrains.annotations.NotNull;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
Expand Down Expand Up @@ -84,6 +84,7 @@
* locate these beans.
*
* @author Sam Brannen
* @author Dmitry Semukhin
* @since 4.1
* @see Sql
* @see SqlConfig
Expand Down Expand Up @@ -111,7 +112,7 @@ public final int getOrder() {
* {@link TestContext} <em>before</em> the current test method.
*/
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
public void beforeTestMethod(TestContext testContext) {
executeSqlScripts(testContext, ExecutionPhase.BEFORE_TEST_METHOD);
}

Expand All @@ -120,43 +121,42 @@ public void beforeTestMethod(TestContext testContext) throws Exception {
* {@link TestContext} <em>after</em> the current test method.
*/
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
public void afterTestMethod(TestContext testContext) {
executeSqlScripts(testContext, ExecutionPhase.AFTER_TEST_METHOD);
}

/**
* Execute SQL scripts configured via {@link Sql @Sql} for the supplied
* {@link TestContext} and {@link ExecutionPhase}.
*/
private void executeSqlScripts(TestContext testContext, ExecutionPhase executionPhase) throws Exception {
Set<Sql> methodLevelSqls = getScriptsFromElement(testContext.getTestMethod());
private void executeSqlScripts(TestContext testContext, ExecutionPhase executionPhase) {
Set<Sql> methodLevelSqls = getSqlAnnotationsFor(testContext.getTestMethod());
List<Sql> methodLevelOverrides = methodLevelSqls.stream()
.filter(s -> s.executionPhase() == executionPhase)
.filter(s -> s.mergeMode() == Sql.MergeMode.OVERRIDE)
.collect(Collectors.toList());
.filter(s -> s.executionPhase() == executionPhase)
.filter(s -> s.mergeMode() == Sql.MergeMode.OVERRIDE)
.collect(Collectors.toList());
if (methodLevelOverrides.isEmpty()) {
executeScripts(getScriptsFromElement(testContext.getTestClass()), testContext, executionPhase, true);
executeScripts(getSqlAnnotationsFor(testContext.getTestClass()), testContext, executionPhase, true);
executeScripts(methodLevelSqls, testContext, executionPhase, false);
} else {
executeScripts(methodLevelOverrides, testContext, executionPhase, false);
}
}

/**
* Get SQL scripts configured via {@link Sql @Sql} for the supplied
* Get the {@link Sql @Sql} annotations declared on the supplied
* {@link AnnotatedElement}.
*/
private Set<Sql> getScriptsFromElement(AnnotatedElement annotatedElement) throws Exception {
private Set<Sql> getSqlAnnotationsFor(AnnotatedElement annotatedElement) {
return AnnotatedElementUtils.getMergedRepeatableAnnotations(annotatedElement, Sql.class, SqlGroup.class);
}

/**
* Execute given {@link Sql @Sql} scripts.
* {@link AnnotatedElement}.
* Execute SQL scripts for the supplied {@link Sql @Sql} annotations.
*/
private void executeScripts(Iterable<Sql> scripts, TestContext testContext, ExecutionPhase executionPhase,
boolean classLevel)
throws Exception {
private void executeScripts(
Iterable<Sql> scripts, TestContext testContext, ExecutionPhase executionPhase, boolean classLevel) {

for (Sql sql : scripts) {
executeSqlScripts(sql, executionPhase, testContext, classLevel);
}
Expand All @@ -172,8 +172,8 @@ private void executeScripts(Iterable<Sql> scripts, TestContext testContext, Exec
* @param testContext the current {@code TestContext}
* @param classLevel {@code true} if {@link Sql @Sql} was declared at the class level
*/
private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestContext testContext, boolean classLevel)
throws Exception {
private void executeSqlScripts(
Sql sql, ExecutionPhase executionPhase, TestContext testContext, boolean classLevel) {

if (executionPhase != sql.executionPhase()) {
return;
Expand All @@ -185,8 +185,6 @@ private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestConte
mergedSqlConfig, executionPhase, testContext));
}

final ResourceDatabasePopulator populator = configurePopulator(mergedSqlConfig);

String[] scripts = getScripts(sql, testContext, classLevel);
scripts = TestContextResourceUtils.convertToClasspathResourcePaths(testContext.getTestClass(), scripts);
List<Resource> scriptResources = TestContextResourceUtils.convertToResourceList(
Expand All @@ -197,6 +195,8 @@ private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestConte
scriptResources.add(new ByteArrayResource(stmt.getBytes(), "from inlined SQL statement: " + stmt));
}
}

ResourceDatabasePopulator populator = configurePopulator(mergedSqlConfig);
populator.setScripts(scriptResources.toArray(new Resource[0]));
if (logger.isDebugEnabled()) {
logger.debug("Executing SQL scripts: " + ObjectUtils.nullSafeToString(scriptResources));
Expand Down Expand Up @@ -237,16 +237,13 @@ private void executeSqlScripts(Sql sql, ExecutionPhase executionPhase, TestConte
TransactionDefinition.PROPAGATION_REQUIRED);
TransactionAttribute txAttr = TestContextTransactionUtils.createDelegatingTransactionAttribute(
testContext, new DefaultTransactionAttribute(propagation));
new TransactionTemplate(txMgr, txAttr).execute(status -> {
populator.execute(finalDataSource);
return null;
});
new TransactionTemplate(txMgr, txAttr).execute(() -> populator.execute(finalDataSource));
}
}

@NotNull
@NonNull
private ResourceDatabasePopulator configurePopulator(MergedSqlConfig mergedSqlConfig) {
final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.setSqlScriptEncoding(mergedSqlConfig.getEncoding());
populator.setSeparator(mergedSqlConfig.getSeparator());
populator.setCommentPrefix(mergedSqlConfig.getCommentPrefix());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.jdbc.JdbcTestUtils;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -59,10 +58,6 @@ public void test02_methodLevelScripts() {
assertNumUsers(2);
}

protected int countRowsInTable(String tableName) {
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
}

protected void assertNumUsers(int expected) {
assertThat(countRowsInTable("user")).as("Number of rows in the 'user' table.").isEqualTo(expected);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.context.jdbc;

import org.junit.Test;

import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

import static org.junit.Assert.assertEquals;
import static org.springframework.test.context.jdbc.Sql.MergeMode.MERGE;

/**
* Transactional integration tests for {@link Sql @Sql} that verify proper
* merging support for class-level and method-level declarations.
*
* @author Dmitry Semukhin
* @author Sam Brannen
* @since 5.2
*/
@ContextConfiguration(classes = EmptyDatabaseConfig.class)
@Sql({ "schema.sql", "data-add-catbert.sql" })
@DirtiesContext
public class SqlMethodMergeTests extends AbstractTransactionalJUnit4SpringContextTests {

@Test
@Sql(scripts = "data-add-dogbert.sql", mergeMode = MERGE)
public void testMerge() {
assertNumUsers(2);
}

protected void assertNumUsers(int expected) {
assertEquals("Number of rows in the 'user' table.", expected, countRowsInTable("user"));
}

}

This file was deleted.

Loading

0 comments on commit ab88762

Please sign in to comment.