added unit test

This commit is contained in:
2022-05-09 22:05:09 +08:00
parent bedff34ecf
commit f60b25bfea
9 changed files with 198 additions and 5 deletions

View File

@@ -1,3 +1,5 @@
import java.nio.file.Files
plugins {
id 'java-gradle-plugin'
id 'net.woggioni.gradle.lombok' apply false
@@ -57,10 +59,7 @@ java {
targetCompatibility = JavaVersion.VERSION_1_8
}
jar {
manifest {
attributes "version" : archiveVersion.get()
}
tasks.named('processResources', ProcessResources) {
from {
configurations.named('embedded').map {
it.collect {
@@ -73,6 +72,12 @@ jar {
}
}
jar {
manifest {
attributes "version" : archiveVersion.get()
}
}
gradlePlugin {
plugins {
create("EnvelopePlugin") {
@@ -100,4 +105,26 @@ wrapper {
distributionType = Wrapper.DistributionType.ALL
}
tasks.named('processTestResources') { ProcessResources it ->
doLast {
Files.newBufferedWriter(it.destinationDir.toPath().resolve('test-resources.txt')).withCloseable { writer ->
sourceSets.test.resources.sourceDirectories.each { srcDir ->
java.nio.file.Path srcPath = srcDir.toPath()
Files.walk(srcPath).forEach {
if(Files.isRegularFile(it) && srcPath != it) {
writer.write(srcPath.relativize(it).toString() + '\n')
}
}
}
}
}
}
tasks.named('test', Test) {
systemProperty('test.gradle.user.home', temporaryDir)
}
tasks.named("pluginUnderTestMetadata", PluginUnderTestMetadata) {
pluginClasspath.from(configurations.compileClasspath)
}

View File

@@ -30,7 +30,7 @@ public class EnvelopePlugin implements Plugin<Project> {
t.getInputs().files(envelopeJarTaskProvider);
t.setGroup("envelope");
t.setDescription("Run the application in the envelope jar");
t.classpath(envelopeJarTaskProvider);
t.classpath(envelopeJarTaskProvider);
});
}
}

View File

@@ -0,0 +1,130 @@
package net.woggioni.gradle.envelope.test;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import org.apache.groovy.util.Arrays;
import org.gradle.testkit.runner.GradleRunner;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.io.TempDir;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.Buffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.security.MessageDigest;
import java.util.Optional;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.junit.jupiter.api.Assertions;
import static java.util.concurrent.TimeUnit.MINUTES;
public class EnvelopePluginTest {
private static URL getResource(String resourceName) {
return getResource(resourceName, null, null);
}
private static URL getResource(String resourceName, ClassLoader cl) {
return getResource(resourceName, null, cl);
}
private static URL getResource(String resourceName, Class<?> cls) {
return getResource(resourceName, cls, null);
}
private static URL getResource(String resourceName, Class<?> cls, ClassLoader cl) {
URL result = null;
if(cl != null) {
result = cl.getResource(resourceName);
}
if(result == null && cls != null) {
result = cls.getClassLoader().getResource(resourceName);
}
if(result == null) {
result = EnvelopePluginTest.class.getClassLoader().getResource(resourceName);
}
return result;
}
// @SneakyThrows
// private static void installResource(String root, String resourceName, Path destination) {
// Path outputFile = with {
// Path realDestination;
// if (Files.isSymbolicLink(destination)) {
// realDestination = destination.toRealPath();
// } else {
// realDestination = destination;
// }
// realDestination = realDestination.resolve(resourceName);
// if(!Files.exists(realDestination)) {
// Files.createDirectories(realDestination.getParent());
// realDestination;
// } else if(Files.isDirectory(realDestination)) {
// realDestination.resolve(resourceName.substring(1 + resourceName.lastIndexOf('/')));
// }
// else if(Files.isRegularFile(realDestination)) {
// realDestination;
// } else throw new IllegalStateException("Path '${realDestination}' is neither a file nor a directory");
// }
// String resourcePath = root + '/' + resourceName;
// URL res = getResource(resourcePath);
// if(res == null) throw new FileNotFoundException(resourceName);
// try(InputStream inputStream = res.openStream()) {
// Files.copy(inputStream, outputFile, StandardCopyOption.REPLACE_EXISTING);
// }
// }
void invokeGradle(Path rootProjectDir, String... taskName) {
GradleRunner runner = GradleRunner.create()
.withDebug(true)
.withProjectDir(rootProjectDir.toFile())
.withArguments(Arrays.concat(new String[] {"-s", "--info", "-g", testGradleHomeDir.toString()}, taskName))
.withPluginClasspath();
System.out.println(runner.build().getOutput());
}
private Path testDir;
private static final Path testGradleHomeDir = Paths.get(System.getProperty("test.gradle.user.home"));
@BeforeEach
@SneakyThrows
public void setup(@TempDir Path testDir) {
this.testDir = testDir;
String resourceFile = "test-resources.txt";
try(BufferedReader reader =
Optional.ofNullable(getResource(resourceFile).openStream())
.map(InputStreamReader::new)
.map(BufferedReader::new)
.orElseThrow(() -> new FileNotFoundException(resourceFile))) {
while(true) {
String line = reader.readLine();
if(line == null) break;
Path destination = testDir.resolve(line);
Files.createDirectories(destination.getParent());
Files.copy(getResource(line).openStream(), destination);
}
}
}
@Test
void legacyTest() {
invokeGradle(testDir.resolve("test-project"), ":legacy-executable:envelopeRun");
}
@Test
void jpmsTest() {
invokeGradle(testDir.resolve("test-project"), ":jpms-executable:envelopeRun");
}
}

View File

@@ -0,0 +1,9 @@
plugins {
id 'java-library'
id 'net.woggioni.gradle.envelope'
}
envelopeJar {
mainClass = 'envelope.test.jpms.Main'
mainModule = 'envelope.test.jpms'
}

View File

@@ -0,0 +1,7 @@
package envelope.test.jpms;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!!");
}
}

View File

@@ -0,0 +1,3 @@
module envelope.test.jpms {
exports envelope.test.jpms;
}

View File

@@ -0,0 +1,8 @@
plugins {
id 'java-library'
id 'net.woggioni.gradle.envelope'
}
envelopeJar {
mainClass = 'envelope.test.legacy.Main'
}

View File

@@ -0,0 +1,7 @@
package envelope.test.legacy;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!!");
}
}

View File

@@ -0,0 +1,2 @@
include 'legacy-executable'
include 'jpms-executable'