Skip to content

Commit 31436b5

Browse files
committed
Adding AlterModelPlugin
1 parent bff9225 commit 31436b5

File tree

2 files changed

+205
-0
lines changed

2 files changed

+205
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.github.dcendents.mybatis.generator.plugin.model;
2+
3+
import java.util.List;
4+
5+
import lombok.NoArgsConstructor;
6+
7+
import org.mybatis.generator.api.IntrospectedTable;
8+
import org.mybatis.generator.api.PluginAdapter;
9+
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
10+
import org.mybatis.generator.api.dom.java.TopLevelClass;
11+
12+
import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
13+
14+
/**
15+
* Mybatis generator plugin to modify the generated model.
16+
*/
17+
@NoArgsConstructor
18+
public class AlterModelPlugin extends PluginAdapter {
19+
public static final String TABLE_NAME = "fullyQualifiedTableName";
20+
public static final String ADD_INTERFACES = "addInterfaces";
21+
22+
private String tableName;
23+
private String[] addInterfaces;
24+
25+
@Override
26+
public boolean validate(List<String> warnings) {
27+
tableName = properties.getProperty(TABLE_NAME);
28+
String interfacesString = properties.getProperty(ADD_INTERFACES);
29+
30+
String warning = "Property %s not set for plugin %s";
31+
if (!stringHasValue(tableName)) {
32+
warnings.add(String.format(warning, TABLE_NAME, this.getClass().getSimpleName()));
33+
}
34+
if (!stringHasValue(interfacesString)) {
35+
warnings.add(String.format(warning, ADD_INTERFACES, this.getClass().getSimpleName()));
36+
} else {
37+
addInterfaces = interfacesString.split(",");
38+
}
39+
40+
return stringHasValue(tableName) && addInterfaces != null;
41+
}
42+
43+
private boolean tableMatches(IntrospectedTable introspectedTable) {
44+
return tableName.equals(introspectedTable.getFullyQualifiedTableNameAtRuntime());
45+
}
46+
47+
@Override
48+
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
49+
if (tableMatches(introspectedTable)) {
50+
for (String theInterface : addInterfaces) {
51+
FullyQualifiedJavaType type = new FullyQualifiedJavaType(theInterface);
52+
topLevelClass.addImportedType(type);
53+
topLevelClass.addSuperInterface(type);
54+
}
55+
}
56+
57+
return true;
58+
}
59+
60+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package com.github.dcendents.mybatis.generator.plugin.model;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.mockito.BDDMockito.given;
5+
import static org.mockito.Matchers.any;
6+
import static org.mockito.Mockito.times;
7+
import static org.mockito.Mockito.verify;
8+
9+
import java.io.Serializable;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
import org.jglue.cdiunit.CdiRunner;
14+
import org.junit.Before;
15+
import org.junit.Test;
16+
import org.junit.runner.RunWith;
17+
import org.mockito.ArgumentCaptor;
18+
import org.mockito.Mock;
19+
import org.mybatis.generator.api.IntrospectedTable;
20+
import org.mybatis.generator.api.dom.java.Field;
21+
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
22+
import org.mybatis.generator.api.dom.java.TopLevelClass;
23+
24+
/**
25+
* Tests for the class AlterModelPlugin.
26+
*/
27+
@RunWith(CdiRunner.class)
28+
public class AlterModelPluginTest {
29+
30+
private AlterModelPlugin plugin;
31+
32+
@Mock
33+
private IntrospectedTable introspectedTable;
34+
@Mock
35+
private TopLevelClass topLevelClass;
36+
37+
private static final String TABLE_NAME = "table_name";
38+
39+
@Before
40+
public void init() throws Exception {
41+
plugin = new AlterModelPlugin();
42+
plugin.getProperties().put(AlterModelPlugin.TABLE_NAME, TABLE_NAME);
43+
plugin.getProperties().put(AlterModelPlugin.ADD_INTERFACES, Serializable.class.getName());
44+
plugin.validate(new ArrayList<String>());
45+
}
46+
47+
@Test
48+
public void shouldBeInvalidWithoutAnyPropertyConfigured() {
49+
// Given
50+
AlterModelPlugin instance = new AlterModelPlugin();
51+
52+
// When
53+
List<String> warnings = new ArrayList<>();
54+
boolean ok = instance.validate(warnings);
55+
56+
// Then
57+
assertThat(ok).isFalse();
58+
assertThat(warnings).hasSize(2);
59+
}
60+
61+
@Test
62+
public void shouldBeInvalidWithOnlyTheTableNameConfigured() {
63+
// Given
64+
AlterModelPlugin instance = new AlterModelPlugin();
65+
instance.getProperties().put(AlterModelPlugin.TABLE_NAME, TABLE_NAME);
66+
67+
// When
68+
List<String> warnings = new ArrayList<>();
69+
boolean ok = instance.validate(warnings);
70+
71+
// Then
72+
assertThat(ok).isFalse();
73+
assertThat(warnings).hasSize(1);
74+
}
75+
76+
@Test
77+
public void shouldBeInvalidWithOnlyTheInterfacesConfigured() {
78+
// Given
79+
AlterModelPlugin instance = new AlterModelPlugin();
80+
instance.getProperties().put(AlterModelPlugin.ADD_INTERFACES, Serializable.class.getName());
81+
82+
// When
83+
List<String> warnings = new ArrayList<>();
84+
boolean ok = instance.validate(warnings);
85+
86+
// Then
87+
assertThat(ok).isFalse();
88+
assertThat(warnings).hasSize(1);
89+
}
90+
91+
@Test
92+
public void shouldBeValidWhenBothPropertiesAreConfigured() {
93+
// Given
94+
AlterModelPlugin instance = new AlterModelPlugin();
95+
instance.getProperties().put(AlterModelPlugin.TABLE_NAME, TABLE_NAME);
96+
instance.getProperties().put(AlterModelPlugin.ADD_INTERFACES, Serializable.class.getName());
97+
98+
// When
99+
List<String> warnings = new ArrayList<>();
100+
boolean ok = instance.validate(warnings);
101+
102+
// Then
103+
assertThat(ok).isTrue();
104+
assertThat(warnings).isEmpty();
105+
}
106+
107+
@Test
108+
public void shouldNotModifyModelBaseRecordClassIfTableDoesNotMatch() throws Exception {
109+
// Given
110+
given(introspectedTable.getFullyQualifiedTableNameAtRuntime()).willReturn("wrong_name");
111+
112+
// When
113+
boolean ok = plugin.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
114+
115+
// Then
116+
assertThat(ok).isTrue();
117+
verify(topLevelClass, times(0)).addField(any(Field.class));
118+
}
119+
120+
@Test
121+
public void shouldAddInterfacesToModelBaseRecordClass() throws Exception {
122+
// Given
123+
given(introspectedTable.getFullyQualifiedTableNameAtRuntime()).willReturn(TABLE_NAME);
124+
125+
// When
126+
boolean ok = plugin.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
127+
128+
// Then
129+
assertThat(ok).isTrue();
130+
131+
ArgumentCaptor<FullyQualifiedJavaType> typeCaptor = ArgumentCaptor.forClass(FullyQualifiedJavaType.class);
132+
133+
verify(topLevelClass).addImportedType(typeCaptor.capture());
134+
FullyQualifiedJavaType importedType = typeCaptor.getValue();
135+
verify(topLevelClass).addSuperInterface(typeCaptor.capture());
136+
FullyQualifiedJavaType interfaceType = typeCaptor.getValue();
137+
138+
assertThat(importedType).isNotNull();
139+
assertThat(interfaceType).isNotNull();
140+
141+
assertThat(importedType).isSameAs(interfaceType);
142+
assertThat(importedType.getFullyQualifiedName()).isEqualTo(Serializable.class.getName());
143+
}
144+
145+
}

0 commit comments

Comments
 (0)