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

Test BeanContainer assignability API methods #526

Merged
merged 1 commit into from
Feb 15, 2024

Conversation

Azquelt
Copy link
Contributor

@Azquelt Azquelt commented Jan 12, 2024

Tests for .isMatchingBean and .isMatchingEvent.

Fixes #518

Comment on lines 71 to 72
assertFalse(getCurrentBeanContainer().isMatchingBean(reducedBeanTypes, Set.of(), Object.class, Set.of()),
"Bean matched Object despite it not being in bean types");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wasn't sure about this test.

Every bean has Object as a bean type. Should Object be implied if not passed in the beanTypes set?

Copy link
Contributor

Choose a reason for hiding this comment

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

Every bean has Object as a bean type. Should Object be implied if not passed in the beanTypes set?

Yes, that's always added.
I think there was even a TCK for @Typed that asserted that Object is added automatically.

Copy link

@ljnelson ljnelson Jan 12, 2024

Choose a reason for hiding this comment

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

(Lurking. Curious if this is generally true in the specification (to be clear, I know about https://jakarta.ee/specifications/cdi/4.0/jakarta-cdi-spec-4.0#bean_types)? I see that certainly for @Typed Object.class is implied, but if I make a synthetic bean and explicitly do not include Object.class in its Set<Type>...? For example, https://jakarta.ee/specifications/cdi/4.0/apidocs/jakarta.cdi/jakarta/enterprise/inject/spi/configurator/beanattributesconfigurator#types(java.util.Set) says nothing about Object.class being intrinsically added.)

Copy link
Contributor

Choose a reason for hiding this comment

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

For example, https://jakarta.ee/specifications/cdi/4.0/apidocs/jakarta.cdi/jakarta/enterprise/inject/spi/configurator/beanattributesconfigurator#types(java.util.Set) says nothing about Object.class being intrinsically added.)

Yes, I am aware of that. However, event a synth bean is a bean and as therefore the first link you posted should still apply. At least in my opinion :)
And I think that's also what Weld does; not sure if there is a TCK for it though 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It might be helpful to clarify whether Object is automatically added with a note on the relevant methods.

For this method, it sounds like we think Object should be automatically added to the set of bean types so this test should change to assertTrue?

Copy link
Contributor

Choose a reason for hiding this comment

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

Here's a proposal: jakartaee/cdi#744 I went full on the precise mode, as that's just easiest frankly :-)

Copy link
Contributor

Choose a reason for hiding this comment

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

their own implementation of Bean which may not return Object from getTypes.

That would be an invalid implementation of Bean in my eyes, though the container can compensate for it. All the more reason for using the BeanConfigurator API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here's a proposal

That looks good, thank you :-)

I've updated the test to match.

That would be an invalid implementation of Bean in my eyes

Yeah, I agree and it's not the only way you could have an invalid Bean implementation either (e.g. returning a non-scope annotation from getScope()). I guess we could be more specific about how and when these sorts of errors are handled.

I note that BeanConfigurator is still vulnerable to some of the same issues. You could fail to pass Object to types as @ljnelson suggested, or pass a non-scope annotation to scope and we don't currently document what should happen.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good points. Out of scope of this PR for sure, though :-)

Copy link
Contributor

Choose a reason for hiding this comment

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

If all synthetic beans are beans, and if all beans have the bean type java.lang.Object, then all synthetic beans have the bean type java.lang.Object. That is a straightforward implication in my eyes.

I am of the same opinion.

The only possible downside is that someone might start asking "why don't we automatically scan for all superclasses and implemented interfaces?", and I wouldn't have a good answer :-)

Because you might want to simulate a scenario where @Typed was used and some types might be omitted.

Yeah, I agree and it's not the only way you could have an invalid Bean implementation either (e.g. returning a non-scope annotation from getScope()). I guess we could be more specific about how and when these sorts of errors are handled.

Well, since you can define arbitrary things if you implement Bean yourself, it would be rather extensive to nail it all down...

@Azquelt
Copy link
Contributor Author

Azquelt commented Jan 12, 2024

Also worth noting:

  • The API that these tests depend on hasn't been released yet. I needed to change the cdi.api.version to 4.1.0-SNAPSHOT and build the CDI API locally.
  • These new API methods aren't implemented yet so I haven't been able to run the tests.

@manovotn
Copy link
Contributor

These new API methods aren't implemented yet so I haven't been able to run the tests.

Should be fairly easy to do in Weld, I'll make a branch next week so that we can test it.

@Ladicek
Copy link
Contributor

Ladicek commented Jan 15, 2024

Other assertions LGTM.

Copy link
Contributor

@manovotn manovotn left a comment

Choose a reason for hiding this comment

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

Two more notes besides the one code comment below:

  • I promised you a Weld branch/pr, which is here -> WELD-2774 Implement BM methods for event/bean assignability weld/core#2906. It passes all tests in the PR's current state apart from the one which I believe is wrong.
  • I am also pondering whether we should assert something about validity of input such as annotations not being CDI qualifiers or bean types being invalid. On one hand it would be nice, OTOH every invocation of this method will have to perform a solid amount of verification just because we assume users may not know what they are doing 🤷

@Ladicek
Copy link
Contributor

