Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WELD-583 Added transactional observer integration tests to Arquillian…
… test suite
- Loading branch information
Showing
7 changed files
with
591 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
tests-arquillian/src/test/java/org/jboss/weld/tests/event/observer/transactional/Agent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
...-arquillian/src/test/java/org/jboss/weld/tests/event/observer/transactional/DogAgent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...uillian/src/test/java/org/jboss/weld/tests/event/observer/transactional/FooException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
|
||
} |
209 changes: 209 additions & 0 deletions
209
...rquillian/src/test/java/org/jboss/weld/tests/event/observer/transactional/Pomeranian.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
{ | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.