|
| 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