diff --git a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/DestroyMarkerBean.java b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/DestroyMarkerBean.java new file mode 100644 index 000000000000..6d1d94bba782 --- /dev/null +++ b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/DestroyMarkerBean.java @@ -0,0 +1,42 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2016, 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.ejb.stateful.exception; + +import javax.ejb.LocalBean; +import javax.ejb.Remote; +import javax.ejb.Singleton; + +@Singleton +@Remote +@LocalBean +public class DestroyMarkerBean implements DestroyMarkerBeanInterface { + private boolean preDestroy = false; + + public boolean is() { + return preDestroy; + } + + public void set(boolean preDestroy) { + this.preDestroy = preDestroy; + } +} diff --git a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/DestroyMarkerBeanInterface.java b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/DestroyMarkerBeanInterface.java new file mode 100644 index 000000000000..d77fd09f5bbd --- /dev/null +++ b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/DestroyMarkerBeanInterface.java @@ -0,0 +1,28 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2016, 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.ejb.stateful.exception; + +public interface DestroyMarkerBeanInterface { + boolean is(); + void set(boolean preDestroy); +} diff --git a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/ExceptionEjbClientTestCase.java b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/ExceptionEjbClientTestCase.java new file mode 100644 index 000000000000..44c26a470bae --- /dev/null +++ b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/ExceptionEjbClientTestCase.java @@ -0,0 +1,66 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2016, 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.ejb.stateful.exception; + +import java.util.Properties; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import org.jboss.arquillian.container.test.api.RunAsClient; +import org.jboss.arquillian.junit.Arquillian; +import org.junit.BeforeClass; +import org.junit.runner.RunWith; + +/** + * The same bundle of tests as runs at {@link ExceptionTestCase} but these ones + * are managed at client mode - all calls runs over ejb remoting. + * + * @author Ondrej Chaloupka + */ +@RunAsClient +@RunWith(Arquillian.class) +public class ExceptionEjbClientTestCase extends ExceptionTestCase { + private static Context context; + + @BeforeClass + public static void beforeClass() throws Exception { + final Properties props = new Properties(); + props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); + context = new InitialContext(props); + } + + private T lookup(Class beanType, Class interfaceType,boolean isStateful) throws NamingException { + String ejbLookup = String.format("ejb:/%s/%s!%s%s", ARCHIVE_NAME, beanType.getSimpleName(), interfaceType.getName(), + (isStateful ? "?stateful" : "")); + return interfaceType.cast(context.lookup(ejbLookup)); + } + + protected SFSB1Interface getBean() throws NamingException { + return lookup(SFSB1.class, SFSB1Interface.class, true); + } + + protected DestroyMarkerBeanInterface getMarker() throws NamingException { + return lookup(DestroyMarkerBean.class, DestroyMarkerBeanInterface.class, false); + } +} diff --git a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/SystemExceptionTestCase.java b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/ExceptionTestCase.java similarity index 50% rename from testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/SystemExceptionTestCase.java rename to testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/ExceptionTestCase.java index cf1611af5d79..deb5efe37f3a 100644 --- a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/SystemExceptionTestCase.java +++ b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/ExceptionTestCase.java @@ -22,6 +22,7 @@ package org.jboss.as.test.integration.ejb.stateful.exception; +import javax.ejb.EJBException; import javax.ejb.NoSuchEJBException; import javax.naming.InitialContext; import javax.naming.NamingException; @@ -33,6 +34,7 @@ import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -43,47 +45,109 @@ * @author Stuart Douglas */ @RunWith(Arquillian.class) -public class SystemExceptionTestCase { +public class ExceptionTestCase { - private static final String ARCHIVE_NAME = "SystemExceptionTestCase"; + protected static final String ARCHIVE_NAME = "ExceptionTestCase"; @Deployment public static Archive deploy() { JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar"); - jar.addPackage(SystemExceptionTestCase.class.getPackage()); + jar.addPackage(ExceptionTestCase.class.getPackage()); return jar; } @ArquillianResource private InitialContext iniCtx; - protected T lookup(Class beanType) throws NamingException { + private DestroyMarkerBeanInterface isPreDestroy; + + private T lookup(Class beanType) throws NamingException { return beanType.cast(iniCtx.lookup("java:global/" + ARCHIVE_NAME + "/" + beanType.getSimpleName() + "!" + beanType.getName())); } + protected SFSB1Interface getBean() throws NamingException { + return lookup(SFSB1.class); + } + + protected DestroyMarkerBeanInterface getMarker() throws NamingException { + return lookup(DestroyMarkerBean.class); + } + + @Before + public void before() throws NamingException { + isPreDestroy = getMarker(); + } + /** * Ensure that a system exception destroys the bean. - * - * @throws Exception */ @Test public void testSystemExceptionDestroysBean() throws Exception { - SFSB1 sfsb1 = lookup(SFSB1.class); - Assert.assertFalse(sfsb1.preDestroy); + SFSB1Interface sfsb1 = getBean(); + Assert.assertFalse(isPreDestroy.is()); try { sfsb1.systemException(); + Assert.fail("It was expected a RuntimeException being thrown"); } catch (RuntimeException e) { Assert.assertTrue(e.getMessage().contains(SFSB1.MESSAGE)); } - Assert.assertFalse(sfsb1.preDestroy); + Assert.assertFalse(isPreDestroy.is()); try { sfsb1.systemException(); - throw new RuntimeException("Expecting NoSuchEjbException"); + Assert.fail("Expecting NoSuchEjbException"); } catch (NoSuchEJBException expected) { + } + } + + /** + * Throwing EJBException which as child of RuntimeException causes + * SFSB to be removed. + */ + @Test + public void testEjbExceptionDestroysBean() throws Exception { + SFSB1Interface sfsb1 = getBean(); + Assert.assertFalse(isPreDestroy.is()); + try { + sfsb1.ejbException(); + Assert.fail("It was expected a EJBException being thrown"); + } catch (EJBException e) { + Assert.assertTrue(e.getMessage().contains(SFSB1.MESSAGE)); } + Assert.assertFalse("Thrown exception removes SFS but does not call any callback", + isPreDestroy.is()); + try { + sfsb1.ejbException(); + Assert.fail("Expecting NoSuchEjbException"); + } catch (NoSuchEJBException expected) { + } + } + + /** + * Throwing non {@link RuntimeException} which does not cause + * SFSB being removed. + */ + @Test + public void testUserExceptionDoesNothing() throws Exception { + + SFSB1Interface sfsb1 = getBean(); + Assert.assertFalse(isPreDestroy.is()); + try { + sfsb1.userException(); + } catch (TestException e) { + Assert.assertTrue(e.getMessage().contains(SFSB1.MESSAGE)); + } + Assert.assertFalse(isPreDestroy.is()); + + try { + sfsb1.userException(); + } catch (TestException e) { + Assert.assertTrue(e.getMessage().contains(SFSB1.MESSAGE)); + } + sfsb1.remove(); + Assert.assertTrue("As remove was called preDestroy callback is expected", isPreDestroy.is()); } } diff --git a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/SFSB1.java b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/SFSB1.java index 50b809a33d5f..1e21eaa14c9f 100644 --- a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/SFSB1.java +++ b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/SFSB1.java @@ -22,26 +22,53 @@ package org.jboss.as.test.integration.ejb.stateful.exception; +import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; +import javax.ejb.EJB; +import javax.ejb.EJBException; +import javax.ejb.LocalBean; +import javax.ejb.Remote; +import javax.ejb.Remove; import javax.ejb.Stateful; /** * stateful session bean - * */ @Stateful -public class SFSB1 { +@Remote +@LocalBean +public class SFSB1 implements SFSB1Interface { public static final String MESSAGE = "Expected Exception"; - public volatile boolean preDestroy = false; + @EJB + private DestroyMarkerBean marker; + + + @PostConstruct + public void postConstruct() { + marker.set(false); + } @PreDestroy public void preDestroy() { - this.preDestroy = true; + marker.set(true); } public void systemException() { throw new RuntimeException(MESSAGE); } + + public void ejbException() { + throw new EJBException(MESSAGE); + } + + public void userException() throws TestException { + throw new TestException(MESSAGE); + } + + @Remove + public void remove() { + // just removed + } } diff --git a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/SFSB1Interface.java b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/SFSB1Interface.java new file mode 100644 index 000000000000..311814f756b2 --- /dev/null +++ b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/SFSB1Interface.java @@ -0,0 +1,30 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2016, 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.ejb.stateful.exception; + +public interface SFSB1Interface { + void systemException(); + void ejbException(); + void userException() throws TestException; + void remove(); +} diff --git a/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/TestException.java b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/TestException.java new file mode 100644 index 000000000000..13dfafb4938a --- /dev/null +++ b/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/ejb/stateful/exception/TestException.java @@ -0,0 +1,31 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 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.ejb.stateful.exception; + +public class TestException extends Exception { + private static final long serialVersionUID = 1L; + + public TestException(String message) { + super(message); + } +}