Polish MergedSqlConfig

This commit is contained in:
Sam Brannen 2019-07-28 18:41:26 +02:00
parent f53cdb8bd2
commit c72bf10eb9
1 changed files with 37 additions and 34 deletions

View File

@ -77,40 +77,7 @@ class MergedSqlConfig {
Assert.notNull(localSqlConfig, "Local @SqlConfig must not be null"); Assert.notNull(localSqlConfig, "Local @SqlConfig must not be null");
Assert.notNull(testClass, "testClass must not be null"); Assert.notNull(testClass, "testClass must not be null");
AnnotationAttributes mergedAttributes; AnnotationAttributes mergedAttributes = mergeAttributes(localSqlConfig, testClass);
AnnotationAttributes localAttributes = AnnotationUtils.getAnnotationAttributes(localSqlConfig, false, false);
// Enforce comment prefix aliases within the local @SqlConfig.
enforceCommentPrefixAliases(localAttributes);
// Get global attributes, if any.
AnnotationAttributes globalAttributes = AnnotatedElementUtils.findMergedAnnotationAttributes(
testClass, SqlConfig.class.getName(), false, false);
if (globalAttributes != null) {
// Enforce comment prefix aliases within the global @SqlConfig.
enforceCommentPrefixAliases(globalAttributes);
for (String key : globalAttributes.keySet()) {
Object value = localAttributes.get(key);
if (isExplicitValue(value)) {
// Override global attribute with local attribute.
globalAttributes.put(key, value);
// Ensure comment prefix aliases are honored during the merge.
if (key.equals(COMMENT_PREFIX) && isEmptyArray(localAttributes.get(COMMENT_PREFIXES))) {
globalAttributes.put(COMMENT_PREFIXES, value);
}
else if (key.equals(COMMENT_PREFIXES) && isEmptyString(localAttributes.get(COMMENT_PREFIX))) {
globalAttributes.put(COMMENT_PREFIX, value);
}
}
}
mergedAttributes = globalAttributes;
}
else {
// Otherwise, use local attributes only.
mergedAttributes = localAttributes;
}
this.dataSource = mergedAttributes.getString("dataSource"); this.dataSource = mergedAttributes.getString("dataSource");
this.transactionManager = mergedAttributes.getString("transactionManager"); this.transactionManager = mergedAttributes.getString("transactionManager");
@ -126,6 +93,42 @@ class MergedSqlConfig {
this.errorMode = getEnum(mergedAttributes, "errorMode", ErrorMode.DEFAULT, ErrorMode.FAIL_ON_ERROR); this.errorMode = getEnum(mergedAttributes, "errorMode", ErrorMode.DEFAULT, ErrorMode.FAIL_ON_ERROR);
} }
private AnnotationAttributes mergeAttributes(SqlConfig localSqlConfig, Class<?> testClass) {
AnnotationAttributes localAttributes = AnnotationUtils.getAnnotationAttributes(localSqlConfig, false, false);
// Enforce comment prefix aliases within the local @SqlConfig.
enforceCommentPrefixAliases(localAttributes);
// Get global attributes, if any.
AnnotationAttributes globalAttributes = AnnotatedElementUtils.findMergedAnnotationAttributes(
testClass, SqlConfig.class.getName(), false, false);
// Use local attributes only?
if (globalAttributes == null) {
return localAttributes;
}
// Enforce comment prefix aliases within the global @SqlConfig.
enforceCommentPrefixAliases(globalAttributes);
for (String key : globalAttributes.keySet()) {
Object value = localAttributes.get(key);
if (isExplicitValue(value)) {
// Override global attribute with local attribute.
globalAttributes.put(key, value);
// Ensure comment prefix aliases are honored during the merge.
if (key.equals(COMMENT_PREFIX) && isEmptyArray(localAttributes.get(COMMENT_PREFIXES))) {
globalAttributes.put(COMMENT_PREFIXES, value);
}
else if (key.equals(COMMENT_PREFIXES) && isEmptyString(localAttributes.get(COMMENT_PREFIX))) {
globalAttributes.put(COMMENT_PREFIX, value);
}
}
}
return globalAttributes;
}
/** /**
* @see SqlConfig#dataSource() * @see SqlConfig#dataSource()
*/ */