From 0d0bf99bee11a88cdaa374acb6b8bd182be886e9 Mon Sep 17 00:00:00 2001 From: Florian McKee <84742327+fmck3516@users.noreply.github.com> Date: Sat, 9 Dec 2023 20:00:29 -0600 Subject: [PATCH] more work on JavaDoc (#38) --- .../main/java/io/skippy/gradle/Profiler.java | 51 ------------------- .../java/io/skippy/gradle/SkippyPlugin.java | 10 ++-- .../gradle/coveragebuild/CoverageBuild.java | 13 +++++ .../CoverageBuildProjectConfigurer.java | 6 ++- .../coveragebuild/CoverageBuildRunner.java | 17 +++++-- ...va => CoverageBuildTasksAndArguments.java} | 26 ++++++++-- .../skippy/gradle/model/SkippifiedTest.java | 7 --- .../java/io/skippy/gradle/util/StopWatch.java | 20 ++++++++ 8 files changed, 79 insertions(+), 71 deletions(-) delete mode 100644 skippy-gradle/src/main/java/io/skippy/gradle/Profiler.java rename skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/{CoverageBuildArguments.java => CoverageBuildTasksAndArguments.java} (58%) create mode 100644 skippy-gradle/src/main/java/io/skippy/gradle/util/StopWatch.java diff --git a/skippy-gradle/src/main/java/io/skippy/gradle/Profiler.java b/skippy-gradle/src/main/java/io/skippy/gradle/Profiler.java deleted file mode 100644 index b6e1422..0000000 --- a/skippy-gradle/src/main/java/io/skippy/gradle/Profiler.java +++ /dev/null @@ -1,51 +0,0 @@ -package io.skippy.gradle; - -import org.gradle.api.logging.Logger; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicLong; -import java.util.function.Supplier; - -public class Profiler { - - private static Map data = new ConcurrentHashMap<>(); - - public static T profile(Class profiledClass, String profiledMethod, Supplier action) { - var identifier = profiledClass.getName() + "#" + profiledMethod; - if ( ! data.containsKey(identifier)) { - data.put(identifier, new AtomicLong(0)); - } - var then = System.currentTimeMillis(); - try { - return action.get(); - } finally { - var now = System.currentTimeMillis(); - data.get(identifier).addAndGet(now - then); - } - } - - public static void profile(Class profiledClass, String profiledMethod, Runnable action) { - profile(profiledClass, profiledMethod, () -> { - action.run(); - return null; - }); - } - - public static long stopWatch(Runnable action) { - var then = System.currentTimeMillis(); - try { - action.run(); - } finally { - var now = System.currentTimeMillis(); - return now - then; - } - } - - static void printResults(Logger logger) { - for (var entry : data.entrySet()) { - logger.info(entry.getKey() + " " + entry.getValue() + "ms"); - } - } - -} diff --git a/skippy-gradle/src/main/java/io/skippy/gradle/SkippyPlugin.java b/skippy-gradle/src/main/java/io/skippy/gradle/SkippyPlugin.java index a9f01d3..2e32929 100644 --- a/skippy-gradle/src/main/java/io/skippy/gradle/SkippyPlugin.java +++ b/skippy-gradle/src/main/java/io/skippy/gradle/SkippyPlugin.java @@ -24,14 +24,18 @@ import org.gradle.api.tasks.SourceSetContainer; /** - * Adds the {@code skippyClean} and {@code skippyAnalyze} tasks to a project. + * Adds Skippy support for Gradle: + *
    + *
  • Add the {@code skippyClean} and {@code skippyAnalyze} tasks if the build has been triggered "normally".
  • + *
  • If the build was triggered by the {@link AnalyzeTask} to capture coverage data for a skippified test, + * the plugin performs the necessary configuration changes.
  • + *
* * @author Florian McKee */ public final class SkippyPlugin implements org.gradle.api.Plugin { - - @Override + @Override public void apply(Project project) { project.getPlugins().apply(JavaPlugin.class); diff --git a/skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/CoverageBuild.java b/skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/CoverageBuild.java index 1d53a17..75699ad 100644 --- a/skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/CoverageBuild.java +++ b/skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/CoverageBuild.java @@ -21,6 +21,7 @@ import org.gradle.tooling.BuildLauncher; /** + * API that is used by plugins and task to detect, configure and run coverage builds. * * @author Florian McKee */ @@ -37,10 +38,22 @@ public static boolean isCoverageBuild(Project project) { return project.hasProperty("skippyCoverageBuild"); } + /** + * Configures the {@param project} for the execution of a coverage build for a skippified test. + * + * @param project + */ public static void configure(Project project) { CoverageBuildProjectConfigurer.configure(project); } + /** + * Runs a coverage build for the {@code skippifiedTest}. + * + * @param project + * @param buildLauncher + * @param skippifiedTest + */ public static void run(Project project, BuildLauncher buildLauncher, SkippifiedTest skippifiedTest) { CoverageBuildRunner.run(project, buildLauncher, skippifiedTest); } diff --git a/skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/CoverageBuildProjectConfigurer.java b/skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/CoverageBuildProjectConfigurer.java index 12c275e..6da1c5a 100644 --- a/skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/CoverageBuildProjectConfigurer.java +++ b/skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/CoverageBuildProjectConfigurer.java @@ -32,14 +32,16 @@ import static java.util.Arrays.asList; /** - * Configures a {@code project} for the execution of a coverage build. + * Configures a {@code project} for the execution of a coverage build for a skippified tests. * * @author Florian McKee */ final class CoverageBuildProjectConfigurer { /** - * Configures the {@code project} for the execution of a coverage build. + * Configures the {@code project} for the execution of a coverage build for a skippified test. + *

+ * Note: The skippified test is inferred from the build arguments. * * @param project */ diff --git a/skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/CoverageBuildRunner.java b/skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/CoverageBuildRunner.java index d37fcb0..9b93867 100644 --- a/skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/CoverageBuildRunner.java +++ b/skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/CoverageBuildRunner.java @@ -24,18 +24,27 @@ import java.io.ByteArrayOutputStream; -import static io.skippy.gradle.Profiler.stopWatch; +import static io.skippy.gradle.util.StopWatch.measureInMs; import static io.skippy.gradle.SkippyConstants.SKIPPY_DIRECTORY; import static java.util.stream.Collectors.joining; /** + * Runs coverage builds for skippified tests. + * * @author Florian McKee */ final class CoverageBuildRunner { + /** + * Runs a coverage build for the {@code skippifiedTest}. + * + * @param project + * @param buildLauncher + * @param skippifiedTest + */ static void run(Project project, BuildLauncher buildLauncher, SkippifiedTest skippifiedTest) { try { - var args = CoverageBuildArguments.forSkippifiedTest(skippifiedTest); + var args = CoverageBuildTasksAndArguments.forSkippifiedTest(skippifiedTest); var csvFile = project.getProjectDir().toPath().resolve(SKIPPY_DIRECTORY).resolve(skippifiedTest.getFullyQualifiedClassName() + ".csv"); var fqn = skippifiedTest.getFullyQualifiedClassName(); @@ -47,7 +56,7 @@ static void run(Project project, BuildLauncher buildLauncher, SkippifiedTest ski )); project.getLogger().lifecycle("%s".formatted(skippifiedTest.getFullyQualifiedClassName())); - long ms = stopWatch(() -> runInternal(project.getLogger(), buildLauncher, skippifiedTest)); + long ms = measureInMs(() -> runInternal(project.getLogger(), buildLauncher, skippifiedTest)); project.getLogger().lifecycle("%s".formatted(skippifiedTest.getFullyQualifiedClassName())); project.getLogger().lifecycle("%s Build executed in %sms.".formatted(skippifiedTest.getFullyQualifiedClassName(), ms)); } catch (Exception e) { @@ -58,7 +67,7 @@ static void run(Project project, BuildLauncher buildLauncher, SkippifiedTest ski private static void runInternal(Logger logger, BuildLauncher buildLauncher, SkippifiedTest skippifiedTest) { // configure tasks and arguments - var args = CoverageBuildArguments.forSkippifiedTest(skippifiedTest); + var args = CoverageBuildTasksAndArguments.forSkippifiedTest(skippifiedTest); buildLauncher.forTasks(args.getTasks().toArray(new String[0])); buildLauncher.addArguments(args.getArguments().toArray(new String[0])); diff --git a/skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/CoverageBuildArguments.java b/skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/CoverageBuildTasksAndArguments.java similarity index 58% rename from skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/CoverageBuildArguments.java rename to skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/CoverageBuildTasksAndArguments.java index 23c45fd..a57ad25 100644 --- a/skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/CoverageBuildArguments.java +++ b/skippy-gradle/src/main/java/io/skippy/gradle/coveragebuild/CoverageBuildTasksAndArguments.java @@ -23,19 +23,27 @@ import static java.util.Arrays.asList; /** + * Represents the tasks and build arguments used to run a coverage build for a skippified test. + * * @author Florian McKee */ -final class CoverageBuildArguments { +final class CoverageBuildTasksAndArguments { private final List tasks; private final List arguments; - private CoverageBuildArguments(List tasks, List arguments) { + private CoverageBuildTasksAndArguments(List tasks, List arguments) { this.tasks = tasks; this.arguments = arguments; } - static CoverageBuildArguments forSkippifiedTest(SkippifiedTest skippifiedTest) { + /** + * Creates a new instance for a {@link SkippifiedTest}. + * + * @param skippifiedTest + * @return a new instance for a {@link SkippifiedTest} + */ + static CoverageBuildTasksAndArguments forSkippifiedTest(SkippifiedTest skippifiedTest) { var tasks = asList( skippifiedTest.getTestTask(), "jacocoTestReport" @@ -45,13 +53,23 @@ static CoverageBuildArguments forSkippifiedTest(SkippifiedTest skippifiedTest) { "-PskippyClassFile=" + skippifiedTest.getRelativePath(), "-PskippyTestTask=" + skippifiedTest.getTestTask() ); - return new CoverageBuildArguments(tasks, arguments); + return new CoverageBuildTasksAndArguments(tasks, arguments); } + /** + * Returns the tasks that need to be executed for the coverage build for the skippified test. + * + * @return the tasks that need to be executed for the coverage build for the skippified test + */ List getTasks() { return tasks; } + /** + * Returns the arguments that need to be passed to the coverage build for the skippified test. + * + * @return the arguments that need to be passed to the coverage build for the skippified test + */ List getArguments() { return arguments; } diff --git a/skippy-gradle/src/main/java/io/skippy/gradle/model/SkippifiedTest.java b/skippy-gradle/src/main/java/io/skippy/gradle/model/SkippifiedTest.java index 7a0a379..507b54e 100644 --- a/skippy-gradle/src/main/java/io/skippy/gradle/model/SkippifiedTest.java +++ b/skippy-gradle/src/main/java/io/skippy/gradle/model/SkippifiedTest.java @@ -16,13 +16,7 @@ package io.skippy.gradle.model; -import org.gradle.api.Project; - import java.nio.file.Path; -import java.util.List; -import java.util.Optional; - -import static java.util.Arrays.asList; /** * Thin wrapper around a skippified test that stores @@ -54,7 +48,6 @@ public SkippifiedTest(ClassFile test, String testTask) { * * @return the fully qualified class name (e.g., com.example.FooTest) */ - public String getFullyQualifiedClassName() { return testClassFile.getFullyQualifiedClassName(); } diff --git a/skippy-gradle/src/main/java/io/skippy/gradle/util/StopWatch.java b/skippy-gradle/src/main/java/io/skippy/gradle/util/StopWatch.java new file mode 100644 index 0000000..569a81d --- /dev/null +++ b/skippy-gradle/src/main/java/io/skippy/gradle/util/StopWatch.java @@ -0,0 +1,20 @@ +package io.skippy.gradle.util; + +public class StopWatch { + + /** + * Measures the runtime of the {@code runnable} in milliseconds. + * + * @param runnable + * @return the runtime of the {@code runnable} in milliseconds + */ + public static long measureInMs(Runnable runnable) { + var then = System.currentTimeMillis(); + try { + runnable.run(); + } finally { + return System.currentTimeMillis() - then; + } + } + +}