[weld-commits] Weld SVN: r5706 - in core/trunk/tests/src/test: java/org/jboss/weld/tests/extensions/interceptors and 2 other directories.
weld-commits at lists.jboss.org
weld-commits at lists.jboss.org
Tue Feb 2 04:00:54 EST 2010
Author: swd847
Date: 2010-02-02 04:00:53 -0500 (Tue, 02 Feb 2010)
New Revision: 5706
Added:
core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/
core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/FullMarathon.java
core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/Incremented.java
core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/IncrementingInterceptor.java
core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/InterceptorExtension.java
core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/InterceptorExtensionTest.java
core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/LifecycleInterceptor.java
core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/Marathon.java
core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/NumberSource.java
core/trunk/tests/src/test/resources/org/jboss/weld/tests/extensions/interceptors/
core/trunk/tests/src/test/resources/org/jboss/weld/tests/extensions/interceptors/beans.xml
core/trunk/tests/src/test/resources/org/jboss/weld/tests/extensions/interceptors/javax.enterprise.inject.spi.Extension
Log:
WELD-312 tests
Added: core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/FullMarathon.java
===================================================================
--- core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/FullMarathon.java (rev 0)
+++ core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/FullMarathon.java 2010-02-02 09:00:53 UTC (rev 5706)
@@ -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;
+
+ at Retention(RetentionPolicy.RUNTIME)
+ at Target(value = { ElementType.TYPE })
+public @interface FullMarathon
+{
+
+}
Added: core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/Incremented.java
===================================================================
--- core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/Incremented.java (rev 0)
+++ core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/Incremented.java 2010-02-02 09:00:53 UTC (rev 5706)
@@ -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;
+
+ at Retention(RetentionPolicy.RUNTIME)
+ at Target(value = { ElementType.TYPE, ElementType.METHOD })
+public @interface Incremented
+{
+
+}
Added: core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/IncrementingInterceptor.java
===================================================================
--- core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/IncrementingInterceptor.java (rev 0)
+++ core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/IncrementingInterceptor.java 2010-02-02 09:00:53 UTC (rev 5706)
@@ -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 at 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;
+ }
+}
Added: core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/InterceptorExtension.java
===================================================================
--- core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/InterceptorExtension.java (rev 0)
+++ core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/InterceptorExtension.java 2010-02-02 09:00:53 UTC (rev 5706)
@@ -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 at 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
+ {
+ };
+
+}
Added: core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/InterceptorExtensionTest.java
===================================================================
--- core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/InterceptorExtensionTest.java (rev 0)
+++ core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/InterceptorExtensionTest.java 2010-02-02 09:00:53 UTC (rev 5706)
@@ -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 at baileyroberts.com.au>
+ *
+ */
+ at Artifact
+ at IntegrationTest
+ at Packaging(PackagingType.EAR)
+ at Extension("javax.enterprise.inject.spi.Extension")
+ at BeansXml("beans.xml")
+ at 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();
+ }
+
+}
Added: core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/LifecycleInterceptor.java
===================================================================
--- core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/LifecycleInterceptor.java (rev 0)
+++ core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/LifecycleInterceptor.java 2010-02-02 09:00:53 UTC (rev 5706)
@@ -0,0 +1,43 @@
+package org.jboss.weld.tests.extensions.interceptors;
+
+import javax.interceptor.InvocationContext;
+
+/**
+ *
+ * @author Stuart Douglas <stuart at 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;
+ }
+
+}
Added: core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/Marathon.java
===================================================================
--- core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/Marathon.java (rev 0)
+++ core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/Marathon.java 2010-02-02 09:00:53 UTC (rev 5706)
@@ -0,0 +1,18 @@
+package org.jboss.weld.tests.extensions.interceptors;
+
+ at FullMarathon
+public class Marathon
+{
+ long length;
+
+ public long getLength()
+ {
+ return length;
+ }
+
+ public void setLength(long length)
+ {
+ this.length = length;
+ }
+
+}
Added: core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/NumberSource.java
===================================================================
--- core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/NumberSource.java (rev 0)
+++ core/trunk/tests/src/test/java/org/jboss/weld/tests/extensions/interceptors/NumberSource.java 2010-02-02 09:00:53 UTC (rev 5706)
@@ -0,0 +1,15 @@
+package org.jboss.weld.tests.extensions.interceptors;
+
+/**
+ *
+ * @author Stuart Douglas <stuart at baileyroberts.com.au>
+ *
+ */
+public class NumberSource
+{
+ @Incremented
+ public int value()
+ {
+ return 1;
+ }
+}
Added: core/trunk/tests/src/test/resources/org/jboss/weld/tests/extensions/interceptors/beans.xml
===================================================================
--- core/trunk/tests/src/test/resources/org/jboss/weld/tests/extensions/interceptors/beans.xml (rev 0)
+++ core/trunk/tests/src/test/resources/org/jboss/weld/tests/extensions/interceptors/beans.xml 2010-02-02 09:00:53 UTC (rev 5706)
@@ -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>
Added: core/trunk/tests/src/test/resources/org/jboss/weld/tests/extensions/interceptors/javax.enterprise.inject.spi.Extension
===================================================================
--- core/trunk/tests/src/test/resources/org/jboss/weld/tests/extensions/interceptors/javax.enterprise.inject.spi.Extension (rev 0)
+++ core/trunk/tests/src/test/resources/org/jboss/weld/tests/extensions/interceptors/javax.enterprise.inject.spi.Extension 2010-02-02 09:00:53 UTC (rev 5706)
@@ -0,0 +1 @@
+org.jboss.weld.tests.extensions.interceptors.InterceptorExtension
\ No newline at end of file
More information about the weld-commits
mailing list