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
1 parent
f41e1ac
commit 5ee064d
Showing
10 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/FullMarathon.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,13 @@ | ||
package org.jboss.weld.tests.extensions.interceptors; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(value = { ElementType.TYPE }) | ||
public @interface FullMarathon | ||
{ | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/Incremented.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,13 @@ | ||
package org.jboss.weld.tests.extensions.interceptors; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(value = { ElementType.TYPE, ElementType.METHOD }) | ||
public @interface Incremented | ||
{ | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...s/src/test/java/org/jboss/weld/tests/extensions/interceptors/IncrementingInterceptor.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,27 @@ | ||
package org.jboss.weld.tests.extensions.interceptors; | ||
|
||
import javax.interceptor.InvocationContext; | ||
|
||
/** | ||
* Interceptor that adds one to the result of a method | ||
* | ||
* @author Stuart Douglas <stuart@baileyroberts.com.au> | ||
* | ||
*/ | ||
public class IncrementingInterceptor | ||
{ | ||
private static boolean doAroundCalled = false; | ||
|
||
// @AroundInvoke | ||
public Object doAround(InvocationContext context) throws Exception | ||
{ | ||
doAroundCalled = true; | ||
Integer res = (Integer)context.proceed(); | ||
return res + 1; | ||
} | ||
|
||
public static boolean isDoAroundCalled() | ||
{ | ||
return doAroundCalled; | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/InterceptorExtension.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,95 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2010, Red Hat, Inc., and individual contributors | ||
* by the @authors tag. See the copyright.txt in the distribution for a | ||
* full listing of individual contributors. | ||
* | ||
* Licensed 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.jboss.weld.tests.extensions.interceptors; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.annotation.PreDestroy; | ||
import javax.enterprise.event.Observes; | ||
import javax.enterprise.inject.spi.BeanManager; | ||
import javax.enterprise.inject.spi.BeforeBeanDiscovery; | ||
import javax.enterprise.inject.spi.Extension; | ||
import javax.enterprise.util.AnnotationLiteral; | ||
import javax.interceptor.AroundInvoke; | ||
import javax.interceptor.Interceptor; | ||
import javax.interceptor.InvocationContext; | ||
|
||
import org.jboss.weld.tests.util.annotated.TestAnnotatedTypeBuilder; | ||
|
||
/** | ||
* | ||
* @author Stuart Douglas <stuart@baileyroberts.com.au> | ||
* | ||
*/ | ||
public class InterceptorExtension implements Extension | ||
{ | ||
/** | ||
* registers two interceptors via the SPI | ||
*/ | ||
public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery event, BeanManager beanManager) throws SecurityException, NoSuchMethodException | ||
{ | ||
event.addInterceptorBinding(Incremented.class); | ||
event.addInterceptorBinding(FullMarathon.class); | ||
|
||
TestAnnotatedTypeBuilder<IncrementingInterceptor> incBuilder = new TestAnnotatedTypeBuilder<IncrementingInterceptor>(IncrementingInterceptor.class); | ||
incBuilder.addToClass(new InterceptorLiteral()); | ||
incBuilder.addToClass(new IncrementedLiteral()); | ||
|
||
Method around = IncrementingInterceptor.class.getMethod("doAround", InvocationContext.class); | ||
incBuilder.addToMethod(around, new AroundInvokeLiteral()); | ||
event.addAnnotatedType(incBuilder.create()); | ||
|
||
TestAnnotatedTypeBuilder<LifecycleInterceptor> marBuilder = new TestAnnotatedTypeBuilder<LifecycleInterceptor>(LifecycleInterceptor.class); | ||
marBuilder.addToClass(new InterceptorLiteral()); | ||
marBuilder.addToClass(new FullMarathonImpl()); | ||
|
||
Method pre = LifecycleInterceptor.class.getMethod("preDestroy", InvocationContext.class); | ||
marBuilder.addToMethod(pre, new PreDestroyLiteral()); | ||
|
||
Method post = LifecycleInterceptor.class.getMethod("postConstruct", InvocationContext.class); | ||
marBuilder.addToMethod(post, new PostConstructLiteral()); | ||
|
||
event.addAnnotatedType(marBuilder.create()); | ||
|
||
} | ||
|
||
private static class InterceptorLiteral extends AnnotationLiteral<Interceptor> implements Interceptor | ||
{ | ||
}; | ||
|
||
private static class IncrementedLiteral extends AnnotationLiteral<Incremented> implements Incremented | ||
{ | ||
}; | ||
|
||
private static class AroundInvokeLiteral extends AnnotationLiteral<AroundInvoke> implements AroundInvoke | ||
{ | ||
}; | ||
|
||
private static class PreDestroyLiteral extends AnnotationLiteral<PreDestroy> implements PreDestroy | ||
{ | ||
}; | ||
|
||
private static class PostConstructLiteral extends AnnotationLiteral<PostConstruct> implements PostConstruct | ||
{ | ||
}; | ||
|
||
private static class FullMarathonImpl extends AnnotationLiteral<FullMarathon> implements FullMarathon | ||
{ | ||
}; | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
.../src/test/java/org/jboss/weld/tests/extensions/interceptors/InterceptorExtensionTest.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,68 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source | ||
* Copyright 2010, Red Hat, Inc., and individual contributors | ||
* by the @authors tag. See the copyright.txt in the distribution for a | ||
* full listing of individual contributors. | ||
* | ||
* Licensed 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.jboss.weld.tests.extensions.interceptors; | ||
|
||
import javax.enterprise.context.spi.CreationalContext; | ||
import javax.enterprise.inject.spi.Bean; | ||
|
||
import org.jboss.testharness.impl.packaging.Artifact; | ||
import org.jboss.testharness.impl.packaging.Classes; | ||
import org.jboss.testharness.impl.packaging.IntegrationTest; | ||
import org.jboss.testharness.impl.packaging.Packaging; | ||
import org.jboss.testharness.impl.packaging.PackagingType; | ||
import org.jboss.testharness.impl.packaging.jsr299.BeansXml; | ||
import org.jboss.testharness.impl.packaging.jsr299.Extension; | ||
import org.jboss.weld.test.AbstractWeldTest; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* Tests that interceptors registered via the SPI work correctly | ||
* | ||
* @author Stuart Douglas <stuart@baileyroberts.com.au> | ||
* | ||
*/ | ||
@Artifact | ||
@IntegrationTest | ||
@Packaging(PackagingType.EAR) | ||
@Extension("javax.enterprise.inject.spi.Extension") | ||
@BeansXml("beans.xml") | ||
@Classes(packages = { "org.jboss.weld.tests.util.annotated" }) | ||
public class InterceptorExtensionTest extends AbstractWeldTest | ||
{ | ||
@Test(groups={"broken"}) | ||
public void testInterceptorCalled() | ||
{ | ||
NumberSource ng = getReference(NumberSource.class); | ||
assert ng.value() == 2; | ||
assert IncrementingInterceptor.isDoAroundCalled(); | ||
} | ||
|
||
@Test(groups={"broken"}) | ||
@SuppressWarnings("unchecked") | ||
public void testLifecycleInterceptor() | ||
{ | ||
Bean bean = getCurrentManager().getBeans(Marathon.class).iterator().next(); | ||
CreationalContext creationalContext = getCurrentManager().createCreationalContext(bean); | ||
Marathon m = (Marathon)bean.create(creationalContext); | ||
|
||
assert LifecycleInterceptor.isPostConstructCalled(); | ||
assert m.getLength()==42; | ||
bean.destroy(m, creationalContext); | ||
assert LifecycleInterceptor.isPreDestroyCalled(); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/LifecycleInterceptor.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,43 @@ | ||
package org.jboss.weld.tests.extensions.interceptors; | ||
|
||
import javax.interceptor.InvocationContext; | ||
|
||
/** | ||
* | ||
* @author Stuart Douglas <stuart@baileyroberts.com.au> | ||
* | ||
*/ | ||
public class LifecycleInterceptor | ||
{ | ||
static private boolean preDestroyCalled = false; | ||
static private boolean postConstructCalled = false; | ||
|
||
// PreDestroy | ||
public void preDestroy(InvocationContext ctx) | ||
{ | ||
preDestroyCalled = true; | ||
} | ||
|
||
// @PostConstruct | ||
public void postConstruct(InvocationContext ctx) | ||
{ | ||
Object marathon = ctx.getTarget(); | ||
if (marathon instanceof Marathon) | ||
{ | ||
Marathon m = (Marathon) marathon; | ||
m.setLength(42); | ||
} | ||
postConstructCalled = true; | ||
} | ||
|
||
static public boolean isPostConstructCalled() | ||
{ | ||
return postConstructCalled; | ||
} | ||
|
||
static public boolean isPreDestroyCalled() | ||
{ | ||
return preDestroyCalled; | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/Marathon.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,18 @@ | ||
package org.jboss.weld.tests.extensions.interceptors; | ||
|
||
@FullMarathon | ||
public class Marathon | ||
{ | ||
long length; | ||
|
||
public long getLength() | ||
{ | ||
return length; | ||
} | ||
|
||
public void setLength(long length) | ||
{ | ||
this.length = length; | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/NumberSource.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,15 @@ | ||
package org.jboss.weld.tests.extensions.interceptors; | ||
|
||
/** | ||
* | ||
* @author Stuart Douglas <stuart@baileyroberts.com.au> | ||
* | ||
*/ | ||
public class NumberSource | ||
{ | ||
@Incremented | ||
public int value() | ||
{ | ||
return 1; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/src/test/resources/org/jboss/weld/tests/extensions/interceptors/beans.xml
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,6 @@ | ||
<beans> | ||
<interceptors> | ||
<class>org.jboss.weld.tests.extensions.interceptors.IncrementingInterceptor</class> | ||
<class>org.jboss.weld.tests.extensions.interceptors.LifecycleInterceptor</class> | ||
</interceptors> | ||
</beans> |
1 change: 1 addition & 0 deletions
1
...ources/org/jboss/weld/tests/extensions/interceptors/javax.enterprise.inject.spi.Extension
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 @@ | ||
org.jboss.weld.tests.extensions.interceptors.InterceptorExtension |