Skip to content

Commit 2f4298b

Browse files
committed
test: added IT test for integrations
Also included a minor fixes for setting the main class from integrations and correctly closing integration class loaders.
1 parent 776fac4 commit 2f4298b

File tree

7 files changed

+121
-13
lines changed

7 files changed

+121
-13
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
//FILES META-INF/jbang-integration.list=jbang-integration.list
3+
4+
import java.nio.file.Path;
5+
import java.util.*;
6+
7+
public class integration {
8+
/**
9+
*
10+
* @param temporaryJar temporary JAR file path
11+
* @param pomFile location of pom.xml representing the projects dependencies
12+
* @param repositories list of the used repositories
13+
* @param dependencies list of GAV to Path of artifact/classpath dependencies
14+
* @param comments comments from the source file
15+
* @param nativeImage true if --native been requested
16+
* @return Map<String, Object> map of returns; special keys are "native-image" which is a and "files" to
17+
* return native-image to be run and list of files to get written to the output directory.
18+
*
19+
*/
20+
public static Map<String, Object> postBuild(
21+
Path temporaryJar,
22+
Path pomFile,
23+
List<Map.Entry<String, String>> repositories,
24+
List<Map.Entry<String, Path>> dependencies,
25+
List<String> comments,
26+
boolean nativeImage) {
27+
System.out.println("Integration called!");
28+
System.out.println("TMPJAR: " + temporaryJar);
29+
System.out.println("POM: " + pomFile);
30+
for (Map.Entry<String, String> r : repositories) {
31+
System.out.println("REPO: " + r.getKey() + " -> " + r.getValue());
32+
}
33+
for (Map.Entry<String, Path> d : dependencies) {
34+
System.out.println("DEP: " + d.getKey() + " -> " + d.getValue());
35+
}
36+
for (String c : comments) {
37+
System.out.println("COMMENT: " + c);
38+
}
39+
40+
String myTest = comments.stream()
41+
.filter(s -> s.startsWith("//MY:TEST "))
42+
.findFirst()
43+
.map(s -> s.substring("//MY:TEST ".length()))
44+
.orElse("default");
45+
Map<String, Object> result = new HashMap<>();
46+
result.put("java-args", Arrays.asList("-Dbar=" + myTest));
47+
result.put("main-class", "altmain");
48+
return result;
49+
}
50+
51+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
integration

itests/integrations/main.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
3+
//DEPS integration.java
4+
//RUNTIME_OPTIONS -Dfoo=fubar
5+
//MY:TEST aap
6+
7+
public class main {
8+
9+
public static void main(String... args) {
10+
System.out.println("Wrong main!!!");
11+
}
12+
}
13+
14+
class altmain {
15+
16+
public static void main(String... args) {
17+
System.out.println("Hello World!");
18+
System.out.println("foo: " + System.getProperty("foo"));
19+
System.out.println("bar: " + System.getProperty("bar"));
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package dev.jbang.it;
2+
3+
import static dev.jbang.it.CommandResultAssert.assertThat;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class IntegrationIT extends BaseIT {
8+
@Test
9+
void testIntegration() {
10+
assertThat(shell("jbang --fresh integrations/main.java"))
11+
.outContains("Hello World")
12+
.outContains("foo: fubar")
13+
.outContains("bar: aap");
14+
}
15+
16+
@Test
17+
void testNoIntegration() {
18+
assertThat(shell("jbang --fresh --no-integrations integrations/main.java"))
19+
.outContains("Wrong main!!!");
20+
}
21+
}

src/main/java/dev/jbang/source/buildsteps/IntegrationBuildStep.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ public IntegrationResult build() throws IOException {
3737
try {
3838
IntegrationResult integrationResult = IntegrationManager.runIntegrations(ctx);
3939

40-
if (project.getMainClass() == null) { // if non-null user forced set main
41-
if (integrationResult.mainClass != null) {
42-
project.setMainClass(integrationResult.mainClass);
43-
}
40+
// TODO the compile step always sets the main class if it can find one
41+
// so this check is no longer valid. Figure out if we can make this work
42+
// if (project.getMainClass() == null) { // if non-null user forced set main
43+
if (integrationResult.mainClass != null) {
44+
project.setMainClass(integrationResult.mainClass);
4445
}
46+
// }
4547
if (integrationResult.javaArgs != null && !integrationResult.javaArgs.isEmpty()) {
4648
// Add integration options to the java options
4749
project.addRuntimeOptions(integrationResult.javaArgs);

src/main/java/dev/jbang/spi/IntegrationManager.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,14 @@ public static IntegrationResult runIntegrations(BuildContext ctx) {
7878
for (ArtifactInfo art : ctx.resolveClassPath().getArtifacts()) {
7979
if (art.getCoordinate() != null) { // skipping dependencies that does not have a GAV
8080
deps.put(art.getCoordinate().toCanonicalForm(), art.getFile());
81+
} else {
82+
deps.put(art.getFile().getFileName().toString(), art.getFile());
8183
}
8284
}
8385

8486
List<String> comments = source.getTags().map(s -> "//" + s).collect(Collectors.toList());
85-
ClassLoader old = Thread.currentThread().getContextClassLoader();
8687
PrintStream oldout = System.out;
87-
try {
88-
URLClassLoader integrationCl = getClassLoader(deps.values());
89-
Thread.currentThread().setContextClassLoader(integrationCl);
88+
try (URLClassLoader integrationCl = getClassLoader(deps.values())) {
9089
String requestedJavaVersion = prj.getJavaVersion();
9190
Set<String> classNames = loadIntegrationClassNames(integrationCl);
9291
for (String className : classNames) {
@@ -95,10 +94,12 @@ public static IntegrationResult runIntegrations(BuildContext ctx) {
9594
: null;
9695
IntegrationInput input = new IntegrationInput(className, srcPath, compileDir, pomPath, repos, deps,
9796
comments, prj.isNativeImage(), Util.isVerbose());
98-
IntegrationResult ir = requestedJavaVersion == null || JavaUtil.satisfiesRequestedVersion(
99-
requestedJavaVersion, JavaUtil.getCurrentMajorJavaVersion())
100-
? runIntegrationEmbedded(input, integrationCl)
101-
: runIntegrationExternal(input, prj.getProperties(), prj.projectJdk());
97+
boolean embedded = (requestedJavaVersion == null || JavaUtil.satisfiesRequestedVersion(
98+
requestedJavaVersion, JavaUtil.getCurrentMajorJavaVersion()))
99+
&& !"true".equals(System.getProperty("jbang.build.integration.forceExternal"));
100+
IntegrationResult ir = embedded
101+
? runIntegrationEmbedded(input, integrationCl)
102+
: runIntegrationExternal(input, prj.getProperties(), prj.projectJdk());
102103
result = result.merged(ir);
103104
}
104105
} catch (ClassNotFoundException e) {
@@ -111,7 +112,6 @@ public static IntegrationResult runIntegrations(BuildContext ctx) {
111112
} catch (Exception e) {
112113
throw new ExitException(EXIT_GENERIC_ERROR, "Issue running postBuild()", e);
113114
} finally {
114-
Thread.currentThread().setContextClassLoader(old);
115115
System.setOut(oldout);
116116
}
117117
return result;
@@ -157,6 +157,17 @@ private static Set<String> loadIntegrationClassNames(URLClassLoader integrationC
157157

158158
private static IntegrationResult runIntegrationEmbedded(IntegrationInput input, URLClassLoader integrationCl)
159159
throws Exception {
160+
ClassLoader old = Thread.currentThread().getContextClassLoader();
161+
try {
162+
Thread.currentThread().setContextClassLoader(integrationCl);
163+
return runIntegrationEmbedded_(input, integrationCl);
164+
} finally {
165+
Thread.currentThread().setContextClassLoader(old);
166+
}
167+
}
168+
169+
private static IntegrationResult runIntegrationEmbedded_(IntegrationInput input, URLClassLoader integrationCl)
170+
throws Exception {
160171
Util.infoMsg("Post build with " + input.integrationClassName);
161172

162173
if (input.source != null) {

src/test/java/dev/jbang/BaseTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ void initEnv(@TempDir Path tempPath) throws IOException {
6767
Util.setCwd(cwdDir);
6868
System.setProperty("user.home", tempPath.toString());
6969
System.setProperty("maven.repo.local", mavenTempDir.toString());
70+
// System.setProperty("jbang.build.integration.forceExternal", "true");
7071
// Each test gets a fresh JBang config folder
7172
environmentVariables.set(Settings.JBANG_DIR, jbangTempDir.toString());
7273
// Each test gets a fresh cache folder

0 commit comments

Comments
 (0)