How zju-aces-ise/chatunitest-maven-plugin works
Project Type and Purpose
• This is a Java-based Maven plugin (“chatunitest-maven-plugin”) whose goal is to automatically generate, validate, and (if necessary) repair unit tests by orchestrating calls to an LLM (e.g., OpenAI GPT).
• It integrates into the standard Maven build lifecycle via custom Mojo classes (one per goal).
File Structure
Top-level:
• pom.xml → defines plugin packaging, dependencies (e.g., FreeMarker, HttpClient, Jacoco).
• src/main/java/zju/cst/aces/*.java → Mojo implementations (ClassTestMojo, MethodTestMojo, CleanMojo, ParseMojo, etc.), plus utility classes (XmlParser, MavenLogger, TelpaInit).
• src/main/resources/prompt* & prompt-cot* → FreeMarker templates for prompts.
• src/main/resources/config.properties → default plugin configuration.
• Embedded JAR (smartut-master-1.1.0.jar) and Jacoco integration zip for coverage.
A. Maven Runtime / CLI
– Invokes plugin goals (mvn chatunitest:class, mvn chatunitest:method, mvn chatunitest:project, etc.).
B. Mojo Layer
– A common abstract base can be shown if present.
C. Project Analyzer
– XmlParser parses pom.xml & project directories to discover target classes/methods.
D. Prompt Manager
– Loads .ftl templates from resources, merges parameters to form LLM requests.
E. LLM Client
– HTTP client module that dispatches chat/completion requests to OpenAI (or local model URL).
F. Test Generator & Repair Orchestrator
– Implements “Generate → Validate (compile & execute) → Fix” loop, potentially multi-threaded.
G. Test Executor & Coverage Collector
– Optionally runs generated code via embedded JUnit/Surefire, collects results and Jacoco coverage data.
H. File System
– Reads source code, writes temporary info (tmpOutput), writes generated tests (testOutput), backs up existing tests.
I. External Libraries
– FreeMarker, HttpClient, Jacoco, smartut JAR.
1. Maven CLI → Mojo Layer: “execute goal”
2. Mojo → Project Analyzer: “parse project structure”
3. Mojo → Prompt Manager: “load template + inject context”
4. Prompt Manager → LLM Client: “send prompt”
5. LLM Client → Prompt Manager: “return generated code snippet”
6. Prompt Manager → Test Generator: “hand off code”
7. Test Generator → File System: “write provisional test files”
8. Test Generator → Test Executor: “compile & run tests”
9. Test Executor → Test Generator: “pass/fail + error tokens”
10. Test Generator → LLM Client (“repair” loop) until success or maxRounds
11. Test Generator → File System: “write final tests to testOutput”
12. CoverageMojo → Jacoco Library: “instrument & collect coverage”
13. CleanMojo / CopyTestMojo / RestoreBackupMojo ↔ File System: “manage backup & restore”
Architectural Patterns / Principles
• Plugin architecture conforming to Maven’s Mojo SPI.
• Single‐responsibility: each Mojo handles one phase (generate, clean, copy, restore).
• Template-driven prompt generation (FreeMarker).
• Modular “Generate-Validate-Fix” pipeline, pluggable “phaseType” strategies (COVERUP, HITS, TELPA, etc.).
• Multi-threaded execution for parallel test generation.