diff --git a/src/integrationTest/java/eu/xenit/gradle/testrunner/AbstractIntegrationTest.java b/src/integrationTest/java/eu/xenit/gradle/testrunner/AbstractIntegrationTest.java index a134c983..04cd4040 100644 --- a/src/integrationTest/java/eu/xenit/gradle/testrunner/AbstractIntegrationTest.java +++ b/src/integrationTest/java/eu/xenit/gradle/testrunner/AbstractIntegrationTest.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import eu.xenit.gradle.testrunner.versions.GradleVersionSeries; import eu.xenit.gradle.testrunner.versions.GradleVersionSpec; import eu.xenit.gradle.testrunner.versions.VersionFetcher; import java.io.File; @@ -20,6 +21,7 @@ import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.commons.io.FileUtils; import org.gradle.testkit.runner.BuildResult; import org.gradle.testkit.runner.GradleRunner; @@ -51,44 +53,28 @@ public static List testData() { } boolean majorsOnly = Boolean.getBoolean("eu.xenit.gradle.integration.majorsOnly"); - Comparator byVersionNumber = Comparator.comparing(versionSpec -> GradleVersion.version(versionSpec.getVersion())); - List fetchedVersions = VersionFetcher.fetchVersions() - .filter(VersionFetcher::isRelease) - .filter(VersionFetcher.greaterThan("5.6")) - .sorted(byVersionNumber) - .collect(Collectors.toList()); - - Map> versionsByMinor = new HashMap<>(); - - for (GradleVersionSpec versionSpec : fetchedVersions) { - String[] versionParts = GradleVersion.version(versionSpec.getVersion()).getBaseVersion().getVersion().split("\\."); - String versionKey = majorsOnly?versionParts[0]:(versionParts[0]+"."+versionParts[1]); - if(versionsByMinor.containsKey(versionKey)) { - versionsByMinor.get(versionKey).add(versionSpec); - } else { - versionsByMinor.put(versionKey, new ArrayList<>(Collections.singletonList(versionSpec))); - if(majorsOnly) { - versionsByMinor.put(versionKey + "-lowest", Collections.singletonList(versionSpec)); - } - } - } - - - List versionsToBuild = versionsByMinor.values() + List versionsToBuild = VersionFetcher.fetchVersionSeries() + // Only release versions >= 5.6 + .map(series -> series.filter(VersionFetcher::isRelease) + .filter(VersionFetcher.greaterThanOrEqual("5.6"))) + .filter(series -> !series.isEmpty()) + // When selecting only majors, select the series with only the major version component + .filter(series -> !majorsOnly || series.getSeriesPriority() == 1) + // For series with only the major version component, select the first and last version, for series with the minor version component, only select the last version + .flatMap(series -> series.getSeriesPriority() == 1 ? Stream.of(series.getLowestVersion(), series.getHighestVersion()): Stream.of( + series.getHighestVersion())) + // Collect to a set to deduplicate versions + .collect(Collectors.toSet()) .stream() - .map(versions -> versions.stream() - // Find latest patch version - .sorted(byVersionNumber.reversed()) - .findFirst() - .orElse(null) - ) - .filter(Objects::nonNull) - .sorted(byVersionNumber) + // Re-sort by version number + .sorted(GradleVersionSpec.BY_VERSION_NUMBER) + // Convert to parameter for junit .map(GradleVersionSpec::getVersion) .map(version -> new String[]{version}) .collect(Collectors.toList()); List slicedVersions = slice(versionsToBuild); + System.out.println(slicedVersions.stream() .map(i -> i[0]) .collect(Collectors.joining(", ", "Running test on versions: ", ""))); diff --git a/src/integrationTest/java/eu/xenit/gradle/testrunner/versions/GradleVersionSeries.java b/src/integrationTest/java/eu/xenit/gradle/testrunner/versions/GradleVersionSeries.java new file mode 100644 index 00000000..36eac77a --- /dev/null +++ b/src/integrationTest/java/eu/xenit/gradle/testrunner/versions/GradleVersionSeries.java @@ -0,0 +1,48 @@ +package eu.xenit.gradle.testrunner.versions; + +import java.util.Collection; +import java.util.Set; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +public class GradleVersionSeries { + private final String series; + private final Set versionSpecs; + + public GradleVersionSeries(String series, Set versionSpecs) { + this.series = series; + this.versionSpecs = versionSpecs; + } + + public static GradleVersionSeries extract(String series, Collection versionSpecs) { + Set extractedVersions = versionSpecs.stream() + .filter(spec -> spec.getVersionSeries().contains(series)) + .collect(Collectors.toSet()); + return new GradleVersionSeries(series, extractedVersions); + } + + public int getSeriesPriority() { + return series.split("\\.").length; + } + + public GradleVersionSpec getLowestVersion() { + return versionSpecs.stream() + .min(GradleVersionSpec.BY_VERSION_NUMBER) + .orElse(null); + } + + public GradleVersionSpec getHighestVersion() { + return versionSpecs.stream() + .max(GradleVersionSpec.BY_VERSION_NUMBER) + .orElse(null); + } + + public GradleVersionSeries filter(Predicate predicate) { + return new GradleVersionSeries(series, versionSpecs.stream().filter(predicate).collect(Collectors.toSet())); + } + + public boolean isEmpty() { + return versionSpecs.isEmpty(); + } + +} diff --git a/src/integrationTest/java/eu/xenit/gradle/testrunner/versions/GradleVersionSpec.java b/src/integrationTest/java/eu/xenit/gradle/testrunner/versions/GradleVersionSpec.java index 2c674262..660ab734 100644 --- a/src/integrationTest/java/eu/xenit/gradle/testrunner/versions/GradleVersionSpec.java +++ b/src/integrationTest/java/eu/xenit/gradle/testrunner/versions/GradleVersionSpec.java @@ -1,11 +1,17 @@ package eu.xenit.gradle.testrunner.versions; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import java.util.Collections; import java.util.Comparator; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; import org.gradle.util.GradleVersion; @JsonIgnoreProperties(ignoreUnknown = true) public class GradleVersionSpec { + public static final Comparator BY_VERSION_NUMBER = Comparator.comparing(versionSpec -> GradleVersion.version(versionSpec.getVersion())); + private String version; private boolean snapshot; private boolean broken; @@ -15,6 +21,22 @@ public String getVersion() { return version; } + public Set getVersionSeries() { + String[] parts = GradleVersion.version(version).getBaseVersion().getVersion().split("\\."); + + Set series = new HashSet<>(); + + for (int i = 0; i < parts.length; i++) { + String serie = ""; + for (int j = 0; j <= i; j++) { + serie+=parts[j]+"."; + } + series.add(serie.substring(0, Math.max(serie.length()-1, 0))); + } + + return Collections.unmodifiableSet(series); + } + public void setVersion(String version) { this.version = version; } @@ -42,4 +64,22 @@ public String getRcFor() { public void setRcFor(String rcFor) { this.rcFor = rcFor; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + GradleVersionSpec that = (GradleVersionSpec) o; + return snapshot == that.snapshot && broken == that.broken && version.equals(that.version) && rcFor.equals( + that.rcFor); + } + + @Override + public int hashCode() { + return Objects.hash(version, snapshot, broken, rcFor); + } } diff --git a/src/integrationTest/java/eu/xenit/gradle/testrunner/versions/VersionFetcher.java b/src/integrationTest/java/eu/xenit/gradle/testrunner/versions/VersionFetcher.java index 1a645be5..110bad87 100644 --- a/src/integrationTest/java/eu/xenit/gradle/testrunner/versions/VersionFetcher.java +++ b/src/integrationTest/java/eu/xenit/gradle/testrunner/versions/VersionFetcher.java @@ -8,9 +8,12 @@ import java.io.UncheckedIOException; import java.net.MalformedURLException; import java.net.URL; +import java.util.HashSet; import java.util.List; import java.util.Objects; +import java.util.Set; import java.util.function.Predicate; +import java.util.stream.Collectors; import java.util.stream.Stream; import org.gradle.util.GradleVersion; @@ -33,16 +36,8 @@ public static boolean isRelease(GradleVersionSpec versionSpec) { return !versionSpec.isSnapshot() && versionSpec.getRcFor().isEmpty() && Objects.equals(gradleVersion.getBaseVersion(), gradleVersion); } - public static boolean isBroken(GradleVersionSpec versionSpec) { - return versionSpec.isBroken(); - } - - public static Predicate greaterThan(String version) { - return versionSpec -> GradleVersion.version(versionSpec.getVersion()).compareTo(GradleVersion.version(version)) > 0; - } - - public static boolean firstPointRelease(GradleVersionSpec versionSpec) { - return GradleVersion.version(versionSpec.getVersion()).getBaseVersion().getVersion().endsWith(".0"); + public static Predicate greaterThanOrEqual(String version) { + return versionSpec -> GradleVersion.version(versionSpec.getVersion()).compareTo(GradleVersion.version(version)) >= 0; } public static Stream fetchVersions() { @@ -51,4 +46,16 @@ public static Stream fetchVersions() { .filter(((Predicate) GradleVersionSpec::isBroken).negate()); } + public static Stream fetchVersionSeries() { + List versions = fetchVersions().collect(Collectors.toList()); + Set versionSeries = new HashSet<>(); + for (GradleVersionSpec version : versions) { + versionSeries.addAll(version.getVersionSeries()); + } + + return versionSeries.stream() + .map(series -> GradleVersionSeries.extract(series, versions)) + .filter(series -> series.getSeriesPriority() <= 2); + } + }