Skip to content

Commit

Permalink
WELD-493 Converted ejb and interceptor tests to Arquillian
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakknutsen committed Aug 5, 2010
1 parent ecfb52c commit 36db20e
Show file tree
Hide file tree
Showing 28 changed files with 598 additions and 136 deletions.
@@ -0,0 +1,51 @@
/*
* 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.ejb;

import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.BeanArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
import org.jboss.weld.tests.category.Integration;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

@Category(Integration.class)
@RunWith(Arquillian.class)
public class EJBTest
{
@Deployment
public static Archive<?> deploy()
{
return ShrinkWrap.create(EnterpriseArchive.class, "test.ear")
.addModule(
ShrinkWrap.create(BeanArchive.class)
.addPackage(EJBTest.class.getPackage())
);
}

@Test
public void testNoInterface(Cow cow)
{
cow.ping();
Assert.assertTrue(cow.isPinged());
}
}
Expand Up @@ -24,25 +24,43 @@
import javax.jms.Session;
import javax.naming.InitialContext;

import org.jboss.testharness.impl.packaging.Artifact;
import org.jboss.testharness.impl.packaging.IntegrationTest;
import org.jboss.testharness.impl.packaging.Packaging;
import org.jboss.testharness.impl.packaging.PackagingType;
import org.jboss.weld.test.AbstractWeldTest;
import org.testng.annotations.Test;
import junit.framework.Assert;

@Artifact
@Packaging(PackagingType.EAR)
@IntegrationTest
public class EJBTest extends AbstractWeldTest
import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.weld.tests.category.Broken;
import org.jboss.weld.tests.category.Integration;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

@Category(Integration.class)
@RunWith(Arquillian.class)
public class EJBTest
{

public static final String MESSAGE = "Hello!";

@Test(groups="broken")
@Deployment
public static Archive<?> deploy()
{
return ShrinkWrap.create(EnterpriseArchive.class, "test.ear")
.addModule(
ShrinkWrap.create(JavaArchive.class)
.addPackage(EJBTest.class.getPackage())
.addManifestResource(EmptyAsset.INSTANCE, "beans.xml")
//.addManifestResource(EJBTest.class.getPackage(), "test-destinations-service.xml", "test-destinations-service.xml")
);
}

@Category(Broken.class)
@Test
// TODO Need a way to deploy test-destinations-service.xml to JBoss AS
// Port the test as broken
public void testMdbUsable() throws Exception
public void testMdbUsable(Control control) throws Exception
{
InitialContext ctx = new InitialContext();
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
Expand All @@ -51,9 +69,11 @@ public void testMdbUsable() throws Exception
Queue queue = (Queue) ctx.lookup("queue/testQueue");
QueueSender sender = session.createSender(queue);
sender.send(session.createTextMessage(MESSAGE));

// TODO: rewrite to use CountDownLatch, avoid Thread.sleep in tests
Thread.sleep(1000);
assert getReference(Control.class).isMessageDelivered();
assert getReference(Control.class).isContextSet();
Assert.assertTrue(control.isMessageDelivered());
Assert.assertTrue(control.isContextSet());
}

}
@@ -0,0 +1,88 @@
/*
* 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.interceptors.ejb;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.ejb.Timer;
import javax.enterprise.inject.spi.InterceptionType;
import javax.inject.Inject;

import junit.framework.Assert;

import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.BeanArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
import org.jboss.weld.bean.SessionBean;
import org.jboss.weld.bean.interceptor.InterceptorBindingsAdapter;
import org.jboss.weld.ejb.spi.InterceptorBindings;
import org.jboss.weld.manager.BeanManagerImpl;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class EnterpriseBeanInterceptionTest
{
@Deployment
public static Archive<?> deploy()
{
return ShrinkWrap.create(EnterpriseArchive.class, "test.ear")
.addModule(
ShrinkWrap.create(BeanArchive.class)
.intercept(Goalkeeper.class, Defender.class, Referee.class)
.addPackage(EnterpriseBeanInterceptionTest.class.getPackage())
);
}

@Inject
private BeanManagerImpl beanManager;

@Test
public void testInterceptors() throws Exception
{
SessionBean<Ball> ballSessionBean = (SessionBean<Ball>)beanManager.getBeans(Ball.class).iterator().next();
InterceptorBindings interceptorBindings = new InterceptorBindingsAdapter(beanManager.getCdiInterceptorsRegistry().getInterceptionModel(ballSessionBean.getType()));
List<javax.enterprise.inject.spi.Interceptor> interceptors =
new ArrayList<javax.enterprise.inject.spi.Interceptor>(interceptorBindings.getAllInterceptors());

Assert.assertEquals(3, interceptors.size());
List<Class<?>> expectedInterceptors = Arrays.<Class<?>>asList(Goalkeeper.class, Defender.class, Referee.class);
Assert.assertTrue(expectedInterceptors.contains(interceptors.get(0).getBeanClass()));
Assert.assertTrue(expectedInterceptors.contains(interceptors.get(1).getBeanClass()));
Assert.assertTrue(expectedInterceptors.contains(interceptors.get(2).getBeanClass()));


Assert.assertEquals(0, interceptorBindings.getMethodInterceptors(InterceptionType.AROUND_TIMEOUT, ballSessionBean.getType().getMethod("shoot")).size());
Assert.assertEquals(1, interceptorBindings.getMethodInterceptors(InterceptionType.AROUND_INVOKE, ballSessionBean.getType().getMethod("shoot")).size());
Assert.assertEquals(Goalkeeper.class, interceptorBindings.getMethodInterceptors(InterceptionType.AROUND_INVOKE, ballSessionBean.getType().getMethod("shoot")).get(0).getBeanClass());

Assert.assertEquals(0, interceptorBindings.getMethodInterceptors(InterceptionType.AROUND_TIMEOUT, ballSessionBean.getType().getMethod("pass")).size());
Assert.assertEquals(1, interceptorBindings.getMethodInterceptors(InterceptionType.AROUND_INVOKE, ballSessionBean.getType().getMethod("pass")).size());
Assert.assertEquals(Defender.class, interceptorBindings.getMethodInterceptors(InterceptionType.AROUND_INVOKE, ballSessionBean.getType().getMethod("pass")).get(0).getBeanClass());

Method finishGameMethod = ballSessionBean.getType().getMethod("finishGame", Timer.class);
Assert.assertEquals(0, interceptorBindings.getMethodInterceptors(InterceptionType.AROUND_INVOKE, finishGameMethod).size());
Assert.assertEquals(1, interceptorBindings.getMethodInterceptors(InterceptionType.AROUND_TIMEOUT, finishGameMethod).size());
Assert.assertEquals(Referee.class, interceptorBindings.getMethodInterceptors(InterceptionType.AROUND_TIMEOUT, finishGameMethod).get(0).getBeanClass());
}
}
@@ -0,0 +1,30 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, 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.interceptors.generic;

/**
* @author Marius Bogoevici
*/
public abstract class AbstractMessageProcessor<I>
{
void process(I input) {
// do nothing;
}

abstract I generate();
}
@@ -0,0 +1,36 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, 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.interceptors.generic;

