Skip to content

Commit 3feacc6

Browse files
authored
Merge pull request #127 from jgallimore/owb_2.0.x-owb-cdi-junit
OWB-1448 Fix Issue with Cdi annotation and alternatives
2 parents 0376bd7 + 129a538 commit 3feacc6

6 files changed

Lines changed: 184 additions & 3 deletions

File tree

webbeans-impl/src/main/java/org/apache/webbeans/corespi/scanner/AbstractMetaDataDiscovery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public abstract class AbstractMetaDataDiscovery implements BdaScannerService
9090
* We store this information since not all containers and storages do support
9191
* new URL(...).
9292
*/
93-
private final Map<String, URL> beanDeploymentUrls = new HashMap<>();
93+
protected final Map<String, URL> beanDeploymentUrls = new HashMap<>();
9494

9595
/**
9696
* for having proper scan mode 'SCOPED' support we need to know which bean class
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.openwebbeans.junit5.features;
20+
21+
import javax.enterprise.context.ApplicationScoped;
22+
import javax.enterprise.inject.Alternative;
23+
import javax.enterprise.inject.Default;
24+
import javax.inject.Inject;
25+
import org.apache.openwebbeans.junit5.Cdi;
26+
import org.junit.jupiter.api.Test;
27+
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
30+
@Cdi(disableDiscovery = true, classes = {
31+
AlternativeTest.Service.class, AlternativeTest.Provider.class, AlternativeTest.DefaultProvider.class, AlternativeTest.AlternativeProvider.class
32+
}, alternatives = AlternativeTest.AlternativeProvider.class)
33+
public class AlternativeTest
34+
{
35+
@Inject
36+
private Service service;
37+
38+
@Test
39+
void test1()
40+
{
41+
assertEquals("alternative", service.run());
42+
}
43+
44+
public interface Provider
45+
{
46+
String provide();
47+
}
48+
49+
@Alternative
50+
public static class AlternativeProvider implements Provider
51+
{
52+
@Override
53+
public String provide()
54+
{
55+
return "alternative";
56+
}
57+
}
58+
59+
@Default
60+
public static class DefaultProvider implements Provider
61+
{
62+
@Override
63+
public String provide()
64+
{
65+
return "default";
66+
}
67+
}
68+
69+
@ApplicationScoped
70+
public static class Service
71+
{
72+
73+
@Inject
74+
private Provider provider;
75+
76+
public String run()
77+
{
78+
return provider.provide();
79+
}
80+
81+
}
82+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.openwebbeans.junit5.features;
20+
21+
import javax.enterprise.context.ApplicationScoped;
22+
import javax.enterprise.context.NormalScope;
23+
import javax.enterprise.inject.Alternative;
24+
import javax.enterprise.inject.Default;
25+
import javax.inject.Inject;
26+
import javax.interceptor.AroundInvoke;
27+
import javax.interceptor.Interceptor;
28+
import javax.interceptor.InterceptorBinding;
29+
import javax.interceptor.InvocationContext;
30+
import org.apache.openwebbeans.junit5.Cdi;
31+
import org.junit.jupiter.api.Test;
32+
33+
import java.lang.annotation.ElementType;
34+
import java.lang.annotation.Retention;
35+
import java.lang.annotation.RetentionPolicy;
36+
import java.lang.annotation.Target;
37+
38+
import static org.junit.jupiter.api.Assertions.assertEquals;
39+
40+
@Cdi(disableDiscovery = true, classes = {
41+
InterceptorTest.Service.class, InterceptorTest.MyInterceptor.class, InterceptorTest.Wrap.class
42+
}, interceptors = InterceptorTest.MyInterceptor.class)
43+
public class InterceptorTest
44+
{
45+
@Inject
46+
private Service service;
47+
48+
@Test
49+
void test1()
50+
{
51+
assertEquals("Intercepted Hello World", service.run());
52+
}
53+
54+
@Target({ElementType.TYPE, ElementType.METHOD})
55+
@Retention(RetentionPolicy.RUNTIME)
56+
@InterceptorBinding
57+
public @interface Wrap {
58+
59+
}
60+
61+
@Interceptor
62+
@Wrap
63+
public static class MyInterceptor {
64+
@AroundInvoke
65+
public Object restrictAccessBasedOnTime(InvocationContext ctx) throws Exception {
66+
final Object result = ctx.proceed();
67+
if (result instanceof String) {
68+
return "Intercepted " + result;
69+
} else {
70+
return result;
71+
}
72+
}
73+
}
74+
75+
@ApplicationScoped
76+
public static class Service
77+
{
78+
@Wrap
79+
public String run()
80+
{
81+
return "Hello World";
82+
}
83+
}
84+
}

webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/parameter/ParameterResolutionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import static org.junit.jupiter.api.Assertions.assertNull;
3535
import static org.junit.jupiter.api.Assertions.assertSame;
3636

37-
@Cdi(classes = MyService.class)
37+
@Cdi(classes = MyService.class, disableDiscovery = true)
3838
class ParameterResolutionTest
3939
{
4040
@Inject MyService service;

webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/perclass/PerMethodTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public Property[] properties()
180180
@Override
181181
public boolean disableDiscovery()
182182
{
183-
return false;
183+
return true;
184184
}
185185

186186
@Override

webbeans-se/src/main/java/org/apache/openwebbeans/se/CDISeScannerService.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import java.util.ArrayList;
3838
import java.util.Collection;
3939
import java.util.Enumeration;
40+
import java.util.HashSet;
41+
import java.util.Set;
4042
import java.util.stream.Stream;
4143
import java.util.stream.StreamSupport;
4244

@@ -217,4 +219,17 @@ public boolean accept(String name)
217219
return accepts;
218220
}
219221
}
222+
223+
@Override
224+
public Set<URL> getBeanXmls()
225+
{
226+
final Set<URL> result = new HashSet<>(super.getBeanXmls());
227+
final URL embeddedUrl = beanDeploymentUrls.get(CDISeBeanArchiveService.EMBEDDED_URL);
228+
if (embeddedUrl != null)
229+
{
230+
result.add(embeddedUrl);
231+
}
232+
233+
return result;
234+
}
220235
}

0 commit comments

Comments
 (0)