CommonTest.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package com.izouma.awesomeAdmin;
  2. import com.izouma.awesomeAdmin.domain.BaseEntity;
  3. import com.izouma.awesomeAdmin.domain.User;
  4. import com.izouma.awesomeAdmin.web.BaseController;
  5. import org.apache.commons.lang3.RandomStringUtils;
  6. import org.apache.commons.text.CaseUtils;
  7. import org.junit.Test;
  8. import org.reflections.ReflectionUtils;
  9. import org.reflections.Reflections;
  10. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  11. import org.springframework.web.bind.annotation.GetMapping;
  12. import org.springframework.web.bind.annotation.PostMapping;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import java.awt.*;
  16. import java.awt.font.FontRenderContext;
  17. import java.awt.geom.AffineTransform;
  18. import java.io.IOException;
  19. import java.lang.reflect.InvocationTargetException;
  20. import java.lang.reflect.Method;
  21. import java.nio.charset.StandardCharsets;
  22. import java.nio.file.Files;
  23. import java.nio.file.Paths;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import java.util.regex.Pattern;
  29. import static java.nio.file.StandardOpenOption.CREATE;
  30. import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
  31. public class CommonTest {
  32. @Test
  33. public void getGenericsClass() {
  34. List<User> data = new ArrayList<>();
  35. data.add(new User());
  36. System.out.println(data.get(0).getClass().getSimpleName());
  37. System.out.println(data.getClass());
  38. Reflections reflections = new Reflections(this.getClass().getPackage().getName() + ".domain");
  39. Set<Class<? extends BaseEntity>> allClasses = reflections.getSubTypesOf(BaseEntity.class);
  40. for (Class<? extends BaseEntity> allClass : allClasses) {
  41. System.out.println(allClass.getName());
  42. }
  43. }
  44. @Test
  45. public void getapis() {
  46. List<Map<String, String>> entities = new ArrayList<>();
  47. Reflections classReflections = new Reflections(this.getClass().getPackage().getName());
  48. Set<Class<? extends BaseController>> controllers = classReflections.getSubTypesOf(BaseController.class);
  49. Set<Class<? extends BaseController>> list = ReflectionUtils.getAll(controllers, ReflectionUtils.withAnnotation(RestController.class));
  50. System.out.println(list);
  51. for (Class<? extends BaseController> aClass : list) {
  52. RequestMapping requestMapping = aClass.getAnnotation(RequestMapping.class);
  53. String baseUrl = requestMapping.value()[0];
  54. for (Method method : ReflectionUtils.getMethods(aClass, ReflectionUtils.withAnnotation(GetMapping.class))) {
  55. GetMapping getMapping = method.getAnnotation(GetMapping.class);
  56. System.out.println(getMapping.value()[0]);
  57. }
  58. for (Method method : ReflectionUtils.getMethods(aClass, ReflectionUtils.withAnnotation(PostMapping.class))) {
  59. PostMapping postMapping = method.getAnnotation(PostMapping.class);
  60. System.out.println(postMapping.value()[0]);
  61. }
  62. }
  63. }
  64. @Test
  65. public void testCaseUtils() {
  66. System.out.println(CaseUtils.toCamelCase("test_Model", true, '_'));
  67. }
  68. @Test
  69. public void testMeasureText() throws IOException, FontFormatException {
  70. AffineTransform affinetransform = new AffineTransform();
  71. FontRenderContext frc = new FontRenderContext(affinetransform, true, true);
  72. Font font = Font.createFont(Font.TRUETYPE_FONT, this.getClass().getResourceAsStream("/font/SourceHanSansCN-Normal.ttf"));
  73. System.out.println((int) (font.deriveFont(14f).getStringBounds("aaa", frc).getWidth()));
  74. }
  75. @Test
  76. public void testIdNoRegexp() {
  77. boolean b = Pattern.matches("^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0-2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0-2]\\d)|3[0-1])\\d{3}[0-9xX]$", "32100219950830461X");
  78. System.out.println(b);
  79. }
  80. @Test
  81. public void tesSms() throws IOException {
  82. Reflections reflections = new Reflections(this.getClass().getPackage().getName() + ".enums");
  83. Set<Class<? extends Enum>> entitySet = reflections.getSubTypesOf(Enum.class);
  84. StringBuilder idxJs = new StringBuilder();
  85. for (Class<? extends Enum> entity : entitySet) {
  86. idxJs.append("import ").append(entity.getSimpleName()).append(" from \"./").append(entity.getSimpleName()).append("\";\n");
  87. StringBuilder str = new StringBuilder("export default {\n");
  88. for (Enum enumConstant : entity.getEnumConstants()) {
  89. str.append(" ").append(enumConstant.name()).append(": \"").append(enumConstant.name()).append("\",\n");
  90. }
  91. str.append("}");
  92. Files.write(Paths.get(System.getProperty("user.dir"), "src", "main", "vue", "src", "constants", entity.getSimpleName() + ".js"), str.toString().getBytes(StandardCharsets.UTF_8), CREATE, TRUNCATE_EXISTING);
  93. Files.write(Paths.get(System.getProperty("user.dir"), "src", "main", "zmj_mp", "src", "constants", entity.getSimpleName() + ".js"), str.toString().getBytes(StandardCharsets.UTF_8), CREATE, TRUNCATE_EXISTING);
  94. }
  95. idxJs.append("export default {\n");
  96. for (Class<? extends Enum> entity : entitySet) {
  97. idxJs.append(" ").append(entity.getSimpleName()).append(": ").append(entity.getSimpleName()).append(",\n");
  98. }
  99. idxJs.append("}");
  100. System.out.println(idxJs.toString());
  101. Files.write(Paths.get(System.getProperty("user.dir"), "src", "main", "vue", "src", "constants", "index.js"), idxJs.toString().getBytes(StandardCharsets.UTF_8), CREATE, TRUNCATE_EXISTING);
  102. Files.write(Paths.get(System.getProperty("user.dir"), "src", "main", "zmj_mp", "src", "constants", "index.js"), idxJs.toString().getBytes(StandardCharsets.UTF_8), CREATE, TRUNCATE_EXISTING);
  103. }
  104. @Test
  105. public void gen() {
  106. System.out.println(RandomStringUtils.randomAlphabetic(32));
  107. }
  108. @Test
  109. public void password() {
  110. String password = new BCryptPasswordEncoder().encode("123456");
  111. System.out.println(password);
  112. }
  113. @Test
  114. public void testEnum() {
  115. System.out.println(String.class.getName());
  116. System.out.println(String.class.getSimpleName());
  117. System.out.println(String.class.getCanonicalName());
  118. System.out.println(String.class.getTypeName());
  119. }
  120. }