Skip to content

Commit

Permalink
WELD-312 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Feb 2, 2010
1 parent f41e1ac commit 5ee064d
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 0 deletions.
@@ -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
{

}
@@ -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
{

}
@@ -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;
}
}
@@ -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
{
};

}
@@ -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();
}

}
@@ -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;
}

}
@@ -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;
}

}
@@ -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;
}
}
@@ -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>
@@ -0,0 +1 @@
org.jboss.weld.tests.extensions.interceptors.InterceptorExtension

0 comments on commit 5ee064d

Please sign in to comment.