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.aop.bean;
023:
024: import org.jboss.tm.TxUtils;
025:
026: import javax.naming.InitialContext;
027: import javax.transaction.Transaction;
028: import javax.transaction.TransactionManager;
029:
030: /**
031: *
032: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
033: * @version $Revision: 57211 $
034: */
035: public class TxPOJO {
036: TransactionManager tm;
037:
038: public TxPOJO() throws Exception {
039: tm = (TransactionManager) new InitialContext()
040: .lookup("java:/TransactionManager");
041: }
042:
043: public void never() {
044: }
045:
046: public void callNever() throws Exception {
047: boolean exceptionThrown = false;
048: tm.begin();
049: try {
050: never();
051: } catch (Exception ex) {
052: exceptionThrown = true;
053: }
054: tm.commit();
055: if (!exceptionThrown)
056: throw new Exception("failed on mandatory no tx call");
057: }
058:
059: public void notsupported() throws Exception {
060: if (tm.getTransaction() != null)
061: throw new Exception("notsupported() method has tx set");
062: }
063:
064: public void callNotSupported() throws Exception {
065: tm.begin();
066: notsupported();
067: tm.commit();
068: }
069:
070: public void supports(Transaction tx) throws Exception {
071: Transaction tmTx = tm.getTransaction();
072: if (tx != tmTx)
073: throw new Exception("supports didn't work");
074: }
075:
076: public boolean hasActiveTransaction() throws Exception {
077: Transaction tx = tm.getTransaction();
078: if (tx == null) {
079: System.out.println("Transaction: is null");
080: } // end of if ()
081: else {
082: System.out.println("Transaction: status " + tx.getStatus()
083: + " of tx" + tx);
084: } // end of else
085:
086: return TxUtils.isActive(tx);
087: }
088:
089: public void callSupportsWithTx() throws Exception {
090: tm.begin();
091: Transaction tx = tm.getTransaction();
092: supports(tx);
093: tm.commit();
094: }
095:
096: public void callSupportsWithoutTx() throws Exception {
097: supports(null);
098: }
099:
100: public void required() throws Exception {
101: if (tm.getTransaction() == null)
102: throw new Exception("rquired() method has no tx set");
103: }
104:
105: public void requiresNew(Transaction tx) throws Exception {
106: Transaction tmTx = tm.getTransaction();
107: if (tx == tmTx || (tx != null && tx.equals(tmTx)))
108: throw new Exception("transactions shouldn't be equal");
109: if (tmTx == null)
110: throw new Exception("tx is null in RequiresNew");
111: }
112:
113: public void callRequiresNew() throws Exception {
114: tm.begin();
115: Transaction tx = tm.getTransaction();
116: requiresNew(tx);
117: tm.commit();
118: }
119:
120: public void mandatory() {
121: }
122:
123: public void callMandatoryNoTx() throws Exception {
124: boolean exceptionThrown = false;
125: try {
126: mandatory();
127: } catch (Exception ex) {
128: exceptionThrown = true;
129: }
130: if (!exceptionThrown)
131: throw new Exception("failed on mandatory no tx call");
132: }
133:
134: public void callMandatoryWithTx() throws Exception {
135: tm.begin();
136: mandatory();
137: tm.commit();
138: }
139:
140: }
|