CommonTest.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package com.izouma.nineth;
  2. import com.github.kevinsawicki.http.HttpRequest;
  3. import com.izouma.nineth.domain.BaseEntity;
  4. import com.izouma.nineth.domain.BlindBoxItem;
  5. import com.izouma.nineth.domain.User;
  6. import com.izouma.nineth.enums.AuthorityName;
  7. import com.izouma.nineth.security.Authority;
  8. import com.izouma.nineth.utils.UrlUtils;
  9. import com.izouma.nineth.web.BaseController;
  10. import io.ipfs.api.IPFS;
  11. import io.ipfs.api.MerkleNode;
  12. import io.ipfs.api.NamedStreamable;
  13. import io.ipfs.cid.Cid;
  14. import io.ipfs.multihash.Multihash;
  15. import lombok.SneakyThrows;
  16. import net.coobird.thumbnailator.Thumbnails;
  17. import net.coobird.thumbnailator.name.Rename;
  18. import org.apache.commons.codec.EncoderException;
  19. import org.apache.commons.codec.net.URLCodec;
  20. import org.apache.commons.lang3.RandomStringUtils;
  21. import org.apache.commons.lang3.RandomUtils;
  22. import org.apache.commons.lang3.Range;
  23. import org.apache.commons.text.CaseUtils;
  24. import org.apache.http.client.utils.URLEncodedUtils;
  25. import org.apache.poi.util.TempFile;
  26. import org.bouncycastle.util.encoders.Base64;
  27. import org.bouncycastle.util.encoders.UrlBase64Encoder;
  28. import org.junit.Assert;
  29. import org.junit.Test;
  30. import org.libjpegturbo.turbojpeg.processor.api.ImageProcessException;
  31. import org.libjpegturbo.turbojpeg.processor.api.ImageProcessInfo;
  32. import org.libjpegturbo.turbojpeg.processor.api.ImageProcessor;
  33. import org.libjpegturbo.turbojpeg.processor.impl.ImageProcessorImpl;
  34. import org.libjpegturbo.turbojpeg.processor.utils.ImageProcessorUtils;
  35. import org.pngquant.PngQuant;
  36. import org.reflections.ReflectionUtils;
  37. import org.reflections.Reflections;
  38. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  39. import org.springframework.util.FileCopyUtils;
  40. import org.springframework.web.bind.annotation.GetMapping;
  41. import org.springframework.web.bind.annotation.PostMapping;
  42. import org.springframework.web.bind.annotation.RequestMapping;
  43. import org.springframework.web.bind.annotation.RestController;
  44. import javax.imageio.ImageIO;
  45. import java.awt.*;
  46. import java.awt.font.FontRenderContext;
  47. import java.awt.geom.AffineTransform;
  48. import java.io.File;
  49. import java.io.IOException;
  50. import java.lang.reflect.Method;
  51. import java.net.URL;
  52. import java.nio.charset.StandardCharsets;
  53. import java.nio.file.Files;
  54. import java.nio.file.Paths;
  55. import java.util.*;
  56. import java.util.List;
  57. import java.util.regex.Pattern;
  58. import static java.nio.file.StandardOpenOption.CREATE;
  59. import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
  60. public class CommonTest {
  61. @Test
  62. public void getGenericsClass() {
  63. List<User> data = new ArrayList<>();
  64. data.add(new User());
  65. System.out.println(data.get(0).getClass().getSimpleName());
  66. System.out.println(data.getClass());
  67. Reflections reflections = new Reflections(this.getClass().getPackage().getName() + ".domain");
  68. Set<Class<? extends BaseEntity>> allClasses = reflections.getSubTypesOf(BaseEntity.class);
  69. for (Class<? extends BaseEntity> allClass : allClasses) {
  70. System.out.println(allClass.getName());
  71. }
  72. }
  73. @Test
  74. public void getapis() {
  75. List<Map<String, String>> entities = new ArrayList<>();
  76. Reflections classReflections = new Reflections(this.getClass().getPackage().getName());
  77. Set<Class<? extends BaseController>> controllers = classReflections.getSubTypesOf(BaseController.class);
  78. Set<Class<? extends BaseController>> list = ReflectionUtils
  79. .getAll(controllers, ReflectionUtils.withAnnotation(RestController.class));
  80. System.out.println(list);
  81. for (Class<? extends BaseController> aClass : list) {
  82. RequestMapping requestMapping = aClass.getAnnotation(RequestMapping.class);
  83. String baseUrl = requestMapping.value()[0];
  84. for (Method method : ReflectionUtils.getMethods(aClass, ReflectionUtils.withAnnotation(GetMapping.class))) {
  85. GetMapping getMapping = method.getAnnotation(GetMapping.class);
  86. System.out.println(getMapping.value()[0]);
  87. }
  88. for (Method method : ReflectionUtils
  89. .getMethods(aClass, ReflectionUtils.withAnnotation(PostMapping.class))) {
  90. PostMapping postMapping = method.getAnnotation(PostMapping.class);
  91. System.out.println(postMapping.value()[0]);
  92. }
  93. }
  94. }
  95. @Test
  96. public void testCaseUtils() {
  97. System.out.println(CaseUtils.toCamelCase("test_Model", true, '_'));
  98. }
  99. @Test
  100. public void testMeasureText() throws IOException, FontFormatException {
  101. AffineTransform affinetransform = new AffineTransform();
  102. FontRenderContext frc = new FontRenderContext(affinetransform, true, true);
  103. Font font = Font.createFont(Font.TRUETYPE_FONT, this.getClass()
  104. .getResourceAsStream("/font/SourceHanSansCN-Normal.ttf"));
  105. System.out.println((int) (font.deriveFont(14f).getStringBounds("aaa", frc).getWidth()));
  106. }
  107. @Test
  108. public void testIdNoRegexp() {
  109. boolean b = Pattern
  110. .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");
  111. System.out.println(b);
  112. }
  113. @Test
  114. public void tesSms() throws IOException {
  115. Reflections reflections = new Reflections(this.getClass().getPackage().getName() + ".enums");
  116. Set<Class<? extends Enum>> entitySet = reflections.getSubTypesOf(Enum.class);
  117. StringBuilder idxJs = new StringBuilder();
  118. for (Class<? extends Enum> entity : entitySet) {
  119. idxJs.append("import ").append(entity.getSimpleName()).append(" from \"./").append(entity.getSimpleName())
  120. .append("\";\n");
  121. StringBuilder str = new StringBuilder("export default {\n");
  122. for (Enum enumConstant : entity.getEnumConstants()) {
  123. str.append(" ").append(enumConstant.name()).append(": \"").append(enumConstant.name())
  124. .append("\",\n");
  125. }
  126. str.append("}");
  127. Files.write(Paths.get(System.getProperty("user.dir"), "src", "main", "vue", "src", "constants", entity
  128. .getSimpleName() + ".js"), str.toString()
  129. .getBytes(StandardCharsets.UTF_8), CREATE, TRUNCATE_EXISTING);
  130. Files.write(Paths.get(System.getProperty("user.dir"), "src", "main", "zmj_mp", "src", "constants", entity
  131. .getSimpleName() + ".js"), str.toString()
  132. .getBytes(StandardCharsets.UTF_8), CREATE, TRUNCATE_EXISTING);
  133. }
  134. idxJs.append("export default {\n");
  135. for (Class<? extends Enum> entity : entitySet) {
  136. idxJs.append(" ").append(entity.getSimpleName()).append(": ").append(entity.getSimpleName())
  137. .append(",\n");
  138. }
  139. idxJs.append("}");
  140. System.out.println(idxJs.toString());
  141. Files.write(Paths
  142. .get(System.getProperty("user.dir"), "src", "main", "vue", "src", "constants", "index.js"), idxJs
  143. .toString().getBytes(StandardCharsets.UTF_8), CREATE, TRUNCATE_EXISTING);
  144. Files.write(Paths
  145. .get(System.getProperty("user.dir"), "src", "main", "zmj_mp", "src", "constants", "index.js"), idxJs
  146. .toString().getBytes(StandardCharsets.UTF_8), CREATE, TRUNCATE_EXISTING);
  147. }
  148. @Test
  149. public void gen() {
  150. System.out.println(RandomStringUtils.randomAlphabetic(32));
  151. }
  152. @Test
  153. public void password() {
  154. String password = new BCryptPasswordEncoder().encode("123456");
  155. System.out.println(password);
  156. }
  157. @SneakyThrows
  158. @Test
  159. public void pngquant() {
  160. PngQuant pngQuant = new PngQuant();
  161. ImageIO.write(pngQuant
  162. .getRemapped(ImageIO.read(new File("/Users/drew/Downloads/2021-11-01-18-07-04JCDfFxKb.HEIC"))),
  163. "jpg", new File("/Users/drew/Downloads/111.jpg"));
  164. }
  165. @SneakyThrows
  166. @Test
  167. public void mozjpeg() {
  168. File out = TempFile.createTempFile("kljasdlkhfasldg", ".jpg");
  169. ImageProcessor processor = new ImageProcessorImpl();
  170. ImageProcessInfo processInfo = ImageProcessInfo.fromMap(ImageProcessorUtils.compressImage(processor,
  171. new File("/Users/drew/Downloads/2020-09-08-17-07-21zwBhaHeQ.jpg"),
  172. out, 75));
  173. FileCopyUtils.copy(out, new File("/Users/drew/Desktop/111.jpg"));
  174. System.out.println(out);
  175. }
  176. @Test
  177. public void thumbnailator() throws IOException, ImageProcessException {
  178. Thumbnails.of(new File("/Users/drew/Downloads/2021-11-01-17-47-55zMwbSlgJ.jpeg"))
  179. .size(3000, 3000)
  180. .outputFormat("jpg")
  181. .toFile("/Users/drew/Desktop/1.jpg");
  182. PngQuant pngQuant = new PngQuant();
  183. ImageIO.write(pngQuant
  184. .getRemapped(ImageIO.read(new File("/Users/drew/Desktop/1.jpg"))),
  185. "jpg", new File("/Users/drew/Desktop/2.jpg"));
  186. ImageProcessor processor = new ImageProcessorImpl();
  187. ImageProcessInfo processInfo = ImageProcessInfo.fromMap(ImageProcessorUtils.compressImage(processor,
  188. new File("/Users/drew/Desktop/1.jpg"),
  189. new File("/Users/drew/Desktop/3.jpg"), 75));
  190. }
  191. @Test
  192. public void base64() {
  193. System.out.println(Base64.decode("e6e6vQJYhGmIkcA1pfnsipTovp10wJ"));
  194. }
  195. @Test
  196. public void resolveUrl() {
  197. try {
  198. System.out.println(new URLCodec().encode("http://www.baidu.com"));
  199. } catch (EncoderException e) {
  200. e.printStackTrace();
  201. }
  202. }
  203. @Test
  204. public void testIPFS() throws IOException {
  205. IPFS ipfs = new IPFS("121.40.132.44", 5001);
  206. HttpRequest request = HttpRequest.get("https://awesomeadmin.oss-cn-hangzhou.aliyuncs.com/image/2021-10-21-16-44-52kZqxuwhH.gif");
  207. File file = File.createTempFile("ipfs", ".tmp");
  208. request.receive(file);
  209. NamedStreamable.FileWrapper file1 = new NamedStreamable.FileWrapper(file);
  210. MerkleNode put = ipfs.add(file1).get(0);
  211. Multihash multihash = ipfs.pin.add(put.hash).get(0);
  212. System.out.println(put.hash.toBase58());
  213. System.out.println(multihash.toBase58());
  214. }
  215. @Test
  216. public void testWin() {
  217. List<BlindBoxItem> items = new ArrayList<>();
  218. items.add(BlindBoxItem.builder()
  219. .name("普通1")
  220. .total(100)
  221. .stock(100)
  222. .build());
  223. items.add(BlindBoxItem.builder()
  224. .name("普通2")
  225. .total(100)
  226. .stock(100)
  227. .build());
  228. items.add(BlindBoxItem.builder()
  229. .name("稀有1")
  230. .total(5)
  231. .stock(5)
  232. .rare(true)
  233. .build());
  234. items.add(BlindBoxItem.builder()
  235. .name("稀有2")
  236. .total(5)
  237. .stock(5)
  238. .rare(true)
  239. .build());
  240. for (int k = 0; k < items.stream().mapToInt(BlindBoxItem::getTotal).sum(); k++) {
  241. Map<BlindBoxItem, Range<Integer>> randomRange = new HashMap<>();
  242. int c = 0, sum = 0;
  243. for (BlindBoxItem item : items) {
  244. randomRange.put(item, Range.between(c, c + item.getStock()));
  245. c += item.getStock();
  246. sum += item.getStock();
  247. }
  248. boolean win = false;
  249. int retry = 0;
  250. BlindBoxItem winItem = null;
  251. while (winItem == null) {
  252. int rand = RandomUtils.nextInt(0, sum + 1);
  253. for (Map.Entry<BlindBoxItem, Range<Integer>> entry : randomRange.entrySet()) {
  254. BlindBoxItem item = entry.getKey();
  255. Range<Integer> range = entry.getValue();
  256. if (rand >= range.getMinimum() && rand < range.getMaximum()) {
  257. int total = items.stream().filter(i -> !i.isRare())
  258. .mapToInt(BlindBoxItem::getTotal).sum();
  259. int stock = items.stream().filter(i -> !i.isRare())
  260. .mapToInt(BlindBoxItem::getStock).sum();
  261. if (item.isRare()) {
  262. double nRate = stock / (double) total;
  263. double rRate = (item.getStock() - 1) / (double) item.getTotal();
  264. if (Math.abs(nRate - rRate) < (1 / (double) item.getTotal()) || retry > 1 || rRate == 0) {
  265. if (!(nRate > 0.1 && item.getStock() == 1)) {
  266. winItem = item;
  267. }
  268. } else {
  269. retry++;
  270. }
  271. } else {
  272. double nRate = (stock - 1) / (double) total;
  273. double rRate = item.getStock() / (double) item.getTotal();
  274. if (Math.abs(nRate - rRate) < 0.2 || retry > 1 || nRate == 0) {
  275. winItem = item;
  276. } else {
  277. retry++;
  278. }
  279. }
  280. }
  281. }
  282. }
  283. winItem.setStock(winItem.getStock() - 1);
  284. System.out.println(winItem.getName() + (winItem.isRare() ? "\t+++" : ""));
  285. }
  286. }
  287. }