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