001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.tm.ejb;
023:
024: import javax.ejb.EJBException;
025: import javax.naming.InitialContext;
026: import javax.transaction.Status;
027: import javax.transaction.TransactionManager;
028:
029: import org.jboss.test.util.ejb.SessionSupport;
030: import org.jboss.tm.TxUtils;
031:
032: /**
033: * @version $Revision: 63571 $
034: */
035: public class TxTimeoutBean extends SessionSupport {
036: /** The serialVersionUID */
037: private static final long serialVersionUID = -6750278789142406435L;
038:
039: public void ejbCreate() {
040: }
041:
042: /**
043: * The harness should have set the default timeout to 10 secs
044: */
045: public void testDefaultTimeout() {
046: sleep(12000, false);
047: int status = getTxStatus();
048: if (status != Status.STATUS_MARKED_ROLLBACK)
049: throw new EJBException("Should be marked rolled back: "
050: + TxUtils.getStatusAsString(status));
051: }
052:
053: /**
054: * This method's timeout should be 5 secs
055: */
056: public void testOverriddenTimeoutExpires() {
057: sleep(7000, false);
058: int status = getTxStatus();
059: log.info("testOverriddenTimeoutExpires: "
060: + TxUtils.getStatusAsString(status));
061: if (TxUtils.isRollback(status) == false) {
062: // give it a second chance
063: sleep(2000, false);
064: status = getTxStatus();
065: log.info("testOverriddenTimeoutExpires: "
066: + TxUtils.getStatusAsString(status));
067:
068: if (TxUtils.isRollback(status) == false)
069: throw new EJBException("Should be marked rolled back: "
070: + TxUtils.getStatusAsString(status));
071: }
072: }
073:
074: /**
075: * This method's timeout should be 20 secs
076: */
077: public void testOverriddenTimeoutDoesNotExpire() {
078: sleep(12000, true);
079: int status = getTxStatus();
080: if (status != Status.STATUS_ACTIVE)
081: throw new EJBException("Should be active: "
082: + TxUtils.getStatusAsString(status));
083: }
084:
085: private int getTxStatus() {
086: try {
087: InitialContext ctx = new InitialContext();
088: TransactionManager tm = (TransactionManager) ctx
089: .lookup("java:/TransactionManager");
090: return tm.getStatus();
091: } catch (Exception e) {
092: throw new EJBException(e);
093: }
094: }
095:
096: private void sleep(int timeout, boolean throwEJBException) {
097: try {
098: Thread.sleep(timeout);
099: } catch (Exception e) {
100: if (throwEJBException)
101: throw new EJBException(e);
102: else
103: log.debug("Ignored", e);
104: }
105: }
106: }
|