-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
415 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
...pl/src/test/java/org/apache/webbeans/test/interceptors/owb1441/CustomInterceptorTest.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,61 @@ | ||
/* | ||
* 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.webbeans.test.interceptors.owb1441; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import org.apache.webbeans.test.AbstractUnitTest; | ||
import org.apache.webbeans.test.interceptors.interceptorbean.BigBrotherInterceptor; | ||
import org.apache.webbeans.test.interceptors.interceptorbean.BigBrothered; | ||
import org.apache.webbeans.test.interceptors.interceptorbean.BigBrotheredExtension; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* Test for programmatically added Interceptors | ||
*/ | ||
public class CustomInterceptorTest extends AbstractUnitTest | ||
{ | ||
@Test | ||
public void testCustomInterceptor() throws Exception | ||
{ | ||
WatchExtension bbe = new WatchExtension(); | ||
addExtension(bbe); | ||
startContainer(LittleJohnDoe.class); | ||
|
||
final LittleJohnDoe joe = getInstance(LittleJohnDoe.class); | ||
|
||
joe.makeACoffee(); | ||
assertTrue(WatchInterceptor.isObserved()); | ||
|
||
joe.watchTv(); | ||
assertTrue(WatchInterceptor.isObserved()); | ||
} | ||
|
||
@ApplicationScoped | ||
@BigBrothered | ||
public static class LittleJohnDoe | ||
{ | ||
public void makeACoffee() { | ||
System.out.println("Making a coffee"); | ||
} | ||
|
||
public void watchTv() { | ||
System.out.println("Watching TV"); | ||
} | ||
} | ||
} |
165 changes: 165 additions & 0 deletions
165
...eans-impl/src/test/java/org/apache/webbeans/test/interceptors/owb1441/WatchExtension.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,165 @@ | ||
/* | ||
* 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.webbeans.test.interceptors.owb1441; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Type; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import jakarta.enterprise.event.Observes; | ||
import jakarta.enterprise.inject.spi.AfterBeanDiscovery; | ||
import jakarta.enterprise.inject.spi.AnnotatedConstructor; | ||
import jakarta.enterprise.inject.spi.AnnotatedField; | ||
import jakarta.enterprise.inject.spi.AnnotatedMethod; | ||
import jakarta.enterprise.inject.spi.AnnotatedParameter; | ||
import jakarta.enterprise.inject.spi.AnnotatedType; | ||
import jakarta.enterprise.inject.spi.BeanManager; | ||
import jakarta.enterprise.inject.spi.BeforeBeanDiscovery; | ||
import jakarta.enterprise.inject.spi.Extension; | ||
import jakarta.enterprise.util.Nonbinding; | ||
import org.apache.webbeans.test.interceptors.interceptorbean.BigBrotherInterceptorBean; | ||
import org.apache.webbeans.test.interceptors.interceptorbean.BigBrothered; | ||
|
||
public class WatchExtension implements Extension | ||
{ | ||
public void registerInterceptorBinding(@Observes BeforeBeanDiscovery beforeBeanDiscovery, BeanManager beanManager) { | ||
beforeBeanDiscovery.addInterceptorBinding( | ||
new WatchedAnnotatedType(beanManager.createAnnotatedType(Watched.class))); | ||
|
||
} | ||
|
||
public void registerInterceptor(@Observes AfterBeanDiscovery afterBeanDiscovery) { | ||
afterBeanDiscovery.addBean(new WatchInterceptorBean(42)); | ||
} | ||
|
||
// To add Nonbinding to @Watched members | ||
@SuppressWarnings("unchecked") | ||
static class WatchedAnnotatedType implements AnnotatedType<Watched> { | ||
private final AnnotatedType<Watched> delegate; | ||
private final Set<AnnotatedMethod<? super Watched>> methods; | ||
|
||
WatchedAnnotatedType(final AnnotatedType<Watched> delegate) { | ||
this.delegate = delegate; | ||
this.methods = new HashSet<>(); | ||
|
||
for (AnnotatedMethod<? super Watched> method : delegate.getMethods()) { | ||
methods.add(new AnnotatedMethod<>() { | ||
private final AnnotatedMethod<Watched> delegate = (AnnotatedMethod<Watched>) method; | ||
private final Set<Annotation> annotations = Collections.singleton(Nonbinding.Literal.INSTANCE); | ||
|
||
@Override | ||
public Method getJavaMember() { | ||
return delegate.getJavaMember(); | ||
} | ||
|
||
@Override | ||
public List<AnnotatedParameter<Watched>> getParameters() { | ||
return delegate.getParameters(); | ||
} | ||
|
||
@Override | ||
public boolean isStatic() { | ||
return delegate.isStatic(); | ||
} | ||
|
||
@Override | ||
public AnnotatedType<Watched> getDeclaringType() { | ||
return delegate.getDeclaringType(); | ||
} | ||
|
||
@Override | ||
public Type getBaseType() { | ||
return delegate.getBaseType(); | ||
} | ||
|
||
@Override | ||
public Set<Type> getTypeClosure() { | ||
return delegate.getTypeClosure(); | ||
} | ||
|
||
@Override | ||
public <T extends Annotation> T getAnnotation(final Class<T> annotationType) { | ||
if (annotationType.equals(Nonbinding.class)) { | ||
return (T) annotations.iterator().next(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Set<Annotation> getAnnotations() { | ||
return annotations; | ||
} | ||
|
||
@Override | ||
public boolean isAnnotationPresent(final Class<? extends Annotation> annotationType) { | ||
return annotationType.equals(Nonbinding.class); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
@Override | ||
public Class<Watched> getJavaClass() { | ||
return delegate.getJavaClass(); | ||
} | ||
|
||
@Override | ||
public Set<AnnotatedConstructor<Watched>> getConstructors() { | ||
return delegate.getConstructors(); | ||
} | ||
|
||
@Override | ||
public Set<AnnotatedMethod<? super Watched>> getMethods() { | ||
return this.methods; | ||
} | ||
|
||
@Override | ||
public Set<AnnotatedField<? super Watched>> getFields() { | ||
return delegate.getFields(); | ||
} | ||
|
||
@Override | ||
public Type getBaseType() { | ||
return delegate.getBaseType(); | ||
} | ||
|
||
@Override | ||
public Set<Type> getTypeClosure() { | ||
return delegate.getTypeClosure(); | ||
} | ||
|
||
@Override | ||
public <T extends Annotation> T getAnnotation(final Class<T> annotationType) { | ||
return delegate.getAnnotation(annotationType); | ||
} | ||
|
||
@Override | ||
public Set<Annotation> getAnnotations() { | ||
return delegate.getAnnotations(); | ||
} | ||
|
||
@Override | ||
public boolean isAnnotationPresent(final Class<? extends Annotation> annotationType) { | ||
return delegate.isAnnotationPresent(annotationType); | ||
} | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...ns-impl/src/test/java/org/apache/webbeans/test/interceptors/owb1441/WatchInterceptor.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,30 @@ | ||
package org.apache.webbeans.test.interceptors.owb1441; | ||
|
||
import jakarta.interceptor.AroundInvoke; | ||
import jakarta.interceptor.InvocationContext; | ||
|
||
public class WatchInterceptor { | ||
|
||
public WatchInterceptor(String totallyUselessParamJustToNotHaveADefaultCt) { | ||
|
||
} | ||
|
||
private static boolean observed = false; | ||
|
||
@AroundInvoke | ||
public Object invoke(InvocationContext context) throws Exception | ||
{ | ||
System.out.println("I am watching you " + context.getMethod()); | ||
observed = true; | ||
|
||
return context.proceed(); | ||
} | ||
|
||
public static boolean isObserved() | ||
{ | ||
boolean wasObserved = observed; | ||
observed = false; | ||
return wasObserved; | ||
} | ||
|
||
} |
124 changes: 124 additions & 0 deletions
124
...mpl/src/test/java/org/apache/webbeans/test/interceptors/owb1441/WatchInterceptorBean.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,124 @@ | ||
/* | ||
* 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.webbeans.test.interceptors.owb1441; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import jakarta.enterprise.context.Dependent; | ||
import jakarta.enterprise.context.spi.CreationalContext; | ||
import jakarta.enterprise.inject.spi.InjectionPoint; | ||
import jakarta.enterprise.inject.spi.InterceptionType; | ||
import jakarta.enterprise.inject.spi.Interceptor; | ||
import jakarta.enterprise.util.AnnotationLiteral; | ||
import jakarta.interceptor.InvocationContext; | ||
import org.apache.webbeans.annotation.DefaultLiteral; | ||
import org.apache.webbeans.test.interceptors.interceptorbean.BigBrotherInterceptor; | ||
import org.apache.webbeans.test.interceptors.interceptorbean.BigBrothered; | ||
|
||
public class WatchInterceptorBean implements Interceptor<WatchInterceptor> | ||
{ | ||
// it's good performance practice to keep the sets static as they are requested tons of times! | ||
public static final Set<Type> TYPES = Set.of(WatchInterceptor.class); | ||
public static final Set<Annotation> QUALIFIERS = Set.of(DefaultLiteral.INSTANCE); | ||
public static final Set<Annotation> INTERCEPTOR_BINDINGS = Set.of(new AnnotationLiteral<BigBrothered>() {}); | ||
|
||
public WatchInterceptorBean(int totallyUselessParamJustToNotHaveADefaultCt) | ||
{ | ||
// all fine ;) | ||
} | ||
|
||
@Override | ||
public Set<Annotation> getInterceptorBindings() | ||
{ | ||
return INTERCEPTOR_BINDINGS; | ||
} | ||
|
||
@Override | ||
public boolean intercepts(InterceptionType type) | ||
{ | ||
return InterceptionType.AROUND_INVOKE.equals(type); | ||
} | ||
|
||
@Override | ||
public Object intercept(InterceptionType type, WatchInterceptor instance, InvocationContext ctx) throws Exception | ||
{ | ||
return instance.invoke(ctx); | ||
} | ||
|
||
@Override | ||
public Class<?> getBeanClass() | ||
{ | ||
return WatchInterceptor.class; | ||
} | ||
|
||
@Override | ||
public Set<InjectionPoint> getInjectionPoints() | ||
{ | ||
return Collections.emptySet(); | ||
} | ||
|
||
@Override | ||
public WatchInterceptor create(CreationalContext<WatchInterceptor> creationalContext) | ||
{ | ||
return new WatchInterceptor(""); | ||
} | ||
|
||
@Override | ||
public void destroy(WatchInterceptor instance, CreationalContext<WatchInterceptor> creationalContext) | ||
{ | ||
// no op | ||
} | ||
|
||
@Override | ||
public Set<Type> getTypes() | ||
{ | ||
return TYPES; | ||
} | ||
|
||
@Override | ||
public Set<Annotation> getQualifiers() | ||
{ | ||
return QUALIFIERS; | ||
} | ||
|
||
@Override | ||
public Class<? extends Annotation> getScope() | ||
{ | ||
return Dependent.class; | ||
} | ||
|
||
@Override | ||
public String getName() | ||
{ | ||
return null; | ||
} | ||
|
||
@Override | ||
public Set<Class<? extends Annotation>> getStereotypes() | ||
{ | ||
return Collections.emptySet(); | ||
} | ||
|
||
@Override | ||
public boolean isAlternative() | ||
{ | ||
return false; | ||
} | ||
} |
Oops, something went wrong.