| 1 | package org.egothor.methodatlas.command; | |
| 2 | ||
| 3 | import java.io.IOException; | |
| 4 | import java.io.PrintWriter; | |
| 5 | import java.nio.file.Path; | |
| 6 | import java.nio.file.Paths; | |
| 7 | import java.util.ArrayList; | |
| 8 | import java.util.LinkedHashMap; | |
| 9 | import java.util.List; | |
| 10 | import java.util.Map; | |
| 11 | import java.util.logging.Level; | |
| 12 | import java.util.logging.Logger; | |
| 13 | import java.util.stream.Collectors; | |
| 14 | ||
| 15 | import org.egothor.methodatlas.CliConfig; | |
| 16 | import org.egothor.methodatlas.ManualMode; | |
| 17 | import org.egothor.methodatlas.ai.AiSuggestionException; | |
| 18 | import org.egothor.methodatlas.ai.ManualPrepareEngine; | |
| 19 | import org.egothor.methodatlas.ai.PromptBuilder; | |
| 20 | import org.egothor.methodatlas.api.DiscoveredMethod; | |
| 21 | import org.egothor.methodatlas.api.TestDiscovery; | |
| 22 | import org.egothor.methodatlas.api.TestDiscoveryConfig; | |
| 23 | ||
| 24 | /** | |
| 25 | * CLI command handler for the {@code -manual-prepare} mode. | |
| 26 | * | |
| 27 | * <p> | |
| 28 | * Discovers test classes via the configured {@link TestDiscovery} providers, | |
| 29 | * then writes one work file per class to the prepare work directory. | |
| 30 | * No CSV output is produced; only progress lines are written to the | |
| 31 | * supplied {@link PrintWriter}. | |
| 32 | * </p> | |
| 33 | * | |
| 34 | * @see org.egothor.methodatlas.ai.ManualPrepareEngine | |
| 35 | * @see org.egothor.methodatlas.ai.ManualConsumeEngine | |
| 36 | */ | |
| 37 | public final class ManualPrepareCommand implements Command { | |
| 38 | ||
| 39 | private static final Logger LOG = Logger.getLogger(ManualPrepareCommand.class.getName()); | |
| 40 | ||
| 41 | private final ManualMode.Prepare prepare; | |
| 42 | private final CliConfig cliConfig; | |
| 43 | private final TestDiscoveryConfig discoveryConfig; | |
| 44 | ||
| 45 | /** | |
| 46 | * Creates a new manual-prepare command. | |
| 47 | * | |
| 48 | * @param prepare manual prepare mode configuration (work and response dirs) | |
| 49 | * @param cliConfig full parsed CLI configuration | |
| 50 | * @param discoveryConfig discovery configuration forwarded to providers | |
| 51 | */ | |
| 52 | public ManualPrepareCommand(ManualMode.Prepare prepare, CliConfig cliConfig, | |
| 53 | TestDiscoveryConfig discoveryConfig) { | |
| 54 | this.prepare = prepare; | |
| 55 | this.cliConfig = cliConfig; | |
| 56 | this.discoveryConfig = discoveryConfig; | |
| 57 | } | |
| 58 | ||
| 59 | /** | |
| 60 | * Discovers test classes, writes work files, and reports progress. | |
| 61 | * | |
| 62 | * @param out writer for progress and summary output | |
| 63 | * @return {@code 0} if all files were processed successfully, {@code 1} if any | |
| 64 | * provider encountered a processing error | |
| 65 | * @throws IOException if traversing a file tree fails | |
| 66 | */ | |
| 67 | @Override | |
| 68 | @SuppressWarnings("PMD.CloseResource") // providers closed by closeAll() in the finally block | |
| 69 | public int execute(PrintWriter out) throws IOException { | |
| 70 | ManualPrepareEngine engine; | |
| 71 | try { | |
| 72 | engine = new ManualPrepareEngine(prepare.workDir(), prepare.responseDir(), cliConfig.aiOptions()); | |
| 73 | } catch (AiSuggestionException e) { | |
| 74 | throw new IllegalStateException("Failed to initialize manual prepare engine", e); | |
| 75 | } | |
| 76 | ||
| 77 |
2
1. execute : removed conditional - replaced equality check with false → SURVIVED 2. execute : removed conditional - replaced equality check with true → KILLED |
List<Path> roots = cliConfig.paths().isEmpty() ? List.of(Paths.get(".")) : cliConfig.paths(); |
| 78 | List<TestDiscovery> providers = CommandSupport.loadProviders(discoveryConfig); | |
| 79 | boolean hadErrors = false; | |
| 80 | int prepared = 0; | |
| 81 | ||
| 82 | try { | |
| 83 | for (Path root : roots) { | |
| 84 | List<DiscoveredMethod> allMethods = new ArrayList<>(); // NOPMD – intentionally one list per scan root | |
| 85 | for (TestDiscovery provider : providers) { | |
| 86 |
1
1. execute : removed call to java/util/stream/Stream::forEach → KILLED |
provider.discover(root).forEach(allMethods::add); |
| 87 |
2
1. execute : removed conditional - replaced equality check with false → KILLED 2. execute : removed conditional - replaced equality check with true → KILLED |
if (provider.hadErrors()) { |
| 88 | hadErrors = true; | |
| 89 | } | |
| 90 | } | |
| 91 | ||
| 92 | // Group by FQCN so each class produces one work file. | |
| 93 | Map<String, List<DiscoveredMethod>> byClass = allMethods.stream() | |
| 94 | .collect(Collectors.groupingBy(DiscoveredMethod::fqcn, | |
| 95 | LinkedHashMap::new, Collectors.toList())); | |
| 96 | ||
| 97 | for (Map.Entry<String, List<DiscoveredMethod>> entry : byClass.entrySet()) { | |
| 98 | String fqcn = entry.getKey(); | |
| 99 | List<DiscoveredMethod> classMethods = entry.getValue(); | |
| 100 | String classSource = classMethods.get(0).sourceContent().get().orElse(null); | |
| 101 |
2
1. execute : removed conditional - replaced equality check with false → SURVIVED 2. execute : removed conditional - replaced equality check with true → KILLED |
if (classSource == null) { |
| 102 | continue; | |
| 103 | } | |
| 104 | String fileStem = classMethods.get(0).fileStem(); | |
| 105 | List<PromptBuilder.TargetMethod> targetMethods = classMethods.stream() | |
| 106 | .map(CommandSupport::toTargetMethod).toList(); | |
| 107 | try { | |
| 108 | Path workFile = engine.prepare(fileStem, fqcn, classSource, targetMethods); | |
| 109 |
1
1. execute : removed call to java/io/PrintWriter::println → KILLED |
out.println("Prepared: " + workFile); |
| 110 |
1
1. execute : Changed increment from 1 to -1 → SURVIVED |
prepared++; |
| 111 | } catch (AiSuggestionException e) { | |
| 112 | if (LOG.isLoggable(Level.WARNING)) { | |
| 113 | LOG.log(Level.WARNING, "Failed to prepare work file for " + fqcn, e); | |
| 114 | } | |
| 115 | } | |
| 116 | } | |
| 117 | } | |
| 118 | } finally { | |
| 119 |
1
1. execute : removed call to org/egothor/methodatlas/command/CommandSupport::closeAll → SURVIVED |
CommandSupport.closeAll(providers); |
| 120 | } | |
| 121 | ||
| 122 |
1
1. execute : removed call to java/io/PrintWriter::println → KILLED |
out.println("Manual prepare complete. Wrote " + prepared + " work file(s) to " + prepare.workDir() |
| 123 | + " (response stubs in " + prepare.responseDir() + ")"); | |
| 124 |
2
1. execute : removed conditional - replaced equality check with false → KILLED 2. execute : removed conditional - replaced equality check with true → KILLED |
return hadErrors ? 1 : 0; |
| 125 | } | |
| 126 | } | |
Mutations | ||
| 77 |
1.1 2.2 |
|
| 86 |
1.1 |
|
| 87 |
1.1 2.2 |
|
| 101 |
1.1 2.2 |
|
| 109 |
1.1 |
|
| 110 |
1.1 |
|
| 119 |
1.1 |
|
| 122 |
1.1 |
|
| 124 |
1.1 2.2 |