Ladicek commented Jan 23, 2024

I am also pondering whether we should assert something about validity of input such as annotations not being CDI qualifiers or bean types being invalid. On one hand it would be nice, OTOH every invocation of this method will have to perform a solid amount of verification just because we assume users may not know what they are doing

We talked about this internally, my personal opinion is a bit different in that it is typical for BeanManager methods to validate inputs, so I don't see why these methods shouldn't. Here's my proposal: jakartaee/cdi#748

@manovotn
Copy link
Contributor

I am also pondering whether we should assert something about validity of input such as annotations not being CDI qualifiers or bean types being invalid. On one hand it would be nice, OTOH every invocation of this method will have to perform a solid amount of verification just because we assume users may not know what they are doing

We talked about this internally, my personal opinion is a bit different in that it is typical for BeanManager methods to validate inputs, so I don't see why these methods shouldn't. Here's my proposal: jakartaee/cdi#748

Yea, added my approval there but @Azquelt might want to take a look as well.

We'll also want some TCK coverage for that - the qualifier validation should be an easy one.
The unresolved type variable might be tricky as the only other case in TCK has this marked as testable=false (see here).
Finally legal bean types - that might be harder because we cannot check how was the set of types trimmed; instead we'd have to check that the type was removed and cannot be used to satisfy given required type.

@Azquelt do you wish to look into these or should me or Ladislav take that?

@manovotn manovotn dismissed their stale review January 23, 2024 18:18

Dismissing as my complaint was invalid

@Azquelt
Copy link
Contributor Author

Azquelt commented Jan 24, 2024

@manovotn I'm happy to look at it, I'll add it to this PR unless you'd rather merge this part first.

I understood "legal bean types" to refer to just 2.1.2.1, which I think is only concerned with the type itself and not whether it's been discovered, trimmed or vetoed?

I'm not entirely sure about what an unresolved type variable is (I have also commented on jakartaee/cdi#748), so I'll struggle to test that unless someone can provide an example of something that wouldn't be a valid event object under that rule.

@Ladicek
Copy link
Contributor

Ladicek commented Jan 24, 2024

I'm not entirely sure about what an unresolved type variable is (I have also commented on jakartaee/cdi#748), so I'll struggle to test that unless someone can provide an example of something that wouldn't be a valid event object under that rule.

The audit file for existing tests says that this provision (or rather, the existing provision on which this one is modeled) is untestable. Maybe there's a way to test it, but I'd be fine with leaving that one uncovered.

@manovotn
Copy link
Contributor

I'm not entirely sure about what an unresolved type variable is (I have also commented on jakartaee/cdi#748), so I'll struggle to test that unless someone can provide an example of something that wouldn't be a valid event object under that rule.

The audit file for existing tests says that this provision (or rather, the existing provision on which this one is modeled) is untestable. Maybe there's a way to test it, but I'd be fine with leaving that one uncovered.

Yes that's exactly why I linked the tck file. I would also be fine leaving this untested.

@manovotn
Copy link
Contributor

@manovotn I'm happy to look at it, I'll add it to this PR unless you'd rather merge this part first.

Feel free to add it here. I would rather merge once we release cdi api anyway.

I understood "legal bean types" to refer to just 2.1.2.1, which I think is only concerned with the type itself and not whether it's been discovered, trimmed or vetoed?

Right, it's just a case where a user can pass in a set of types where some would normally be filtered as illegal for given bean

@Azquelt
Copy link
Contributor Author

Azquelt commented Jan 25, 2024

I've added tests for the changes in jakartaee/cdi#748.

The only one that didn't pass against @manovotn's branch was this one for having a type variable in the event type. I'd tried to test this in a similar way to FireEventTest.testTypeVariableEventTypeFails.

@manovotn
Copy link
Contributor

I've added tests for the changes in jakartaee/cdi#748.

The only one that didn't pass against @manovotn's branch was this one for having a type variable in the event type. I'd tried to test this in a similar way to FireEventTest.testTypeVariableEventTypeFails.

Thanks, I'll check on it.

Copy link
Contributor

@manovotn manovotn left a comment

Choose a reason for hiding this comment

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

LGTM
The Weld branch I linked earlier is also passing fine.

@Ladicek
Copy link
Contributor

Ladicek commented Jan 29, 2024

My implementation in ArC also passes.

We should probably merge jakartaee/cdi#748 first and then update the audit files here, but I wouldn't really mind otherwise. Except we'll need a new CDI API release (Alpha2?) first, anyway.

@Azquelt
Copy link
Contributor Author

Azquelt commented Feb 5, 2024

I've rebased and updated the audit files based on jakartaee/cdi#748 now that it's merged.

@manovotn
Copy link
Contributor

@Azquelt master branch of CDI TCK repo now has CDI API 4.1.0.Beta1 - please rebase this PR and we should be able to get it merged

Tests for .isMatchingBean and .isMatchingEvent.
@Azquelt
Copy link
Contributor Author

Azquelt commented Feb 15, 2024

Rebased, squashed and copyright headers updated.

@starksm64 starksm64 merged commit 8d19cf7 into jakartaee:master Feb 15, 2024
4 checks passed
@Ladicek Ladicek added this to the CDI 4.1 milestone Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants