From 682e8fb3ad5e66ae42995b999bec062eb76247c3 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 18 Jul 2014 14:39:23 +0200 Subject: [PATCH] Reduce code duplication in MergedSqlConfig --- .../test/context/jdbc/MergedSqlConfig.java | 67 ++++++------------- 1 file changed, 19 insertions(+), 48 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/jdbc/MergedSqlConfig.java b/spring-test/src/main/java/org/springframework/test/context/jdbc/MergedSqlConfig.java index 96da9191e57..679a07ae766 100644 --- a/spring-test/src/main/java/org/springframework/test/context/jdbc/MergedSqlConfig.java +++ b/spring-test/src/main/java/org/springframework/test/context/jdbc/MergedSqlConfig.java @@ -56,52 +56,21 @@ class MergedSqlConfig { private final ErrorMode errorMode; - private static TransactionMode retrieveTransactionMode(AnnotationAttributes attributes) { - TransactionMode transactionMode = attributes.getEnum("transactionMode"); - if (transactionMode == TransactionMode.DEFAULT) { - transactionMode = TransactionMode.INFERRED; + private static > E getEnum(AnnotationAttributes attributes, String attributeName, + E inheritOrOverrideValue, E defaultValue) { + E value = attributes.getEnum(attributeName); + if (value == inheritOrOverrideValue) { + value = defaultValue; } - return transactionMode; + return value; } - private static ErrorMode retrieveErrorMode(AnnotationAttributes attributes) { - ErrorMode errorMode = attributes.getEnum("errorMode"); - if (errorMode == ErrorMode.DEFAULT) { - errorMode = ErrorMode.FAIL_ON_ERROR; + private static String getString(AnnotationAttributes attributes, String attributeName, String defaultValue) { + String value = attributes.getString(attributeName); + if ("".equals(value)) { + value = defaultValue; } - return errorMode; - } - - private static String retrieveSeparator(AnnotationAttributes attributes) { - String separator = attributes.getString("separator"); - if ("".equals(separator)) { - separator = ScriptUtils.DEFAULT_STATEMENT_SEPARATOR; - } - return separator; - } - - private static String retrieveCommentPrefix(AnnotationAttributes attributes) { - String commentPrefix = attributes.getString("commentPrefix"); - if ("".equals(commentPrefix)) { - commentPrefix = ScriptUtils.DEFAULT_COMMENT_PREFIX; - } - return commentPrefix; - } - - private static String retrieveBlockCommentStartDelimiter(AnnotationAttributes attributes) { - String blockCommentStartDelimiter = attributes.getString("blockCommentStartDelimiter"); - if ("".equals(blockCommentStartDelimiter)) { - blockCommentStartDelimiter = ScriptUtils.DEFAULT_BLOCK_COMMENT_START_DELIMITER; - } - return blockCommentStartDelimiter; - } - - private static String retrieveBlockCommentEndDelimiter(AnnotationAttributes attributes) { - String blockCommentEndDelimiter = attributes.getString("blockCommentEndDelimiter"); - if ("".equals(blockCommentEndDelimiter)) { - blockCommentEndDelimiter = ScriptUtils.DEFAULT_BLOCK_COMMENT_END_DELIMITER; - } - return blockCommentEndDelimiter; + return value; } /** @@ -139,13 +108,15 @@ class MergedSqlConfig { this.dataSource = attributes.getString("dataSource"); this.transactionManager = attributes.getString("transactionManager"); - this.transactionMode = retrieveTransactionMode(attributes); + this.transactionMode = getEnum(attributes, "transactionMode", TransactionMode.DEFAULT, TransactionMode.INFERRED); this.encoding = attributes.getString("encoding"); - this.separator = retrieveSeparator(attributes); - this.commentPrefix = retrieveCommentPrefix(attributes); - this.blockCommentStartDelimiter = retrieveBlockCommentStartDelimiter(attributes); - this.blockCommentEndDelimiter = retrieveBlockCommentEndDelimiter(attributes); - this.errorMode = retrieveErrorMode(attributes); + this.separator = getString(attributes, "separator", ScriptUtils.DEFAULT_STATEMENT_SEPARATOR); + this.commentPrefix = getString(attributes, "commentPrefix", ScriptUtils.DEFAULT_COMMENT_PREFIX); + this.blockCommentStartDelimiter = getString(attributes, "blockCommentStartDelimiter", + ScriptUtils.DEFAULT_BLOCK_COMMENT_START_DELIMITER); + this.blockCommentEndDelimiter = getString(attributes, "blockCommentEndDelimiter", + ScriptUtils.DEFAULT_BLOCK_COMMENT_END_DELIMITER); + this.errorMode = getEnum(attributes, "errorMode", ErrorMode.DEFAULT, ErrorMode.FAIL_ON_ERROR); } /**