Skip to content

Commit

Permalink
refactor tests,
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Jul 31, 2024
1 parent 7dc7918 commit 7e86cb6
Show file tree
Hide file tree
Showing 38 changed files with 436 additions and 152 deletions.
62 changes: 8 additions & 54 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import org.gradle.api.tasks.testing.logging.TestLogEvent
import xyz.wagyourtail.gradle.shadow.ShadowJar

plugins {
Expand All @@ -10,6 +9,8 @@ plugins {
application
id("io.github.sgtsilvio.gradle.metadata") version "0.5.0"
id("com.gradleup.nmcp") version "0.0.7"

id ("com.dorongold.task-tree") version "4.0.0"
}

allprojects {
Expand Down Expand Up @@ -44,14 +45,17 @@ allprojects {
}

java {
if (project.name != "downgradetest") {
if (!project.path.startsWith(":testing")) {
withSourcesJar()
withJavadocJar()
}
}

version =
if (project.hasProperty("version_snapshot")) "${project.properties["version"]}-SNAPSHOT" else project.properties["version"] as String
version = if (project.hasProperty("version_snapshot")) {
"${project.properties["version"]}-SNAPSHOT"
} else {
project.properties["version"] as String
}
group = project.properties["maven_group"] as String

base {
Expand Down Expand Up @@ -119,13 +123,6 @@ sourceSets {
}

dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")

testImplementation("com.google.code.gson:gson:2.10")
testImplementation("org.apache.commons:commons-compress:1.26.1")
testImplementation("io.github.java-diff-utils:java-diff-utils:4.12")

val api by configurations.getting

api("org.ow2.asm:asm:${project.properties["asm_version"]}")
Expand Down Expand Up @@ -177,49 +174,6 @@ tasks.javadoc {
source = sourceSets.main.get().allJava + sourceSets["shared"].allJava
}

val testVersion = project.properties["testVersion"] as String
val testTargetVersion = project.properties["testTargetVersion"] as String

tasks.compileTestJava {
options.encoding = "UTF-8"

javaCompiler = javaToolchains.compilerFor {
languageVersion.set(JavaLanguageVersion.of(testVersion.toInt()))
}
}

tasks.test {
useJUnitPlatform()

dependsOn(
project(":downgradetest").tasks.build,
project(":java-api").tasks.named("testJar")
)
javaLauncher = javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(testVersion.toInt()))
}

val versions = listOf(testVersion, testTargetVersion, "7").associateWith {
javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(it.toInt()))
}.get().executablePath.toString()
}

jvmArgs(
"-Djvmdg.test.version=$version",
"-Djvmdg.test.originalVersion=$testVersion",
"-Djvmdg.test.javaVersion=${versions.keys.joinToString(File.pathSeparator)}",
"-Djvmdg.test.launcher=${versions.values.joinToString(File.pathSeparator)}",
"-Djvmdg.test.downgradeClasspath=${shared.compileClasspath.joinToString(File.pathSeparator) { it.absolutePath }}",
)

testLogging {
events.add(TestLogEvent.PASSED)
events.add(TestLogEvent.SKIPPED)
events.add(TestLogEvent.FAILED)
}
}

project.evaluationDependsOnChildren()

val shadowJar by tasks.registering(ShadowJar::class) {
Expand Down
5 changes: 4 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version ("0.7.0")
}

include("downgradetest")
include("gradle-plugin")
include("java-api")
include("site")

include("testing")
include("testing:downgrade")
include("testing:multi-version")

rootProject.name = "JvmDowngrader"

2 changes: 2 additions & 0 deletions src/main/java/xyz/wagyourtail/jvmdg/cli/Flags.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ public Flags copy() {
flags.printDebug = printDebug;
flags.debugSkipStubs = new HashSet<>(debugSkipStubs);
flags.debugDumpClasses = debugDumpClasses;
flags.multiReleaseOriginal = multiReleaseOriginal;
flags.multiReleaseVersions = new HashSet<>(multiReleaseVersions);
return flags;
}

Expand Down
77 changes: 77 additions & 0 deletions testing/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import org.gradle.api.tasks.testing.logging.TestLogEvent

plugins {
java
}

val testVersion = JavaVersion.toVersion(project.properties["testVersion"] as String)

java {
sourceCompatibility = testVersion
targetCompatibility = testVersion
toolchain {
languageVersion.set(JavaLanguageVersion.of(testVersion.majorVersion))
}
}


repositories {
mavenCentral()
}

sourceSets {
test {
compileClasspath += rootProject.sourceSets.main.get().compileClasspath + rootProject.sourceSets.main.get().output
runtimeClasspath += rootProject.sourceSets.main.get().runtimeClasspath + rootProject.sourceSets.main.get().output
}
}

dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")

testImplementation("com.google.code.gson:gson:2.10")
testImplementation("org.apache.commons:commons-compress:1.26.1")
testImplementation("io.github.java-diff-utils:java-diff-utils:4.12")
}

val testTargetVersion = JavaVersion.toVersion(project.properties["testTargetVersion"] as String)

tasks.test {
useJUnitPlatform()

dependsOn(
project(":testing:downgrade").tasks.build,
project(":testing:multi-version").tasks.build,
project(":java-api").tasks.named("testJar")
)

val versions = listOf(
testVersion,
testTargetVersion,
JavaVersion.VERSION_1_7,
JavaVersion.VERSION_11,
JavaVersion.VERSION_17
).associateWith {
javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(it.majorVersion))
}.get().executablePath.toString()
}

