KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > bmt > TesterBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.test.bmt;
23
24 import javax.ejb.EJB JavaDoc;
25 import javax.ejb.TransactionAttribute JavaDoc;
26 import javax.ejb.TransactionAttributeType JavaDoc;
27 import javax.ejb.Stateless JavaDoc;
28 import javax.transaction.TransactionManager JavaDoc;
29 import javax.transaction.Transaction JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import org.jboss.logging.Logger;
32 import org.jboss.annotation.JndiInject;
33
34 /**
35  * Comment
36  *
37  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
38  * @version $Revision: 45473 $
39  */

40 @Stateless JavaDoc
41 public class TesterBean implements TesterRemote
42 {
43    @EJB JavaDoc StatelessLocal stateless;
44    @JndiInject(jndiName="java:/TransactionManager") TransactionManager JavaDoc tm;
45
46
47    protected static Logger log = Logger.getLogger(TesterBean.class);
48
49    public void testStatelessWithTx() throws Exception JavaDoc
50    {
51       stateless.beginCommitEnd();
52       stateless.beginRollbackEnd();
53
54       try
55       {
56          stateless.beginNoEnd();
57       }
58       catch (Exception JavaDoc e)
59       {
60          if (e instanceof TestException) throw e;
61          log.info("should be EJBException thrown that begin and no end was called: ", e);
62       }
63    }
64
65    @TransactionAttribute JavaDoc(TransactionAttributeType.NOT_SUPPORTED)
66    public void testStatelessWithoutTx() throws Exception JavaDoc
67    {
68       stateless.beginCommitEnd();
69       if (tm.getTransaction() != null) throw new RuntimeException JavaDoc("tx is associated");
70       stateless.beginRollbackEnd();
71       if (tm.getTransaction() != null) throw new RuntimeException JavaDoc("tx is associated");
72
73       try
74       {
75          stateless.beginNoEnd();
76       }
77       catch (Exception JavaDoc e)
78       {
79          if (e instanceof TestException) throw e;
80          log.info("should be EJBException thrown that begin and no end was called: ", e);
81       }
82       if (tm.getTransaction() != null) throw new RuntimeException JavaDoc("tx is associated");
83    }
84
85    StatefulLocal getStateful() throws Exception JavaDoc
86    {
87       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
88       return (StatefulLocal)ctx.lookup("StatefulBean/local");
89    }
90
91    public void testStatefulWithTx() throws Exception JavaDoc
92    {
93       StatefulLocal stateful = getStateful();
94       Transaction JavaDoc tx = stateful.beginNoEnd();
95       stateful.endCommit(tx);
96       tx = stateful.beginNoEnd();
97       stateful.endRollback(tx);
98
99       stateful.beginCommitEnd();
100       stateful.beginRollbackEnd();
101
102    }
103
104    @TransactionAttribute JavaDoc(TransactionAttributeType.NOT_SUPPORTED)
105    public void testStatefulWithoutTx() throws Exception JavaDoc
106    {
107       StatefulLocal stateful = getStateful();
108       Transaction JavaDoc tx = stateful.beginNoEnd();
109       if (tm.getTransaction() != null) throw new RuntimeException JavaDoc("tx is associated");
110       stateful.endCommit(tx);
111       if (tm.getTransaction() != null) throw new RuntimeException JavaDoc("tx is associated");
112       tx = stateful.beginNoEnd();
113       if (tm.getTransaction() != null) throw new RuntimeException JavaDoc("tx is associated");
114       stateful.endRollback(tx);
115       if (tm.getTransaction() != null) throw new RuntimeException JavaDoc("tx is associated");
116
117       stateful.beginCommitEnd();
118       if (tm.getTransaction() != null) throw new RuntimeException JavaDoc("tx is associated");
119       stateful.beginRollbackEnd();
120       if (tm.getTransaction() != null) throw new RuntimeException JavaDoc("tx is associated");
121
122    }
123 }
124
Popular Tags