1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.battery.business;
 
import static org.junit.jupiter.api.Assertions.assertFalse;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
 
/** 防止业务实现重新依赖厂商、模拟器或传输基础设施。 */
class ModuleDependencyContractTest
{
    @Test
    void businessPomKeepsVendorImplementationsOutOfItsDependencies() throws Exception
    {
        Path pom = Path.of("pom.xml");
        if (!Files.exists(pom)) pom = Path.of("battery-business", "pom.xml");
        String text = Files.readString(pom);
        assertFalse(text.contains("battery-device-bts"));
        assertFalse(text.contains("battery-device-simulator"));
        assertFalse(text.contains("spring-boot-starter-data-redis"));
    }
 
    @Test void businessSourcesDependOnlyOnDeviceApi() throws Exception
    {
        Path sources = Path.of("src/main/java");
        try (var files = Files.walk(sources))
        {
            for (Path file : files.filter(path -> path.toString().endsWith(".java")).toList())
            {
                String source = Files.readString(file);
                for (String forbidden : java.util.List.of("com.battery.device.bts", "com.battery.device.simulator",
                        "com.ruoyi.system", "org.springframework.web", "org.springframework.data.redis", "SseEmitter"))
                    assertFalse(source.contains(forbidden), file + " contains " + forbidden);
            }
        }
    }
}