Skip to content
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

Provide easy way for QuarkusTestResourceLifecycleManager implementations to inject into test #18714

Merged
merged 1 commit into from
Jul 16, 2021

Conversation

geoand
Copy link
Contributor

@geoand geoand commented Jul 15, 2021

Resolves: #18698

* @param annotations Optional annotations that the field must have. If more than one are specified, the field
* must be annotated with all the provided annotations
*/
void injectIntoFields(Object fieldValue, Class<?> fieldType, Class<? extends Annotation>... annotations);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could add a variant with javax.enterprise.util.TypeLiteral<?> fieldType to handle the parameterized types but the problem is that you would have to specify the rules for matching... or rely on existing rules, e.g. from CDI. But these need to be inherently complex and thus it's probably not worth the effort.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

I would prefer we start with this simple approach for now and keep your suggestion in mind for when / if people need something more complex

@quarkus-bot
Copy link

quarkus-bot bot commented Jul 15, 2021

This workflow status is outdated as a new workflow run has been triggered.

🚫 This workflow run has been cancelled.

Failing Jobs - Building eb43068

⚠️ Artifacts of the workflow run were not available thus the report misses some details.

Status Name Step Test failures Logs Raw logs
Gradle Tests - JDK 11 Windows Build ⚠️ Check → Logs Raw logs
JVM Tests - JDK 11 Build ⚠️ Check → Logs Raw logs
JVM Tests - JDK 11 Windows Build ⚠️ Check → Logs Raw logs
JVM Tests - JDK 16 Build ⚠️ Check → Logs Raw logs
Native Tests - Cache Build ⚠️ Check → Logs Raw logs
Native Tests - DevTools Integration Tests ⚠️ Check → Logs Raw logs
Native Tests - HTTP Build ⚠️ Check → Logs Raw logs
Native Tests - Messaging1 Build ⚠️ Check → Logs Raw logs
Native Tests - Messaging2 Build ⚠️ Check → Logs Raw logs
Native Tests - Misc1 Download Maven Repo ⚠️ Check → Logs Raw logs
Native Tests - Misc2 ⚠️ Check → Logs Raw logs
Native Tests - Misc3 ⚠️ Check → Logs Raw logs
Native Tests - Misc4 ⚠️ Check → Logs Raw logs
Native Tests - Security1 Build ⚠️ Check → Logs Raw logs
Native Tests - Security2 Build ⚠️ Check → Logs Raw logs
Native Tests - Security3 Build ⚠️ Check → Logs Raw logs
Native Tests - Spring ⚠️ Check → Logs Raw logs
Native Tests - Windows - hibernate-validator ⚠️ Check → Logs Raw logs
Native Tests - gRPC ⚠️ Check → Logs Raw logs

@quarkus-bot
Copy link

quarkus-bot bot commented Jul 15, 2021

This workflow status is outdated as a new workflow run has been triggered.

Failing Jobs - Building ec38e0a

Status Name Step Test failures Logs Raw logs
Gradle Tests - JDK 11 Build Test failures Logs Raw logs

Full information is available in the Build summary check run.

Test Failures

⚙️ Gradle Tests - JDK 11 #

📦 integration-tests/gradle

io.quarkus.gradle.MultiSourceProjectTest.shouldRunTest() line 16 - More details - Source on GitHub

