OmgPaymentAttemptI18nContractTest.java 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package com.ruoyi.system.omgpay.service;
  2. import org.junit.jupiter.api.Test;
  3. import java.io.IOException;
  4. import java.io.Reader;
  5. import java.nio.charset.StandardCharsets;
  6. import java.nio.file.Files;
  7. import java.nio.file.Path;
  8. import java.nio.file.Paths;
  9. import java.util.List;
  10. import java.util.Properties;
  11. import static org.junit.jupiter.api.Assertions.assertNotNull;
  12. import static org.junit.jupiter.api.Assertions.assertTrue;
  13. class OmgPaymentAttemptI18nContractTest {
  14. private static final List<String> OMG_KEYS = List.of(
  15. "omg.payment.ddid.required",
  16. "omg.payment.merchantTradeNo.required",
  17. "omg.payment.storeId.required",
  18. "omg.payment.merchantId.required",
  19. "omg.payment.amount.invalid"
  20. );
  21. @Test
  22. void omgValidationKeysResolveInAllBundlesAndBoundaryIsSeparated() throws IOException {
  23. for (Path bundle : allBundles()) {
  24. Properties properties = loadProperties(bundle);
  25. for (String key : OMG_KEYS) {
  26. assertNotNull(properties.getProperty(key),
  27. () -> bundle.getFileName() + " should resolve " + key);
  28. }
  29. }
  30. for (Path bundle : malformedBoundaryBundles()) {
  31. String normalized = Files.readString(bundle, StandardCharsets.UTF_8).replace("\r\n", "\n");
  32. assertTrue(normalized.contains("\n\nomg.payment.ddid.required="),
  33. () -> bundle.getFileName() + " should start OMG keys on a separated block boundary");
  34. }
  35. }
  36. private static Properties loadProperties(Path bundle) throws IOException {
  37. Properties properties = new Properties();
  38. try (Reader reader = Files.newBufferedReader(bundle, StandardCharsets.UTF_8)) {
  39. properties.load(reader);
  40. }
  41. return properties;
  42. }
  43. private static List<Path> allBundles() {
  44. return List.of(
  45. bundle("messages.properties"),
  46. bundle("messages_en_US.properties"),
  47. bundle("messages_vi.properties"),
  48. bundle("messages_zh_CN.properties"),
  49. bundle("messages_zh_TW.properties")
  50. );
  51. }
  52. private static List<Path> malformedBoundaryBundles() {
  53. return List.of(
  54. bundle("messages.properties"),
  55. bundle("messages_zh_CN.properties"),
  56. bundle("messages_zh_TW.properties")
  57. );
  58. }
  59. private static Path bundle(String name) {
  60. return Paths.get("..", "ruoyi-admin", "src", "main", "resources", "i18n", name);
  61. }
  62. }