From cafb99a33d1b644de9012142ab69c7ecd23215a4 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 23 Feb 2016 14:31:09 +0100 Subject: [PATCH] Polishing (cherry picked from commit 028a690) --- .../springframework/core/OrderComparator.java | 4 +- .../test/context/jdbc/Sql.java | 42 +-- .../test/context/jdbc/SqlConfig.java | 251 +++++++++--------- .../test/context/jdbc/SqlGroup.java | 4 +- 4 files changed, 151 insertions(+), 150 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/OrderComparator.java b/spring-core/src/main/java/org/springframework/core/OrderComparator.java index 0a121dd2278..3d976d94af1 100644 --- a/spring-core/src/main/java/org/springframework/core/OrderComparator.java +++ b/spring-core/src/main/java/org/springframework/core/OrderComparator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -203,7 +203,7 @@ public class OrderComparator implements Comparator { * Strategy interface to provide an order source for a given object. * @since 4.1 */ - public static interface OrderSourceProvider { + public interface OrderSourceProvider { /** * Return an order source for the specified object, i.e. an object that diff --git a/spring-test/src/main/java/org/springframework/test/context/jdbc/Sql.java b/spring-test/src/main/java/org/springframework/test/context/jdbc/Sql.java index b5659127162..e413b7b3e0c 100644 --- a/spring-test/src/main/java/org/springframework/test/context/jdbc/Sql.java +++ b/spring-test/src/main/java/org/springframework/test/context/jdbc/Sql.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -68,29 +68,10 @@ import static java.lang.annotation.RetentionPolicy.*; @Documented @Inherited @Retention(RUNTIME) -@Target({ TYPE, METHOD }) +@Target({TYPE, METHOD}) @Repeatable(SqlGroup.class) public @interface Sql { - /** - * Enumeration of phases that dictate when SQL scripts are executed. - */ - static enum ExecutionPhase { - - /** - * The configured SQL scripts and statements will be executed - * before the corresponding test method. - */ - BEFORE_TEST_METHOD, - - /** - * The configured SQL scripts and statements will be executed - * after the corresponding test method. - */ - AFTER_TEST_METHOD - } - - /** * Alias for {@link #scripts}. *

This attribute may not be used in conjunction with @@ -173,4 +154,23 @@ public @interface Sql { */ SqlConfig config() default @SqlConfig(); + + /** + * Enumeration of phases that dictate when SQL scripts are executed. + */ + enum ExecutionPhase { + + /** + * The configured SQL scripts and statements will be executed + * before the corresponding test method. + */ + BEFORE_TEST_METHOD, + + /** + * The configured SQL scripts and statements will be executed + * after the corresponding test method. + */ + AFTER_TEST_METHOD + } + } diff --git a/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlConfig.java b/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlConfig.java index 6a94a827549..9c0d55a6a0a 100644 --- a/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlConfig.java +++ b/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 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. @@ -65,130 +65,6 @@ import static java.lang.annotation.RetentionPolicy.*; @Target(TYPE) public @interface SqlConfig { - /** - * Enumeration of modes that dictate whether SQL scripts should be - * executed within a transaction and what the transaction propagation behavior - * should be. - */ - static enum TransactionMode { - - /** - * Indicates that the default transaction mode should be used. - *

The meaning of default depends on the context in which - * {@code @SqlConfig} is declared: - *

- */ - DEFAULT, - - /** - * Indicates that the transaction mode to use when executing SQL - * scripts should be inferred using the rules listed below. - * In the context of these rules, the term "available" - * means that the bean for the data source or transaction manager - * is either explicitly specified via a corresponding annotation - * attribute in {@code @SqlConfig} or discoverable via conventions. See - * {@link org.springframework.test.context.transaction.TestContextTransactionUtils TestContextTransactionUtils} - * for details on the conventions used to discover such beans in - * the {@code ApplicationContext}. - * - *

Inference Rules

- *
    - *
  1. If neither a transaction manager nor a data source is - * available, an exception will be thrown. - *
  2. If a transaction manager is not available but a data source - * is available, SQL scripts will be executed directly against the - * data source without a transaction. - *
  3. If a transaction manager is available: - *
      - *
    • If a data source is not available, an attempt will be made - * to retrieve it from the transaction manager by using reflection - * to invoke a public method named {@code getDataSource()} on the - * transaction manager. If the attempt fails, an exception will be - * thrown. - *
    • Using the resolved transaction manager and data source, SQL - * scripts will be executed within an existing transaction if - * present; otherwise, scripts will be executed in a new transaction - * that will be immediately committed. An existing - * transaction will typically be managed by the - * {@link org.springframework.test.context.transaction.TransactionalTestExecutionListener TransactionalTestExecutionListener}. - *
    - *
- * @see #ISOLATED - * @see org.springframework.test.context.transaction.TestContextTransactionUtils#retrieveDataSource - * @see org.springframework.test.context.transaction.TestContextTransactionUtils#retrieveTransactionManager - */ - INFERRED, - - /** - * Indicates that SQL scripts should always be executed in a new, - * isolated transaction that will be immediately committed. - *

In contrast to {@link #INFERRED}, this mode requires the - * presence of a transaction manager and a data - * source. - */ - ISOLATED - } - - /** - * Enumeration of modes that dictate how errors are handled while - * executing SQL statements. - */ - static enum ErrorMode { - - /** - * Indicates that the default error mode should be used. - *

The meaning of default depends on the context in which - * {@code @SqlConfig} is declared: - *

- */ - DEFAULT, - - /** - * Indicates that script execution will fail if an error is encountered. - * In other words, no errors should be ignored. - *

This is effectively the default error mode so that if a script - * is accidentally executed, it will fail fast if any SQL statement in - * the script results in an error. - * @see #CONTINUE_ON_ERROR - */ - FAIL_ON_ERROR, - - /** - * Indicates that all errors in SQL scripts should be logged but not - * propagated as exceptions. - *

{@code CONTINUE_ON_ERROR} is the logical opposite of - * {@code FAIL_ON_ERROR} and a superset of {@code IGNORE_FAILED_DROPS}. - * @see #FAIL_ON_ERROR - * @see #IGNORE_FAILED_DROPS - */ - CONTINUE_ON_ERROR, - - /** - * Indicates that failed SQL {@code DROP} statements can be ignored. - *

This is useful for a non-embedded database whose SQL dialect does - * not support an {@code IF EXISTS} clause in a {@code DROP} statement. - * @see #CONTINUE_ON_ERROR - */ - IGNORE_FAILED_DROPS - } - - /** * The bean name of the {@link javax.sql.DataSource} against which the * scripts should be executed. @@ -298,4 +174,129 @@ public @interface SqlConfig { */ ErrorMode errorMode() default ErrorMode.DEFAULT; + + /** + * Enumeration of modes that dictate whether SQL scripts should be + * executed within a transaction and what the transaction propagation behavior + * should be. + */ + enum TransactionMode { + + /** + * Indicates that the default transaction mode should be used. + *

The meaning of default depends on the context in which + * {@code @SqlConfig} is declared: + *

+ */ + DEFAULT, + + /** + * Indicates that the transaction mode to use when executing SQL + * scripts should be inferred using the rules listed below. + * In the context of these rules, the term "available" + * means that the bean for the data source or transaction manager + * is either explicitly specified via a corresponding annotation + * attribute in {@code @SqlConfig} or discoverable via conventions. See + * {@link org.springframework.test.context.transaction.TestContextTransactionUtils TestContextTransactionUtils} + * for details on the conventions used to discover such beans in + * the {@code ApplicationContext}. + * + *

Inference Rules

+ *
    + *
  1. If neither a transaction manager nor a data source is + * available, an exception will be thrown. + *
  2. If a transaction manager is not available but a data source + * is available, SQL scripts will be executed directly against the + * data source without a transaction. + *
  3. If a transaction manager is available: + *
      + *
    • If a data source is not available, an attempt will be made + * to retrieve it from the transaction manager by using reflection + * to invoke a public method named {@code getDataSource()} on the + * transaction manager. If the attempt fails, an exception will be + * thrown. + *
    • Using the resolved transaction manager and data source, SQL + * scripts will be executed within an existing transaction if + * present; otherwise, scripts will be executed in a new transaction + * that will be immediately committed. An existing + * transaction will typically be managed by the + * {@link org.springframework.test.context.transaction.TransactionalTestExecutionListener TransactionalTestExecutionListener}. + *
    + *
+ * @see #ISOLATED + * @see org.springframework.test.context.transaction.TestContextTransactionUtils#retrieveDataSource + * @see org.springframework.test.context.transaction.TestContextTransactionUtils#retrieveTransactionManager + */ + INFERRED, + + /** + * Indicates that SQL scripts should always be executed in a new, + * isolated transaction that will be immediately committed. + *

In contrast to {@link #INFERRED}, this mode requires the + * presence of a transaction manager and a data + * source. + */ + ISOLATED + } + + + /** + * Enumeration of modes that dictate how errors are handled while + * executing SQL statements. + */ + enum ErrorMode { + + /** + * Indicates that the default error mode should be used. + *

The meaning of default depends on the context in which + * {@code @SqlConfig} is declared: + *

+ */ + DEFAULT, + + /** + * Indicates that script execution will fail if an error is encountered. + * In other words, no errors should be ignored. + *

This is effectively the default error mode so that if a script + * is accidentally executed, it will fail fast if any SQL statement in + * the script results in an error. + * @see #CONTINUE_ON_ERROR + */ + FAIL_ON_ERROR, + + /** + * Indicates that all errors in SQL scripts should be logged but not + * propagated as exceptions. + *

{@code CONTINUE_ON_ERROR} is the logical opposite of + * {@code FAIL_ON_ERROR} and a superset of {@code IGNORE_FAILED_DROPS}. + * @see #FAIL_ON_ERROR + * @see #IGNORE_FAILED_DROPS + */ + CONTINUE_ON_ERROR, + + /** + * Indicates that failed SQL {@code DROP} statements can be ignored. + *

This is useful for a non-embedded database whose SQL dialect does + * not support an {@code IF EXISTS} clause in a {@code DROP} statement. + * @see #CONTINUE_ON_ERROR + */ + IGNORE_FAILED_DROPS + } + } diff --git a/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlGroup.java b/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlGroup.java index fb6a09fd7c8..6e052bd607f 100644 --- a/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlGroup.java +++ b/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlGroup.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 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. @@ -42,7 +42,7 @@ import static java.lang.annotation.RetentionPolicy.*; @Documented @Inherited @Retention(RUNTIME) -@Target({ TYPE, METHOD }) +@Target({TYPE, METHOD}) public @interface SqlGroup { Sql[] value();