/**
* @author Marius Bogoevici
*/
public class IntegerProcessor extends AbstractMessageProcessor<Integer>
{
@Override
void process(Integer input)
{
super.process(input); //To change body of overridden methods use File | Settings | File Templates.
}

@Override
Integer generate()
{
return 1;
}
}
@@ -0,0 +1,67 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, 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.interceptors.generic;

import java.util.Collection;
import java.util.List;

import junit.framework.Assert;

import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.BeanArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.weld.introspector.WeldClass;
import org.jboss.weld.introspector.WeldMethod;
import org.jboss.weld.introspector.jlr.WeldClassImpl;
import org.jboss.weld.metadata.TypeStore;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.tests.category.Broken;
import org.jboss.weld.util.Beans;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

/**
* @author Marius Bogoevici
*/
@RunWith(Arquillian.class)
public class SimpleWeldClassTest
{
@Deployment
public static Archive<?> deploy()
{
return ShrinkWrap.create(BeanArchive.class)
.addPackage(SimpleWeldClassTest.class.getPackage());
}

/*
* description = "WELD-568"
*/
@Category(Broken.class)
@Test
public void testWeldClassForGenericSuperclass()
{
WeldClass<StringProcessor> weldClass = WeldClassImpl.of(StringProcessor.class, new ClassTransformer(new TypeStore()));
Collection<WeldMethod<?, ?>> methods = weldClass.getWeldMethods();
//assert methods.size() == 2;
List<WeldMethod<?,?>> interceptableMethods = Beans.getInterceptableMethods(weldClass);
Assert.assertEquals(4, interceptableMethods.size());
}

}
@@ -0,0 +1,36 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, 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.interceptors.generic;

/**
* @author Marius Bogoevici
*/
public class StringProcessor extends AbstractMessageProcessor<String>
{
@Override
void process(String input)
{
super.process(input);
}

@Override
String generate()
{
return "1";
}
}

0 comments on commit 36db20e

Please sign in to comment.