Skip to content

Commit

Permalink
WELD-583 Added transactional observer integration tests to Arquillian…
Browse files Browse the repository at this point in the history
… test suite
  • Loading branch information
drallen authored and pmuir committed Aug 9, 2010
1 parent 6ea1457 commit a7e1144
Show file tree
Hide file tree
Showing 7 changed files with 591 additions and 0 deletions.
@@ -0,0 +1,31 @@
/*
* 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.event.observer.transactional;

import javax.ejb.Local;

@Local
public interface Agent
{

public abstract void sendInTransaction(Object event);

public abstract void sendOutsideTransaction(Object event);

public void sendInTransactionAndFail(Object event) throws Exception;

}
@@ -0,0 +1,80 @@
/*
* 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.event.observer.transactional;

import static javax.ejb.TransactionManagementType.BEAN;

import javax.annotation.Resource;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import javax.inject.Named;
import javax.transaction.UserTransaction;

@Stateless
@TransactionManagement(BEAN)
@Named
@Default
public class DogAgent implements Agent
{
@Resource
private UserTransaction userTransaction;

@Inject
private BeanManager jsr299Manager;

public void sendInTransaction(Object event)
{
try
{
userTransaction.begin();
jsr299Manager.fireEvent(event);
userTransaction.commit();
}
catch (EJBException ejbException)
{
throw ejbException;
}
catch (Exception e)
{
throw new EJBException("Transaction failure", e);
}
}

public void sendInTransactionAndFail(Object event) throws Exception
{
try
{
userTransaction.begin();
jsr299Manager.fireEvent(event);
throw new FooException();
}
finally
{
userTransaction.rollback();
}

}

public void sendOutsideTransaction(Object event)
{
jsr299Manager.fireEvent(event);
}
}
@@ -0,0 +1,26 @@
/*
* 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.event.observer.transactional;

/**
* @author pmuir
*
*/
public class FooException extends Exception
{

}
@@ -0,0 +1,209 @@
/*
* 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.event.observer.transactional;

import static javax.ejb.TransactionManagementType.BEAN;
import static javax.enterprise.event.TransactionPhase.AFTER_COMPLETION;
import static javax.enterprise.event.TransactionPhase.AFTER_FAILURE;
import static javax.enterprise.event.TransactionPhase.AFTER_SUCCESS;
import static javax.enterprise.event.TransactionPhase.BEFORE_COMPLETION;
import static javax.transaction.Status.STATUS_NO_TRANSACTION;

import java.math.BigInteger;
import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.ejb.EJBException;
import javax.ejb.Remove;
import javax.ejb.SessionContext;
import javax.ejb.Stateful;
import javax.ejb.TransactionManagement;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Observes;
import javax.inject.Named;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;


@Stateful
@TransactionManagement(BEAN)
@Tame
@Named("Teddy")
@SessionScoped
public class Pomeranian implements PomeranianInterface
{
private static final Logger log = Logger.getLogger(Pomeranian.class.getName());

@Resource
private UserTransaction transaction;

@Resource
private SessionContext context;

private boolean correctContext = false;
private boolean correctTransactionState = false;

/**
* Observes a String event only after the transaction is completed.
*
* @param someEvent
*/
public void observeStringEvent(@Observes(during=AFTER_COMPLETION) String someEvent)
{
try
{
log.finer("Observing string event with tx status: " + transaction.getStatus());
if (transaction.getStatus() == STATUS_NO_TRANSACTION)
{
setCorrectTransactionState(true);
}
}
catch (SystemException e)
{
throw new EJBException("Failed to detect transaction status", e);
}
}

/**
* Observes an Integer event if the transaction is successfully completed.
*
* @param event
*/
public void observeIntegerEvent(@Observes(during=AFTER_SUCCESS) Integer event)
{
try
{
log.finer("Observing integer event with tx status: " + transaction.getStatus());
if (transaction.getStatus() == STATUS_NO_TRANSACTION)
{
setCorrectTransactionState(true);
}
}
catch (SystemException e)
{
throw new EJBException("Failed to detect transaction status", e);
}
}

/**
* Observes a Float event only if the transaction failed.
*
* @param event
*/
public void observeFloatEvent(@Observes(during=AFTER_FAILURE) Float event)
{
try
{
log.finer("Observing float event with tx status: " + transaction.getStatus());
if (transaction.getStatus() == STATUS_NO_TRANSACTION)
{
setCorrectTransactionState(true);
}
}
catch (SystemException e)
{
throw new EJBException("Failed to detect transaction status", e);
}
}

public void observeBigIntegerEvent(@Observes BigInteger event)
{
try
{
log.finer("Observing BigInteger event with tx status: " + transaction.getStatus());
if (transaction.getStatus() == STATUS_NO_TRANSACTION)
{
setCorrectTransactionState(true);
}

if (context.getCallerPrincipal().getName().equals("Bubba"))
{
setCorrectContext(true);
}
log.finer("Principal caller is " + context.getCallerPrincipal().getName());
}
catch (SystemException e)
{
throw new EJBException("Failed to detect transaction status", e);
}
}

public void observeExceptionEvent(@Observes(during=BEFORE_COMPLETION) RuntimeException event)
{
try
{
log.finer("Observing exception event with tx status: " + transaction.getStatus());
if (transaction.getStatus() == STATUS_NO_TRANSACTION)
{
setCorrectTransactionState(true);
}

if (context.getCallerPrincipal().getName().equals("Bubba"))
{
setCorrectContext(true);
}
log.finer("Principal caller is " + context.getCallerPrincipal().getName());
}
catch (SystemException e)
{
throw new EJBException("Failed to detect transaction status", e);
}
}

public void observeCharEvent(Character event)
{
try
{
log.finer("Observing character event with tx status: " + transaction.getStatus());
if (transaction.getStatus() == STATUS_NO_TRANSACTION)
{
setCorrectTransactionState(true);
}
}
catch (SystemException e)
{
throw new EJBException("Failed to detect transaction status", e);
}
}

public boolean isCorrectContext()
{
return correctContext;
}

public void setCorrectContext(boolean correctContext)
{
this.correctContext = correctContext;
}

public boolean isCorrectTransactionState()
{
return correctTransactionState;
}

public void setCorrectTransactionState(boolean correctTransactionState)
{
this.correctTransactionState = correctTransactionState;
}

@Remove
public void removeSessionBean()
{

}

}

0 comments on commit a7e1144

Please sign in to comment.