Skip to content

Commit

Permalink
Testcase for AS7-6739
Browse files Browse the repository at this point in the history
  • Loading branch information
jharting authored and jaikiran committed Apr 4, 2013
1 parent 17944b5 commit 051c6d4
Show file tree
Hide file tree
Showing 7 changed files with 366 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright (c) 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.test.integration.weld.ejb.interceptor.ordering.lifecycle;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ActionSequence {

private static final List<String> actions = Collections.synchronizedList(new ArrayList<String>());

public static void addAction(String action) {
actions.add(action);
}

public static List<String> getActions() {
return Collections.unmodifiableList(actions);
}

public static void reset() {
actions.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright (c) 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.test.integration.weld.ejb.interceptor.ordering.lifecycle;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.interceptor.InterceptorBinding;

@InterceptorBinding
@Inherited
@Target({ TYPE })
@Retention(RUNTIME)
public @interface CdiIntercepted {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright (c) 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.test.integration.weld.ejb.interceptor.ordering.lifecycle;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

@Interceptor
@CdiIntercepted
public class CdiInterceptor implements Serializable {

private static final long serialVersionUID = -5949866804898740300L;

@PostConstruct
void postConstruct(InvocationContext ctx) {
try {
ActionSequence.addAction(CdiInterceptor.class.getSimpleName());
ctx.proceed();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@PreDestroy
void preDestroy(InvocationContext ctx) {
try {
ActionSequence.addAction(CdiInterceptor.class.getSimpleName());
ctx.proceed();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright (c) 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.test.integration.weld.ejb.interceptor.ordering.lifecycle;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateful;
import javax.interceptor.Interceptors;

@Stateful
@Interceptors(LegacyInterceptor.class)
@CdiIntercepted
public class InterceptedBean extends Superclass {

@PostConstruct
void postConstruct() {
ActionSequence.addAction(InterceptedBean.class.getSimpleName());
}

@PreDestroy
void preDestroy() {
ActionSequence.addAction(InterceptedBean.class.getSimpleName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright (c) 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.test.integration.weld.ejb.interceptor.ordering.lifecycle;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.interceptor.InvocationContext;

public class LegacyInterceptor implements Serializable {

private static final long serialVersionUID = -3142706070329564629L;

@PostConstruct
void postConstruct(InvocationContext ctx) {
try {
ActionSequence.addAction(LegacyInterceptor.class.getSimpleName());
ctx.proceed();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@PreDestroy
void preDestroy(InvocationContext ctx) {
try {
ActionSequence.addAction(LegacyInterceptor.class.getSimpleName());
ctx.proceed();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright (c) 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.test.integration.weld.ejb.interceptor.ordering.lifecycle;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import javax.interceptor.Interceptors;

import org.jboss.arquillian.container.test.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.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* Tests that session bean lifecycle interceptors are executed in the following order:
*
* 1) Interceptors bound using the {@link Interceptors} annotation
* 2) Interceptors bound using CDI interceptor bindings
* 3) Interceptors defined on a superclass of the bean defining class
* 4) Interceptors defined on the bean defining class
*
*@see https://issues.jboss.org/browse/AS7-6739
*
* @author Jozef Hartinger
*
*/
@RunWith(Arquillian.class)
public class SessionBeanLifecycleInterceptorOrderingTest {

@Inject
private BeanManager manager;

@Deployment
public static Archive<?> getDeployment() {
return ShrinkWrap
.create(WebArchive.class)
.addPackage(SessionBeanLifecycleInterceptorOrderingTest.class.getPackage())
.addAsWebInfResource(
new StringAsset("<beans><interceptors><class>" + CdiInterceptor.class.getName()
+ "</class></interceptors></beans>"), "beans.xml");
}

@Test
public void testSessionBeanLifecycleInterceptorOrdering() {
@SuppressWarnings("unchecked")
Bean<InterceptedBean> bean = (Bean<InterceptedBean>) manager.resolve(manager.getBeans(InterceptedBean.class));
CreationalContext<InterceptedBean> ctx = manager.createCreationalContext(bean);

List<String> expected = new ArrayList<String>();
expected.add(LegacyInterceptor.class.getSimpleName());
expected.add(CdiInterceptor.class.getSimpleName());
expected.add(Superclass.class.getSimpleName());
expected.add(InterceptedBean.class.getSimpleName());

ActionSequence.reset();
// create a new instance
InterceptedBean instance = bean.create(ctx);
assertEquals(expected, ActionSequence.getActions());

ActionSequence.reset();
bean.destroy(instance, ctx);
assertEquals(expected, ActionSequence.getActions());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright (c) 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.as.test.integration.weld.ejb.interceptor.ordering.lifecycle;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class Superclass {

@PostConstruct
void postConstructSuperclass() {
ActionSequence.addAction(Superclass.class.getSimpleName());
}

@PreDestroy
void preDestroySuperclass() {
ActionSequence.addAction(Superclass.class.getSimpleName());
}
}

0 comments on commit 051c6d4

Please sign in to comment.