-
Notifications
You must be signed in to change notification settings - Fork 61
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
OWB-1448 Fix Issue with Cdi annotation and alternatives #126
Open
jgallimore
wants to merge
4
commits into
apache:main
Choose a base branch
from
jgallimore:main-owb-cdi-junit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
82 changes: 82 additions & 0 deletions
82
webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/features/AlternativeTest.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,82 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.openwebbeans.junit5.features; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.inject.Alternative; | ||
import jakarta.enterprise.inject.Default; | ||
import jakarta.inject.Inject; | ||
import org.apache.openwebbeans.junit5.Cdi; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@Cdi(disableDiscovery = true, classes = { | ||
AlternativeTest.Service.class, AlternativeTest.Provider.class, AlternativeTest.DefaultProvider.class, AlternativeTest.AlternativeProvider.class | ||
}, alternatives = AlternativeTest.AlternativeProvider.class) | ||
public class AlternativeTest | ||
{ | ||
@Inject | ||
private Service service; | ||
|
||
@Test | ||
void test1() | ||
{ | ||
assertEquals("alternative", service.run()); | ||
} | ||
|
||
public interface Provider | ||
{ | ||
String provide(); | ||
} | ||
|
||
@Alternative | ||
public static class AlternativeProvider implements Provider | ||
{ | ||
@Override | ||
public String provide() | ||
{ | ||
return "alternative"; | ||
} | ||
} | ||
|
||
@Default | ||
public static class DefaultProvider implements Provider | ||
{ | ||
@Override | ||
public String provide() | ||
{ | ||
return "default"; | ||
} | ||
} | ||
|
||
@ApplicationScoped | ||
public static class Service | ||
{ | ||
|
||
@Inject | ||
private Provider provider; | ||
|
||
public String run() | ||
{ | ||
return provider.provide(); | ||
} | ||
|
||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/features/InterceptorTest.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,84 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.openwebbeans.junit5.features; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.NormalScope; | ||
import jakarta.enterprise.inject.Alternative; | ||
import jakarta.enterprise.inject.Default; | ||
import jakarta.inject.Inject; | ||
import jakarta.interceptor.AroundInvoke; | ||
import jakarta.interceptor.Interceptor; | ||
import jakarta.interceptor.InterceptorBinding; | ||
import jakarta.interceptor.InvocationContext; | ||
import org.apache.openwebbeans.junit5.Cdi; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@Cdi(disableDiscovery = true, classes = { | ||
InterceptorTest.Service.class, InterceptorTest.MyInterceptor.class, InterceptorTest.Wrap.class | ||
}, interceptors = InterceptorTest.MyInterceptor.class) | ||
public class InterceptorTest | ||
{ | ||
@Inject | ||
private Service service; | ||
|
||
@Test | ||
void test1() | ||
{ | ||
assertEquals("Intercepted Hello World", service.run()); | ||
} | ||
|
||
@Target({ElementType.TYPE, ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@InterceptorBinding | ||
public @interface Wrap { | ||
|
||
} | ||
|
||
@Interceptor | ||
@Wrap | ||
public static class MyInterceptor { | ||
@AroundInvoke | ||
public Object restrictAccessBasedOnTime(InvocationContext ctx) throws Exception { | ||
final Object result = ctx.proceed(); | ||
if (result instanceof String) { | ||
return "Intercepted " + result; | ||
} else { | ||
return result; | ||
} | ||
} | ||
} | ||
|
||
@ApplicationScoped | ||
public static class Service | ||
{ | ||
@Wrap | ||
public String run() | ||
{ | ||
return "Hello World"; | ||
} | ||
} | ||
} |
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
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.
wonder why the url is not in the scanner since it should so we do not need that hack at all in impl
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 was hoping there would be a
public static final URL CDI_STANDALONE = ...
somewhere, in order to avoid this. org.apache.openwebbeans.se.CDISeBeanArchiveService#EMBEDDED_URL was the closest I could find, but is a String as opposed to a URL.Maybe we could make this a constant URL in org.apache.webbeans.xml.DefaultBeanArchiveService?
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.
hmm not sure I got it right but my point is that this code shouldn't be since the url should be injected in the scanner in SE mode so in owb-impl the url should already be seen (thanks addDeploymentUrl call)
what we likely do not want is impl to depend on se (like in this pr)
hope it makes more sense phrased this way
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 think I understand :-).
Happy to debug around
addDeploymentUrl
, and see what I can find. What I can tell you is that if you revert out the BeansDeployer change in this PR, the tests in this PR will fail (or at least they do for me). I'm very happy to look at a different fix (I might have questions.... :-) ), but I do think a fix is needed.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.
Think we all agree the API should be respected and the regression be fixed if any (maybe java related, didnt check if latest jre changed url handling), but think we should stick to the original design of se on top of impl and not a cycle dep and url 100% taken from the scanner, even virtual ones which were just a hack to make it work without a new spi. This part can change if it help - but think we can make it work, there should be something fishy making the fake url dropped somewhere in the process.
can try to help to debug next week if there is no new finding
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.
addDeploymentUrl
is called fromorg.apache.openwebbeans.se.CDISeScannerService#addClassesDeploymentUrl
with the embedded URL (great), however, thescanner.getBeanXmls()
call fromorg.apache.webbeans.config.BeansDeployer#deployFromXML
does not return that URL, asaddClassesDeploymentUrl
adds the URL toorg.apache.webbeans.corespi.scanner.AbstractMetaDataDiscovery#beanDeploymentUrls
andscanner.getBeanXmls()
gets the URLs fromorg.apache.webbeans.corespi.scanner.AbstractMetaDataDiscovery#beanArchiveLocations
.Could we just include the embedded URL in
org.apache.webbeans.corespi.scanner.AbstractMetaDataDiscovery#beanArchiveLocations
?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.
Doing this:
in
org.apache.webbeans.corespi.scanner.AbstractMetaDataDiscovery#addDeploymentUrl
works. Would that change be ok?
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.
guess it will need to call doAddWebBeansXmlLocation but looks like the additional call to do
that said doing it in addDeploymentUrl will change the lifecycle for the nominal case so i'd just align on the nominal case so adjust CDISeScannerService (a bit like web scanner has web-inb url handling specifically)
hope it makes sense