# zju-aces-ise/chatunitest-maven-plugin architecture

> Architecture diagram of the GitHub repository zju-aces-ise/chatunitest-maven-plugin, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zju-aces-ise/chatunitest-maven-plugin
- Repository: https://github.com/zju-aces-ise/chatunitest-maven-plugin

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Maven Runtime Layer
    MavenCLI["Maven Runtime / CLI"]:::external
    click MavenCLI "https://github.com/zju-aces-ise/chatunitest-maven-plugin/blob/main/pom.xml"

    %% Plugin Goals
    subgraph "Plugin Goals"
        ClassTestMojo["ClassTestMojo"]:::plugin
        MethodTestMojo["MethodTestMojo"]:::plugin
        CoverageMojo["CoverageMojo"]:::plugin
        CleanMojo["CleanMojo"]:::plugin
        CopyTestMojo["CopyTestMojo"]:::plugin
        RestoreBackupMojo["RestoreBackupMojo"]:::plugin
        ParseMojo["ParseMojo"]:::plugin
        ProjectTestMojo["ProjectTestMojo"]:::plugin
        MavenLogger["MavenLogger"]:::plugin
        TelpaInit["TelpaInit"]:::plugin
    end
    click ClassTestMojo "https://github.com/zju-aces-ise/chatunitest-maven-plugin/blob/main/src/main/java/zju/cst/aces/ClassTestMojo.java"
    click MethodTestMojo "https://github.com/zju-aces-ise/chatunitest-maven-plugin/blob/main/src/main/java/zju/cst/aces/MethodTestMojo.java"
    click CoverageMojo "https://github.com/zju-aces-ise/chatunitest-maven-plugin/blob/main/src/main/java/zju/cst/aces/CoverageMojo.java"
    click CleanMojo "https://github.com/zju-aces-ise/chatunitest-maven-plugin/blob/main/src/main/java/zju/cst/aces/CleanMojo.java"
    click CopyTestMojo "https://github.com/zju-aces-ise/chatunitest-maven-plugin/blob/main/src/main/java/zju/cst/aces/CopyTestMojo.java"
    click RestoreBackupMojo "https://github.com/zju-aces-ise/chatunitest-maven-plugin/blob/main/src/main/java/zju/cst/aces/RestoreBackupMojo.java"
    click ParseMojo "https://github.com/zju-aces-ise/chatunitest-maven-plugin/blob/main/src/main/java/zju/cst/aces/ParseMojo.java"
    click ProjectTestMojo "https://github.com/zju-aces-ise/chatunitest-maven-plugin/blob/main/src/main/java/zju/cst/aces/ProjectTestMojo.java"
    click MavenLogger "https://github.com/zju-aces-ise/chatunitest-maven-plugin/blob/main/src/main/java/zju/cst/aces/logger/MavenLogger.java"
    click TelpaInit "https://github.com/zju-aces-ise/chatunitest-maven-plugin/blob/main/src/main/java/zju/cst/aces/TelpaInit.java"

    %% Project Analyzer
    XmlParser["XmlParser"]:::plugin
    click XmlParser "https://github.com/zju-aces-ise/chatunitest-maven-plugin/blob/main/src/main/java/zju/cst/aces/XmlParser.java"

    %% Prompt Manager
    subgraph "Prompt Manager"
        Templates[".ftl Templates"]:::third
    end
    click Templates "https://github.com/zju-aces-ise/chatunitest-maven-plugin/tree/main/src/main/resources/prompt"
    click Templates "https://github.com/zju-aces-ise/chatunitest-maven-plugin/tree/main/src/main/resources/prompt-cot"

    %% LLM Client
    LLMClient["LLM Client"]:::external

    %% Test Generator & Repair
    TestGenerator["Test Generator & Repair"]:::plugin

    %% Test Executor & Coverage Collector
    subgraph "Execution & Coverage"
        TestExecutor["Test Executor"]:::plugin
        Jacoco["Jacoco Instrumentation"]:::third
    end
    click Jacoco "https://github.com/zju-aces-ise/chatunitest-maven-plugin/blob/main/src/main/resources/smartut-master-1.1.0.jar"

    %% File System
    FileSystem["File System"]:::fs

    %% Configuration
    Config["config.properties"]:::third
    click Config "https://github.com/zju-aces-ise/chatunitest-maven-plugin/blob/main/src/main/resources/config.properties"

    %% Relationships
    MavenCLI -->|"execute goal"| ClassTestMojo
    MavenCLI -->|"execute goal"| MethodTestMojo
    MavenCLI -->|"execute goal"| ProjectTestMojo
    MavenCLI -->|"execute goal"| CoverageMojo
    MavenCLI -->|"execute goal"| CleanMojo
    MavenCLI -->|"execute goal"| CopyTestMojo
    MavenCLI -->|"execute goal"| RestoreBackupMojo

    ClassTestMojo -->|"parseProject()"| XmlParser
    MethodTestMojo -->|"parseProject()"| XmlParser
    ProjectTestMojo -->|"parseProject()"| XmlParser
    ParseMojo -->|"parseProject()"| XmlParser

    ClassTestMojo -->|"loadTemplate()"| Templates
    MethodTestMojo -->|"loadTemplate()"| Templates
    ProjectTestMojo -->|"loadTemplate()"| Templates

    Templates -->|"sendPrompt()"| LLMClient
    LLMClient -->|"response()"| Templates

    Templates -->|"provideCode"| TestGenerator
    TestGenerator -->|"write provisional tests"| FileSystem
    TestGenerator -->|"compile & run"| TestExecutor
    TestExecutor -->|"results"| TestGenerator
    TestGenerator -->|"repairLoop()"| LLMClient
    TestGenerator -->|"write final tests"| FileSystem

    CoverageMojo -->|"instrument & collect"| Jacoco
    CoverageMojo -->|"write data"| FileSystem

    CleanMojo -->|"manage backup"| FileSystem
    CopyTestMojo -->|"manage backup"| FileSystem
    RestoreBackupMojo -->|"manage backup"| FileSystem

    %% Configuration usage
    ClassTestMojo -->|"load config"| Config
    MethodTestMojo -->|"load config"| Config

    %% Styles
    classDef plugin fill:#D0E8FF,stroke:#0366d6,stroke-width:1.5px
    classDef external fill:#C6F6D5,stroke:#22863a,stroke-width:1.5px
    classDef fs fill:#FFEDD5,stroke:#d15700,stroke-width:1.5px
    classDef third fill:#E2E8F0,stroke:#6a737d,stroke-width:1.5px
```
