KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > initial > TxTester


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.initial;
23
24 import org.jboss.tm.TransactionManagerService;
25
26 import javax.naming.InitialContext JavaDoc;
27 import javax.transaction.Transaction JavaDoc;
28 import javax.transaction.TransactionManager JavaDoc;
29
30 /**
31  * Comment
32  *
33  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
34  * @version $Revision: 39547 $
35  */

36 public class TxTester implements TxTesterMBean
37 {
38    public void testTransactions() throws Exception JavaDoc
39    {
40       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
41       TransactionManager JavaDoc tm = (TransactionManager JavaDoc) ctx.lookup(TransactionManagerService.JNDI_NAME);
42       TestLocal test = (TestLocal) ctx.lookup("TestBean/local");
43       callNever(tm, test);
44       callNotSupported(tm, test);
45       callSupportsWithTx(tm, test);
46       callSupportsWithoutTx(tm, test);
47       test.required();
48       callMandatoryNoTx(tm, test);
49       callMandatoryWithTx(tm, test);
50       callRequiresNew(tm, test);
51    }
52
53    public void callRequiresNew(TransactionManager JavaDoc tm, TestLocal test) throws Exception JavaDoc
54    {
55       tm.begin();
56       Transaction JavaDoc tx = tm.getTransaction();
57       test.requiresNew(tx);
58       tm.commit();
59    }
60
61    public void callNever(TransactionManager JavaDoc tm, TestLocal test) throws Exception JavaDoc
62    {
63       boolean exceptionThrown = false;
64       tm.begin();
65       try
66       {
67          test.never();
68       }
69       catch (Exception JavaDoc ex)
70       {
71          //System.out.println("**************");
72
//ex.printStackTrace();
73
//System.out.println("******************");
74
exceptionThrown = true;
75       }
76       tm.rollback();
77       if (!exceptionThrown) throw new Exception JavaDoc("failed on mandatory no tx call");
78    }
79
80    public void callNotSupported(TransactionManager JavaDoc tm, TestLocal test) throws Exception JavaDoc
81    {
82       tm.begin();
83       test.notSupported();
84       tm.commit();
85    }
86
87    public void callSupportsWithTx(TransactionManager JavaDoc tm, TestLocal test) throws Exception JavaDoc
88    {
89       tm.begin();
90       Transaction JavaDoc tx = tm.getTransaction();
91       test.supports(tx);
92       tm.commit();
93    }
94
95    public void callSupportsWithoutTx(TransactionManager JavaDoc tm, TestLocal test) throws Exception JavaDoc
96    {
97       test.supports(null);
98    }
99
100    public void callMandatoryNoTx(TransactionManager JavaDoc tm, TestLocal test) throws Exception JavaDoc
101    {
102       boolean exceptionThrown = false;
103       try
104       {
105          test.mandatory();
106       }
107       catch (Exception JavaDoc ex)
108       {
109          exceptionThrown = true;
110       }
111       if (!exceptionThrown) throw new Exception JavaDoc("failed on mandatory no tx call");
112    }
113
114    public void callMandatoryWithTx(TransactionManager JavaDoc tm, Test test) throws Exception JavaDoc
115    {
116       tm.begin();
117       test.mandatory();
118       tm.commit();
119    }
120 }
121
122
123
124
Popular Tags