CommonTest.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package com.izouma.nineth;
  2. import com.izouma.nineth.domain.BaseEntity;
  3. import com.izouma.nineth.domain.User;
  4. import com.izouma.nineth.utils.UrlUtils;
  5. import com.izouma.nineth.web.BaseController;
  6. import lombok.SneakyThrows;
  7. import org.apache.commons.lang3.RandomStringUtils;
  8. import org.apache.commons.text.CaseUtils;
  9. import org.apache.poi.util.TempFile;
  10. import org.bouncycastle.util.encoders.Base64;
  11. import org.junit.Test;
  12. import org.libjpegturbo.turbojpeg.processor.api.ImageProcessInfo;
  13. import org.libjpegturbo.turbojpeg.processor.api.ImageProcessor;
  14. import org.libjpegturbo.turbojpeg.processor.impl.ImageProcessorImpl;
  15. import org.libjpegturbo.turbojpeg.processor.utils.ImageProcessorUtils;
  16. import org.pngquant.PngQuant;
  17. import org.reflections.ReflectionUtils;
  18. import org.reflections.Reflections;
  19. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  20. import org.springframework.util.FileCopyUtils;
  21. import org.springframework.web.bind.annotation.GetMapping;
  22. import org.springframework.web.bind.annotation.PostMapping;
  23. import org.springframework.web.bind.annotation.RequestMapping;
  24. import org.springframework.web.bind.annotation.RestController;
  25. import javax.imageio.ImageIO;
  26. import java.awt.*;
  27. import java.awt.font.FontRenderContext;
  28. import java.awt.geom.AffineTransform;
  29. import java.io.File;
  30. import java.io.IOException;
  31. import java.lang.reflect.Method;
  32. import java.net.URL;
  33. import java.nio.charset.StandardCharsets;
  34. import java.nio.file.Files;
  35. import java.nio.file.Paths;
  36. import java.util.ArrayList;
  37. import java.util.List;
  38. import java.util.Map;
  39. import java.util.Set;
  40. import java.util.regex.Pattern;
  41. import static java.nio.file.StandardOpenOption.CREATE;
  42. import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
  43. public class CommonTest {
  44. @Test
  45. public void getGenericsClass() {
  46. List<User> data = new ArrayList<>();
  47. data.add(new User());
  48. System.out.println(data.get(0).getClass().getSimpleName());
  49. System.out.println(data.getClass());
  50. Reflections reflections = new Reflections(this.getClass().getPackage().getName() + ".domain");
  51. Set<Class<? extends BaseEntity>> allClasses = reflections.getSubTypesOf(BaseEntity.class);
  52. for (Class<? extends BaseEntity> allClass : allClasses) {
  53. System.out.println(allClass.getName());
  54. }
  55. }
  56. @Test
  57. public void getapis() {
  58. List<Map<String, String>> entities = new ArrayList<>();
  59. Reflections classReflections = new Reflections(this.getClass().getPackage().getName());
  60. Set<Class<? extends BaseController>> controllers = classReflections.getSubTypesOf(BaseController.class);
  61. Set<Class<? extends BaseController>> list = ReflectionUtils
  62. .getAll(controllers, ReflectionUtils.withAnnotation(RestController.class));
  63. System.out.println(list);
  64. for (Class<? extends BaseController> aClass : list) {
  65. RequestMapping requestMapping = aClass.getAnnotation(RequestMapping.class);
  66. String baseUrl = requestMapping.value()[0];
  67. for (Method method : ReflectionUtils.getMethods(aClass, ReflectionUtils.withAnnotation(GetMapping.class))) {
  68. GetMapping getMapping = method.getAnnotation(GetMapping.class);
  69. System.out.println(getMapping.value()[0]);
  70. }
  71. for (Method method : ReflectionUtils
  72. .getMethods(aClass, ReflectionUtils.withAnnotation(PostMapping.class))) {
  73. PostMapping postMapping = method.getAnnotation(PostMapping.class);
  74. System.out.println(postMapping.value()[0]);
  75. }
  76. }
  77. }
  78. @Test
  79. public void testCaseUtils() {
  80. System.out.println(CaseUtils.toCamelCase("test_Model", true, '_'));
  81. }
  82. @Test
  83. public void testMeasureText() throws IOException, FontFormatException {
  84. AffineTransform affinetransform = new AffineTransform();
  85. FontRenderContext frc = new FontRenderContext(affinetransform, true, true);
  86. Font font = Font.createFont(Font.TRUETYPE_FONT, this.getClass()
  87. .getResourceAsStream("/font/SourceHanSansCN-Normal.ttf"));
  88. System.out.println((int) (font.deriveFont(14f).getStringBounds("aaa", frc).getWidth()));
  89. }
  90. @Test
  91. public void testIdNoRegexp() {
  92. boolean b = Pattern
  93. .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");
  94. System.out.println(b);
  95. }
  96. @Test
  97. public void tesSms() throws IOException {
  98. Reflections reflections = new Reflections(this.getClass().getPackage().getName() + ".enums");
  99. Set<Class<? extends Enum>> entitySet = reflections.getSubTypesOf(Enum.class);
  100. StringBuilder idxJs = new StringBuilder();
  101. for (Class<? extends Enum> entity : entitySet) {
  102. idxJs.append("import ").append(entity.getSimpleName()).append(" from \"./").append(entity.getSimpleName())
  103. .append("\";\n");
  104. StringBuilder str = new StringBuilder("export default {\n");
  105. for (Enum enumConstant : entity.getEnumConstants()) {
  106. str.append(" ").append(enumConstant.name()).append(": \"").append(enumConstant.name())
  107. .append("\",\n");
  108. }
  109. str.append("}");
  110. Files.write(Paths.get(System.getProperty("user.dir"), "src", "main", "vue", "src", "constants", entity
  111. .getSimpleName() + ".js"), str.toString()
  112. .getBytes(StandardCharsets.UTF_8), CREATE, TRUNCATE_EXISTING);
  113. Files.write(Paths.get(System.getProperty("user.dir"), "src", "main", "zmj_mp", "src", "constants", entity
  114. .getSimpleName() + ".js"), str.toString()
  115. .getBytes(StandardCharsets.UTF_8), CREATE, TRUNCATE_EXISTING);
  116. }
  117. idxJs.append("export default {\n");
  118. for (Class<? extends Enum> entity : entitySet) {
  119. idxJs.append(" ").append(entity.getSimpleName()).append(": ").append(entity.getSimpleName())
  120. .append(",\n");
  121. }
  122. idxJs.append("}");
  123. System.out.println(idxJs.toString());
  124. Files.write(Paths
  125. .get(System.getProperty("user.dir"), "src", "main", "vue", "src", "constants", "index.js"), idxJs
  126. .toString().getBytes(StandardCharsets.UTF_8), CREATE, TRUNCATE_EXISTING);
  127. Files.write(Paths
  128. .get(System.getProperty("user.dir"), "src", "main", "zmj_mp", "src", "constants", "index.js"), idxJs
  129. .toString().getBytes(StandardCharsets.UTF_8), CREATE, TRUNCATE_EXISTING);
  130. }
  131. @Test
  132. public void gen() {
  133. System.out.println(RandomStringUtils.randomAlphabetic(32));
  134. }
  135. @Test
  136. public void password() {
  137. String password = new BCryptPasswordEncoder().encode("123456");
  138. System.out.println(password);
  139. }
  140. @SneakyThrows
  141. @Test
  142. public void pngquant() {
  143. PngQuant pngQuant = new PngQuant();
  144. ImageIO.write(pngQuant
  145. .getRemapped(ImageIO.read(new File("/Users/drew/Downloads/国内申请-案件新增(客户经理).png"))),
  146. "png", new File("/Users/drew/Downloads/111.png"));
  147. }
  148. @SneakyThrows
  149. @Test
  150. public void mozjpeg() {
  151. File out = TempFile.createTempFile("kljasdlkhfasldg", ".jpg");
  152. ImageProcessor processor = new ImageProcessorImpl();
  153. ImageProcessInfo processInfo = ImageProcessInfo.fromMap(ImageProcessorUtils.compressImage(processor,
  154. new File("/Users/drew/Downloads/2020-09-08-17-07-21zwBhaHeQ.jpg"),
  155. out, 75));
  156. FileCopyUtils.copy(out, new File("/Users/drew/Desktop/111.jpg"));
  157. System.out.println(out);
  158. }
  159. @Test
  160. public void base64() {
  161. System.out.println(Base64.decode("e6e6vQJYhGmIkcA1pfnsipTovp10wJ"));
  162. }
  163. @Test
  164. public void resolveUrl() {
  165. System.out.println(UrlUtils.resolve("http://123123/", "/asdfsd/sdf"));
  166. }
  167. }