Skip to content

Commit 682e8fb

Browse files
committed
Reduce code duplication in MergedSqlConfig
1 parent dd713c7 commit 682e8fb

File tree

1 file changed

+19
-48
lines changed

1 file changed

+19
-48
lines changed

spring-test/src/main/java/org/springframework/test/context/jdbc/MergedSqlConfig.java

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -56,52 +56,21 @@ class MergedSqlConfig {
5656
private final ErrorMode errorMode;
5757

5858

59-
private static TransactionMode retrieveTransactionMode(AnnotationAttributes attributes) {
60-
TransactionMode transactionMode = attributes.getEnum("transactionMode");
61-
if (transactionMode == TransactionMode.DEFAULT) {
62-
transactionMode = TransactionMode.INFERRED;
59+
private static <E extends Enum<?>> E getEnum(AnnotationAttributes attributes, String attributeName,
60+
E inheritOrOverrideValue, E defaultValue) {
61+
E value = attributes.getEnum(attributeName);
62+
if (value == inheritOrOverrideValue) {
63+
value = defaultValue;
6364
}
64-
return transactionMode;
65+
return value;
6566
}
6667

67-
private static ErrorMode retrieveErrorMode(AnnotationAttributes attributes) {
68-
ErrorMode errorMode = attributes.getEnum("errorMode");
69-
if (errorMode == ErrorMode.DEFAULT) {
70-
errorMode = ErrorMode.FAIL_ON_ERROR;
68+
private static String getString(AnnotationAttributes attributes, String attributeName, String defaultValue) {
69+
String value = attributes.getString(attributeName);
70+
if ("".equals(value)) {
71+
value = defaultValue;
7172
}
72-
return errorMode;
73-
}
74-
75-
private static String retrieveSeparator(AnnotationAttributes attributes) {
76-
String separator = attributes.getString("separator");
77-
if ("".equals(separator)) {
78-
separator = ScriptUtils.DEFAULT_STATEMENT_SEPARATOR;
79-
}
80-
return separator;
81-
}
82-
83-
private static String retrieveCommentPrefix(AnnotationAttributes attributes) {
84-
String commentPrefix = attributes.getString("commentPrefix");
85-
if ("".equals(commentPrefix)) {
86-
commentPrefix = ScriptUtils.DEFAULT_COMMENT_PREFIX;
87-
}
88-
return commentPrefix;
89-
}
90-
91-
private static String retrieveBlockCommentStartDelimiter(AnnotationAttributes attributes) {
92-
String blockCommentStartDelimiter = attributes.getString("blockCommentStartDelimiter");
93-
if ("".equals(blockCommentStartDelimiter)) {
94-
blockCommentStartDelimiter = ScriptUtils.DEFAULT_BLOCK_COMMENT_START_DELIMITER;
95-
}
96-
return blockCommentStartDelimiter;
97-
}
98-
99-
private static String retrieveBlockCommentEndDelimiter(AnnotationAttributes attributes) {
100-
String blockCommentEndDelimiter = attributes.getString("blockCommentEndDelimiter");
101-
if ("".equals(blockCommentEndDelimiter)) {
102-
blockCommentEndDelimiter = ScriptUtils.DEFAULT_BLOCK_COMMENT_END_DELIMITER;
103-
}
104-
return blockCommentEndDelimiter;
73+
return value;
10574
}
10675

10776
/**
@@ -139,13 +108,15 @@ private static String retrieveBlockCommentEndDelimiter(AnnotationAttributes attr
139108

140109
this.dataSource = attributes.getString("dataSource");
141110
this.transactionManager = attributes.getString("transactionManager");
142-
this.transactionMode = retrieveTransactionMode(attributes);
111+
this.transactionMode = getEnum(attributes, "transactionMode", TransactionMode.DEFAULT, TransactionMode.INFERRED);
143112
this.encoding = attributes.getString("encoding");
144-
this.separator = retrieveSeparator(attributes);
145-
this.commentPrefix = retrieveCommentPrefix(attributes);
146-
this.blockCommentStartDelimiter = retrieveBlockCommentStartDelimiter(attributes);
147-
this.blockCommentEndDelimiter = retrieveBlockCommentEndDelimiter(attributes);
148-
this.errorMode = retrieveErrorMode(attributes);
113+
this.separator = getString(attributes, "separator", ScriptUtils.DEFAULT_STATEMENT_SEPARATOR);
114+
this.commentPrefix = getString(attributes, "commentPrefix", ScriptUtils.DEFAULT_COMMENT_PREFIX);
115+
this.blockCommentStartDelimiter = getString(attributes, "blockCommentStartDelimiter",
116+
ScriptUtils.DEFAULT_BLOCK_COMMENT_START_DELIMITER);
117+
this.blockCommentEndDelimiter = getString(attributes, "blockCommentEndDelimiter",
118+
ScriptUtils.DEFAULT_BLOCK_COMMENT_END_DELIMITER);
119+
this.errorMode = getEnum(attributes, "errorMode", ErrorMode.DEFAULT, ErrorMode.FAIL_ON_ERROR);
149120
}
150121

151122
/**

0 commit comments

Comments
 (0)