CheckEntity.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package com.x.build;
  2. import java.lang.reflect.Field;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Objects;
  6. import javax.persistence.Column;
  7. import javax.persistence.EnumType;
  8. import javax.persistence.Enumerated;
  9. import javax.persistence.Index;
  10. import javax.persistence.Lob;
  11. import javax.persistence.Table;
  12. import javax.persistence.UniqueConstraint;
  13. import org.apache.commons.lang3.StringUtils;
  14. import org.apache.commons.lang3.reflect.FieldUtils;
  15. import org.apache.openjpa.persistence.jdbc.ContainerTable;
  16. import org.junit.Test;
  17. import com.x.base.core.entity.JpaObject;
  18. import com.x.base.core.entity.annotation.ContainerEntity;
  19. import com.x.base.core.project.annotation.FieldDescribe;
  20. import com.x.base.core.project.annotation.Module;
  21. import com.x.base.core.project.gson.XGsonBuilder;
  22. import io.github.classgraph.ClassGraph;
  23. import io.github.classgraph.ClassInfo;
  24. import io.github.classgraph.ScanResult;
  25. public class CheckEntity {
  26. /*
  27. * 检查数据库字段名是否是ColumnNamePrefix + fieldName
  28. */
  29. @Test
  30. public void checkColumnName() throws Exception {
  31. try (ScanResult scanResult = new ClassGraph().enableAllInfo().scan()) {
  32. List<ClassInfo> classInfos = scanResult.getClassesWithAnnotation(ContainerEntity.class.getName());
  33. List<Class<?>> list = new ArrayList<>();
  34. for (ClassInfo info : classInfos) {
  35. list.add(Class.forName(info.getName()));
  36. }
  37. for (Class<?> cls : list) {
  38. List<Field> fields = FieldUtils.getAllFieldsList(cls);
  39. for (Field field : fields) {
  40. Column col = field.getAnnotation(Column.class);
  41. if (null != col) {
  42. if (StringUtils.equals(JpaObject.ColumnNamePrefix + field.getName(), col.name())) {
  43. // System.out.println(cls + ":" + field.getName() + ":" + col.name() + ":" +
  44. // col.length());
  45. } else {
  46. System.err.println(cls + ":" + field.getName() + ":" + col.name() + ":" + col.length());
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. /*
  54. * 检查是否有将Lob类型字段增加索引
  55. */
  56. @Test
  57. public void checkLobIndex() throws Exception {
  58. try (ScanResult scanResult = new ClassGraph().enableAllInfo().scan()) {
  59. List<ClassInfo> classInfos = scanResult.getClassesWithAnnotation(ContainerEntity.class.getName());
  60. List<Class<?>> list = new ArrayList<>();
  61. for (ClassInfo info : classInfos) {
  62. list.add(Class.forName(info.getName()));
  63. }
  64. for (Class<?> cls : list) {
  65. List<Field> fields = FieldUtils.getAllFieldsList(cls);
  66. for (Field field : fields) {
  67. Lob lob = field.getAnnotation(Lob.class);
  68. Index index = field.getAnnotation(Index.class);
  69. if ((null != lob) && (null != index)) {
  70. System.err.println(cls + ":" + field.getName());
  71. } else {
  72. // System.out.println(cls + ":" + field.getName() ;
  73. }
  74. }
  75. }
  76. }
  77. }
  78. /*
  79. * 检查是否有将Lob类型字段增加索引
  80. *
  81. * @FieldDescribe("群组的个人成员.存放个人 ID.")
  82. *
  83. * @PersistentCollection(fetch = FetchType.EAGER)
  84. *
  85. * @OrderColumn(name = ORDERCOLUMNCOLUMN)
  86. *
  87. * @ContainerTable(name = TABLE + ContainerTableNameMiddle +
  88. * personList_FIELDNAME, joinIndex = @Index(name = TABLE + IndexNameMiddle +
  89. * personList_FIELDNAME + JoinIndexNameSuffix))
  90. *
  91. * @ElementColumn(length = JpaObject.length_id, name = ColumnNamePrefix +
  92. * personList_FIELDNAME)
  93. *
  94. * @ElementIndex(name = TABLE + IndexNameMiddle + personList_FIELDNAME +
  95. * ElementIndexNameSuffix)
  96. *
  97. * @CheckPersist(allowEmpty = true, citationExists = @CitationExist(type =
  98. * Person.class)) private List<String> personList;
  99. */
  100. @Test
  101. public void checkListFieldContainerTableName() throws Exception {
  102. try (ScanResult scanResult = new ClassGraph().enableAllInfo().scan()) {
  103. List<ClassInfo> classInfos = scanResult.getClassesWithAnnotation(ContainerEntity.class.getName());
  104. List<Class<?>> list = new ArrayList<>();
  105. for (ClassInfo info : classInfos) {
  106. list.add(Class.forName(info.getName()));
  107. }
  108. for (Class<?> cls : list) {
  109. List<Field> fields = FieldUtils.getAllFieldsList(cls);
  110. for (Field field : fields) {
  111. if (List.class.isAssignableFrom(field.getType())) {
  112. ContainerTable containerTable = field.getAnnotation(ContainerTable.class);
  113. if (null != containerTable) {
  114. String name = FieldUtils.readStaticField(cls, "TABLE", true).toString()
  115. + JpaObject.ContainerTableNameMiddle + field.getName();
  116. if (!StringUtils.equals(name, containerTable.name())) {
  117. System.err.println(cls.getName() + ":" + field.getName());
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. /*
  126. * 检查是否将@FieldDescribe注解到static字段上,如果是意味着上下行搞错了
  127. */
  128. @Test
  129. public void checkFieldDescribeOnStatic() throws Exception {
  130. try (ScanResult scanResult = new ClassGraph().enableAllInfo().scan()) {
  131. List<ClassInfo> classInfos = scanResult.getClassesWithAnnotation(ContainerEntity.class.getName());
  132. List<Class<?>> list = new ArrayList<>();
  133. for (ClassInfo info : classInfos) {
  134. list.add(Class.forName(info.getName()));
  135. }
  136. for (Class<?> cls : list) {
  137. List<Field> fields = FieldUtils.getFieldsListWithAnnotation(cls, FieldDescribe.class);
  138. for (Field field : fields) {
  139. if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
  140. System.out.println(cls + ":" + field.getName());
  141. }
  142. }
  143. }
  144. }
  145. }
  146. /*
  147. * 检查约束名中的table名称和entity类中的TABLE名称是否一致
  148. */
  149. @Test
  150. public void checkTableNameUniqueConstraintName() throws Exception {
  151. try (ScanResult scanResult = new ClassGraph().enableAllInfo().scan()) {
  152. List<ClassInfo> classInfos = scanResult.getClassesWithAnnotation(ContainerEntity.class.getName());
  153. List<Class<?>> list = new ArrayList<>();
  154. for (ClassInfo info : classInfos) {
  155. list.add(Class.forName(info.getName()));
  156. }
  157. for (Class<?> cls : list) {
  158. Table table = cls.getAnnotation(Table.class);
  159. String name = Objects.toString(FieldUtils.readStaticField(cls, "TABLE", true));
  160. if (!StringUtils.equals(table.name(), name)) {
  161. System.out.println("table name not match:" + cls);
  162. }
  163. for (UniqueConstraint u : table.uniqueConstraints()) {
  164. if (!StringUtils.startsWith(u.name(), table.name())) {
  165. System.out.println("uniqueConstraint name not match:" + cls);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. /*
  172. * 检查类中是否有在createTime,updateTime和sequence上的索引,这几个索引已经用约束在类上了
  173. */
  174. @Test
  175. public void checkIdCreateTimeUpdateTimeSequenceIndex() throws Exception {
  176. try (ScanResult scanResult = new ClassGraph().enableAllInfo().scan()) {
  177. List<ClassInfo> classInfos = scanResult.getClassesWithAnnotation(ContainerEntity.class.getName());
  178. List<Class<?>> list = new ArrayList<>();
  179. for (ClassInfo info : classInfos) {
  180. list.add(Class.forName(info.getName()));
  181. }
  182. for (Class<?> cls : list) {
  183. Field idField = FieldUtils.getField(cls, JpaObject.id_FIELDNAME, true);
  184. Field createTimeField = FieldUtils.getField(cls, JpaObject.createTime_FIELDNAME, true);
  185. Field updateTimeField = FieldUtils.getField(cls, JpaObject.updateTime_FIELDNAME, true);
  186. Field sequenceField = FieldUtils.getField(cls, JpaObject.sequence_FIELDNAME, true);
  187. if ((null != idField.getAnnotation(Index.class)) || (null != createTimeField.getAnnotation(Index.class))
  188. || (null != updateTimeField.getAnnotation(Index.class))
  189. || (null != sequenceField.getAnnotation(Index.class))) {
  190. System.out.println(cls.getName() + " has IdCreateTimeUpdateTimeSequenceIndex");
  191. }
  192. }
  193. }
  194. }
  195. /*
  196. * 检查entity是否有重复的字段
  197. */
  198. @Test
  199. public void checkMutiField() throws Exception {
  200. try (ScanResult scanResult = new ClassGraph().enableAllInfo().scan()) {
  201. List<ClassInfo> classInfos = scanResult.getClassesWithAnnotation(ContainerEntity.class.getName());
  202. List<Class<?>> list = new ArrayList<>();
  203. for (ClassInfo info : classInfos) {
  204. list.add(Class.forName(info.getName()));
  205. }
  206. for (Class<?> cls : list) {
  207. Object o = cls.newInstance();
  208. XGsonBuilder.toJson(o);
  209. }
  210. }
  211. }
  212. /*
  213. * 检查entity是否有重复的字段
  214. */
  215. @Test
  216. public void checkEnum() throws Exception {
  217. try (ScanResult scanResult = new ClassGraph().enableAllInfo().scan()) {
  218. List<ClassInfo> classInfos = scanResult.getClassesWithAnnotation(ContainerEntity.class.getName());
  219. List<Class<?>> list = new ArrayList<>();
  220. for (ClassInfo info : classInfos) {
  221. list.add(Class.forName(info.getName()));
  222. }
  223. for (Class<?> cls : list) {
  224. List<Field> fields = FieldUtils.getFieldsListWithAnnotation(cls, FieldDescribe.class);
  225. for (Field field : fields) {
  226. if (field.getType().isEnum()) {
  227. Enumerated enumerated = field.getAnnotation(Enumerated.class);
  228. Column column = field.getAnnotation(Column.class);
  229. if (null == enumerated || (!Objects.equals(EnumType.STRING, enumerated.value()))
  230. || (null == column) || column.length() > 200) {
  231. System.out.println(cls + ":" + field.getName());
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }
  238. /*
  239. * 检查entity是否有重复的字段
  240. */
  241. @Test
  242. public void checkContainerEntities() throws Exception {
  243. try (ScanResult scanResult = new ClassGraph().enableAllInfo().scan()) {
  244. List<ClassInfo> classInfos = scanResult.getClassesWithAnnotation(Module.class.getName());
  245. List<Class<?>> list = new ArrayList<>();
  246. for (ClassInfo info : classInfos) {
  247. list.add(Class.forName(info.getName()));
  248. }
  249. for (Class<?> cls : list) {
  250. List<String> containerEntities = (List<String>) FieldUtils.readStaticField(cls, "containerEntities");
  251. for (String str : containerEntities) {
  252. try {
  253. Class clazz = Class.forName(str);
  254. } catch (Exception e) {
  255. System.out.println(cls.getName() + " error " + str);
  256. }
  257. }
  258. }
  259. }
  260. }
  261. }