jvmArgs(
"-Djvmdg.test.version=$version",
"-Djvmdg.test.originalVersion=$testVersion",
"-Djvmdg.test.javaVersion=${versions.keys.joinToString(File.pathSeparator) { it.majorVersion }}",
"-Djvmdg.test.launcher=${versions.values.joinToString(File.pathSeparator)}",
"-Djvmdg.test.downgradeClasspath=${rootProject.sourceSets["shared"].compileClasspath.joinToString(File.pathSeparator) { it.absolutePath }}",
"-Djvmdg.test.downgradePath=${project(":testing:downgrade").tasks.jar.get().outputs.files.singleFile.absolutePath}",
"-Djvmdg.test.multiVersionPath=${project(":testing:multi-version").tasks.jar.get().outputs.files.singleFile.absolutePath}",
"-Djvmdg.test.javaApiPath=${project(":java-api").tasks.named("testJar").get().outputs.files.singleFile.absolutePath}",
)

testLogging {
events.add(TestLogEvent.PASSED)
events.add(TestLogEvent.SKIPPED)
events.add(TestLogEvent.FAILED)
}
}
File renamed without changes.
32 changes: 32 additions & 0 deletions testing/multi-version/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
plugins {
java
}

base {
archivesName = "multi-version"
}
version = "1.0.0"

val testVersion = JavaVersion.toVersion(project.properties["testVersion"] as String)

java {
sourceCompatibility = testVersion
targetCompatibility = testVersion
toolchain {
languageVersion.set(JavaLanguageVersion.of(testVersion.majorVersion))
}
}

tasks.compileJava {
options.encoding = "UTF-8"
}

val removeLibs by tasks.registering {
doLast {
delete(fileTree("dir" to "build/libs", "include" to "**/*.jar"))
}
}

tasks.jar {
dependsOn(removeLibs)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package xyz.wagyourtail.multiversion;

public class MatchExcJ21 {

public static void main(String[] args) {
throw new MatchException("This is a test", null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package xyz.wagyourtail.jvmdg.test.integration;

import xyz.wagyourtail.jvmdg.ClassDowngrader;
import xyz.wagyourtail.jvmdg.cli.Flags;
import xyz.wagyourtail.jvmdg.compile.ZipDowngrader;
import xyz.wagyourtail.jvmdg.test.JavaRunner;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public abstract class BaseIntegrationTests {
private static final String JVMDG_VERSION_KEY = "jvmdg.test.version";
private static final String ORIGINAL_VERSION_KEY = "jvmdg.test.originalVersion";
private static final String JAVA_VERSION_KEY = "jvmdg.test.javaVersion";
private static final String LAUNCHER_KEY = "jvmdg.test.launcher";
private static final String DOWNGRADE_CLASSPATH = "jvmdg.test.downgradeClasspath";

private static final String JAVA_API_PATH = "jvmdg.test.javaApiPath";

public static final Path javaApi = Path.of(System.getProperty(JAVA_API_PATH));
public static final List<Path> downgradeClasspath = Arrays.stream(System.getProperty(DOWNGRADE_CLASSPATH).split(File.pathSeparator)).map(Path::of).toList();

public static final Flags flags = new Flags();

public static final JavaRunner.JavaVersion originalVersion = JavaRunner.JavaVersion.fromMajor(Integer.parseInt(System.getProperty(ORIGINAL_VERSION_KEY)));

private static final List<JavaRunner.JavaVersion> javaVersions = Arrays.stream(System.getProperty(JAVA_VERSION_KEY).split(File.pathSeparator)).map(e -> JavaRunner.JavaVersion.fromMajor(Integer.parseInt(e))).toList();
private static final List<Path> launchers = Arrays.stream(System.getProperty(LAUNCHER_KEY).split(File.pathSeparator)).map(Path::of).toList();

public static final Map<JavaRunner.JavaVersion, Path> launchersByVersion = zip(javaVersions.stream(), launchers.stream()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

static {
flags.api = List.of(javaApi.toFile());
}


private static <T, U> Stream<Map.Entry<T, U>> zip(Stream<T> a, Stream<U> b) {
Iterator<T> aIt = a.iterator();
Iterator<U> bIt = b.iterator();
List<Map.Entry<T, U>> out = new ArrayList<>();
while (aIt.hasNext() && bIt.hasNext()) {
out.add(Map.entry(aIt.next(), bIt.next()));
}
return out.stream();
}

private static final Map<FlagsAndRunner, Path> apiPaths = new ConcurrentHashMap<>();

public static Path getApiPath(FlagsAndRunner flags) {
String fName = javaApi.getFileName().toString();
String withoutExt = fName.substring(0, fName.lastIndexOf('.'));
String ext = fName.substring(fName.lastIndexOf('.'));
return Path.of("./build/tmp/test/" + withoutExt + "-downgrade-" + flags.readableSlug() + ext);
}

public static synchronized Path getApiJar(FlagsAndRunner flags) {
return apiPaths.computeIfAbsent(flags, e -> {
try {
Path target = getApiPath(flags);
ZipDowngrader.downgradeZip(
ClassDowngrader.downgradeTo(flags.flags()),
javaApi,
Set.of(),
target
);
return target;
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
});
}

}
Loading

0 comments on commit 7e86cb6

Please sign in to comment.