Files
crawler-plugin/backend-java/src/test/java/com/nanri/aiimage/common/MovedComponentScanTest.java
T
huangzd1997 dbe35dfcb5 refactor(phase-4): 解散万能 util——similarasin/util 与 common/util 按域归位
similarasin/util 7 支:
- 域内件(SimilarAsinImageEmbedder/PuzzleImageMerger/SimilarAsinLogSupport/
  BoundedImageCache)并入既有 service/support 先例;
- 通用 Excel 件(ExcelCellImageWriter/WpsCellImageWriter)归 common/excel;
- 仅测试用的 SimilarAsinPerfFixture 移出生产源到 src/test。

common/util 13 支解散为四个关注点包:excel(读/写/单元格图片)、
text(BrandLabel/SecretMasking/UserMessages)、task(行过滤/分组传播/判死文案/
删除台账/锁跳过留痕)、support(LRU/下载头/线程池)。老 util 目录不再存在。

纯粹的包搬迁与 import 重写(diff 219 增 / 217 删),零行为改动:
搬迁前后由 SimilarAsinCreateTaskSnapshotTest 的建单入口 golden 对撞相等;
新增 MovedComponentScanTest 钉住三枚 @Component 仍被根包扫描发现、老包零残留。
2026-10-23 00:59:53 +08:00

64 lines
3.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.nanri.aiimage.common;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Phase 4 归位的装配面护栏:@Component 换了包,组件扫描必须还能发现它。
*
* <p>为什么单立一条:本机没有可起的隔离 MySQL/Redis(Phase 3 已登记同一缺口),
* {@code mvn -o spring-boot:run} 只能连线上库,不能为这次判据去碰它。这里改用
* Spring 自己的扫描器按应用根包 {@code com.nanri.aiimage} 实扫一遍——
* 扫描根没变、Bean 简单名没变,被搬动的三枚组件必须仍然在候选集里;
* 同时钉住「解散后的包不得再有组件」这条,防止老包被重新写回。
*/
class MovedComponentScanTest {
private static final Set<String> MOVED_COMPONENTS = Set.of(
"com.nanri.aiimage.common.task.LockSkipWitness",
"com.nanri.aiimage.modules.similarasin.service.support.PuzzleImageMerger",
"com.nanri.aiimage.modules.similarasin.service.support.SimilarAsinImageEmbedder");
private static Set<String> componentsUnder(String basePackage) {
ClassPathScanningCandidateComponentProvider provider =
new ClassPathScanningCandidateComponentProvider(false) {
@Override
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
return beanDefinition.getMetadata().isIndependent();
}
};
provider.addIncludeFilter(new AnnotationTypeFilter(Component.class));
List<BeanDefinition> found = provider.findCandidateComponents(basePackage).stream().toList();
return found.stream().map(BeanDefinition::getBeanClassName).collect(Collectors.toSet());
}
@Test
void movedComponentsAreStillDiscoveredFromTheApplicationRootPackage() {
Set<String> discovered = componentsUnder("com.nanri.aiimage");
assertFalse(discovered.isEmpty(), "组件扫描一个候选都没找到——扫描根或类路径接错了");
for (String bean : MOVED_COMPONENTS) {
assertTrue(discovered.contains(bean),
"归位后组件扫描抓不到 " + bean + "(Bean 装配会缺件)");
}
}
@Test
void dissolvedPackagesHaveNoComponentsLeft() {
assertTrue(componentsUnder("com.nanri.aiimage.common.util").isEmpty(),
"common.util 已解散,老包里不许再有 @Component");
assertTrue(componentsUnder("com.nanri.aiimage.modules.similarasin.util").isEmpty(),
"modules.similarasin.util 已解散,老包里不许再有 @Component");
}
}