LegacyPipeTableRetirementTest.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package com.zksy.watersupply;
  2. import org.junit.jupiter.api.Test;
  3. import java.io.IOException;
  4. import java.nio.charset.StandardCharsets;
  5. import java.nio.file.Files;
  6. import java.nio.file.Path;
  7. import java.nio.file.Paths;
  8. import java.util.ArrayList;
  9. import java.util.Collections;
  10. import java.util.List;
  11. import java.util.Set;
  12. import java.util.stream.Collectors;
  13. import java.util.stream.Stream;
  14. import static org.junit.jupiter.api.Assertions.assertTrue;
  15. /**
  16. * Gogs #53:旧 water_pipe_info 链路必须彻底退场。
  17. *
  18. * <p>管网设施的权威数据源是统一表 {@code pipe_network_base}({@code network_type = '2'});
  19. * 旧 WaterPipeInfo 模块、旧列名与指向旧表的测试数据都不得再出现在代码库中。
  20. * 历史迁移脚本属于既成事实,按票面口径单列白名单,且白名单只允许这一个文件。</p>
  21. */
  22. class LegacyPipeTableRetirementTest {
  23. /** 历史迁移脚本:记录“曾经 ALTER 旧表”这一事实,不随旧模块一起删除。 */
  24. private static final Set<String> HISTORICAL_MIGRATION_ALLOWLIST = Set.of("sql/water_supply_facility_gis.sql");
  25. /** 旧表与旧模块标识:代码、Mapper、SQL 中都不得再出现。 */
  26. private static final List<String> LEGACY_TABLE_TOKENS = List.of(
  27. "water_pipe_info",
  28. "WaterPipeInfo",
  29. "WaterPipeExportDTO");
  30. /** 旧表列名:管网口径统一到 pipe_network_base 后不应再被引用。 */
  31. private static final List<String> LEGACY_COLUMN_TOKENS = List.of(
  32. "pipe_length",
  33. "pipe_diameter",
  34. "pipe_material",
  35. "laying_year",
  36. "pressure_rating");
  37. private static final List<String> SCANNED_ROOTS = List.of("zksy-admin", "zksy-system", "zksy-common", "zksy-framework", "sql");
  38. @Test
  39. void codeAndTestDataNoLongerReferenceTheRetiredPipeTable() throws IOException {
  40. Path repoRoot = repoRoot();
  41. List<String> offenders = new ArrayList<>();
  42. for (Path file : scannedFiles(repoRoot)) {
  43. String relative = repoRoot.relativize(file).toString().replace('\\', '/');
  44. // 本测试自身声明了这些 token,不作为命中项。
  45. if (relative.endsWith("LegacyPipeTableRetirementTest.java")) continue;
  46. if (HISTORICAL_MIGRATION_ALLOWLIST.contains(relative)) continue;
  47. String content = Files.readString(file, StandardCharsets.UTF_8);
  48. for (String token : LEGACY_TABLE_TOKENS) {
  49. if (content.contains(token)) offenders.add(relative + " -> " + token);
  50. }
  51. for (String token : LEGACY_COLUMN_TOKENS) {
  52. if (content.contains(token)) offenders.add(relative + " -> " + token);
  53. }
  54. }
  55. assertTrue(offenders.isEmpty(),
  56. "管网设施已统一到 pipe_network_base(network_type='2'),以下位置仍在引用旧表或旧列:\n"
  57. + String.join("\n", offenders));
  58. }
  59. @Test
  60. void historicalMigrationAllowlistStaysMinimal() throws IOException {
  61. Path repoRoot = repoRoot();
  62. List<String> actual = Files.walk(repoRoot.resolve("sql"))
  63. .filter(Files::isRegularFile)
  64. .filter(path -> path.toString().endsWith(".sql"))
  65. .filter(path -> {
  66. try {
  67. return Files.readString(path, StandardCharsets.UTF_8).contains("water_pipe_info");
  68. } catch (IOException e) {
  69. throw new IllegalStateException(e);
  70. }
  71. })
  72. .map(path -> repoRoot.relativize(path).toString().replace('\\', '/'))
  73. .collect(Collectors.toList());
  74. Collections.sort(actual);
  75. assertTrue(actual.equals(new ArrayList<>(HISTORICAL_MIGRATION_ALLOWLIST)),
  76. "仅允许历史迁移脚本保留 water_pipe_info,实际为:" + actual);
  77. }
  78. @Test
  79. void pipeTestDataIsSeededIntoTheUnifiedNetworkTable() throws IOException {
  80. Path seed = repoRoot().resolve("sql/water_supply_facility_test_data.sql");
  81. String sql = Files.readString(seed, StandardCharsets.UTF_8);
  82. assertTrue(sql.contains("app_user.pipe_network_base"),
  83. "管网测试数据必须写入统一表 app_user.pipe_network_base");
  84. assertTrue(sql.contains("'2'"),
  85. "写入 pipe_network_base 的管网设施必须显式标注 network_type = '2'(供水设施)");
  86. }
  87. private static List<Path> scannedFiles(Path repoRoot) throws IOException {
  88. List<Path> files = new ArrayList<>();
  89. for (String root : SCANNED_ROOTS) {
  90. Path start = repoRoot.resolve(root);
  91. if (!Files.exists(start)) continue;
  92. try (Stream<Path> walk = Files.walk(start)) {
  93. walk.filter(Files::isRegularFile)
  94. .filter(LegacyPipeTableRetirementTest::isScannedFile)
  95. .forEach(files::add);
  96. }
  97. }
  98. return files;
  99. }
  100. private static boolean isScannedFile(Path path) {
  101. String name = path.getFileName().toString();
  102. if (name.endsWith(".java") || name.endsWith(".sql") || name.endsWith(".yml") || name.endsWith(".yaml")) return true;
  103. if (!name.endsWith(".xml")) return false;
  104. String normalized = path.toString().replace('\\', '/');
  105. return normalized.contains("/resources/") || normalized.contains("/src/test/");
  106. }
  107. private static Path repoRoot() {
  108. Path current = Paths.get(System.getProperty("user.dir")).toAbsolutePath();
  109. while (current != null) {
  110. if (Files.exists(current.resolve("zksy-system")) && Files.exists(current.resolve("zksy-admin"))) {
  111. return current;
  112. }
  113. current = current.getParent();
  114. }
  115. throw new IllegalStateException("无法定位 pipe-network-service 仓库根目录");
  116. }
  117. }