Skip to content

Commit

Permalink
Adding unit test for the class AlterResultMapPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
dcendents committed Jun 6, 2015
1 parent 3f223b7 commit eebb562
Show file tree
Hide file tree
Showing 2 changed files with 402 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.dcendents.mybatis.generator.plugin.mapper;
package com.github.dcendents.mybatis.generator.plugin.client;

import java.util.List;
import java.util.regex.Pattern;
Expand All @@ -25,8 +25,10 @@ public class AlterResultMapPlugin extends PluginAdapter {

private String tableName;
private String resultMapId;

private static Pattern annotationPattern = Pattern.compile("@ResultMap\\(\".*\"\\)");

static final String RESULT_MAP_ATTRIBUTE = "resultMap";
static final Pattern ANNOTATION_PATTERN = Pattern.compile("@ResultMap\\(\".*\"\\)");
static final String ANNOTATION_FORMAT = "@ResultMap(\"%s\")";

@Override
public boolean validate(List<String> warnings) {
Expand All @@ -48,138 +50,128 @@ private boolean tableMatches(IntrospectedTable introspectedTable) {
return tableName.equals(introspectedTable.getFullyQualifiedTableNameAtRuntime());
}

private void renameResultMapAttribute(XmlElement element) {
List<Attribute> attributes = element.getAttributes();

for (int i = 0; i < attributes.size(); i++) {
Attribute attribute = attributes.get(i);
if ("resultMap".equals(attribute.getName())) {
Attribute newAtt = new Attribute("resultMap", resultMapId);
attributes.remove(i);
attributes.add(newAtt);
break;
void renameResultMapAttribute(XmlElement element, IntrospectedTable introspectedTable) {
if (tableMatches(introspectedTable)) {
List<Attribute> attributes = element.getAttributes();

for (int i = 0; i < attributes.size(); i++) {
Attribute attribute = attributes.get(i);
if (RESULT_MAP_ATTRIBUTE.equals(attribute.getName())) {
Attribute newAtt = new Attribute(RESULT_MAP_ATTRIBUTE, resultMapId);
attributes.remove(i);
attributes.add(newAtt);
break;
}
}
}
}

private void renameResultMapAttribute(Method method) {
List<String> annotations = method.getAnnotations();

for (int i = 0; i < annotations.size(); i++) {
String annotation = annotations.get(i);
if (annotationPattern.matcher(annotation).matches()) {
String newAnnotation = String.format("@ResultMap(\"%s\")", resultMapId);
annotations.remove(i);
annotations.add(newAnnotation);
break;
void renameResultMapAttribute(Method method, IntrospectedTable introspectedTable) {
if (tableMatches(introspectedTable)) {
List<String> annotations = method.getAnnotations();

for (int i = 0; i < annotations.size(); i++) {
String annotation = annotations.get(i);
if (ANNOTATION_PATTERN.matcher(annotation).matches()) {
String newAnnotation = String.format(ANNOTATION_FORMAT, resultMapId);
annotations.remove(i);
annotations.add(newAnnotation);
break;
}
}
}
}

@Override
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
if (tableMatches(introspectedTable)) {
renameResultMapAttribute(element);
}
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element,
IntrospectedTable introspectedTable) {
renameResultMapAttribute(element, introspectedTable);

return true;
}

@Override
public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
if (tableMatches(introspectedTable)) {
renameResultMapAttribute(element);
}
public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element,
IntrospectedTable introspectedTable) {
renameResultMapAttribute(element, introspectedTable);

return true;
}

@Override
public boolean sqlMapSelectByPrimaryKeyElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
if (tableMatches(introspectedTable)) {
renameResultMapAttribute(element);
}
renameResultMapAttribute(element, introspectedTable);

return true;
}

@Override
public boolean sqlMapSelectAllElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
if (tableMatches(introspectedTable)) {
renameResultMapAttribute(element);
}
renameResultMapAttribute(element, introspectedTable);

return true;
}

@Override
public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
if (tableMatches(introspectedTable)) {
renameResultMapAttribute(method);
}
public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method, Interface interfaze,
IntrospectedTable introspectedTable) {
renameResultMapAttribute(method, introspectedTable);

return true;
}

@Override
public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
if (tableMatches(introspectedTable)) {
renameResultMapAttribute(method);
}
public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method, TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
renameResultMapAttribute(method, introspectedTable);

return true;
}

@Override
public boolean clientSelectByExampleWithoutBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
if (tableMatches(introspectedTable)) {
renameResultMapAttribute(method);
}
public boolean clientSelectByExampleWithoutBLOBsMethodGenerated(Method method, Interface interfaze,
IntrospectedTable introspectedTable) {
renameResultMapAttribute(method, introspectedTable);

return true;
}

@Override
public boolean clientSelectByExampleWithoutBLOBsMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
if (tableMatches(introspectedTable)) {
renameResultMapAttribute(method);
}
public boolean clientSelectByExampleWithoutBLOBsMethodGenerated(Method method, TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
renameResultMapAttribute(method, introspectedTable);

return true;
}

@Override
public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
if (tableMatches(introspectedTable)) {
renameResultMapAttribute(method);
}
public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, Interface interfaze,
IntrospectedTable introspectedTable) {
renameResultMapAttribute(method, introspectedTable);

return true;
}

@Override
public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
if (tableMatches(introspectedTable)) {
renameResultMapAttribute(method);
}
public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
renameResultMapAttribute(method, introspectedTable);

return true;
}

@Override
public boolean clientSelectAllMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
if (tableMatches(introspectedTable)) {
renameResultMapAttribute(method);
}
public boolean clientSelectAllMethodGenerated(Method method, Interface interfaze,
IntrospectedTable introspectedTable) {
renameResultMapAttribute(method, introspectedTable);

return true;
}

@Override
public boolean clientSelectAllMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
if (tableMatches(introspectedTable)) {
renameResultMapAttribute(method);
}
public boolean clientSelectAllMethodGenerated(Method method, TopLevelClass topLevelClass,
IntrospectedTable introspectedTable) {
renameResultMapAttribute(method, introspectedTable);

return true;
}
Expand Down
Loading

0 comments on commit eebb562

Please sign in to comment.