CommonTest.java 8.9 KB

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