Skip to content

Commit eebb562

Browse files
committed
Adding unit test for the class AlterResultMapPlugin
1 parent 3f223b7 commit eebb562

File tree

2 files changed

+402
-69
lines changed

2 files changed

+402
-69
lines changed

src/main/java/com/github/dcendents/mybatis/generator/plugin/mapper/AlterResultMapPlugin.java renamed to src/main/java/com/github/dcendents/mybatis/generator/plugin/client/AlterResultMapPlugin.java

Lines changed: 61 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.dcendents.mybatis.generator.plugin.mapper;
1+
package com.github.dcendents.mybatis.generator.plugin.client;
22

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

2626
private String tableName;
2727
private String resultMapId;
28-
29-
private static Pattern annotationPattern = Pattern.compile("@ResultMap\\(\".*\"\\)");
28+
29+
static final String RESULT_MAP_ATTRIBUTE = "resultMap";
30+
static final Pattern ANNOTATION_PATTERN = Pattern.compile("@ResultMap\\(\".*\"\\)");
31+
static final String ANNOTATION_FORMAT = "@ResultMap(\"%s\")";
3032

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

51-
private void renameResultMapAttribute(XmlElement element) {
52-
List<Attribute> attributes = element.getAttributes();
53-
54-
for (int i = 0; i < attributes.size(); i++) {
55-
Attribute attribute = attributes.get(i);
56-
if ("resultMap".equals(attribute.getName())) {
57-
Attribute newAtt = new Attribute("resultMap", resultMapId);
58-
attributes.remove(i);
59-
attributes.add(newAtt);
60-
break;
53+
void renameResultMapAttribute(XmlElement element, IntrospectedTable introspectedTable) {
54+
if (tableMatches(introspectedTable)) {
55+
List<Attribute> attributes = element.getAttributes();
56+
57+
for (int i = 0; i < attributes.size(); i++) {
58+
Attribute attribute = attributes.get(i);
59+
if (RESULT_MAP_ATTRIBUTE.equals(attribute.getName())) {
60+
Attribute newAtt = new Attribute(RESULT_MAP_ATTRIBUTE, resultMapId);
61+
attributes.remove(i);
62+
attributes.add(newAtt);
63+
break;
64+
}
6165
}
6266
}
6367
}
6468

65-
private void renameResultMapAttribute(Method method) {
66-
List<String> annotations = method.getAnnotations();
67-
68-
for (int i = 0; i < annotations.size(); i++) {
69-
String annotation = annotations.get(i);
70-
if (annotationPattern.matcher(annotation).matches()) {
71-
String newAnnotation = String.format("@ResultMap(\"%s\")", resultMapId);
72-
annotations.remove(i);
73-
annotations.add(newAnnotation);
74-
break;
69+
void renameResultMapAttribute(Method method, IntrospectedTable introspectedTable) {
70+
if (tableMatches(introspectedTable)) {
71+
List<String> annotations = method.getAnnotations();
72+
73+
for (int i = 0; i < annotations.size(); i++) {
74+
String annotation = annotations.get(i);
75+
if (ANNOTATION_PATTERN.matcher(annotation).matches()) {
76+
String newAnnotation = String.format(ANNOTATION_FORMAT, resultMapId);
77+
annotations.remove(i);
78+
annotations.add(newAnnotation);
79+
break;
80+
}
7581
}
7682
}
7783
}
7884

7985
@Override
80-
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
81-
if (tableMatches(introspectedTable)) {
82-
renameResultMapAttribute(element);
83-
}
86+
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element,
87+
IntrospectedTable introspectedTable) {
88+
renameResultMapAttribute(element, introspectedTable);
8489

8590
return true;
8691
}
8792

8893
@Override
89-
public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
90-
if (tableMatches(introspectedTable)) {
91-
renameResultMapAttribute(element);
92-
}
94+
public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element,
95+
IntrospectedTable introspectedTable) {
96+
renameResultMapAttribute(element, introspectedTable);
9397

9498
return true;
9599
}
96100

97101
@Override
98102
public boolean sqlMapSelectByPrimaryKeyElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
99-
if (tableMatches(introspectedTable)) {
100-
renameResultMapAttribute(element);
101-
}
103+
renameResultMapAttribute(element, introspectedTable);
102104

103105
return true;
104106
}
105107

106108
@Override
107109
public boolean sqlMapSelectAllElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
108-
if (tableMatches(introspectedTable)) {
109-
renameResultMapAttribute(element);
110-
}
110+
renameResultMapAttribute(element, introspectedTable);
111111

112112
return true;
113113
}
114114

115115
@Override
116-
public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
117-
if (tableMatches(introspectedTable)) {
118-
renameResultMapAttribute(method);
119-
}
116+
public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method, Interface interfaze,
117+
IntrospectedTable introspectedTable) {
118+
renameResultMapAttribute(method, introspectedTable);
120119

121120
return true;
122121
}
123122

124123
@Override
125-
public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
126-
if (tableMatches(introspectedTable)) {
127-
renameResultMapAttribute(method);
128-
}
124+
public boolean clientSelectByExampleWithBLOBsMethodGenerated(Method method, TopLevelClass topLevelClass,
125+
IntrospectedTable introspectedTable) {
126+
renameResultMapAttribute(method, introspectedTable);
129127

130128
return true;
131129
}
132130

133131
@Override
134-
public boolean clientSelectByExampleWithoutBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
135-
if (tableMatches(introspectedTable)) {
136-
renameResultMapAttribute(method);
137-
}
132+
public boolean clientSelectByExampleWithoutBLOBsMethodGenerated(Method method, Interface interfaze,
133+
IntrospectedTable introspectedTable) {
134+
renameResultMapAttribute(method, introspectedTable);
138135

139136
return true;
140137
}
141138

142139
@Override
143-
public boolean clientSelectByExampleWithoutBLOBsMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
144-
if (tableMatches(introspectedTable)) {
145-
renameResultMapAttribute(method);
146-
}
140+
public boolean clientSelectByExampleWithoutBLOBsMethodGenerated(Method method, TopLevelClass topLevelClass,
141+
IntrospectedTable introspectedTable) {
142+
renameResultMapAttribute(method, introspectedTable);
147143

148144
return true;
149145
}
150146

151147
@Override
152-
public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
153-
if (tableMatches(introspectedTable)) {
154-
renameResultMapAttribute(method);
155-
}
148+
public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, Interface interfaze,
149+
IntrospectedTable introspectedTable) {
150+
renameResultMapAttribute(method, introspectedTable);
156151

157152
return true;
158153
}
159154

