diff --git a/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/build/LookupBuildInfoCommand.java b/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/build/LookupBuildInfoCommand.java index e584f430c..90f8e8e20 100644 --- a/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/build/LookupBuildInfoCommand.java +++ b/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/build/LookupBuildInfoCommand.java @@ -124,7 +124,7 @@ public class LookupBuildInfoCommand implements Runnable { BootstrapMavenContext mavenContext; // Variable so can be overridden by the tests. - String CACHE_PATH = "/v2/cache/rebuild-default/0"; + String cachePath = "/v2/cache/rebuild-default/0"; @Override public void run() { @@ -271,7 +271,7 @@ protected void onUpdate(String taskName, int cmp, int totalWork, int pcnt, Durat if (artifact != null && (buildRecipeInfo == null || buildRecipeInfo.getJavaVersion() == null)) { Log.infof("Lookup Build JDK for artifact %s", artifact); - var optBuildJdk = getBuildJdk(cacheUrl + CACHE_PATH, artifact); + var optBuildJdk = getBuildJdk(cacheUrl + cachePath, artifact); if (optBuildJdk.isPresent()) { var buildJdk = optBuildJdk.get(); Log.infof("Setting build JDK to %s for artifact %s", buildJdk.version(), artifact); @@ -507,7 +507,8 @@ private static void handleGradleBuild(InvocationBuilder builder, Path gradleFile var specifiedJavaVersion = GradleUtils.getSpecifiedJavaVersion(gradleFile); if (!specifiedJavaVersion.isEmpty()) { - builder.minJavaVersion(new JavaVersion(specifiedJavaVersion)); + Log.infof("Detected Java version %s in Gradle build file %s", specifiedJavaVersion, gradleFile); + MavenJavaVersionDiscovery.filterJavaVersions(builder, specifiedJavaVersion); } ArrayList inv = new ArrayList<>(GradleUtils.getGradleArgs(gradleFile)); if (skipTests) { diff --git a/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/build/gradle/GradleUtils.java b/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/build/gradle/GradleUtils.java index 5e01f59ba..6f225701b 100644 --- a/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/build/gradle/GradleUtils.java +++ b/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/build/gradle/GradleUtils.java @@ -16,7 +16,6 @@ * Utility class for Gradle. */ public final class GradleUtils { - /** * Format for applying plugins. */ @@ -33,11 +32,12 @@ public final class GradleUtils { */ public static final String MAVEN_PUBLISH_PLUGIN = String.format(PLUGIN_FORMAT, "maven-publish", "maven-publish"); - static final String BUILD_GRADLE = "build.gradle"; + public static final String BUILD_GRADLE = "build.gradle"; - static final String BUILD_GRADLE_KTS = "build.gradle.kts"; + public static final String BUILD_GRADLE_KTS = "build.gradle.kts"; private static final String GRADLE_WRAPPER_PROPERTIES = "gradle/wrapper/gradle-wrapper.properties"; + private static final String OLD_GRADLE_WRAPPER_PROPERTIES = ".gradle-wrapper/gradle-wrapper.properties"; private static final String DISTRIBUTION_URL_KEY = "distributionUrl"; @@ -61,6 +61,8 @@ public final class GradleUtils { */ public static final List MAVEN_PLUGIN_GRADLE_ARGS = List.of("assemble", "uploadArchives"); + public static final String JAVA_COMPATIBILITY = "^\\s*(source|target)Compatibility\\s*=\\s*(JavaVersion\\.VERSION_)?['\"]?(?(\\d+)([._]\\d+){0,2})['\"]?"; + private GradleUtils() { } @@ -125,22 +127,9 @@ private static int getMajorVersion(String version) { * @throws IOException if an error occurs while reading from the build files */ public static String getSpecifiedJavaVersion(Path buildFile) throws IOException { - if (isInBuildGradle(buildFile, - "^\\s*(source|target)Compatibility\\s*=\\s*(JavaVersion\\.VERSION_)?['\"]?1[2-7][^0-9]['\"]?")) { - return "17"; - } - - if (isInBuildGradle(buildFile, - "^\\s*(source|target)Compatibility\\s*=\\s*(JavaVersion\\.VERSION_)?['\"]?(1[._])?(9|1[0-1])[^0-9]['\"]?")) { - return "11"; - } - - if (isInBuildGradle(buildFile, - "^\\s*(source|target)Compatibility\\s*=\\s*(JavaVersion\\.VERSION_)?['\"]?(1[._])?[1-8][^0-9]['\"]?")) { - return "8"; - } - - return ""; + var content = Files.readString(buildFile); + var matcher = Pattern.compile(JAVA_COMPATIBILITY, Pattern.DOTALL | Pattern.MULTILINE).matcher(content); + return matcher.find() ? matcher.group("version").replace('_', '.') : ""; } /** diff --git a/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/build/maven/MavenJavaVersionDiscovery.java b/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/build/maven/MavenJavaVersionDiscovery.java index 7686c12da..eb7e22158 100644 --- a/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/build/maven/MavenJavaVersionDiscovery.java +++ b/java-components/build-request-processor/src/main/java/com/redhat/hacbs/container/analyser/build/maven/MavenJavaVersionDiscovery.java @@ -5,7 +5,6 @@ import static com.redhat.hacbs.container.verifier.MavenUtils.getCompilerSource; import static com.redhat.hacbs.container.verifier.MavenUtils.getCompilerTarget; -import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; @@ -21,7 +20,6 @@ import org.codehaus.plexus.interpolation.PrefixedObjectValueSource; import org.codehaus.plexus.interpolation.PropertiesBasedValueSource; import org.codehaus.plexus.interpolation.StringSearchInterpolator; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import com.redhat.hacbs.container.analyser.build.InvocationBuilder; import com.redhat.hacbs.container.analyser.build.JavaVersion; @@ -48,9 +46,7 @@ public static String interpolate(String value, Model model) { return value; } - public static void filterJavaVersions(Path pomFile, Model model, InvocationBuilder invocationBuilder) - throws IOException, XmlPullParserException { - + public static void filterJavaVersions(Path pomFile, Model model, InvocationBuilder invocationBuilder) { //if the toolchains plugin is configured we don't filter anything if (model.getBuild() != null && model.getBuild().getPlugins() != null) { for (var i : model.getBuild().getPlugins()) { @@ -90,15 +86,8 @@ public static void filterJavaVersions(Path pomFile, Model model, InvocationBuild javaVersion = parsed; } } - if (javaVersion > 0) { - if (javaVersion <= 5) { - invocationBuilder.maxJavaVersion(JAVA_8); - } else if (javaVersion == 6) { - invocationBuilder.maxJavaVersion(JAVA_11); - } else { - invocationBuilder.minJavaVersion(new JavaVersion(Integer.toString(javaVersion))); - } - } + + filterJavaVersions(invocationBuilder, javaVersion); for (var module : model.getModules()) { try { @@ -115,4 +104,24 @@ public static void filterJavaVersions(Path pomFile, Model model, InvocationBuild } } } + + public static void filterJavaVersions(InvocationBuilder invocationBuilder, String javaVersion) { + filterJavaVersions(invocationBuilder, JavaVersion.toVersion(javaVersion)); + } + + public static void filterJavaVersions(InvocationBuilder invocationBuilder, int javaVersion) { + if (javaVersion > 0) { + if (javaVersion <= 5) { + invocationBuilder.maxJavaVersion(JAVA_8); + Log.infof("Set max Java version to %s", JAVA_8); + } else if (javaVersion == 6) { + invocationBuilder.maxJavaVersion(JAVA_11); + Log.infof("Set max Java version to %s", JAVA_11); + } else { + var version = new JavaVersion(Integer.toString(javaVersion)); + invocationBuilder.minJavaVersion(version); + Log.infof("Set min Java version to %s", version); + } + } + } } diff --git a/java-components/build-request-processor/src/test/java/com/redhat/hacbs/container/analyser/build/LookupBuildInfoCommandTest.java b/java-components/build-request-processor/src/test/java/com/redhat/hacbs/container/analyser/build/LookupBuildInfoCommandTest.java index 133486e14..dffdd4fbb 100644 --- a/java-components/build-request-processor/src/test/java/com/redhat/hacbs/container/analyser/build/LookupBuildInfoCommandTest.java +++ b/java-components/build-request-processor/src/test/java/com/redhat/hacbs/container/analyser/build/LookupBuildInfoCommandTest.java @@ -1,5 +1,8 @@ package com.redhat.hacbs.container.analyser.build; +import static com.redhat.hacbs.container.analyser.build.BuildInfo.ANT; +import static com.redhat.hacbs.container.analyser.build.BuildInfo.GRADLE; +import static com.redhat.hacbs.container.analyser.build.BuildInfo.MAVEN; import static com.redhat.hacbs.container.verifier.MavenUtils.getBuildJdk; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -13,6 +16,7 @@ import java.util.List; import java.util.Set; import java.util.logging.LogRecord; +import java.util.stream.Collectors; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -21,7 +25,6 @@ import com.redhat.hacbs.recipes.tools.BuildToolInfo; import io.quarkus.bootstrap.resolver.maven.BootstrapMavenContext; -import io.quarkus.bootstrap.resolver.maven.BootstrapMavenException; import io.quarkus.logging.Log; import io.quarkus.test.LogCollectingTestResource; import io.quarkus.test.common.QuarkusTestResource; @@ -31,14 +34,17 @@ @QuarkusTest @QuarkusTestResource(value = LogCollectingTestResource.class, restrictToAnnotatedClass = true, initArgs = @ResourceArg(name = LogCollectingTestResource.LEVEL, value = "FINE")) class LookupBuildInfoCommandTest { + private static final String TOOL_VERSIONS = "sbt:1.8.0,jdk:7;8;11;17;21,maven:3.8.8;3.9.5,ant:1.9.16;1.10.13,gradle:8.4;8.3;8.0.2;7.4.2;7.6.3;7.5.1;6.9.2;5.6.4;4.10.3"; + + private static final String CACHE_URL = "https://repo1.maven.org/maven2"; + + private static final String CACHE_PATH = ""; @BeforeEach - public void clearLogs() { + void clearLogs() { LogCollectingTestResource.current().clear(); } - private final String toolVersions = "sbt:1.8.0,jdk:7;8;11;17;21,maven:3.8.8;3.9.5,ant:1.9.16;1.10.13,gradle:8.4;8.3;8.0.2;7.4.2;7.6.3;7.5.1;6.9.2;5.6.4;4.10.3"; - CacheBuildInfoLocator cacheBuildInfoLocator = new CacheBuildInfoLocator() { @Override public BuildRecipeInfo resolveBuildInfo(String scmUrl, String version) { @@ -87,36 +93,50 @@ public List lookupDisabledPlugins(String tool) { } }; + private BuildInfo getBuildInfo(String scmUrl, String commit) throws Exception { + return getBuildInfo(scmUrl, commit, null, null, null, null, null); + } + + private BuildInfo getBuildInfo(String scmUrl, String commit, String tag) throws Exception { + return getBuildInfo(scmUrl, commit, tag, null, null, null, null); + } + + private BuildInfo getBuildInfo(String scmUrl, String commit, String tag, String artifact) throws Exception { + return getBuildInfo(scmUrl, commit, tag, artifact, CACHE_URL, CACHE_PATH, null); + } + + private BuildInfo getBuildInfo(String scmUrl, String commit, String tag, String artifact, String cacheUrl, String cachePath, String context) throws Exception { + var lookupBuildInfoCommand = new LookupBuildInfoCommand(); + lookupBuildInfoCommand.toolVersions = TOOL_VERSIONS; + lookupBuildInfoCommand.scmUrl = scmUrl; + lookupBuildInfoCommand.commit = commit; + lookupBuildInfoCommand.tag = tag; + lookupBuildInfoCommand.artifact = artifact; + lookupBuildInfoCommand.cacheUrl = cacheUrl; + lookupBuildInfoCommand.cachePath = cachePath; + lookupBuildInfoCommand.context = context; + lookupBuildInfoCommand.mavenContext = new BootstrapMavenContext(); + return lookupBuildInfoCommand.doBuildAnalysis(scmUrl, new BuildRecipeInfo(), cacheBuildInfoLocator); + } + @Test - public void testBuildAnalysis() + void testBuildAnalysis() throws Exception { - LookupBuildInfoCommand lookupBuildInfoCommand = new LookupBuildInfoCommand(); - lookupBuildInfoCommand.mavenContext = new BootstrapMavenContext(); - lookupBuildInfoCommand.toolVersions = toolVersions; - lookupBuildInfoCommand.commit = "7ab205a40853486c1d978e6a7555808b9435407d"; - var info = lookupBuildInfoCommand.doBuildAnalysis("https://github.com/codehaus/jaxen.git", new BuildRecipeInfo(), - cacheBuildInfoLocator); + var info = getBuildInfo("https://github.com/codehaus/jaxen.git", "7ab205a40853486c1d978e6a7555808b9435407d"); assertEquals("jaxen", info.getContextPath()); List logRecords = LogCollectingTestResource.current().getRecords(); assertTrue(logRecords.stream() .anyMatch(r -> LogCollectingTestResource.format(r) .contains("Unable to locate a build script within"))); - assertThat(info.invocations).isNotEmpty(); + assertThat(info.invocations).hasSize(3); assertEquals("8", info.invocations.get(0).getToolVersion().get("jdk")); assertEquals("7", info.invocations.get(1).getToolVersion().get("jdk")); assertEquals("8", info.invocations.get(2).getToolVersion().get("jdk")); } @Test - public void testBuildAnalysisWithContext() - throws Exception { - LookupBuildInfoCommand lookupBuildInfoCommand = new LookupBuildInfoCommand(); - lookupBuildInfoCommand.mavenContext = new BootstrapMavenContext(); - lookupBuildInfoCommand.toolVersions = toolVersions; - lookupBuildInfoCommand.context = "jaxen"; - lookupBuildInfoCommand.commit = "7ab205a40853486c1d978e6a7555808b9435407d"; - var info = lookupBuildInfoCommand.doBuildAnalysis("https://github.com/codehaus/jaxen.git", new BuildRecipeInfo(), - cacheBuildInfoLocator); + void testBuildAnalysisWithContext() throws Exception { + var info = getBuildInfo("https://github.com/codehaus/jaxen.git", "7ab205a40853486c1d978e6a7555808b9435407d", null, null, null, null, "jaxen"); List logRecords = LogCollectingTestResource.current().getRecords(); assertNull(info.getContextPath()); assertTrue(logRecords.stream() @@ -125,16 +145,9 @@ public void testBuildAnalysisWithContext() } @Test - public void testBuildAnalysisFails() - throws BootstrapMavenException { - LookupBuildInfoCommand lookupBuildInfoCommand = new LookupBuildInfoCommand(); - lookupBuildInfoCommand.mavenContext = new BootstrapMavenContext(); - lookupBuildInfoCommand.toolVersions = toolVersions; - lookupBuildInfoCommand.commit = "0750e49665855b4b85487aa3780bf0daf09e8e4c"; - + void testBuildAnalysisFails() { assertThrows(RuntimeException.class, - () -> lookupBuildInfoCommand.doBuildAnalysis("https://github.com/jakartaee/jaf-api.git", new BuildRecipeInfo(), - cacheBuildInfoLocator), + () -> getBuildInfo("https://github.com/jakartaee/jaf-api.git", "0750e49665855b4b85487aa3780bf0daf09e8e4c"), "Multiple subdirectories have build files"); List logRecords = LogCollectingTestResource.current().getRecords(); assertTrue(logRecords.stream() @@ -144,129 +157,72 @@ public void testBuildAnalysisFails() } @Test - public void testModelResolver1() - throws Exception { - LookupBuildInfoCommand lookupBuildInfoCommand = new LookupBuildInfoCommand(); - lookupBuildInfoCommand.mavenContext = new BootstrapMavenContext(); - lookupBuildInfoCommand.toolVersions = toolVersions; - lookupBuildInfoCommand.commit = "ea39ccf03d38b65df7aa5153a1bbddc3197b1597"; - var info = lookupBuildInfoCommand.doBuildAnalysis("https://github.com/wso2/balana", new BuildRecipeInfo(), - cacheBuildInfoLocator); + void testModelResolver1() throws Exception { + var info = getBuildInfo("https://github.com/wso2/balana", "ea39ccf03d38b65df7aa5153a1bbddc3197b1597"); assertTrue(info.repositories.contains("https://maven.wso2.org/nexus/content/groups/wso2-public/")); } @Test - public void testModelResolver2() - throws Exception { - LookupBuildInfoCommand lookupBuildInfoCommand = new LookupBuildInfoCommand(); - - lookupBuildInfoCommand.mavenContext = new BootstrapMavenContext(); - lookupBuildInfoCommand.toolVersions = toolVersions; - lookupBuildInfoCommand.tag = "02d3e524110982ec2b420ff8f9126707d483374f"; - lookupBuildInfoCommand.commit = "02d3e524110982ec2b420ff8f9126707d483374f"; - var info = lookupBuildInfoCommand.doBuildAnalysis( - "https://github.com/jtablesaw/tablesaw", - new BuildRecipeInfo(), - cacheBuildInfoLocator); + void testModelResolver2() throws Exception { + var info = getBuildInfo("https://github.com/jtablesaw/tablesaw", "02d3e524110982ec2b420ff8f9126707d483374f", "02d3e524110982ec2b420ff8f9126707d483374f"); assertTrue(info.repositories.contains("https://jitpack.io")); } @Test - public void testModelResolver3() - throws Exception { - LookupBuildInfoCommand lookupBuildInfoCommand = new LookupBuildInfoCommand(); - - lookupBuildInfoCommand.mavenContext = new BootstrapMavenContext(); - lookupBuildInfoCommand.toolVersions = toolVersions; - lookupBuildInfoCommand.commit = "7da937230df898d8d1c05522cdfa5e3931921f8f"; - var info = lookupBuildInfoCommand.doBuildAnalysis( - "https://github.com/jvm-build-service-test-data/maven-symlink-in-repo", - new BuildRecipeInfo(), - cacheBuildInfoLocator); + void testModelResolver3() throws Exception { + var info = getBuildInfo( + "https://github.com/jvm-build-service-test-data/maven-symlink-in-repo", "7da937230df898d8d1c05522cdfa5e3931921f8f"); assertTrue(info.repositories.contains("https://repo.maven.apache.org/maven2")); } @Test - public void testModelResolver4() - throws Exception { - LookupBuildInfoCommand lookupBuildInfoCommand = new LookupBuildInfoCommand(); - - lookupBuildInfoCommand.mavenContext = new BootstrapMavenContext(); - lookupBuildInfoCommand.toolVersions = toolVersions; - lookupBuildInfoCommand.commit = "a586e706aea82dc80fb05bdf59f2a25150ee1801"; - var info = lookupBuildInfoCommand.doBuildAnalysis( - "https://github.com/javaee/jsonp", - new BuildRecipeInfo(), - cacheBuildInfoLocator); + void testModelResolver4() throws Exception { + var info = getBuildInfo("https://github.com/javaee/jsonp", "a586e706aea82dc80fb05bdf59f2a25150ee1801"); assertTrue(info.repositories.contains("https://maven.java.net/content/repositories/snapshots")); } @Test - public void testBuildAnalysisAnt() + void testBuildAnalysisAnt() throws Exception { - LookupBuildInfoCommand lookupBuildInfoCommand = new LookupBuildInfoCommand(); - lookupBuildInfoCommand.toolVersions = toolVersions; // https://gitlab.ow2.org/asm/asm/-/tree/ASM_6_0?ref_type=tags - lookupBuildInfoCommand.commit = "016a5134e8bab1d4239fc8dcc47baef11e05d33e"; - var info = lookupBuildInfoCommand.doBuildAnalysis("https://gitlab.ow2.org/asm/asm.git", new BuildRecipeInfo(), - cacheBuildInfoLocator); + var info = getBuildInfo("https://gitlab.ow2.org/asm/asm.git", "016a5134e8bab1d4239fc8dcc47baef11e05d33e"); assertThat(info.invocations).isNotEmpty(); assertTrue(info.invocations.get(0).getCommands().contains("-v")); - assertTrue(info.invocations.get(0).getTool().contains("ant")); + assertTrue(info.invocations.get(0).getTool().contains(ANT)); } @Test - public void testBuildAnalysisGradleReleaseLegacy() - throws Exception { - LookupBuildInfoCommand lookupBuildInfoCommand = new LookupBuildInfoCommand(); - lookupBuildInfoCommand.toolVersions = toolVersions; + void testBuildAnalysisGradleReleaseLegacy() throws Exception { // https://gitlab.ow2.org/asm/asm/-/tree/ASM_7_0?ref_type=tags - lookupBuildInfoCommand.commit = "1f6020a3f17d9d88dfd54a31370e91e3361c216b"; - var info = lookupBuildInfoCommand.doBuildAnalysis("https://gitlab.ow2.org/asm/asm.git", new BuildRecipeInfo(), - cacheBuildInfoLocator); + var info = getBuildInfo("https://gitlab.ow2.org/asm/asm.git", "1f6020a3f17d9d88dfd54a31370e91e3361c216b"); assertThat(info.invocations).isNotEmpty(); assertTrue(info.invocations.get(0).getCommands().contains("uploadArchives")); assertTrue(info.invocations.get(0).getCommands().contains("-Prelease")); } @Test - public void testBuildAnalysisGradleReleaseCurrent() - throws Exception { - LookupBuildInfoCommand lookupBuildInfoCommand = new LookupBuildInfoCommand(); - lookupBuildInfoCommand.toolVersions = toolVersions; + void testBuildAnalysisGradleReleaseCurrent() throws Exception { // https://gitlab.ow2.org/asm/asm/-/tree/ASM_9_6?ref_type=tags - lookupBuildInfoCommand.commit = "85cf1aeb0d08be8446f6efbda962817d2a9707dd"; - var info = lookupBuildInfoCommand.doBuildAnalysis("https://gitlab.ow2.org/asm/asm.git", new BuildRecipeInfo(), - cacheBuildInfoLocator); + var info = getBuildInfo("https://gitlab.ow2.org/asm/asm.git", "85cf1aeb0d08be8446f6efbda962817d2a9707dd"); assertThat(info.invocations).isNotEmpty(); assertTrue(info.invocations.get(0).getCommands().contains("publishToMavenLocal")); assertTrue(info.invocations.get(0).getCommands().contains("-Prelease")); } @Test - public void testBuildAnalysisOSGIR7CoreSpecFinal() - throws Exception { - LookupBuildInfoCommand lookupBuildInfoCommand = new LookupBuildInfoCommand(); - lookupBuildInfoCommand.toolVersions = toolVersions; + void testBuildAnalysisOSGIR7CoreSpecFinal() throws Exception { // r7-core-spec-final - lookupBuildInfoCommand.commit = "ac877b9fdaa36e26adb939cf9dd425e77243f449"; - var info = lookupBuildInfoCommand.doBuildAnalysis("https://github.com/osgi/osgi.git", new BuildRecipeInfo(), - cacheBuildInfoLocator); + var info = getBuildInfo("https://github.com/osgi/osgi.git", "ac877b9fdaa36e26adb939cf9dd425e77243f449"); assertThat(info.invocations).isNotEmpty(); assertTrue(info.invocations.get(0).getCommands().contains("publishToMavenLocal")); - info.invocations.forEach( i -> assertEquals( "4.10.3", i.getToolVersion().get( "gradle" ) ) ); + info.invocations.forEach( i -> assertEquals( "4.10.3", i.getToolVersion().get( GRADLE ) ) ); assertFalse(info.invocations.get(0).getCommands().contains("-Prelease")); } @Test - public void testBuildAnalysisMicrometer() - throws Exception { - LookupBuildInfoCommand lookupBuildInfoCommand = new LookupBuildInfoCommand(); - lookupBuildInfoCommand.toolVersions = toolVersions; + void testBuildAnalysisMicrometer() throws Exception { // 1.12.1 - lookupBuildInfoCommand.commit = "3c39cb09d50ad7e5b94683e9695cc00dba346b13"; - var info = lookupBuildInfoCommand.doBuildAnalysis("https://github.com/micrometer-metrics/micrometer.git", new BuildRecipeInfo(), - cacheBuildInfoLocator); + var info = getBuildInfo("https://github.com/micrometer-metrics/micrometer.git", "3c39cb09d50ad7e5b94683e9695cc00dba346b13"); assertThat(info.invocations).isNotEmpty(); assertTrue(info.invocations.get(0).getCommands().contains("publishToMavenLocal")); // Ensure we don't have -Prelease as that conflicts with the nebula plugin using release.stage @@ -274,52 +230,54 @@ public void testBuildAnalysisMicrometer() } @Test - public void testBuildAnalysisRelaxingDatatype() - throws Exception { - LookupBuildInfoCommand lookupBuildInfoCommand = new LookupBuildInfoCommand(); - lookupBuildInfoCommand.mavenContext = new BootstrapMavenContext(); - lookupBuildInfoCommand.toolVersions = toolVersions; + void testBuildAnalysisRelaxngDatatypeJava() throws Exception { // 20020414 - lookupBuildInfoCommand.tag = "a83e0896eddbd57a6c8c7afe9cae907199d5108b"; - lookupBuildInfoCommand.commit = "a83e0896eddbd57a6c8c7afe9cae907199d5108b"; - var info = lookupBuildInfoCommand.doBuildAnalysis("https://github.com/java-schema-utilities/relaxng-datatype-java.git", new BuildRecipeInfo(), - cacheBuildInfoLocator); - assertThat(info.invocations).isNotEmpty(); + var info = getBuildInfo("https://github.com/java-schema-utilities/relaxng-datatype-java.git", "a83e0896eddbd57a6c8c7afe9cae907199d5108b", "a83e0896eddbd57a6c8c7afe9cae907199d5108b"); + assertThat(info.invocations.stream().map(Invocation::getTool).collect(Collectors.toSet())).containsExactlyInAnyOrder(ANT, MAVEN); } @Test - public void testBuildAnalysisSmallRye() - throws Exception { - LookupBuildInfoCommand lookupBuildInfoCommand = new LookupBuildInfoCommand(); - lookupBuildInfoCommand.mavenContext = new BootstrapMavenContext(); - lookupBuildInfoCommand.cacheUrl = "https://repo1.maven.org/maven2"; - lookupBuildInfoCommand.toolVersions = toolVersions; - lookupBuildInfoCommand.artifact = "io.smallrye.config:smallrye-config:2.13.3"; - lookupBuildInfoCommand.CACHE_PATH = ""; + void testBuildAnalysisSmallryeConfig() throws Exception { // 2.13.3 - lookupBuildInfoCommand.commit = "e6ab5b2b6be149e028ae21be55598cfb0d2b1d37"; - var info = lookupBuildInfoCommand.doBuildAnalysis("https://github.com/smallrye/smallrye-config.git", new BuildRecipeInfo(), - cacheBuildInfoLocator); + var info = getBuildInfo("https://github.com/smallrye/smallrye-config.git", "e6ab5b2b6be149e028ae21be55598cfb0d2b1d37", null, "io.smallrye.config:smallrye-config:2.13.3", "https://repo1.maven.org/maven2", "", null); List logRecords = LogCollectingTestResource.current().getRecords(); assertTrue(logRecords.stream() .anyMatch(r -> LogCollectingTestResource.format(r).matches("Overriding release date.*"))); assertThat(info.invocations.size()).isEqualTo(2); - assertTrue(info.invocations.get(0).getToolVersion().get("maven").contains("3.9.5")); - assertTrue(info.invocations.get(1).getToolVersion().get("maven").contains("3.8.8")); + assertTrue(info.invocations.get(0).getToolVersion().get(MAVEN).contains("3.9.5")); + assertTrue(info.invocations.get(1).getToolVersion().get(MAVEN).contains("3.8.8")); + } + + @Test + void testBuildAnalysisIsoRelax() throws Exception { + var info = getBuildInfo("https://github.com/jvm-build-service-code/iso-relax.git", "bbc253738f4eb82ecc7d7b011b664ef5d53a58c5",null, "com.sun.xml.bind.jaxb:isorelax:20090621"); + assertThat(info.invocations).hasSize(1); + assertThat(info.invocations.get(0).getToolVersion()).extractingByKey(MAVEN).asString().isEqualTo("3.8.8"); + } + + @Test + void testBuildAnalysisJavaAnnotations() throws Exception { + var info = getBuildInfo("https://github.com/jvm-build-service-code/JetBrains-java-annotations.git", "1f1515d801ad49263fbf861673aaa5e3913a625e", null, "org.jetbrains:annotations:13.0"); + assertThat(info.invocations).hasSize(1); + assertThat(info.invocations.get(0).getToolVersion()).extractingByKey(MAVEN).asString().isEqualTo("3.8.8"); + } + + @Test + void testBuildAnalysisMultiverse() throws Exception { + var info = getBuildInfo("https://github.com/pveentjer/Multiverse", "4b41a46a627dd7b4a3dcf8fbf4db5d0a4df84bb4", null, "org.multiverse:multiverse-core:0.7.0"); + assertThat(info.invocations.stream().map(Invocation::getTool).collect(Collectors.toSet())).containsExactlyInAnyOrder(GRADLE, MAVEN); } + @Test + void testBuildAnalysisJunit5() throws Exception { + var info = getBuildInfo("https://github.com/junit-team/junit5.git", "5bdb20cf1adc3089c9de4af30ca22c9d96055dc8", null, "org.junit.jupiter:junit-jupiter-api:5.2.0"); + assertThat(LogCollectingTestResource.current().getRecords()).map(LogCollectingTestResource::format).contains("Set Java version to [9, 11] due to preferred version 10"); + assertThat(info.invocations.stream().map(Invocation::getTool).collect(Collectors.toSet())).containsExactlyInAnyOrder(GRADLE); + } @Test void testBuildJdkSetsJavaVersion() throws Exception { - var lookupBuildInfoCommand = new LookupBuildInfoCommand(); - lookupBuildInfoCommand.mavenContext = new BootstrapMavenContext(); - lookupBuildInfoCommand.toolVersions = toolVersions; - lookupBuildInfoCommand.commit = "7ab205a40853486c1d978e6a7555808b9435407d"; - lookupBuildInfoCommand.cacheUrl = "https://repo1.maven.org/maven2"; - lookupBuildInfoCommand.artifact = "jaxen:jaxen:1.1.6"; - lookupBuildInfoCommand.CACHE_PATH = ""; - lookupBuildInfoCommand.doBuildAnalysis("https://github.com/codehaus/jaxen.git", new BuildRecipeInfo(), - cacheBuildInfoLocator); + getBuildInfo("https://github.com/codehaus/jaxen.git", "7ab205a40853486c1d978e6a7555808b9435407d", null, "jaxen:jaxen:1.1.6"); var logRecords = LogCollectingTestResource.current().getRecords(); assertTrue(logRecords.stream() .anyMatch(r -> LogCollectingTestResource.format(r).contains("Set Java version to [null, 7]"))); @@ -346,7 +304,6 @@ void testGetBuildJdkFromJarManifest() throws IOException { } assertThat(list).containsExactly("1.5:5", "1.6:6", "1.7:7", "1.8:8", "9:9", "11:11", "16:16", "17:17", "21:21"); - var logRecords = LogCollectingTestResource.current().getRecords(); - assertThat(logRecords).flatMap(LogCollectingTestResource::format).contains("Invalid JDK version: 2.3"); + assertThat(LogCollectingTestResource.current().getRecords()).map(LogCollectingTestResource::format).contains("Invalid JDK version: 2.3"); } } diff --git a/java-components/build-request-processor/src/test/java/com/redhat/hacbs/container/analyser/build/gradle/GradleUtilsTest.java b/java-components/build-request-processor/src/test/java/com/redhat/hacbs/container/analyser/build/gradle/GradleUtilsTest.java index d20b0c201..08dd7fc87 100644 --- a/java-components/build-request-processor/src/test/java/com/redhat/hacbs/container/analyser/build/gradle/GradleUtilsTest.java +++ b/java-components/build-request-processor/src/test/java/com/redhat/hacbs/container/analyser/build/gradle/GradleUtilsTest.java @@ -52,16 +52,16 @@ void testSpecifiedJavaVersion(@TempDir Path basedir) throws IOException { Path buildGradle = basedir.resolve(GradleUtils.BUILD_GRADLE); Files.writeString(buildGradle, "sourceCompatibility = 1.6" + System.lineSeparator() + "targetCompatibility = 1.6" + System.lineSeparator()); - assertThat(GradleUtils.getSpecifiedJavaVersion(buildGradle)).isEqualTo("8"); + assertThat(GradleUtils.getSpecifiedJavaVersion(buildGradle)).isEqualTo("1.6"); Files.writeString(buildGradle, "sourceCompatibility = JavaVersion.VERSION_1_7;" + System.lineSeparator() + "targetCompatibility = JavaVersion.VERSION_1_7;" + System.lineSeparator()); - assertThat(GradleUtils.getSpecifiedJavaVersion(buildGradle)).isEqualTo("8"); + assertThat(GradleUtils.getSpecifiedJavaVersion(buildGradle)).isEqualTo("1.7"); Files.writeString(buildGradle, "sourceCompatibility = JavaVersion.VERSION_1_8" + System.lineSeparator() + "targetCompatibility = JavaVersion.VERSION_1_8" + System.lineSeparator()); - assertThat(GradleUtils.getSpecifiedJavaVersion(buildGradle)).isEqualTo("8"); + assertThat(GradleUtils.getSpecifiedJavaVersion(buildGradle)).isEqualTo("1.8"); Files.writeString(buildGradle, "sourceCompatibility = JavaVersion.VERSION_1_8.toString()" + System.lineSeparator() + "targetCompatibility = JavaVersion.VERSION_1_8.toString()" + System.lineSeparator()); - assertThat(GradleUtils.getSpecifiedJavaVersion(buildGradle)).isEqualTo("8"); + assertThat(GradleUtils.getSpecifiedJavaVersion(buildGradle)).isEqualTo("1.8"); Files.writeString(buildGradle, "sourceCompatibility = 1.8" + System.lineSeparator() + "targetCompatibility = 1.8" + System.lineSeparator()); Files.writeString(buildGradle, "sourceCompatibility = 8" + System.lineSeparator() @@ -69,7 +69,7 @@ void testSpecifiedJavaVersion(@TempDir Path basedir) throws IOException { assertThat(GradleUtils.getSpecifiedJavaVersion(buildGradle)).isEqualTo("8"); Files.writeString(buildGradle, "sourceCompatibility = 9" + System.lineSeparator() + "targetCompatibility = 9" + System.lineSeparator()); - assertThat(GradleUtils.getSpecifiedJavaVersion(buildGradle)).isEqualTo("11"); + assertThat(GradleUtils.getSpecifiedJavaVersion(buildGradle)).isEqualTo("9"); Files.writeString(buildGradle, "sourceCompatibility = JavaVersion.VERSION_11" + System.lineSeparator() + "targetCompatibility = JavaVersion.VERSION_11" + System.lineSeparator()); assertThat(GradleUtils.getSpecifiedJavaVersion(buildGradle)).isEqualTo("11"); @@ -81,10 +81,13 @@ void testSpecifiedJavaVersion(@TempDir Path basedir) throws IOException { assertThat(GradleUtils.getSpecifiedJavaVersion(buildGradle)).isEqualTo("17"); Files.writeString(buildGradle, "sourceCompatibility = JavaVersion.VERSION_18" + System.lineSeparator() + "targetCompatibility = JavaVersion.VERSION_18" + System.lineSeparator()); - assertThat(GradleUtils.getSpecifiedJavaVersion(buildGradle)).isEmpty(); + assertThat(GradleUtils.getSpecifiedJavaVersion(buildGradle)).isEqualTo("18"); Files.writeString(buildGradle, "sourceCompatibility = JavaVersion.VERSION_20" + System.lineSeparator() + "targetCompatibility = JavaVersion.VERSION_20" + System.lineSeparator()); - assertThat(GradleUtils.getSpecifiedJavaVersion(buildGradle)).isEmpty(); + assertThat(GradleUtils.getSpecifiedJavaVersion(buildGradle)).isEqualTo("20"); + Files.writeString(buildGradle, "sourceCompatibility = JavaVersion.VERSION_21" + System.lineSeparator() + + "targetCompatibility = JavaVersion.VERSION_20" + System.lineSeparator()); + assertThat(GradleUtils.getSpecifiedJavaVersion(buildGradle)).isEqualTo("21"); } @Test