diff --git a/CHANGELOG.md b/CHANGELOG.md index fa164d989..cdf0b1764 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,18 @@ We use [semantic versioning](http://semver.org/): # Next Release - [feature] add installer for Windows + +# 33.1.2 +- [fix] _teamscale-maven-plugin_: Revision and end commit could not be set via command line (user property) + +# 33.1.1 +- [fix] _agent_: NPE during agent startup probably due to missing read permissions in a shared folder + +# 33.1.0 +- [feature] _teamscale-maven-plugin_: Add new execution goal to batch convert .exec files into testwise coverage report. - [feature] _agent_: Extended list of packages excluded by default - [maintenance] _agent_: Removed HTTP upload (i.e. `upload-url` option) +- [feature] _agent_: New option `proxy-password-file` allows users to provide a file containing a proxy password # 33.0.0 - [feature] add installer for system-wide installation (see agent/MIGRATION.md for a migration guide) diff --git a/agent/README.md b/agent/README.md index 4433a0376..e606960e0 100644 --- a/agent/README.md +++ b/agent/README.md @@ -61,6 +61,7 @@ The following options are available: Use this to change the logging behaviour of the agent. Some sample configurations are provided with the agent in the `logging` folder, e.g. to enable debug logging or log directly to the console. (For details see path format section below) +- `proxy-password-file` (optional): path to a file that contains the password for a proxy server authentication. This file may only contain the password and nothing else. - `mode` (optional): which coverage collection mode to use. Can be either `normal` or `testwise` (Default is `normal`) - `debug` (optional): `true`, `false` or a path to which the logs should be written to. `true` if no explicit value given. This option turns on debug mode. The logs will be written to console and the given file path. If no file path is given, diff --git a/agent/src/main/java/com/teamscale/jacoco/agent/Agent.java b/agent/src/main/java/com/teamscale/jacoco/agent/Agent.java index 36bea2bf6..c738b2147 100644 --- a/agent/src/main/java/com/teamscale/jacoco/agent/Agent.java +++ b/agent/src/main/java/com/teamscale/jacoco/agent/Agent.java @@ -19,7 +19,7 @@ import java.util.List; import java.util.Properties; -import org.conqat.lib.commons.filesystem.FileSystemUtils; +import com.teamscale.jacoco.agent.util.FileSystemUtilsClone; import org.conqat.lib.commons.string.StringUtils; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.server.ServerProperties; @@ -84,7 +84,14 @@ private void retryUnsuccessfulUploads(AgentOptions options, IUploader uploader) // Default fallback outputPath = AgentUtils.getAgentDirectory().resolve("coverage"); } - List reuploadCandidates = FileSystemUtils.listFilesRecursively(outputPath.getParent().toFile(), + + Path parentPath = outputPath.getParent(); + if (parentPath == null) { + logger.error("The output path '{}' does not have a parent path. Canceling upload retry.", outputPath.toAbsolutePath()); + return; + } + + List reuploadCandidates = FileSystemUtilsClone.listFilesRecursively(parentPath.toFile(), filepath -> filepath.getName().endsWith(RETRY_UPLOAD_FILE_SUFFIX)); for (File file : reuploadCandidates) { reuploadCoverageFromPropertiesFile(file, uploader); diff --git a/agent/src/main/java/com/teamscale/jacoco/agent/AgentBase.java b/agent/src/main/java/com/teamscale/jacoco/agent/AgentBase.java index e3180898c..afe1cd3a8 100644 --- a/agent/src/main/java/com/teamscale/jacoco/agent/AgentBase.java +++ b/agent/src/main/java/com/teamscale/jacoco/agent/AgentBase.java @@ -1,7 +1,9 @@ package com.teamscale.jacoco.agent; +import com.teamscale.client.ProxySystemProperties; import com.teamscale.jacoco.agent.options.AgentOptions; import com.teamscale.jacoco.agent.util.LoggingUtils; +import org.conqat.lib.commons.filesystem.FileSystemUtils; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.servlet.ServletContextHandler; @@ -12,7 +14,9 @@ import org.jacoco.agent.rt.RT; import org.slf4j.Logger; +import java.io.IOException; import java.lang.management.ManagementFactory; +import java.nio.file.Path; /** * Base class for agent implementations. Handles logger shutdown, store creation and instantiation of the @@ -36,6 +40,7 @@ public abstract class AgentBase { /** Constructor. */ public AgentBase(AgentOptions options) throws IllegalStateException { this.options = options; + setProxyPasswordFromFile(options.getProxyPasswordPath()); try { controller = new JacocoRuntimeController(RT.getAgent()); } catch (IllegalStateException e) { @@ -55,6 +60,23 @@ public AgentBase(AgentOptions options) throws IllegalStateException { } } + /** Sets the proxy password JVM property from a file for both http and https. */ + private void setProxyPasswordFromFile(Path proxyPasswordFilePath) { + if (proxyPasswordFilePath == null) { + return; + } + try { + String proxyPassword = FileSystemUtils.readFileUTF8(proxyPasswordFilePath.toFile()).trim(); + new ProxySystemProperties(ProxySystemProperties.Protocol.HTTP).setProxyPassword(proxyPassword); + new ProxySystemProperties(ProxySystemProperties.Protocol.HTTPS).setProxyPassword(proxyPassword); + } catch (IOException e) { + logger.error( + "Unable to open file containing proxy password. Please make sure the file exists and the user has the permissions to read the file.", + e); + } + + } + /** * Lazily generated string representation of the command line arguments to print to the log. */ diff --git a/agent/src/main/java/com/teamscale/jacoco/agent/options/AgentOptions.java b/agent/src/main/java/com/teamscale/jacoco/agent/options/AgentOptions.java index f343f4a4a..d95145c4f 100644 --- a/agent/src/main/java/com/teamscale/jacoco/agent/options/AgentOptions.java +++ b/agent/src/main/java/com/teamscale/jacoco/agent/options/AgentOptions.java @@ -108,7 +108,10 @@ public class AgentOptions { * The directory to write the XML traces to. */ private Path outputDirectory; - + /** + * A path to the file that contains the password for the proxy authentication. + */ + /* package */ Path proxyPasswordPath; /** * Additional meta data files to upload together with the coverage XML. */ @@ -226,6 +229,10 @@ public String getOriginalOptionsString() { return originalOptionsString; } + public Path getProxyPasswordPath() { + return proxyPasswordPath; + } + /** * Remove parts of the API key for security reasons from the options string. String is used for logging purposes. *

diff --git a/agent/src/main/java/com/teamscale/jacoco/agent/options/AgentOptionsParser.java b/agent/src/main/java/com/teamscale/jacoco/agent/options/AgentOptionsParser.java index 98ff9d939..d16bdac3a 100644 --- a/agent/src/main/java/com/teamscale/jacoco/agent/options/AgentOptionsParser.java +++ b/agent/src/main/java/com/teamscale/jacoco/agent/options/AgentOptionsParser.java @@ -193,6 +193,9 @@ private boolean handleAgentOptions(AgentOptions options, String key, String valu case LOGGING_CONFIG_OPTION: options.loggingConfig = filePatternResolver.parsePath(key, value); return true; + case "proxy-password-file": + options.proxyPasswordPath = filePatternResolver.parsePath(key, value); + return true; case "interval": options.dumpIntervalInMinutes = parseInt(key, value); return true; diff --git a/agent/src/main/java/com/teamscale/jacoco/agent/util/FileSystemUtilsClone.java b/agent/src/main/java/com/teamscale/jacoco/agent/util/FileSystemUtilsClone.java new file mode 100644 index 000000000..a47f0fede --- /dev/null +++ b/agent/src/main/java/com/teamscale/jacoco/agent/util/FileSystemUtilsClone.java @@ -0,0 +1,51 @@ +package com.teamscale.jacoco.agent.util; + +import java.io.File; +import java.io.FileFilter; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +/** + * Clone of the file system utilities. + *

+ * This shall be removed with TS-37964. + */ +public class FileSystemUtilsClone { + + /** + * Clone of {@link com.teamscale.client.FileSystemUtils#listFilesRecursively(File, FileFilter)} + */ + public static List listFilesRecursively(File directory, FileFilter filter) { + if (directory == null || !directory.isDirectory()) { + return Collections.emptyList(); + } + List result = new ArrayList<>(); + listFilesRecursively(directory, result, filter); + return result; + } + + /** + * Clone of {@link com.teamscale.client.FileSystemUtils#listFilesRecursively(File, Collection, FileFilter)} + */ + private static void listFilesRecursively(File directory, Collection result, FileFilter filter) { + File[] files = directory.listFiles(); + if (files == null) { + // From the docs of `listFiles`: + // "If this abstract pathname does not denote a directory, then this method returns null." + // Based on this, it seems to be ok to just return here without throwing an exception. + return; + } + + for (File file : files) { + if (file.isDirectory()) { + listFilesRecursively(file, result, filter); + } + if (filter == null || filter.accept(file)) { + result.add(file); + } + } + } + +} \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index f797f08aa..e2a16c037 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { group = "com.teamscale" -val appVersion by extra("33.0.0") +val appVersion by extra("33.1.2") val snapshotVersion = appVersion + if (VersionUtils.isTaggedRelease()) "" else "-SNAPSHOT" diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index a4d94b179..9e9d1f8e0 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,5 @@ [versions] -jetty = "9.4.53.v20231009" +jetty = "9.4.54.v20240208" jersey = "2.41" jackson = "2.16.0" # When upgrading JaCoCo to a newer version make sure to @@ -9,8 +9,8 @@ jacoco = "0.8.11" # We need to stay on the 1.3.x release line as 1.4.x requires Java 11 logback = "1.3.14" retrofit = "2.9.0" -junit = "5.10.1" -junitPlatform = "1.10.1" +junit = "5.10.2" +junitPlatform = "1.10.2" okhttp = "4.12.0" mockito = "4.11.0" picocli = "4.7.5" @@ -49,12 +49,12 @@ okhttp-mockwebserver = { module = "com.squareup.okhttp3:mockwebserver", version. spark = { module = "com.sparkjava:spark-core", version = "2.9.4" } jcommander = { module = "com.beust:jcommander", version = "1.82" } teamscaleLibCommons = { module = "com.teamscale:teamscale-lib-commons", version = "9.4.1" } -commonsCodec = { module = "commons-codec:commons-codec", version = "1.16.0" } +commonsCodec = { module = "commons-codec:commons-codec", version = "1.16.1" } commonsLang = { module="org.apache.commons:commons-lang3", version="3.14.0" } commonsIo = { module="commons-io:commons-io", version="2.15.1" } -slf4j-api = { module = "org.slf4j:slf4j-api", version = "2.0.11" } -jgit = { module = "org.eclipse.jgit:org.eclipse.jgit", version = "6.8.0.202311291450-r" } -okio = { module = "com.squareup.okio:okio", version = "3.7.0" } +slf4j-api = { module = "org.slf4j:slf4j-api", version = "2.0.12" } +jgit = { module = "org.eclipse.jgit:org.eclipse.jgit", version = "6.9.0.202403050737-r" } +okio = { module = "com.squareup.okio:okio", version = "3.8.0" } picocli-core = { module = "info.picocli:picocli", version.ref = "picocli" } picocli-codegen = { module = "info.picocli:picocli-codegen", version.ref = "picocli" } @@ -67,7 +67,7 @@ junit-platform-engine = { module = "org.junit.platform:junit-platform-engine", v junit-platform-commons = { module = "org.junit.platform:junit-platform-commons", version.ref = "junitPlatform" } junit4 = { module = "junit:junit", version = "4.13.2" } -assertj = { module = "org.assertj:assertj-core", version = "3.25.2" } +assertj = { module = "org.assertj:assertj-core", version = "3.25.3" } jsonassert = { module = "org.skyscreamer:jsonassert", version = "1.5.1" } mockito-core = { module = "org.mockito:mockito-core", version.ref = "mockito" } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index e6aba2515..2ea3535dc 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-all.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/system-tests/tia-maven/maven-exec-file-project/.gitignore b/system-tests/tia-maven/maven-exec-file-project/.gitignore new file mode 100644 index 000000000..c5078494e --- /dev/null +++ b/system-tests/tia-maven/maven-exec-file-project/.gitignore @@ -0,0 +1,2 @@ +target +.idea diff --git a/system-tests/tia-maven/maven-exec-file-project/.mvn/wrapper/maven-wrapper.properties b/system-tests/tia-maven/maven-exec-file-project/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 000000000..e83fa6959 --- /dev/null +++ b/system-tests/tia-maven/maven-exec-file-project/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar diff --git a/system-tests/tia-maven/maven-exec-file-project/mvnw b/system-tests/tia-maven/maven-exec-file-project/mvnw new file mode 100755 index 000000000..5643201c7 --- /dev/null +++ b/system-tests/tia-maven/maven-exec-file-project/mvnw @@ -0,0 +1,316 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /usr/local/etc/mavenrc ] ; then + . /usr/local/etc/mavenrc + fi + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`\\unset -f command; \\command -v java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + $MAVEN_DEBUG_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" \ + "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/system-tests/tia-maven/maven-exec-file-project/mvnw.cmd b/system-tests/tia-maven/maven-exec-file-project/mvnw.cmd new file mode 100644 index 000000000..8a15b7f31 --- /dev/null +++ b/system-tests/tia-maven/maven-exec-file-project/mvnw.cmd @@ -0,0 +1,188 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %* +if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %* +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + +FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% ^ + %JVM_CONFIG_MAVEN_PROPS% ^ + %MAVEN_OPTS% ^ + %MAVEN_DEBUG_OPTS% ^ + -classpath %WRAPPER_JAR% ^ + "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^ + %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat" +if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%"=="on" pause + +if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE% + +cmd /C exit /B %ERROR_CODE% diff --git a/system-tests/tia-maven/maven-exec-file-project/pom.xml b/system-tests/tia-maven/maven-exec-file-project/pom.xml new file mode 100644 index 000000000..b82fd8702 --- /dev/null +++ b/system-tests/tia-maven/maven-exec-file-project/pom.xml @@ -0,0 +1,69 @@ + + + 4.0.0 + + org.example + tia-maven + 1.0-SNAPSHOT + + + 1.8 + 1.8 + ${env.TEAMSCALE_PORT} + ${env.AGENT_VERSION} + junit-jupiter + false + + + + + org.junit.jupiter + junit-jupiter-engine + 5.8.2 + test + + + + com.teamscale + impacted-test-engine + ${tia.agent.version} + test + + + + + + + com.teamscale + teamscale-maven-plugin + ${tia.agent.version} + + + + prepare-tia-unit-test + prepare-tia-integration-test + testwise-coverage-converter + + + + + MyPartition + exec-file + true + false + + *foo.* + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.1 + + + + + diff --git a/system-tests/tia-maven/maven-exec-file-project/src/main/java/foo/SUT.java b/system-tests/tia-maven/maven-exec-file-project/src/main/java/foo/SUT.java new file mode 100644 index 000000000..52533cf36 --- /dev/null +++ b/system-tests/tia-maven/maven-exec-file-project/src/main/java/foo/SUT.java @@ -0,0 +1,17 @@ +package foo; + +public class SUT { + + public void bla() { + System.out.println("bla"); + } + + public void foo() { + System.out.println("foo"); + } + + public void goo() { + System.out.println("goo"); + } + +} diff --git a/system-tests/tia-maven/maven-exec-file-project/src/test/java/foo/TwoUnitTest.java b/system-tests/tia-maven/maven-exec-file-project/src/test/java/foo/TwoUnitTest.java new file mode 100644 index 000000000..2cccb7f48 --- /dev/null +++ b/system-tests/tia-maven/maven-exec-file-project/src/test/java/foo/TwoUnitTest.java @@ -0,0 +1,19 @@ +package bar; + +import org.junit.jupiter.api.Test; +import foo.SUT; + +/** Dummy test class 2 */ +public class TwoUnitTest { + + /** Dummy test*/ + @Test + public void itBla() throws Exception { + new SUT().bla(); + } + /** Dummy test*/ + @Test + public void itFoo() throws Exception { + new SUT().foo(); + } +} diff --git a/system-tests/tia-maven/maven-exec-file-project/src/test/java/foo/UnitTest.java b/system-tests/tia-maven/maven-exec-file-project/src/test/java/foo/UnitTest.java new file mode 100644 index 000000000..f264e9eb4 --- /dev/null +++ b/system-tests/tia-maven/maven-exec-file-project/src/test/java/foo/UnitTest.java @@ -0,0 +1,19 @@ +package bar; + +import org.junit.jupiter.api.Test; +import foo.SUT; + +/** Dummy test class */ +public class UnitTest { + + /** Dummy test*/ + @Test + public void itBla() throws Exception { + new SUT().bla(); + } + /** Dummy test*/ + @Test + public void itFoo() throws Exception { + new SUT().foo(); + } +} diff --git a/system-tests/tia-maven/src/test/java/com/teamscale/tia/TiaMavenCoverageConverterTest.java b/system-tests/tia-maven/src/test/java/com/teamscale/tia/TiaMavenCoverageConverterTest.java new file mode 100644 index 000000000..7f8f1baaa --- /dev/null +++ b/system-tests/tia-maven/src/test/java/com/teamscale/tia/TiaMavenCoverageConverterTest.java @@ -0,0 +1,52 @@ +package com.teamscale.tia; + +import com.teamscale.client.JsonUtils; +import com.teamscale.report.testwise.model.ETestExecutionResult; +import com.teamscale.report.testwise.model.TestwiseCoverageReport; +import com.teamscale.test.commons.SystemTestUtils; +import org.conqat.lib.commons.filesystem.FileSystemUtils; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.nio.file.Paths; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * Tests the automatic conversion of .exec files to a testwise coverage report. + */ +public class TiaMavenCoverageConverterTest { + + /** + * The name of the project that the maven plugin is tested with. + */ + private static final String PROJECT_NAME = "maven-exec-file-project"; + + /** + * Starts a maven process with the reuseForks flag set to "false" and tiaMode + * "exec-file". Checks if the coverage can be converted to a testwise coverage + * report afterward. + */ + @Test + public void testMavenTia() throws Exception { + File workingDirectory = new File(PROJECT_NAME); + SystemTestUtils.runMavenTests(PROJECT_NAME); + File testwiseCoverage = new File( + Paths.get(workingDirectory.getAbsolutePath(), "target", "tia", "reports", "testwise-coverage-1.json") + .toUri()); + TestwiseCoverageReport testwiseCoverageReport = JsonUtils + .deserialize(FileSystemUtils.readFile(testwiseCoverage), TestwiseCoverageReport.class); + assertNotNull(testwiseCoverageReport); + assertAll(() -> { + assertThat(testwiseCoverageReport.tests).extracting(test -> test.uniformPath).contains( + "bar/TwoUnitTest/itBla()", "bar/TwoUnitTest/itFoo()", "bar/UnitTest/itBla()", + "bar/UnitTest/itFoo()"); + assertThat(testwiseCoverageReport.tests).extracting(test -> test.result) + .doesNotContain(ETestExecutionResult.FAILURE); + assertThat(testwiseCoverageReport.tests).extracting(SystemTestUtils::getCoverageString).containsExactly( + "SUT.java:3,6-7", "SUT.java:3,10-11", "", "SUT.java:3,6-7", "SUT.java:3,10-11", ""); + }); + } +} diff --git a/teamscale-client/build.gradle.kts b/teamscale-client/build.gradle.kts index ac8e381db..f0e2da8e7 100644 --- a/teamscale-client/build.gradle.kts +++ b/teamscale-client/build.gradle.kts @@ -1,21 +1,22 @@ plugins { - `java-library` - com.teamscale.`java-convention` - com.teamscale.coverage - com.teamscale.publish + `java-library` + com.teamscale.`java-convention` + com.teamscale.coverage + com.teamscale.publish } publishAs { - readableName.set("Teamscale Upload Client") - description.set( - "A tiny service client that only supports Teamscale's the external upload interface and impacted-tests service." - ) + readableName.set("Teamscale Upload Client") + description.set( + "A tiny service client that only supports Teamscale's the external upload interface and impacted-tests service." + ) } dependencies { - api(libs.retrofit.core) - implementation(libs.okhttp.core) - implementation(libs.commonsCodec) - implementation(libs.slf4j.api) - implementation(libs.retrofit.converter.jackson) + api(libs.retrofit.core) + implementation(libs.okhttp.core) + implementation(libs.commonsCodec) + implementation(libs.slf4j.api) + implementation(libs.retrofit.converter.jackson) + testImplementation(libs.okhttp.mockwebserver) } diff --git a/teamscale-client/src/main/java/com/teamscale/client/FileSystemUtils.java b/teamscale-client/src/main/java/com/teamscale/client/FileSystemUtils.java index 51c569fcc..f9748f5d1 100644 --- a/teamscale-client/src/main/java/com/teamscale/client/FileSystemUtils.java +++ b/teamscale-client/src/main/java/com/teamscale/client/FileSystemUtils.java @@ -86,7 +86,15 @@ public static String getFileExtension(File file) { * files and directories are included. */ private static void listFilesRecursively(File directory, Collection result, FileFilter filter) { - for (File file : directory.listFiles()) { + File[] files = directory.listFiles(); + if (files == null) { + // From the docs of `listFiles`: + // "If this abstract pathname does not denote a directory, then this method returns null." + // Based on this, it seems to be ok to just return here without throwing an exception. + return; + } + + for (File file : files) { if (file.isDirectory()) { listFilesRecursively(file, result, filter); } diff --git a/teamscale-client/src/main/java/com/teamscale/client/HttpUtils.java b/teamscale-client/src/main/java/com/teamscale/client/HttpUtils.java index 407f72b01..fbe0f1a50 100644 --- a/teamscale-client/src/main/java/com/teamscale/client/HttpUtils.java +++ b/teamscale-client/src/main/java/com/teamscale/client/HttpUtils.java @@ -1,5 +1,7 @@ package com.teamscale.client; +import okhttp3.Authenticator; +import okhttp3.Credentials; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -15,6 +17,8 @@ import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Proxy; import java.security.GeneralSecurityException; import java.security.SecureRandom; import java.security.cert.X509Certificate; @@ -39,6 +43,11 @@ public class HttpUtils { */ public static final Duration DEFAULT_WRITE_TIMEOUT = Duration.ofSeconds(60); + /** + * HTTP header used for authenticating against a proxy server + */ + public static final String PROXY_AUTHORIZATION_HTTP_HEADER = "Proxy-Authorization"; + /** Controls whether {@link OkHttpClient}s built with this class will validate SSL certificates. */ private static boolean shouldValidateSsl = true; @@ -66,6 +75,7 @@ public static Retrofit createRetrofit(Consumer retrofitBuilder OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder(); setTimeouts(httpClientBuilder, readTimeout, writeTimeout); setUpSslValidation(httpClientBuilder); + setUpProxyServer(httpClientBuilder); okHttpBuilderAction.accept(httpClientBuilder); Retrofit.Builder builder = new Retrofit.Builder().client(httpClientBuilder.build()); @@ -73,6 +83,60 @@ public static Retrofit createRetrofit(Consumer retrofitBuilder return builder.build(); } + /** + * Java and/or OkHttp do not pick up the http.proxy* and https.proxy* system properties reliably. We need to teach + * OkHttp to always pick them up. + *

+ * Sources: https://memorynotfound.com/configure-http-proxy-settings-java/ + * & + * https://stackoverflow.com/a/35567936 + */ + private static void setUpProxyServer(OkHttpClient.Builder httpClientBuilder) { + boolean setHttpsProxyWasSuccessful = setUpProxyServerForProtocol(ProxySystemProperties.Protocol.HTTPS, + httpClientBuilder); + if (!setHttpsProxyWasSuccessful) { + setUpProxyServerForProtocol(ProxySystemProperties.Protocol.HTTP, httpClientBuilder); + } + } + + private static boolean setUpProxyServerForProtocol(ProxySystemProperties.Protocol protocol, + OkHttpClient.Builder httpClientBuilder) { + + ProxySystemProperties proxySystemProperties = new ProxySystemProperties(protocol); + String proxyHost = proxySystemProperties.getProxyHost(); + int proxyPort = proxySystemProperties.getProxyPort(); + String proxyUser = proxySystemProperties.getProxyUser(); + String proxyPassword = proxySystemProperties.getProxyPassword(); + + if (proxySystemProperties.proxyServerIsSet()) { + useProxyServer(httpClientBuilder, proxyHost, proxyPort); + + if (proxySystemProperties.proxyAuthIsSet()) { + useProxyAuthenticator(httpClientBuilder, proxyUser, proxyPassword); + } + + return true; + } + return false; + + } + + private static void useProxyServer(OkHttpClient.Builder httpClientBuilder, String proxyHost, int proxyPort) { + httpClientBuilder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort))); + } + + private static void useProxyAuthenticator(OkHttpClient.Builder httpClientBuilder, String user, String password) { + Authenticator proxyAuthenticator = (route, response) -> { + String credential = Credentials.basic(user, password); + return response.request().newBuilder() + .header(PROXY_AUTHORIZATION_HTTP_HEADER, credential) + .build(); + }; + httpClientBuilder.proxyAuthenticator(proxyAuthenticator); + } + + /** * Sets sensible defaults for the {@link OkHttpClient}. */ diff --git a/teamscale-client/src/main/java/com/teamscale/client/ProxySystemProperties.java b/teamscale-client/src/main/java/com/teamscale/client/ProxySystemProperties.java new file mode 100644 index 000000000..403971c23 --- /dev/null +++ b/teamscale-client/src/main/java/com/teamscale/client/ProxySystemProperties.java @@ -0,0 +1,158 @@ +package com.teamscale.client; + +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Reads and writes Java system properties values for + *

+ * or the corresponding HTTPS counterpart (starting with https instead of http). + * These values set the proxy server and credentials that should be used later to reach Teamscale. + */ +public class ProxySystemProperties { + + private static final Logger LOGGER = LoggerFactory.getLogger(ProxySystemProperties.class); + + private static final String PROXY_HOST_SYSTEM_PROPERTY = ".proxyHost"; + private static final String PROXY_PORT_SYSTEM_PROPERTY = ".proxyPort"; + private static final String PROXY_USER_SYSTEM_PROPERTY = ".proxyUser"; + private static final String PROXY_PASSWORD_SYSTEM_PROPERTY = ".proxyPassword"; + + private final Protocol protocol; + + /** + * Indicates, whether the {@link ProxySystemProperties} should return values for the http.proxy* system properties + * or the https.proxy* ones + */ + public enum Protocol { + HTTP, + HTTPS; + + @Override + public String toString() { + return name().toLowerCase(); + } + } + + /** + * @param protocol Indicates, whether the {@link ProxySystemProperties} should use values for the http.proxy* system + * properties or the https.proxy* ones + */ + public ProxySystemProperties(Protocol protocol) { + this.protocol = protocol; + } + + /** + * Checks whether proxyHost and proxyPort are set + */ + public boolean proxyServerIsSet() { + return !StringUtils.isEmpty(getProxyHost()) && getProxyPort() > 0; + } + + /** + * Checks whether proxyUser and proxyPassword are set + */ + public boolean proxyAuthIsSet() { + return !StringUtils.isEmpty(getProxyUser()) && !StringUtils.isEmpty(getProxyPassword()); + } + + /** + * Read the http(s).proxyHost system variable + */ + public String getProxyHost() { + return System.getProperty(getProxyHostSystemPropertyName()); + } + + /** + * Read the http(s).proxyPort system variable + */ + public int getProxyPort() { + return parsePort(System.getProperty(getProxyPortSystemPropertyName())); + } + + /** + * Set the http(s).proxyHost system variable + */ + public void setProxyHost(String proxyHost) { + System.setProperty(getProxyHostSystemPropertyName(), proxyHost); + } + + @NotNull + private String getProxyHostSystemPropertyName() { + return protocol + PROXY_HOST_SYSTEM_PROPERTY; + } + + /** + * Set the http(s).proxyPort system variable + */ + public void setProxyPort(int proxyPort) { + setProxyPort(proxyPort + ""); + } + + /** + * Set the http(s).proxyPort system variable + */ + public void setProxyPort(String proxyPort) { + System.setProperty(getProxyPortSystemPropertyName(), proxyPort); + } + + @NotNull + private String getProxyPortSystemPropertyName() { + return protocol + PROXY_PORT_SYSTEM_PROPERTY; + } + + /** + * Get the http(s).proxyUser system variable + */ + public String getProxyUser() { + return System.getProperty(getProxyUserSystemPropertyName()); + } + + /** + * Set the http(s).proxyUser system variable + */ + public void setProxyUser(String proxyUser) { + System.setProperty(getProxyUserSystemPropertyName(), proxyUser); + } + + @NotNull + private String getProxyUserSystemPropertyName() { + return protocol + PROXY_USER_SYSTEM_PROPERTY; + } + + /** + * Get the http(s).proxyPassword system variable + */ + public String getProxyPassword() { + return System.getProperty(getProxyPasswordSystemPropertyName()); + } + + + /** + * Set the http(s).proxyPassword system variable + */ + public void setProxyPassword(String proxyPassword) { + System.setProperty(getProxyPasswordSystemPropertyName(), proxyPassword); + } + + @NotNull + private String getProxyPasswordSystemPropertyName() { + return protocol + PROXY_PASSWORD_SYSTEM_PROPERTY; + } + + private int parsePort(String portString) { + try { + return Integer.parseInt(portString); + } catch (NumberFormatException e) { + LOGGER.warn("Could not parse proxy port \"" + portString + + "\" set via \"" + getProxyPortSystemPropertyName() + "\""); + return -1; + } + } +} diff --git a/teamscale-client/src/main/java/com/teamscale/client/TeamscaleServiceGenerator.java b/teamscale-client/src/main/java/com/teamscale/client/TeamscaleServiceGenerator.java index 79778536b..4da346738 100644 --- a/teamscale-client/src/main/java/com/teamscale/client/TeamscaleServiceGenerator.java +++ b/teamscale-client/src/main/java/com/teamscale/client/TeamscaleServiceGenerator.java @@ -5,6 +5,7 @@ import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; +import org.jetbrains.annotations.NotNull; import retrofit2.Retrofit; import retrofit2.converter.jackson.JacksonConverterFactory; @@ -24,16 +25,9 @@ public class TeamscaleServiceGenerator { */ public static S createService(Class serviceClass, HttpUrl baseUrl, String username, String accessToken, Duration readTimeout, Duration writeTimeout, Interceptor... interceptors) { - Retrofit retrofit = HttpUtils.createRetrofit( - retrofitBuilder -> retrofitBuilder.baseUrl(baseUrl) - .addConverterFactory(JacksonConverterFactory.create(JsonUtils.OBJECT_MAPPER)), - okHttpBuilder -> addInterceptors(okHttpBuilder, interceptors) - .addInterceptor(HttpUtils.getBasicAuthInterceptor(username, accessToken)) - .addInterceptor(new AcceptJsonInterceptor()) - .addNetworkInterceptor(new CustomUserAgentInterceptor()) - , readTimeout, writeTimeout - ); - return retrofit.create(serviceClass); + return createServiceWithRequestLogging(serviceClass, baseUrl, username, accessToken, null, readTimeout, + writeTimeout, + interceptors); } /** @@ -46,11 +40,15 @@ public static S createServiceWithRequestLogging(Class serviceClass, HttpU Retrofit retrofit = HttpUtils.createRetrofit( retrofitBuilder -> retrofitBuilder.baseUrl(baseUrl) .addConverterFactory(JacksonConverterFactory.create(JsonUtils.OBJECT_MAPPER)), - okHttpBuilder -> addInterceptors(okHttpBuilder, interceptors) - .addInterceptor(HttpUtils.getBasicAuthInterceptor(username, accessToken)) - .addInterceptor(new AcceptJsonInterceptor()) - .addNetworkInterceptor(new CustomUserAgentInterceptor()) - .addInterceptor(new FileLoggingInterceptor(logfile)), + okHttpBuilder -> { + addInterceptors(okHttpBuilder, interceptors) + .addInterceptor(HttpUtils.getBasicAuthInterceptor(username, accessToken)) + .addInterceptor(new AcceptJsonInterceptor()) + .addNetworkInterceptor(new CustomUserAgentInterceptor()); + if (logfile != null) { + okHttpBuilder.addInterceptor(new FileLoggingInterceptor(logfile)); + } + }, readTimeout, writeTimeout ); return retrofit.create(serviceClass); @@ -69,6 +67,7 @@ private static OkHttpClient.Builder addInterceptors(OkHttpClient.Builder builder */ private static class AcceptJsonInterceptor implements Interceptor { + @NotNull @Override public Response intercept(Chain chain) throws IOException { Request newRequest = chain.request().newBuilder().header("Accept", "application/json").build(); @@ -80,6 +79,7 @@ public Response intercept(Chain chain) throws IOException { * Sets the custom user agent {@link #USER_AGENT} header on all requests. */ public static class CustomUserAgentInterceptor implements Interceptor { + @NotNull @Override public Response intercept(Chain chain) throws IOException { Request newRequest = chain.request().newBuilder().header("User-Agent", USER_AGENT).build(); diff --git a/teamscale-client/src/test/java/com/teamscale/client/TeamscaleServiceGeneratorProxyServerTest.java b/teamscale-client/src/test/java/com/teamscale/client/TeamscaleServiceGeneratorProxyServerTest.java new file mode 100644 index 000000000..07024df05 --- /dev/null +++ b/teamscale-client/src/test/java/com/teamscale/client/TeamscaleServiceGeneratorProxyServerTest.java @@ -0,0 +1,74 @@ +package com.teamscale.client; + +import okhttp3.HttpUrl; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.RecordedRequest; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Base64; + +import static com.teamscale.client.HttpUtils.PROXY_AUTHORIZATION_HTTP_HEADER; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests that our Retrofit + OkHttp client is using the Java proxy system properties ({@code http.proxy*}) if set + */ +class TeamscaleServiceGeneratorProxyServerTest { + + private MockWebServer mockProxyServer; + private final ProxySystemProperties proxySystemProperties = new ProxySystemProperties( + ProxySystemProperties.Protocol.HTTP); + + @BeforeEach + void setUp() throws IOException { + mockProxyServer = new MockWebServer(); + mockProxyServer.start(); + } + + @Test + void testProxyAuthentication() throws IOException, InterruptedException { + String proxyUser = "myProxyUser"; + String proxyPassword = "myProxyPassword"; + String base64EncodedBasicAuth = Base64.getEncoder().encodeToString((proxyUser + ":" + proxyPassword).getBytes( + StandardCharsets.UTF_8)); + proxySystemProperties.setProxyHost(mockProxyServer.getHostName()); + proxySystemProperties.setProxyPort(mockProxyServer.getPort()); + proxySystemProperties.setProxyUser(proxyUser); + proxySystemProperties.setProxyPassword(proxyPassword); + + ITeamscaleService service = TeamscaleServiceGenerator.createService(ITeamscaleService.class, + HttpUrl.parse("http://localhost:1337"), + "someUser", "someAccesstoken", HttpUtils.DEFAULT_READ_TIMEOUT, + HttpUtils.DEFAULT_WRITE_TIMEOUT); + + // First time Retrofit/OkHttp tires without proxy auth. + // When we return 407 Proxy Authentication Required, it retries with proxy authentication. + mockProxyServer.enqueue(new MockResponse().setResponseCode(407)); + mockProxyServer.enqueue(new MockResponse().setResponseCode(200)); + service.sendHeartbeat("", new ProfilerInfo(new ProcessInformation("", "", 0), null)).execute(); + + assertThat(mockProxyServer.getRequestCount()).isEqualTo(2); + + mockProxyServer.takeRequest(); // First request which doesn't have the proxy authentication set yet + RecordedRequest requestWithProxyAuth = mockProxyServer.takeRequest();// Request we are actually interested in + + assertThat(requestWithProxyAuth.getHeader(PROXY_AUTHORIZATION_HTTP_HEADER)).isEqualTo( + "Basic " + base64EncodedBasicAuth); + } + + @AfterEach + void tearDown() throws IOException { + proxySystemProperties.setProxyHost(""); + proxySystemProperties.setProxyPort(""); + proxySystemProperties.setProxyUser(""); + proxySystemProperties.setProxyPassword(""); + + mockProxyServer.shutdown(); + mockProxyServer.close(); + } +} \ No newline at end of file diff --git a/teamscale-gradle-plugin/src/test/resources/calculator_groovy/build.gradle b/teamscale-gradle-plugin/src/test/resources/calculator_groovy/build.gradle index ab40e0744..7534ad288 100644 --- a/teamscale-gradle-plugin/src/test/resources/calculator_groovy/build.gradle +++ b/teamscale-gradle-plugin/src/test/resources/calculator_groovy/build.gradle @@ -14,9 +14,9 @@ plugins { import com.teamscale.TestImpacted ext.junit4Version = '4.13.2' -ext.junitVintageVersion = '5.10.1' +ext.junitVintageVersion = '5.10.2' ext.junitPlatformVersion = '1.4.0' -ext.junitJupiterVersion = '5.10.1' +ext.junitJupiterVersion = '5.10.2' if (!project.hasProperty("withoutServerConfig")) { teamscale { diff --git a/teamscale-maven-plugin/pom.xml b/teamscale-maven-plugin/pom.xml index d569279e9..74956ffbc 100644 --- a/teamscale-maven-plugin/pom.xml +++ b/teamscale-maven-plugin/pom.xml @@ -65,7 +65,7 @@ org.junit.jupiter junit-jupiter-engine - 5.10.1 + 5.10.2 test @@ -94,7 +94,7 @@ org.eclipse.jgit org.eclipse.jgit - 6.8.0.202311291450-r + 6.9.0.202403050737-r com.teamscale @@ -184,7 +184,7 @@ org.apache.maven.plugins maven-gpg-plugin - 3.1.0 + 3.2.0 sign-artifacts diff --git a/teamscale-maven-plugin/src/main/java/com/teamscale/maven/TeamscaleMojoBase.java b/teamscale-maven-plugin/src/main/java/com/teamscale/maven/TeamscaleMojoBase.java index 78c808a2e..29b1cb6c2 100644 --- a/teamscale-maven-plugin/src/main/java/com/teamscale/maven/TeamscaleMojoBase.java +++ b/teamscale-maven-plugin/src/main/java/com/teamscale/maven/TeamscaleMojoBase.java @@ -51,14 +51,14 @@ public abstract class TeamscaleMojoBase extends AbstractMojo { *

* If no end commit is manually specified, the plugin will try to determine the currently checked out Git commit. */ - @Parameter + @Parameter(property = "teamscale.endCommit") public String endCommit; /** * You can optionally use this property to override the revision to which the coverage will be uploaded. * If no revision is manually specified, the plugin will try to determine the current git revision. */ - @Parameter + @Parameter(property = "teamscale.revision") public String revision; /** diff --git a/teamscale-maven-plugin/src/main/java/com/teamscale/maven/tia/TiaCoverageConvertMojo.java b/teamscale-maven-plugin/src/main/java/com/teamscale/maven/tia/TiaCoverageConvertMojo.java new file mode 100644 index 000000000..294699b66 --- /dev/null +++ b/teamscale-maven-plugin/src/main/java/com/teamscale/maven/tia/TiaCoverageConvertMojo.java @@ -0,0 +1,157 @@ +package com.teamscale.maven.tia; + +import com.google.common.base.Strings; +import com.teamscale.jacoco.agent.options.AgentOptionParseException; +import com.teamscale.jacoco.agent.options.ClasspathUtils; +import com.teamscale.jacoco.agent.options.FilePatternResolver; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.apache.maven.project.MavenProject; +import shadow.com.teamscale.client.TestDetails; +import shadow.com.teamscale.report.EDuplicateClassFileBehavior; +import shadow.com.teamscale.report.ReportUtils; +import shadow.com.teamscale.report.testwise.ETestArtifactFormat; +import shadow.com.teamscale.report.testwise.TestwiseCoverageReportWriter; +import shadow.com.teamscale.report.testwise.jacoco.JaCoCoTestwiseReportGenerator; +import shadow.com.teamscale.report.testwise.model.TestExecution; +import shadow.com.teamscale.report.testwise.model.factory.TestInfoFactory; +import shadow.com.teamscale.report.util.ClasspathWildcardIncludeFilter; +import shadow.com.teamscale.report.util.CommandLineLogger; +import shadow.com.teamscale.report.util.ILogger; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; + +/** + * Batch converts all created .exec file reports into a testwise coverage + * report. + */ +@Mojo(name = "testwise-coverage-converter", defaultPhase = LifecyclePhase.VERIFY, requiresDependencyResolution = ResolutionScope.RUNTIME) +public class TiaCoverageConvertMojo extends AbstractMojo { + /** + * Wildcard include patterns to apply during JaCoCo's traversal of class files. + */ + @Parameter(defaultValue = "**") + public String[] includes; + /** + * Wildcard exclude patterns to apply during JaCoCo's traversal of class files. + */ + @Parameter() + public String[] excludes; + + /** + * After how many tests the testwise coverage should be split into multiple + * reports (Default is 5000). + */ + @Parameter(defaultValue = "5000") + public int splitAfter; + + /** + * The project build directory (usually: {@code ./target}). Provided + * automatically by Maven. + */ + @Parameter(defaultValue = "${project.build.directory}") + public String projectBuildDir; + + /** + * The output directory of the testwise coverage reports. + */ + @Parameter() + public String outputFolder; + + /** + * The running Maven session. Provided automatically by Maven. + */ + @Parameter(defaultValue = "${session}") + public MavenSession session; + private final ILogger logger = new CommandLineLogger(); + + @Override + public void execute() throws MojoFailureException { + + List reportFileDirectories = new ArrayList<>(); + reportFileDirectories.add(Paths.get(projectBuildDir, "tia").toAbsolutePath().resolve("reports").toFile()); + List classFileDirectories; + if (Strings.isNullOrEmpty(outputFolder)) { + outputFolder = Paths.get(projectBuildDir, "tia", "reports").toString(); + } + try { + Files.createDirectories(Paths.get(outputFolder)); + classFileDirectories = getClassDirectoriesOrZips(projectBuildDir); + findSubprojectReportAndClassDirectories(reportFileDirectories, classFileDirectories); + } catch (IOException | AgentOptionParseException e) { + logger.error("Could not create testwise report generator. Aborting."); + throw new MojoFailureException(e); + } + logger.info("Generating the testwise coverage report"); + JaCoCoTestwiseReportGenerator generator = createJaCoCoTestwiseReportGenerator(classFileDirectories); + TestInfoFactory testInfoFactory = createTestInfoFactory(reportFileDirectories); + List jacocoExecutionDataList = ReportUtils.listFiles(ETestArtifactFormat.JACOCO, reportFileDirectories); + String reportFilePath = Paths.get(outputFolder, "testwise-coverage.json").toString(); + + try (TestwiseCoverageReportWriter coverageWriter = new TestwiseCoverageReportWriter(testInfoFactory, + new File(reportFilePath), splitAfter)) { + for (File executionDataFile : jacocoExecutionDataList) { + logger.info("Writing execution data for file: " + executionDataFile.getName()); + generator.convertAndConsume(executionDataFile, coverageWriter); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private void findSubprojectReportAndClassDirectories(List reportFiles, + List classFiles) throws AgentOptionParseException { + + for (MavenProject subProject : session.getTopLevelProject().getCollectedProjects()) { + String subprojectBuildDirectory = subProject.getBuild().getDirectory(); + reportFiles.add(Paths.get(subprojectBuildDirectory, "tia").toAbsolutePath().resolve("reports") + .toFile()); + classFiles.addAll(getClassDirectoriesOrZips(subprojectBuildDirectory)); + } + } + + private TestInfoFactory createTestInfoFactory(List reportFiles) throws MojoFailureException { + try { + List testDetails = ReportUtils.readObjects(ETestArtifactFormat.TEST_LIST, TestDetails[].class, + reportFiles); + List testExecutions = ReportUtils.readObjects(ETestArtifactFormat.TEST_EXECUTION, + TestExecution[].class, reportFiles); + logger.info("Writing report with " + testDetails.size() + " Details/" + testExecutions.size() + " Results"); + return new TestInfoFactory(testDetails, testExecutions); + } catch (IOException e) { + logger.error("Could not read test details from reports. Aborting."); + throw new MojoFailureException(e); + } + + } + + private JaCoCoTestwiseReportGenerator createJaCoCoTestwiseReportGenerator(List classFiles) { + String includes = null; + if (this.includes != null) { + includes = String.join(":", this.includes); + } + String excludes = null; + if (this.excludes != null) { + excludes = String.join(":", this.excludes); + } + return new JaCoCoTestwiseReportGenerator(classFiles, + new ClasspathWildcardIncludeFilter(includes, excludes), EDuplicateClassFileBehavior.WARN, logger); + } + + private List getClassDirectoriesOrZips(String projectBuildDir) throws AgentOptionParseException { + List classDirectoriesOrZips = new ArrayList<>(); + classDirectoriesOrZips.add(projectBuildDir); + return ClasspathUtils.resolveClasspathTextFiles("classes", new FilePatternResolver(new CommandLineLogger()), + classDirectoriesOrZips); + } +} diff --git a/teamscale-maven-plugin/src/main/java/com/teamscale/maven/tia/TiaMojoBase.java b/teamscale-maven-plugin/src/main/java/com/teamscale/maven/tia/TiaMojoBase.java index 19e767c52..cfb40364c 100644 --- a/teamscale-maven-plugin/src/main/java/com/teamscale/maven/tia/TiaMojoBase.java +++ b/teamscale-maven-plugin/src/main/java/com/teamscale/maven/tia/TiaMojoBase.java @@ -241,13 +241,12 @@ private void validateTestPluginConfiguration(PluginExecution execution) throws M if (parameterDom == null) { return; } - String value = parameterDom.getValue(); if (value != null && !value.equals("true")) { - throw new MojoFailureException( - "You configured the " + getTestPluginArtifact() + " plugin to not reuse forks via the reuseForks configuration parameter." + - " This is not supported when performing Test Impact analysis as it prevents properly recording testwise coverage." + - " Please enable fork reuse when running Test Impact analysis."); + getLog().warn( + "You configured surefire to not reuse forks." + + " This has been shown to lead to performance decreases in combination with the Teamscale Maven Plugin." + + " If you notice performance problems, please have a look at our troubleshooting section for possible solutions: https://docs.teamscale.com/howto/providing-testwise-coverage/#troubleshooting."); } } diff --git a/teamscale-maven-plugin/src/main/java/com/teamscale/maven/upload/CoverageUploadMojo.java b/teamscale-maven-plugin/src/main/java/com/teamscale/maven/upload/CoverageUploadMojo.java index 6efc2ab76..e361ed8ab 100644 --- a/teamscale-maven-plugin/src/main/java/com/teamscale/maven/upload/CoverageUploadMojo.java +++ b/teamscale-maven-plugin/src/main/java/com/teamscale/maven/upload/CoverageUploadMojo.java @@ -1,5 +1,6 @@ package com.teamscale.maven.upload; +import com.google.common.base.Strings; import com.teamscale.maven.GitCommit; import com.teamscale.maven.TeamscaleMojoBase; import org.apache.commons.lang3.StringUtils; @@ -19,18 +20,22 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; /** - * Run this goal after the Jacoco report generation to upload them to a configured Teamscale instance. - * The configuration can be specified in the root Maven project. - * Offers the following functionality: + * Run this goal after the Jacoco report generation to upload them to a + * configured Teamscale instance. The configuration can be specified in the root + * Maven project. Offers the following functionality: *

    - *
  1. Validate Jacoco Maven plugin configuration
  2. - *
  3. Locate and upload all reports in one session
  4. + *
  5. Validate Jacoco Maven plugin configuration
  6. + *
  7. Locate and upload all reports in one session
  8. *
- * @see Jacoco Plugin + * + * @see Jacoco + * Plugin */ @Mojo(name = "upload-coverage", defaultPhase = LifecyclePhase.VERIFY, requiresDependencyResolution = ResolutionScope.RUNTIME, threadSafe = true) @@ -41,38 +46,67 @@ public class CoverageUploadMojo extends TeamscaleMojoBase { private static final String COVERAGE_UPLOAD_MESSAGE = "Coverage upload via Teamscale Maven plugin"; /** - * The Teamscale partition name to which unit test reports will be uploaded + * The Teamscale partition name to which unit test reports will be uploaded. */ @Parameter(property = "teamscale.unitTestPartition", defaultValue = "Unit Tests") public String unitTestPartition; /** - * The Teamscale partition name to which integration test reports will be uploaded + * The Teamscale partition name to which integration test reports will be + * uploaded. */ @Parameter(property = "teamscale.integrationTestPartition", defaultValue = "Integration Tests") public String integrationTestPartition; /** - * The Teamscale partition name to which aggregated test reports will be uploaded + * The Teamscale partition name to which aggregated test reports will be + * uploaded. */ @Parameter(property = "teamscale.aggregatedTestPartition", defaultValue = "Aggregated Tests") public String aggregatedTestPartition; + /** + * The output directory of the testwise coverage reports. Should only be set if + * testwise coverage is uploaded. + */ + @Parameter() + public String testwiseCoverageOutputFolder; + + /** + * The Teamscale partition name to which testwise coverage reports will be + * uploaded. + */ + @Parameter(property = "teamscale.testwisePartition", defaultValue = "Testwise Coverage") + public String testwisePartition; + /** * Paths to all reports generated by subprojects - * @see report + * + * @see report */ private final List reportGoalOutputFiles = new ArrayList<>(); /** * Paths to all integration reports generated by subprojects - * @see report-integration + * + * @see report-integration */ private final List reportIntegrationGoalOutputFiles = new ArrayList<>(); + /** + * The project build directory (usually: {@code ./target}). Provided + * automatically by Maven. + */ + @Parameter(defaultValue = "${project.build.directory}") + public String projectBuildDir; + /** * Paths to all aggregated reports generated by subprojects - * @see report-aggregate + * + * @see report-aggregate */ private final List reportAggregateGoalOutputFiles = new ArrayList<>(); @@ -101,53 +135,86 @@ public void execute() throws MojoFailureException { getLog().debug("Uploading coverage reports"); uploadCoverageReports(); } catch (IOException e) { - throw new MojoFailureException("Uploading coverage reports failed. No upload to Teamscale was performed. You can try again or upload the XML coverage reports manually, see https://docs.teamscale.com/reference/ui/project/project/#manual-report-upload", e); + throw new MojoFailureException( + "Uploading coverage reports failed. No upload to Teamscale was performed. You can try again or upload the XML coverage reports manually, see https://docs.teamscale.com/reference/ui/project/project/#manual-report-upload", + e); } } /** - * Check that Jacoco is set up correctly and read any custom settings that may have been set - * @throws MojoFailureException If Jacoco is not set up correctly + * Check that Jacoco is set up correctly and read any custom settings that may + * have been set + * + * @throws MojoFailureException + * If Jacoco is not set up correctly */ private void parseJacocoConfiguration() throws MojoFailureException { collectReportOutputDirectory(session.getTopLevelProject(), "report", "jacoco", reportGoalOutputFiles); - collectReportOutputDirectory(session.getTopLevelProject(), "report-integration", "jacoco-it", reportIntegrationGoalOutputFiles); - collectReportOutputDirectory(session.getTopLevelProject(), "report-aggregate", "jacoco-aggregate", reportAggregateGoalOutputFiles); - getLog().debug(String.format("Found %d sub-modules", session.getTopLevelProject().getCollectedProjects().size())); + collectReportOutputDirectory(session.getTopLevelProject(), "report-integration", "jacoco-it", + reportIntegrationGoalOutputFiles); + collectReportOutputDirectory(session.getTopLevelProject(), "report-aggregate", "jacoco-aggregate", + reportAggregateGoalOutputFiles); + getLog().debug( + String.format("Found %d sub-modules", session.getTopLevelProject().getCollectedProjects().size())); for (MavenProject subProject : session.getTopLevelProject().getCollectedProjects()) { collectReportOutputDirectory(subProject, "report", "jacoco", reportGoalOutputFiles); - collectReportOutputDirectory(subProject, "report-integration", "jacoco-it", reportIntegrationGoalOutputFiles); - collectReportOutputDirectory(subProject, "report-aggregate", "jacoco-aggregate", reportAggregateGoalOutputFiles); + collectReportOutputDirectory(subProject, "report-integration", "jacoco-it", + reportIntegrationGoalOutputFiles); + collectReportOutputDirectory(subProject, "report-aggregate", "jacoco-aggregate", + reportAggregateGoalOutputFiles); } } /** - * Collect the file locations in which JaCoCo is configured to save the XML coverage reports - * @param project The project - * @param reportGoal The JaCoCo report goal - * @param jacocoDirectory The name of the directory, matching the JaCoCo goal + * Collect the file locations in which JaCoCo is configured to save the XML + * coverage reports + * + * @param project + * The project + * @param reportGoal + * The JaCoCo report goal + * @param jacocoDirectory + * The name of the directory, matching the JaCoCo goal * @see Goals */ - private void collectReportOutputDirectory(MavenProject project, String reportGoal, String jacocoDirectory, List reportOutputFiles) throws MojoFailureException { + private void collectReportOutputDirectory(MavenProject project, String reportGoal, String jacocoDirectory, + List reportOutputFiles) throws MojoFailureException { Path defaultOutputDirectory = Paths.get(project.getReporting().getOutputDirectory()); // If a Dom is null it means the execution goal uses default parameters which work correctly - Xpp3Dom reportConfigurationDom = getJacocoGoalExecutionConfiguration(project,reportGoal); + Xpp3Dom reportConfigurationDom = getJacocoGoalExecutionConfiguration(project, reportGoal); String errorMessage = "Skipping upload for %s as %s is not configured to produce XML reports for goal %s. See https://www.jacoco.org/jacoco/trunk/doc/report-mojo.html#formats"; if (!validateReportFormat(reportConfigurationDom)) { - throw new MojoFailureException(String.format(errorMessage, project.getName(), JACOCO_PLUGIN_NAME, jacocoDirectory)); + throw new MojoFailureException( + String.format(errorMessage, project.getName(), JACOCO_PLUGIN_NAME, jacocoDirectory)); } - Path resolvedOutputDir = getCustomOutputDirectory(reportConfigurationDom).orElse(defaultOutputDirectory.resolve(jacocoDirectory).resolve("jacoco.xml")); - getLog().debug(String.format("Adding possible report location: %s", resolvedOutputDir)); - reportOutputFiles.add(resolvedOutputDir); + Path resolvedCoverageFile = getCustomOutputDirectory(reportConfigurationDom).orElse(defaultOutputDirectory) + .resolve(jacocoDirectory).resolve("jacoco.xml"); + getLog().debug(String.format("Adding possible report location: %s", resolvedCoverageFile)); + reportOutputFiles.add(resolvedCoverageFile); } private void uploadCoverageReports() throws IOException { - uploadCoverage(reportGoalOutputFiles, unitTestPartition); - uploadCoverage(reportIntegrationGoalOutputFiles, integrationTestPartition); - uploadCoverage(reportAggregateGoalOutputFiles, aggregatedTestPartition); + Path reportPath; + if (Strings.isNullOrEmpty(testwiseCoverageOutputFolder)) { + reportPath = Paths.get(projectBuildDir, "tia", "reports"); + } else { + reportPath = Paths.get(testwiseCoverageOutputFolder); + } + File[] files = reportPath.toFile().listFiles(File::isFile); + if (files != null) { + List testwiseCoverageFiles = Arrays.asList(files); + getLog().debug("Uploading testwise coverage to partition " + testwisePartition); + uploadCoverage(testwiseCoverageFiles.stream().map(File::toPath).collect(Collectors.toList()), + testwisePartition, EReportFormat.TESTWISE_COVERAGE); + + } + uploadCoverage(reportGoalOutputFiles, unitTestPartition, EReportFormat.JACOCO); + uploadCoverage(reportIntegrationGoalOutputFiles, integrationTestPartition, EReportFormat.JACOCO); + uploadCoverage(reportAggregateGoalOutputFiles, aggregatedTestPartition, EReportFormat.JACOCO); } - private void uploadCoverage(List reportOutputFiles, String partition) throws IOException { + private void uploadCoverage(List reportOutputFiles, String partition, EReportFormat format) + throws IOException { List reports = new ArrayList<>(); getLog().debug(String.format("Scanning through %d locations for %s...", reportOutputFiles.size(), partition)); for (Path reportPath : reportOutputFiles) { @@ -163,8 +230,11 @@ private void uploadCoverage(List reportOutputFiles, String partition) thro reports.add(report); } if (!reports.isEmpty()) { - getLog().info(String.format("Uploading %d Jacoco report for project %s to %s", reports.size(), projectId, partition)); - teamscaleClient.uploadReports(EReportFormat.JACOCO, reports, CommitDescriptor.parse(resolvedCommit), revision, partition, COVERAGE_UPLOAD_MESSAGE); + getLog().info( + String.format("Uploading %d report for project %s to %s", reports.size(), projectId, + partition)); + teamscaleClient.uploadReports(format, reports, CommitDescriptor.parse(resolvedCommit), revision, partition, + COVERAGE_UPLOAD_MESSAGE); } else { getLog().info(String.format("Found no valid reports for %s", partition)); } @@ -172,7 +242,9 @@ private void uploadCoverage(List reportOutputFiles, String partition) thro /** * Validates that a configuration Dom is set up to generate XML reports - * @param configurationDom The configuration Dom of a goal execution + * + * @param configurationDom + * The configuration Dom of a goal execution */ private boolean validateReportFormat(Xpp3Dom configurationDom) { if (configurationDom == null || configurationDom.getChild("formats") == null) {