160155
@Override
161-
public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
162-
if (tableMatches(introspectedTable)) {
163-
renameResultMapAttribute(method);
164-
}
156+
public boolean clientSelectByPrimaryKeyMethodGenerated(Method method, TopLevelClass topLevelClass,
157+
IntrospectedTable introspectedTable) {
158+
renameResultMapAttribute(method, introspectedTable);
165159

166160
return true;
167161
}
168162

169163
@Override
170-
public boolean clientSelectAllMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
171-
if (tableMatches(introspectedTable)) {
172-
renameResultMapAttribute(method);
173-
}
164+
public boolean clientSelectAllMethodGenerated(Method method, Interface interfaze,
165+
IntrospectedTable introspectedTable) {
166+
renameResultMapAttribute(method, introspectedTable);
174167

175168
return true;
176169
}
177170

178171
@Override
179-
public boolean clientSelectAllMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
180-
if (tableMatches(introspectedTable)) {
181-
renameResultMapAttribute(method);
182-
}
172+
public boolean clientSelectAllMethodGenerated(Method method, TopLevelClass topLevelClass,
173+
IntrospectedTable introspectedTable) {
174+
renameResultMapAttribute(method, introspectedTable);
183175

184176
return true;
185177
}

0 commit comments

Comments
 (0)