All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.creekservice.api.test.util.coverage.CodeCoverage Maven / Gradle / Ivy

/*
 * Copyright 2022 Creek Contributors (https://github.com/creek-service)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.creekservice.api.test.util.coverage;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.nio.file.Path;
import java.util.Optional;

/** Util class for capturing code coverage specific JVM arguments. */
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public final class CodeCoverage {

    private CodeCoverage() {}

    /**
     * Finds the code coverage Java agent command line arg this JVM was started with, if any.
     *
     * 

Useful to set on child processes started by tests to ensure the code they execute is * included in coverage metrics. * * @return the Java agent command line arg containing potentially relative directories, or * empty. */ public static Optional codeCoverageCmdLineArg() { return codeCoverageCmdLineArg(ManagementFactory.getRuntimeMXBean(), Optional.empty()); } /** * Finds the code coverage Java agent command line arg this JVM was started with, if any, and * ensures the paths used in it are absolute. * *

Useful to set on child processes started by tests to ensure the code they execute is * included in coverage metrics. * * @param buildDir the path to the build directory. * @return the Java agent command line arg, or empty. */ public static Optional codeCoverageCmdLineArg(final Path buildDir) { return codeCoverageCmdLineArg(ManagementFactory.getRuntimeMXBean(), Optional.of(buildDir)); } // @VisibleForTesting static Optional codeCoverageCmdLineArg( final RuntimeMXBean runtimeMXBean, final Optional buildDir) { final Optional found = runtimeMXBean.getInputArguments().stream() .filter(arg -> arg.startsWith("-javaagent:")) .filter(arg -> arg.contains("org.jacoco.agent")) .reduce((first, second) -> first); return buildDir.map( dir -> found.map( arg -> arg.replaceAll( "build/", dir.toAbsolutePath() + "/"))) .orElse(found); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy