Skip to content

Commit c72bf10

Browse files
committed
Polish MergedSqlConfig
1 parent f53cdb8 commit c72bf10

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

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

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -77,40 +77,7 @@ class MergedSqlConfig {
7777
Assert.notNull(localSqlConfig, "Local @SqlConfig must not be null");
7878
Assert.notNull(testClass, "testClass must not be null");
7979

80-
AnnotationAttributes mergedAttributes;
81-
AnnotationAttributes localAttributes = AnnotationUtils.getAnnotationAttributes(localSqlConfig, false, false);
82-
// Enforce comment prefix aliases within the local @SqlConfig.
83-
enforceCommentPrefixAliases(localAttributes);
84-
85-
// Get global attributes, if any.
86-
AnnotationAttributes globalAttributes = AnnotatedElementUtils.findMergedAnnotationAttributes(
87-
testClass, SqlConfig.class.getName(), false, false);
88-
89-
if (globalAttributes != null) {
90-
// Enforce comment prefix aliases within the global @SqlConfig.
91-
enforceCommentPrefixAliases(globalAttributes);
92-
93-
for (String key : globalAttributes.keySet()) {
94-
Object value = localAttributes.get(key);
95-
if (isExplicitValue(value)) {
96-
// Override global attribute with local attribute.
97-
globalAttributes.put(key, value);
98-
99-
// Ensure comment prefix aliases are honored during the merge.
100-
if (key.equals(COMMENT_PREFIX) && isEmptyArray(localAttributes.get(COMMENT_PREFIXES))) {
101-
globalAttributes.put(COMMENT_PREFIXES, value);
102-
}
103-
else if (key.equals(COMMENT_PREFIXES) && isEmptyString(localAttributes.get(COMMENT_PREFIX))) {
104-
globalAttributes.put(COMMENT_PREFIX, value);
105-
}
106-
}
107-
}
108-
mergedAttributes = globalAttributes;
109-
}
110-
else {
111-
// Otherwise, use local attributes only.
112-
mergedAttributes = localAttributes;
113-
}
80+
AnnotationAttributes mergedAttributes = mergeAttributes(localSqlConfig, testClass);
11481

11582
this.dataSource = mergedAttributes.getString("dataSource");
11683
this.transactionManager = mergedAttributes.getString("transactionManager");
@@ -126,6 +93,42 @@ else if (key.equals(COMMENT_PREFIXES) && isEmptyString(localAttributes.get(COMME
12693
this.errorMode = getEnum(mergedAttributes, "errorMode", ErrorMode.DEFAULT, ErrorMode.FAIL_ON_ERROR);
12794
}
12895

96+
private AnnotationAttributes mergeAttributes(SqlConfig localSqlConfig, Class<?> testClass) {
97+
AnnotationAttributes localAttributes = AnnotationUtils.getAnnotationAttributes(localSqlConfig, false, false);
98+
99+
// Enforce comment prefix aliases within the local @SqlConfig.
100+
enforceCommentPrefixAliases(localAttributes);
101+
102+
// Get global attributes, if any.
103+
AnnotationAttributes globalAttributes = AnnotatedElementUtils.findMergedAnnotationAttributes(
104+
testClass, SqlConfig.class.getName(), false, false);
105+
106+
// Use local attributes only?
107+
if (globalAttributes == null) {
108+
return localAttributes;
109+
}
110+
111+
// Enforce comment prefix aliases within the global @SqlConfig.
112+
enforceCommentPrefixAliases(globalAttributes);
113+
114+
for (String key : globalAttributes.keySet()) {
115+
Object value = localAttributes.get(key);
116+
if (isExplicitValue(value)) {
117+
// Override global attribute with local attribute.
118+
globalAttributes.put(key, value);
119+
120+
// Ensure comment prefix aliases are honored during the merge.
121+
if (key.equals(COMMENT_PREFIX) && isEmptyArray(localAttributes.get(COMMENT_PREFIXES))) {
122+
globalAttributes.put(COMMENT_PREFIXES, value);
123+
}
124+
else if (key.equals(COMMENT_PREFIXES) && isEmptyString(localAttributes.get(COMMENT_PREFIX))) {
125+
globalAttributes.put(COMMENT_PREFIX, value);
126+
}
127+
}
128+
}
129+
return globalAttributes;
130+
}
131+
129132
/**
130133
* @see SqlConfig#dataSource()
131134
*/

0 commit comments

Comments
 (0)