-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ArC: fix in-application build compatible extensions #37397
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...loyment/src/test/java/io/quarkus/arc/test/cdi/bcextensions/SynthBeanForExternalClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package io.quarkus.arc.test.cdi.bcextensions; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.Dependent; | ||
import jakarta.enterprise.inject.Instance; | ||
import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; | ||
import jakarta.enterprise.inject.build.compatible.spi.Parameters; | ||
import jakarta.enterprise.inject.build.compatible.spi.Synthesis; | ||
import jakarta.enterprise.inject.build.compatible.spi.SyntheticBeanCreator; | ||
import jakarta.enterprise.inject.build.compatible.spi.SyntheticBeanDisposer; | ||
import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents; | ||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.test.supplement.SomeClassInExternalLibrary; | ||
import io.quarkus.builder.Version; | ||
import io.quarkus.maven.dependency.Dependency; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class SynthBeanForExternalClass { | ||
// the test includes an _application_ that declares a build compatible extension | ||
// (in the Runtime CL), which creates a synthetic bean for a class that is _outside_ | ||
// of the application (in the Base Runtime CL) | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(MyBean.class, MyExtension.class, MySyntheticBeanCreator.class) | ||
.addAsServiceProvider(BuildCompatibleExtension.class, MyExtension.class)) | ||
// we need a non-application archive, so cannot use `withAdditionalDependency()` | ||
.setForcedDependencies(List.of(Dependency.of("io.quarkus", "quarkus-arc-test-supplement", Version.getVersion()))); | ||
|
||
@Inject | ||
MyBean bean; | ||
|
||
@Test | ||
public void test() { | ||
assertFalse(MySyntheticBeanCreator.created); | ||
assertFalse(MySyntheticBeanDisposer.disposed); | ||
|
||
assertEquals("OK", bean.doSomething()); | ||
assertTrue(MySyntheticBeanCreator.created); | ||
assertTrue(MySyntheticBeanDisposer.disposed); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class MyBean { | ||
@Inject | ||
Instance<SomeClassInExternalLibrary> someClass; | ||
|
||
public String doSomething() { | ||
SomeClassInExternalLibrary instance = someClass.get(); | ||
instance.toString(); // force instantiating the bean | ||
someClass.destroy(instance); // force destroying the instance | ||
return "OK"; | ||
} | ||
} | ||
|
||
public static class MyExtension implements BuildCompatibleExtension { | ||
@Synthesis | ||
public void synthesis(SyntheticComponents syn) { | ||
syn.addBean(SomeClassInExternalLibrary.class) | ||
.type(SomeClassInExternalLibrary.class) | ||
.scope(Dependent.class) | ||
.createWith(MySyntheticBeanCreator.class) | ||
.disposeWith(MySyntheticBeanDisposer.class); | ||
} | ||
} | ||
|
||
public static class MySyntheticBeanCreator implements SyntheticBeanCreator<SomeClassInExternalLibrary> { | ||
public static boolean created; | ||
|
||
public SomeClassInExternalLibrary create(Instance<Object> lookup, Parameters params) { | ||
SomeClassInExternalLibrary result = new SomeClassInExternalLibrary(); | ||
created = true; | ||
return result; | ||
} | ||
} | ||
|
||
public static class MySyntheticBeanDisposer implements SyntheticBeanDisposer<SomeClassInExternalLibrary> { | ||
public static boolean disposed; | ||
|
||
@Override | ||
public void dispose(SomeClassInExternalLibrary instance, Instance<Object> lookup, Parameters params) { | ||
disposed = true; | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
.../src/test/java/io/quarkus/arc/test/cdi/bcextensions/SynthObserverAsIfInExternalClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package io.quarkus.arc.test.cdi.bcextensions; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.event.Event; | ||
import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension; | ||
import jakarta.enterprise.inject.build.compatible.spi.Parameters; | ||
import jakarta.enterprise.inject.build.compatible.spi.Synthesis; | ||
import jakarta.enterprise.inject.build.compatible.spi.SyntheticComponents; | ||
import jakarta.enterprise.inject.build.compatible.spi.SyntheticObserver; | ||
import jakarta.enterprise.inject.spi.EventContext; | ||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.test.supplement.SomeClassInExternalLibrary; | ||
import io.quarkus.builder.Version; | ||
import io.quarkus.maven.dependency.Dependency; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class SynthObserverAsIfInExternalClass { | ||
// the test includes an _application_ that declares a build compatible extension | ||
// (in the Runtime CL), which creates a synthetic observer which is "as if" declared | ||
// in a class that is _outside_ of the application (in the Base Runtime CL) | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(MyBean.class, MyExtension.class, MySyntheticObserver.class) | ||
.addAsServiceProvider(BuildCompatibleExtension.class, MyExtension.class)) | ||
// we need a non-application archive, so cannot use `withAdditionalDependency()` | ||
.setForcedDependencies(List.of(Dependency.of("io.quarkus", "quarkus-arc-test-supplement", Version.getVersion()))); | ||
|
||
@Inject | ||
MyBean bean; | ||
|
||
@Test | ||
public void test() { | ||
assertFalse(MySyntheticObserver.notified); | ||
|
||
assertEquals("OK", bean.doSomething()); | ||
assertTrue(MySyntheticObserver.notified); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class MyBean { | ||
@Inject | ||
Event<String> event; | ||
|
||
public String doSomething() { | ||
event.fire(""); // force notifying the observer | ||
return "OK"; | ||
} | ||
} | ||
|
||
public static class MyExtension implements BuildCompatibleExtension { | ||
@Synthesis | ||
public void synthesis(SyntheticComponents syn) { | ||
syn.addObserver(String.class) | ||
.declaringClass(SomeClassInExternalLibrary.class) | ||
.observeWith(MySyntheticObserver.class); | ||
} | ||
} | ||
|
||
public static class MySyntheticObserver implements SyntheticObserver<String> { | ||
public static boolean notified; | ||
|
||
@Override | ||
public void observe(EventContext<String> event, Parameters params) { | ||
notified = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>quarkus-arc-parent</artifactId> | ||
<groupId>io.quarkus</groupId> | ||
<version>999-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>quarkus-arc-test-supplement</artifactId> | ||
<name>Quarkus - ArC - Test Supplement</name> | ||
<description>Supplement archive for ArC tests</description> | ||
|
||
</project> |
4 changes: 4 additions & 0 deletions
4
...t-supplement/src/main/java/io/quarkus/arc/test/supplement/SomeClassInExternalLibrary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package io.quarkus.arc.test.supplement; | ||
|
||
public class SomeClassInExternalLibrary { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I wonder if this artifact will get released and deployed to the maven central as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably. I was thinking I could abuse some other dependency of the ArC
deployment
module, like AssertJ, but that would require keeping the version string in the test in sync with the POM, which felt like a worse deal than an extra module with a single class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could try to skip this module during a release. I've found https://stackoverflow.com/questions/31495316/skip-submodule-during-maven-release where the following config is used in the module that should be skipped:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would work, but the ArC
deployment
module has a [test-scoped] dependency on this artifact, soquarkus-arc-deployment
POM might cause errors. Is this tiny artifact a problem? I'd much rather keep it than to face surprises down the line.It would be best if I could create an in-memory archive using ShrinkWrap and add it as a non-application archive, but
QuarkusUnitTest
doesn't offer an API for that and I don't really know ifQuarkusBootstrap.Builder
allows it or not. @aloubyansky would you please know if that would be feasible?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not. The central repo is probably full of other huge useless artifacts anyway 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
QuarkusUnitTest
has a nice API for creating application archives using ShrinkWrap, but for this particular test, I need a non-application archive.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually haven't tested that. It may fail during dependency resolution due to a missing pom.xml. What I tested was
BootstrapAppModelResolver.resolveModel(WorkspaceModule module)
.But if anything, we could probably enhance the API to do what we need. It just haven't come up before, afaict.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to do something like
(where
groupId
,artifactId
andversion
are things I made up, andpath
is the directory where the.class
file is), and when I add thatdep
to forced dependencies, Quarkus tries to download it from Central.I also tried to add an
AdditionalDependency
with theforceApplicationArchive
flag set tofalse
to the set of additional dependencies, but it doesn't do what I need (probably is still considered an application archive).Having an extra module in the ArC extension isn't super pretty, but works as I need, so I'll probably leave it be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It probably fails to resolve the POM artifact.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It actually tries to download both the POM and the JAR:
🤷