Skip to content

Commit

Permalink
[WFLY-4672] Adding tests for exception being thrown from SFSB
Browse files Browse the repository at this point in the history
  • Loading branch information
ochaloup committed Nov 9, 2016
1 parent 0d29a8d commit 95cd7a6
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 14 deletions.
@@ -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;
}
}
@@ -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);
}
@@ -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> T lookup(Class<? extends T> beanType, Class<T> 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);
}
}
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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> T lookup(Class<T> beanType) throws NamingException {
private DestroyMarkerBeanInterface isPreDestroy;

private <T> T lookup(Class<T> 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());
}
}
Expand Up @@ -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
}
}
@@ -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();
}
@@ -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);
}
}

0 comments on commit 95cd7a6

Please sign in to comment.