CommonTest.java 6.7 KB

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