if (annotationMatchResult == AnnotationMatchResult.MATCHED) {
// if the annotations match, we throw an exception if the types don't match as it's almost certainly a user error
// if however there were no annotation supplied, then we continue searching for candidate fields
throw new RuntimeException("'" + Arrays.toString(annotations)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, this prevents some thing like:

@InjectTestObject
Foo foo;

@InjectTestObject
Bar bar;

when injectIntoFields(foo, Foo.class, InjectTestObject.class) is called?

Isn't that too strict when a manager provides multiple objects that can be injected? I mean we would force users to create multiple annotations for that (or none at all).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is indeed true.

When writing this, I did get the feeling that the type parameter is likely unnecessary.
What do you think about making it an Optional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is that then the method becomes difficult to use...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm going to do something different. I'll have two methods: One which only takes an annotation as a param and one which takes a Predicate. That way if more advanced checking is needed, a user can specify it.

Copy link
Member

@famod famod Jul 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, even though it's more "hidden"/less expressive than using an annotation, injection simply by type is the easiest option after all because the user doesn't have to implement an annotation for that (KISS).

So I think (ideally) there should be four options:

  • inject by type
  • inject by annotation
  • inject by type and annotation
  • inject by whatever via Predicate

The first tree methods could simply call that Predicate method... 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then you'll end up with a new CDI ;-).

I think that injectIntoFields(Object value, Predicate<Field> predicate) is flexible enough.

Copy link
Member

@famod famod Jul 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I thought there is much more to CDI than that. 😉
Anyway, my primary focus was to support lazy devs (aka developer joy) but yeah, you can do everything with the Predicate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the PR. WDYT?

@quarkus-bot
Copy link

quarkus-bot bot commented Jul 16, 2021

This workflow status is outdated as a new workflow run has been triggered.

🚫 This workflow run has been cancelled.

Failing Jobs - Building 876caec

⚠️ Artifacts of the workflow run were not available thus the report misses some details.

Status Name Step Test failures Logs Raw logs
Initial JDK 11 Build Build ⚠️ Check → Logs Raw logs

Copy link
Member

@famod famod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
A MatchesType predicate would make it complete or even a dead simple injectIntoFields(Object fieldValue) that infers the type from fieldValue, but both can be added later.

@geoand geoand changed the title Provide easy way for QuarkusTestResourceLifecycleManager impls to inject into test Provide easy way for QuarkusTestResourceLifecycleManager implementations to inject into test Jul 16, 2021
@geoand geoand added triage/waiting-for-ci Ready to merge when CI successfully finishes and removed area/hibernate-search Hibernate Search labels Jul 16, 2021
Copy link
Contributor

@mkouba mkouba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@quarkus-bot
Copy link

quarkus-bot bot commented Jul 16, 2021

This workflow status is outdated as a new workflow run has been triggered.

Failing Jobs - Building 341d7e1

Status Name Step Test failures Logs Raw logs
JVM Tests - JDK 11 Build Test failures Logs Raw logs
JVM Tests - JDK 11 Windows Build Test failures Logs Raw logs
JVM Tests - JDK 16 Build Test failures Logs Raw logs
Native Tests - Misc3 Build Test failures Logs Raw logs

Full information is available in the Build summary check run.

Test Failures

⚙️ JVM Tests - JDK 11 #

📦 integration-tests/openshift-client

io.quarkus.it.openshift.client.OpenShiftClientTest.createRoute line 29 - More details - Source on GitHub

io.quarkus.it.openshift.client.OpenShiftClientTest.getRoutes line 49 - More details - Source on GitHub


⚙️ JVM Tests - JDK 11 Windows #

📦 integration-tests/openshift-client

io.quarkus.it.openshift.client.OpenShiftClientTest.createRoute line 29 - More details - Source on GitHub

io.quarkus.it.openshift.client.OpenShiftClientTest.getRoutes line 49 - More details - Source on GitHub


⚙️ JVM Tests - JDK 16 #

📦 integration-tests/hibernate-reactive-panache

io.quarkus.it.panache.reactive.PanacheFunctionalityTest.testPanacheFunctionality line 47 - More details - Source on GitHub

📦 integration-tests/openshift-client

io.quarkus.it.openshift.client.OpenShiftClientTest.createRoute line 29 - More details - Source on GitHub

io.quarkus.it.openshift.client.OpenShiftClientTest.getRoutes line 49 - More details - Source on GitHub


⚙️ Native Tests - Misc3 #

📦 integration-tests/openshift-client

io.quarkus.it.openshift.client.OpenShiftClientTestIT.createRoute - More details - Source on GitHub

io.quarkus.it.openshift.client.OpenShiftClientTestIT.getRoutes - More details - Source on GitHub

@quarkus-bot
Copy link

quarkus-bot bot commented Jul 16, 2021

This workflow status is outdated as a new workflow run has been triggered.

🚫 This workflow run has been cancelled.

Failing Jobs - Building 3f640de

⚠️ Artifacts of the workflow run were not available thus the report misses some details.

Status Name Step Test failures Logs Raw logs
Initial JDK 11 Build Build ⚠️ Check → Logs Raw logs

@geoand geoand merged commit 328f64b into quarkusio:main Jul 16, 2021
@quarkus-bot quarkus-bot bot removed the triage/waiting-for-ci Ready to merge when CI successfully finishes label Jul 16, 2021
@quarkus-bot quarkus-bot bot added this to the 2.2 - main milestone Jul 16, 2021
@geoand geoand deleted the #18698 branch July 17, 2021 07:46
@gsmet gsmet modified the milestones: 2.2 - main, 2.1.0.Final Jul 19, 2021
Copy link
Member

@FroMage FroMage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, sorry about the late review :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add basic injection capabilities to QuarkusTestResourceLifecycleManager
5 participants